Skip to content

Commit 0acf8c5

Browse files
committed
fixup! [RISCV][VLS] Support RISCV VLS calling convention
1 parent 9689ac0 commit 0acf8c5

File tree

13 files changed

+45
-52
lines changed

13 files changed

+45
-52
lines changed

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
19421942
/// Extra information which affects how the function is called, like
19431943
/// regparm and the calling convention.
19441944
LLVM_PREFERRED_TYPE(CallingConv)
1945-
unsigned ExtInfo : 17;
1945+
unsigned ExtInfo : 18;
19461946

19471947
/// The ref-qualifier associated with a \c FunctionProtoType.
19481948
///
@@ -4409,7 +4409,7 @@ class FunctionType : public Type {
44094409
};
44104410
enum { NoCfCheckMask = 0x800 };
44114411
enum { CmseNSCallMask = 0x1000 };
4412-
enum { Log2RISCVABIVLenMask = 0x1E000, Log2RISCVABIVLenOffset = 13 };
4412+
enum { Log2RISCVABIVLenMask = 0x3E000, Log2RISCVABIVLenOffset = 13 };
44134413
uint32_t Bits = CC_C;
44144414

44154415
ExtInfo(unsigned Bits) : Bits(static_cast<uint32_t>(Bits)) {}

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ ENUM_CODEGENOPT(ZeroCallUsedRegs, llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind,
457457
/// non-deleting destructors. (No effect on Microsoft ABI.)
458458
CODEGENOPT(CtorDtorReturnThis, 1, 0)
459459

460+
/// Specify the VLEN for VLS calling convention.
461+
CODEGENOPT(RISCVABIVLen, 17, 0)
462+
460463
/// FIXME: Make DebugOptions its own top-level .def file.
461464
#include "DebugOptions.def"
462465

clang/include/clang/CodeGen/CGFunctionInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ class CGFunctionInfo final
609609
unsigned MaxVectorWidth : 4;
610610

611611
/// Log2 of ABI_VLEN used in RISCV VLS calling convention.
612-
unsigned Log2RISCVABIVLen : 4;
612+
unsigned Log2RISCVABIVLen : 5;
613613

614614
RequiredArgs Required;
615615

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,8 +4866,9 @@ def mrvv_vector_bits_EQ : Joined<["-"], "mrvv-vector-bits=">, Group<m_Group>,
48664866
true: " The value will be reflected in __riscv_v_fixed_vlen preprocessor define"),
48674867
" (RISC-V only)")>;
48684868
def mriscv_abi_vlen_EQ : Joined<["-"], "mriscv-abi-vlen=">, Group<m_Group>,
4869-
HelpText<"Specify the VLEN for VLS calling convention.">;
4870-
4869+
Visibility<[ClangOption, CC1Option]>,
4870+
HelpText<"Specify the VLEN for VLS calling convention.">,
4871+
MarshallingInfoInt<CodeGenOpts<"RISCVABIVLen">>;
48714872
def munaligned_access : Flag<["-"], "munaligned-access">, Group<m_Group>,
48724873
HelpText<"Allow memory accesses to be unaligned (AArch32/MIPSr6 only)">;
48734874
def mno_unaligned_access : Flag<["-"], "mno-unaligned-access">, Group<m_Group>,

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ createTargetCodeGenInfo(CodeGenModule &CGM) {
238238
else if (ABIStr.ends_with("d"))
239239
ABIFLen = 64;
240240
bool EABI = ABIStr.ends_with("e");
241-
return createRISCVTargetCodeGenInfo(CGM, XLen, ABIFLen, EABI);
241+
return createRISCVTargetCodeGenInfo(CGM, XLen, ABIFLen,
242+
CodeGenOpts.RISCVABIVLen, EABI);
242243
}
243244

