Skip to content

Commit 54b5d68

Browse files
committed
[RISCV][FMV] Support target_version
1 parent 9cd9377 commit 54b5d68

File tree

8 files changed

+1026
-16
lines changed

8 files changed

+1026
-16
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14270,9 +14270,16 @@ void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
1427014270
Target->initFeatureMap(FeatureMap, getDiagnostics(), TargetCPU, Features);
1427114271
}
1427214272
} else if (const auto *TV = FD->getAttr<TargetVersionAttr>()) {
14273-
llvm::SmallVector<StringRef, 8> Feats;
14274-
TV->getFeatures(Feats);
14275-
std::vector<std::string> Features = getFMVBackendFeaturesFor(Feats);
14273+
std::vector<std::string> Features;
14274+
if (Target->getTriple().isRISCV()) {
14275+
ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(TV->getName());
14276+
Features.insert(Features.begin(), ParsedAttr.Features.begin(),
14277+
ParsedAttr.Features.end());
14278+
} else {
14279+
llvm::SmallVector<StringRef, 8> Feats;
14280+
TV->getFeatures(Feats);
14281+
Features = getFMVBackendFeaturesFor(Feats);
14282+
}
1427614283
Features.insert(Features.begin(),
1427714284
Target->getTargetOpts().FeaturesAsWritten.begin(),
1427814285
Target->getTargetOpts().FeaturesAsWritten.end());

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4268,8 +4268,12 @@ void CodeGenModule::emitMultiVersionFunctions() {
42684268
} else if (const auto *TVA = CurFD->getAttr<TargetVersionAttr>()) {
42694269
if (TVA->isDefaultVersion() && IsDefined)
42704270
ShouldEmitResolver = true;
4271-
TVA->getFeatures(Feats);
42724271
llvm::Function *Func = createFunction(CurFD);
4272+
if (getTarget().getTriple().isRISCV()) {
4273+
Feats.push_back(TVA->getName());
4274+
} else {
4275+
TVA->getFeatures(Feats);
4276+
}
42734277
Options.emplace_back(Func, /*Architecture*/ "", Feats);
42744278
} else if (const auto *TC = CurFD->getAttr<TargetClonesAttr>()) {
42754279
if (IsDefined)

clang/lib/Sema/SemaDecl.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10319,8 +10319,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1031910319
// Handle attributes.
1032010320
ProcessDeclAttributes(S, NewFD, D);
1032110321
const auto *NewTVA = NewFD->getAttr<TargetVersionAttr>();
10322-
if (NewTVA && !NewTVA->isDefaultVersion() &&
10323-
!Context.getTargetInfo().hasFeature("fmv")) {
10322+
if (Context.getTargetInfo().getTriple().isRISCV()) {
10323+
// Go thought anyway.
10324+
} else if (NewTVA && !NewTVA->isDefaultVersion() &&
10325+
!Context.getTargetInfo().hasFeature("fmv")) {
1032410326
// Don't add to scope fmv functions declarations if fmv disabled
1032510327
AddToScope = false;
1032610328
return NewFD;
@@ -11027,13 +11029,27 @@ static bool CheckMultiVersionValue(Sema &S, const FunctionDecl *FD) {
1102711029
}
1102811030

1102911031
if (TVA) {
11030-
llvm::SmallVector<StringRef, 8> Feats;
11031-
TVA->getFeatures(Feats);
11032-
for (const auto &Feat : Feats) {
11033-
if (!TargetInfo.validateCpuSupports(Feat)) {
11034-
S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
11035-
<< Feature << Feat;
11036-
return true;
11032+
if (S.getASTContext().getTargetInfo().getTriple().isRISCV()) {
11033+
ParsedTargetAttr ParseInfo =
11034+
S.getASTContext().getTargetInfo().parseTargetAttr(TVA->getName());
11035+
for (const auto &Feat : ParseInfo.Features) {
11036+
StringRef BareFeat = StringRef{Feat}.substr(1);
11037+
11038+
if (!TargetInfo.isValidFeatureName(BareFeat)) {
11039+
S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
11040+
<< Feature << BareFeat;
11041+
return true;
11042+
}
11043+
}
11044+
} else {
11045+
llvm::SmallVector<StringRef, 8> Feats;
11046+
TVA->getFeatures(Feats);
11047+
for (const auto &Feat : Feats) {
11048+
if (!TargetInfo.validateCpuSupports(Feat)) {
11049+
S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
11050+
<< Feature << Feat;
11051+
return true;
11052+
}
1103711053
}
1103811054
}
1103911055
}
@@ -11314,7 +11330,8 @@ static bool PreviousDeclsHaveMultiVersionAttribute(const FunctionDecl *FD) {
1131411330
}
1131511331

1131611332
static void patchDefaultTargetVersion(FunctionDecl *From, FunctionDecl *To) {
11317-
if (!From->getASTContext().getTargetInfo().getTriple().isAArch64())
11333+
if (!From->getASTContext().getTargetInfo().getTriple().isAArch64() &&
11334+
!From->getASTContext().getTargetInfo().getTriple().isRISCV())
1131811335
return;
1131911336

1132011337
MultiVersionKind MVKindFrom = From->getMultiVersionKind();
@@ -15501,8 +15518,10 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
1550115518
FD->setInvalidDecl();
1550215519
}
1550315520
if (const auto *Attr = FD->getAttr<TargetVersionAttr>()) {
15504-
if (!Context.getTargetInfo().hasFeature("fmv") &&
15505-
!Attr->isDefaultVersion()) {
15521+
if (Context.getTargetInfo().getTriple().isRISCV()) {
15522+
// pass thought anyway.
15523+
} else if (!Context.getTargetInfo().hasFeature("fmv") &&
15524+
!Attr->isDefaultVersion()) {
1550615525
// If function multi versioning disabled skip parsing function body
1550715526
// defined with non-default target_version attribute
1550815527
if (SkipBody)

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,6 +3056,45 @@ bool Sema::checkTargetVersionAttr(SourceLocation LiteralLoc, Decl *D,
30563056
enum SecondParam { None };
30573057
enum ThirdParam { Target, TargetClones, TargetVersion };
30583058
llvm::SmallVector<StringRef, 8> Features;
3059+
if (Context.getTargetInfo().getTriple().isRISCV()) {
3060+
3061+
llvm::SmallVector<StringRef, 8> AttrStrs;
3062+
AttrStr.split(AttrStrs, ";");
3063+
3064+
bool IsPriority = false;
3065+
bool IsDefault = false;
3066+
for (auto &AttrStr : AttrStrs) {
3067+
// Only support arch=+ext,... syntax.
3068+
if (AttrStr.starts_with("arch=+")) {
3069+
ParsedTargetAttr TargetAttr =
3070+
Context.getTargetInfo().parseTargetAttr(AttrStr);
3071+
3072+
if (TargetAttr.Features.empty() ||
3073+
llvm::any_of(TargetAttr.Features, [&](const StringRef Ext) {
3074+
return !RISCV().isValidFMVExtension(Ext);
3075+
}))
3076+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3077+
<< Unsupported << None << AttrStr << TargetVersion;
3078+
} else if (AttrStr.starts_with("default")) {
3079+
IsDefault = true;
3080+
} else if (AttrStr.consume_front("priority=")) {
3081+
IsPriority = true;
3082+
int Digit;
3083+
if (AttrStr.getAsInteger(0, Digit))
3084+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3085+
<< Unsupported << None << AttrStr << TargetVersion;
3086+
} else {
3087+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3088+
<< Unsupported << None << AttrStr << TargetVersion;
3089+
}
3090+
}
3091+
3092+
if (IsPriority && IsDefault)
3093+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3094+
<< Unsupported << None << AttrStr << TargetVersion;
3095+
3096+
return false;
3097+
}
30593098
AttrStr.split(Features, "+");
30603099
for (auto &CurFeature : Features) {
30613100
CurFeature = CurFeature.trim();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: not %clang_cc1 -triple riscv64 -target-feature +i -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORT-OS
2+
3+
// CHECK-UNSUPPORT-OS: error: target_clones is currently only supported on Linux
4+
__attribute__((target_version("default"))) int foo(void) {
5+
return 2;
6+
}
7+
8+
__attribute__((target_version("arch=+c"))) int foo(void) {
9+
return 2;
10+
}
11+
12+
13+
int bar() { return foo(); }

0 commit comments

Comments
 (0)