Skip to content

Commit 51c5233

Browse files
committed
[clang][RISCV] Support function attribute __attribute__((target("+attr")))
It is currently not possible to use "RVV type" and "RVV intrinsics" if the "zve32x" is not enabled globally. However in some cases we may want to use them only in some functions, for instance: ``` #include <riscv_vector.h> __attribute__((target("+zve32x"))) vint32m1_t rvv_add(vint32m1_t v1, vint32m1_t v2, size_t vl) { return __riscv_vadd(v1, v2, vl); } int other_add(int i1, int i2) { return i1 + i2; } ``` , it is supposed to be compilable even the vector is not specified, e.g. `clang -target riscv64 -march=rv64gc -S test.c`.
1 parent ca827d5 commit 51c5233

File tree

10 files changed

+73
-82
lines changed

10 files changed

+73
-82
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14067,7 +14067,8 @@ class Sema final {
1406714067
bool CheckRISCVLMUL(CallExpr *TheCall, unsigned ArgNum);
1406814068
bool CheckRISCVBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
1406914069
CallExpr *TheCall);
14070-
void checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D);
14070+
void checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D,
14071+
const llvm::StringMap<bool> &FeatureMap);
1407114072
bool CheckLoongArchBuiltinFunctionCall(const TargetInfo &TI,
1407214073
unsigned BuiltinID, CallExpr *TheCall);
1407314074
bool CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI,

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ ParsedTargetAttr RISCVTargetInfo::parseTargetAttr(StringRef Features) const {
463463
Ret.Duplicate = "tune=";
464464

465465
Ret.Tune = AttrString;
466-
}
466+
} else if (Feature.starts_with("+"))
467+
Ret.Features.push_back(Feature.str());
467468
}
468469
return Ret;
469470
}

clang/lib/Sema/Sema.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,8 +2077,11 @@ void Sema::checkTypeSupport(QualType Ty, SourceLocation Loc, ValueDecl *D) {
20772077
targetDiag(D->getLocation(), diag::note_defined_here, FD) << D;
20782078
}
20792079

2080-
if (TI.hasRISCVVTypes() && Ty->isRVVSizelessBuiltinType())
2081-
checkRVVTypeSupport(Ty, Loc, D);
2080+
if (TI.hasRISCVVTypes() && Ty->isRVVSizelessBuiltinType() && FD) {
2081+
llvm::StringMap<bool> CallerFeatureMap;
2082+
Context.getFunctionFeatureMap(CallerFeatureMap, FD);
2083+
checkRVVTypeSupport(Ty, Loc, D, CallerFeatureMap);
2084+
}
20822085