244245
case llvm::Triple::systemz: {

clang/lib/CodeGen/TargetInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ createPPC64_SVR4_TargetCodeGenInfo(CodeGenModule &CGM, PPC64_SVR4_ABIKind Kind,
519519

520520
std::unique_ptr<TargetCodeGenInfo>
521521
createRISCVTargetCodeGenInfo(CodeGenModule &CGM, unsigned XLen, unsigned FLen,
522-
bool EABI);
522+
unsigned ABIVLen, bool EABI);
523523

524524
std::unique_ptr<TargetCodeGenInfo>
525525
createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM);

clang/lib/CodeGen/Targets/RISCV.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class RISCVABIInfo : public DefaultABIInfo {
2626
// ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
2727
// with soft float ABI has FLen==0).
2828
unsigned FLen;
29+
unsigned ABIVLen;
2930
const int NumArgGPRs;
3031
const int NumArgFPRs;
3132
const bool EABI;
@@ -37,17 +38,17 @@ class RISCVABIInfo : public DefaultABIInfo {
3738

3839
public:
3940
RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen,
40-
bool EABI)
41-
: DefaultABIInfo(CGT), XLen(XLen), FLen(FLen), NumArgGPRs(EABI ? 6 : 8),
42-
NumArgFPRs(FLen != 0 ? 8 : 0), EABI(EABI) {}
41+
unsigned ABIVLen, bool EABI)
42+
: DefaultABIInfo(CGT), XLen(XLen), FLen(FLen), ABIVLen(ABIVLen),
43+
NumArgGPRs(EABI ? 6 : 8), NumArgFPRs(FLen != 0 ? 8 : 0), EABI(EABI) {}
4344

4445
// DefaultABIInfo's classifyReturnType and classifyArgumentType are
4546
// non-virtual, but computeInfo is virtual, so we overload it.
4647
void computeInfo(CGFunctionInfo &FI) const override;
4748

4849
ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
49-
int &ArgFPRsLeft, unsigned ABIVLen) const;
50-
ABIArgInfo classifyReturnType(QualType RetTy, unsigned ABIVLen) const;
50+
int &ArgFPRsLeft, unsigned ArgABIVLen) const;
51+
ABIArgInfo classifyReturnType(QualType RetTy, unsigned ArgABIVLen) const;
5152

5253
RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
5354
AggValueSlot Slot) const override;
@@ -63,23 +64,18 @@ class RISCVABIInfo : public DefaultABIInfo {
6364
llvm::Type *Field2Ty,
6465
CharUnits Field2Off) const;
6566

66-
ABIArgInfo coerceVLSVector(QualType Ty, unsigned ABIVLen = 0) const;
67+
ABIArgInfo coerceVLSVector(QualType Ty, unsigned ArgABIVLen = 0) const;
6768
};
6869
} // end anonymous namespace
6970

7071
void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
71-
unsigned ABIVLen = 1 << FI.getExtInfo().getLog2RISCVABIVLen();
72-
if (ABIVLen == 1)
73-
// No riscv_vls_cc in the function, check if there's one passed from
74-
// compiler options.
75-
for (unsigned i = 5; i <= 16; ++i)
76-
if (getContext().getTargetInfo().getTargetOpts().FeatureMap.contains(
77-
"abi-vlen-" + llvm::utostr(1 << i) + "b"))
78-
ABIVLen = 1 << i;
72+
unsigned ArgABIVLen = 1 << FI.getExtInfo().getLog2RISCVABIVLen();
73+
if (ArgABIVLen == 1)
74+
ArgABIVLen = ABIVLen;
7975

8076
QualType RetTy = FI.getReturnType();
8177
if (!getCXXABI().classifyReturnType(FI))
82-
FI.getReturnInfo() = classifyReturnType(RetTy, ABIVLen);
78+
FI.getReturnInfo() = classifyReturnType(RetTy, ArgABIVLen);
8379

8480
// IsRetIndirect is true if classifyArgumentType indicated the value should
8581
// be passed indirect, or if the type size is a scalar greater than 2*XLen
@@ -106,7 +102,7 @@ void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
106102
for (auto &ArgInfo : FI.arguments()) {
107103
bool IsFixed = ArgNum < NumFixedArgs;
108104
ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft,
109-
ArgFPRsLeft, ABIVLen);
105+
ArgFPRsLeft, ArgABIVLen);
110106
ArgNum++;
111107
}
112108
}
@@ -327,7 +323,8 @@ ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
327323

328324
// Fixed-length RVV vectors are represented as scalable vectors in function
329325
// args/return and must be coerced from fixed vectors.
330-
ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
326+
ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty,
327+
unsigned ArgABIVLen) const {
331328
assert(Ty->isVectorType() && "expected vector type!");
332329

333330
const auto *VT = Ty->castAs<VectorType>();
@@ -337,7 +334,7 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
337334
llvm::ScalableVectorType *ResType;
338335
llvm::Type *EltType = CGT.ConvertType(VT->getElementType());
339336

340-
if (ABIVLen == 0) {
337+
if (ArgABIVLen == 0) {
341338
// RVV fixed-length vector
342339
auto VScale =
343340
getContext().getTargetInfo().getVScaleRange(getContext().getLangOpts());
@@ -355,7 +352,7 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
355352
} else {
356353
// Generic vector
357354
ResType = llvm::ScalableVectorType::get(
358-
EltType, NumElts * llvm::RISCV::RVVBitsPerBlock / ABIVLen);
355+
EltType, NumElts * llvm::RISCV::RVVBitsPerBlock / ArgABIVLen);
359356
}
360357

361358
return ABIArgInfo::getDirect(ResType);
@@ -364,7 +361,7 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
364361
ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
365362
int &ArgGPRsLeft,
366363
int &ArgFPRsLeft,
367-
unsigned ABIVLen) const {
364+
unsigned ArgABIVLen) const {
368365
assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
369366
Ty = useFirstFieldIfTransparentUnion(Ty);
370367

@@ -471,10 +468,10 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
471468
if (VT->getVectorKind() == VectorKind::RVVFixedLengthData ||
472469
VT->getVectorKind() == VectorKind::RVVFixedLengthMask)
473470
return coerceVLSVector(Ty);
474-
if (VT->getVectorKind() == VectorKind::Generic && ABIVLen != 1)
471+
if (VT->getVectorKind() == VectorKind::Generic && ArgABIVLen != 0)
475472
// Generic vector without riscv_vls_cc should fall through and pass by
476473
// reference.
477-
return coerceVLSVector(Ty, ABIVLen);
474+
return coerceVLSVector(Ty, ArgABIVLen);
478475
}
479476

480477
// Aggregates which are <= 2*XLen will be passed in registers if possible,
@@ -499,7 +496,7 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
499496
}
500497

501498
ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy,
502-
unsigned ABIVLen) const {
499+
unsigned ArgABIVLen) const {
503500
if (RetTy->isVoidType())
504501
return ABIArgInfo::getIgnore();
505502

@@ -509,7 +506,7 @@ ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy,
509506
// The rules for return and argument types are the same, so defer to
510507
// classifyArgumentType.
511508
return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft, ArgFPRsLeft,
512-
ABIVLen);
509+
ArgABIVLen);
513510
}
514511

515512
RValue RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
@@ -548,9 +545,9 @@ namespace {
548545
class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
549546
public:
550547
RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
551-
unsigned FLen, bool EABI)
548+
unsigned FLen, unsigned ABIVLen, bool EABI)
552549
: TargetCodeGenInfo(
553-
std::make_unique<RISCVABIInfo>(CGT, XLen, FLen, EABI)) {
550+
std::make_unique<RISCVABIInfo>(CGT, XLen, FLen, ABIVLen, EABI)) {
554551
SwiftInfo =
555552
std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
556553
}
@@ -579,7 +576,8 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
579576

580577
std::unique_ptr<TargetCodeGenInfo>
581578
CodeGen::createRISCVTargetCodeGenInfo(CodeGenModule &CGM, unsigned XLen,
582-
unsigned FLen, bool EABI) {
579+
unsigned FLen, unsigned ABIVLen,
580+
bool EABI) {
583581
return std::make_unique<RISCVTargetCodeGenInfo>(CGM.getTypes(), XLen, FLen,
584-
EABI);
582+
ABIVLen, EABI);
585583
}