20832086
// Don't allow SVE types in functions without a SVE target.
20842087
if (Ty->isSVESizelessBuiltinType() && FD && FD->hasBody()) {

clang/lib/Sema/SemaChecking.cpp

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5415,57 +5415,6 @@ static bool CheckInvalidVLENandLMUL(const TargetInfo &TI, CallExpr *TheCall,
54155415
bool Sema::CheckRISCVBuiltinFunctionCall(const TargetInfo &TI,
54165416
unsigned BuiltinID,
54175417
CallExpr *TheCall) {
5418-
// CodeGenFunction can also detect this, but this gives a better error
5419-
// message.
5420-
bool FeatureMissing = false;
5421-
SmallVector<StringRef> ReqFeatures;
5422-
StringRef Features = Context.BuiltinInfo.getRequiredFeatures(BuiltinID);
5423-
Features.split(ReqFeatures, ',', -1, false);
5424-
5425-
// Check if each required feature is included
5426-
for (StringRef F : ReqFeatures) {
5427-
SmallVector<StringRef> ReqOpFeatures;
5428-
F.split(ReqOpFeatures, '|');
5429-
5430-
if (llvm::none_of(ReqOpFeatures,
5431-
[&TI](StringRef OF) { return TI.hasFeature(OF); })) {
5432-
std::string FeatureStrs;
5433-
bool IsExtension = true;
5434-
for (StringRef OF : ReqOpFeatures) {
5435-
// If the feature is 64bit, alter the string so it will print better in
5436-
// the diagnostic.
5437-
if (OF == "64bit") {
5438-
assert(ReqOpFeatures.size() == 1 && "Expected '64bit' to be alone");
5439-
OF = "RV64";
5440-
IsExtension = false;
5441-
}
5442-
if (OF == "32bit") {
5443-
assert(ReqOpFeatures.size() == 1 && "Expected '32bit' to be alone");
5444-
OF = "RV32";
5445-
IsExtension = false;
5446-
}
5447-
5448-
// Convert features like "zbr" and "experimental-zbr" to "Zbr".
5449-
OF.consume_front("experimental-");
5450-
std::string FeatureStr = OF.str();
5451-
FeatureStr[0] = std::toupper(FeatureStr[0]);
5452-
// Combine strings.
5453-
FeatureStrs += FeatureStrs.empty() ? "" : ", ";
5454-
FeatureStrs += "'";
5455-
FeatureStrs += FeatureStr;
5456-
FeatureStrs += "'";
5457-
}
5458-
// Error message
5459-
FeatureMissing = true;
5460-
Diag(TheCall->getBeginLoc(), diag::err_riscv_builtin_requires_extension)
5461-
<< IsExtension
5462-
<< TheCall->getSourceRange() << StringRef(FeatureStrs);
5463-
}
5464-
}
5465-
5466-
if (FeatureMissing)
5467-
return true;
5468-
54695418
// vmulh.vv, vmulh.vx, vmulhu.vv, vmulhu.vx, vmulhsu.vv, vmulhsu.vx,
54705419
// vsmul.vv, vsmul.vx are not included for EEW=64 in Zve64*.
54715420
switch (BuiltinID) {
@@ -6369,9 +6318,8 @@ bool Sema::CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI,
63696318
return false;
63706319
}
63716320

6372-
void Sema::checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D) {
6373-
const TargetInfo &TI = Context.getTargetInfo();
6374-
6321+
void Sema::checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D,
6322+
const llvm::StringMap<bool> &FeatureMap) {
63756323
ASTContext::BuiltinVectorTypeInfo Info =
63766324
Context.getBuiltinVectorTypeInfo(Ty->castAs<BuiltinType>());
63776325
unsigned EltSize = Context.getTypeSize(Info.ElementType);
@@ -6380,24 +6328,24 @@ void Sema::checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D) {
63806328
// (ELEN, LMUL) pairs of (8, mf8), (16, mf4), (32, mf2), (64, m1) requires at
63816329
// least zve64x
63826330
if (((EltSize == 64 && Info.ElementType->isIntegerType()) || MinElts == 1) &&
6383-
!TI.hasFeature("zve64x"))
6331+
!FeatureMap.lookup("zve64x"))
63846332
Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve64x";
6385-
else if (Info.ElementType->isFloat16Type() && !TI.hasFeature("zvfh") &&
6386-
!TI.hasFeature("zvfhmin"))
6333+
else if (Info.ElementType->isFloat16Type() && !FeatureMap.lookup("zvfh") &&
6334+
!FeatureMap.lookup("zvfhmin"))
63876335
Diag(Loc, diag::err_riscv_type_requires_extension, D)
63886336
<< Ty << "zvfh or zvfhmin";
63896337
else if (Info.ElementType->isBFloat16Type() &&
6390-
!TI.hasFeature("experimental-zvfbfmin"))
6338+
!FeatureMap.lookup("experimental-zvfbfmin"))
63916339
Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zvfbfmin";
63926340
else if (Info.ElementType->isSpecificBuiltinType(BuiltinType::Float) &&
6393-
!TI.hasFeature("zve32f"))
6341+
!FeatureMap.lookup("zve32f"))
63946342
Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve32f";
63956343
else if (Info.ElementType->isSpecificBuiltinType(BuiltinType::Double) &&
6396-
!TI.hasFeature("zve64d"))
6344+
!FeatureMap.lookup("zve64d"))
63976345
Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve64d";
63986346
// Given that caller already checked isRVVType() before calling this function,
63996347
// if we don't have at least zve32x supported, then we need to emit error.
6400-
else if (!TI.hasFeature("zve32x"))
6348+
else if (!FeatureMap.lookup("zve32x"))
64016349
Diag(Loc, diag::err_riscv_type_requires_extension, D) << Ty << "zve32x";
64026350
}
64036351

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8917,8 +8917,13 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
89178917
}
89188918
}
89198919

8920-
if (T->isRVVSizelessBuiltinType())
8921-
checkRVVTypeSupport(T, NewVD->getLocation(), cast<Decl>(CurContext));
8920+
if (T->isRVVSizelessBuiltinType() && isa<FunctionDecl>(CurContext)) {
8921+
const FunctionDecl *FD = cast<FunctionDecl>(CurContext);
8922+
llvm::StringMap<bool> CallerFeatureMap;
8923+
Context.getFunctionFeatureMap(CallerFeatureMap, FD);
8924+
checkRVVTypeSupport(T, NewVD->getLocation(), cast<Decl>(CurContext),
8925+
CallerFeatureMap);
8926+
}
89228927
}
89238928