clang/lib/Driver/ToolChains/Arch/RISCV.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
9595
CPUFastVectorUnaligned = true;
9696
}
9797

98-
if (Arg *A = Args.getLastArg(options::OPT_mriscv_abi_vlen_EQ))
99-
Features.push_back(
100-
Args.MakeArgString(Twine("+abi-vlen-") + A->getValue() + "b"));
101-
10298
// Handle features corresponding to "-ffixed-X" options
10399
if (Args.hasArg(options::OPT_ffixed_x1))
104100
Features.push_back("+reserve-x1");

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,10 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,
22062206
<< A->getSpelling() << Val;
22072207
}
22082208
}
2209+
2210+
if (Arg *A = Args.getLastArg(options::OPT_mriscv_abi_vlen_EQ))
2211+
CmdArgs.push_back(
2212+
Args.MakeArgString(Twine("-mriscv-abi-vlen=") + A->getValue()));
22092213
}
22102214

22112215
void Clang::AddSparcTargetArgs(const ArgList &Args,

clang/test/CodeGen/RISCV/riscv-vector-callingconv-llvm-ir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// RUN: -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-LLVM %s
44
// RUN: %clang_cc1 -std=c23 -triple riscv64 -target-feature +v \
55
// RUN: -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-LLVM %s
6-
// RUN: %clang_cc1 -triple riscv64 -target-feature +v -target-feature +abi-vlen-256b \
6+
// RUN: %clang_cc1 -triple riscv64 -mriscv-abi-vlen=256 -target-feature +v \
77
// RUN: -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-LLVM-ABI-VLEN %s
8-
// RUN: %clang_cc1 -std=c23 -triple riscv64 -target-feature +v -target-feature +abi-vlen-256b \
8+
// RUN: %clang_cc1 -std=c23 -triple riscv64 -mriscv-abi-vlen=256 -target-feature +v \
99
// RUN: -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-LLVM-ABI-VLEN %s
1010

1111
#include <riscv_vector.h>

clang/test/CodeGen/RISCV/riscv-vector-callingconv-llvm-ir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// REQUIRES: riscv-registered-target
22
// RUN: %clang_cc1 -std=c++11 -triple riscv64 -target-feature +v \
33
// RUN: -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-LLVM %s
4-
// RUN: %clang_cc1 -std=c++11 -triple riscv64 -target-feature +v -target-feature +abi-vlen-256b \
4+
// RUN: %clang_cc1 -std=c++11 -triple riscv64 -mriscv-abi-vlen=256 -target-feature +v \
55
// RUN: -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-LLVM-ABI-VLEN %s
66

77
#include <riscv_vector.h>

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,15 +1333,6 @@ def Experimental
13331333
: SubtargetFeature<"experimental", "HasExperimental",
13341334
"true", "Experimental intrinsics">;
13351335

1336-
def FeatureABIVLen32B
1337-
: SubtargetFeature<"abi-vlen-32b", "ABIVLen", "32", "ABI_VLEN desc">;
1338-
1339-
foreach i = { 6-16 } in {
1340-
defvar I = !shl(1, i);
1341-
def FeatureABIVLen # I # B
1342-
: SubtargetFeature<"abi-vlen-"#I#"b", "ABIVLen", !cast<string>(I), "ABI_VLEN desc">;
1343-
}
1344-
13451336
// Some vector hardware implementations do not process all VLEN bits in parallel
13461337
// and instead split over multiple cycles. DLEN refers to the datapath width
13471338
// that can be done in parallel.

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
7575
#include "RISCVGenSubtargetInfo.inc"
7676

7777
unsigned ZvlLen = 0;
78-
unsigned ABIVLen = 0;
7978
unsigned RVVVectorBitsMin;
8079
unsigned RVVVectorBitsMax;
8180
uint8_t MaxInterleaveFactor = 2;

0 commit comments

Comments
 (0)