89248929
/// Perform semantic checking on a newly-created variable
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2-
// RUN: %clang_cc1 -triple riscv32 -target-feature +zbb -verify %s -o -
2+
// RUN: %clang_cc1 -triple riscv32 -target-feature +zbb -S -verify %s -o -
33

44
unsigned int orc_b_64(unsigned int a) {
5-
return __builtin_riscv_orc_b_64(a); // expected-error {{builtin requires: 'RV64'}}
5+
return __builtin_riscv_orc_b_64(a); // expected-error {{'__builtin_riscv_orc_b_64' needs target feature zbb,64bit}}
66
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2-
// RUN: %clang_cc1 -triple riscv64 -target-feature +zbkb -verify %s -o -
2+
// RUN: %clang_cc1 -triple riscv64 -target-feature +zbkb -S -verify %s -o -
33

44
#include <stdint.h>
55

6-
uint32_t zip(uint32_t rs1)
6+
uint32_t zip_unzip(uint32_t rs1)
77
{
8-
return __builtin_riscv_zip_32(rs1); // expected-error {{builtin requires: 'RV32'}}
9-
}
10-
11-
uint32_t unzip(uint32_t rs1)
12-
{
13-
return __builtin_riscv_unzip_32(rs1); // expected-error {{builtin requires: 'RV32'}}
8+
(void)__builtin_riscv_zip_32(rs1); // expected-error {{'__builtin_riscv_zip_32' needs target feature zbkb,32bit}}
9+
return __builtin_riscv_unzip_32(rs1); // expected-error {{'__builtin_riscv_unzip_32' needs target feature zbkb,32bit}}
1410
}

clang/test/CodeGen/RISCV/rvv-intrinsics-handcrafted/rvv-error.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// CHECK-RV64V-NEXT: ret i32 [[CONV]]
1212
//
1313

14-
// CHECK-RV64-ERR: error: builtin requires at least one of the following extensions: 'Zve32x'
14+
// CHECK-RV64-ERR: error: '__builtin_rvv_vsetvli' needs target feature zve32x
1515

1616
int test() {
1717
return __builtin_rvv_vsetvli(1, 0, 0);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2+
// RUN: %clang_cc1 -triple riscv64 -S -verify %s
3+
4+
// REQUIRES: riscv-registered-target
5+
#include <riscv_vector.h>
6+
7+
void test_builtin() {
8+
__riscv_vsetvl_e8m8(1); // expected-error {{'__builtin_rvv_vsetvli' needs target feature zve32x}}
9+
}
10+
11+
__attribute__((target("+zve32x")))
12+
void test_builtin_w_zve32x() {
13+
__riscv_vsetvl_e8m8(1);
14+
}
15+
16+
void test_rvv_i32_type() {
17+
vint32m1_t v; // expected-error {{RISC-V type 'vint32m1_t' (aka '__rvv_int32m1_t') requires the 'zve32x' extension}}
18+
}
19+
20+
__attribute__((target("+zve32x")))
21+
void test_rvv_i32_type_w_zve32x() {
22+
vint32m1_t v;
23+
}
24+
25+
void test_rvv_f32_type() {
26+
vfloat32m1_t v; // expected-error {{RISC-V type 'vfloat32m1_t' (aka '__rvv_float32m1_t') requires the 'zve32f' extension}}
27+
}
28+
29+
__attribute__((target("+zve32f")))
30+
void test_rvv_f32_type_w_zve32f() {
31+
vfloat32m1_t v;
32+
}
33+
34+
void test_rvv_f64_type() {
35+
vfloat64m1_t v; // expected-error {{RISC-V type 'vfloat64m1_t' (aka '__rvv_float64m1_t') requires the 'zve64x' extension}}
36+
}
37+
38+
__attribute__((target("+zve64d")))
39+
void test_rvv_f64_type_w_zve64d() {
40+
vfloat64m1_t v;
41+
}

clang/utils/TableGen/RISCVVEmitter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,6 @@ void RVVEmitter::createHeader(raw_ostream &OS) {
334334
OS << "#include <stdint.h>\n";
335335
OS << "#include <stddef.h>\n\n";
336336

337-
OS << "#ifndef __riscv_vector\n";
338-
OS << "#error \"Vector intrinsics require the vector extension.\"\n";
339-
OS << "#endif\n\n";
340-
341337
OS << "#ifdef __cplusplus\n";
342338
OS << "extern \"C\" {\n";
343339
OS << "#endif\n\n";

0 commit comments

Comments
 (0)