Skip to content

Commit af2ba0f

Browse files
committed
[RISCV][FMV] Support target_version
1 parent fc134b2 commit af2ba0f

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
@@ -14203,9 +14203,16 @@ void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
1420314203
Target->initFeatureMap(FeatureMap, getDiagnostics(), TargetCPU, Features);
1420414204
}
1420514205
} else if (const auto *TV = FD->getAttr<TargetVersionAttr>()) {
14206-
llvm::SmallVector<StringRef, 8> Feats;
14207-
TV->getFeatures(Feats);
14208-
std::vector<std::string> Features = getFMVBackendFeaturesFor(Feats);
14206+
std::vector<std::string> Features;
14207+
if (Target->getTriple().isRISCV()) {
14208+
ParsedTargetAttr ParsedAttr = Target->parseTargetAttr(TV->getName());
14209+
Features.insert(Features.begin(), ParsedAttr.Features.begin(),
14210+
ParsedAttr.Features.end());
14211+
} else {
14212+
llvm::SmallVector<StringRef, 8> Feats;
14213+
TV->getFeatures(Feats);
14214+
Features = getFMVBackendFeaturesFor(Feats);
14215+
}
1420914216
Features.insert(Features.begin(),
1421014217
Target->getTargetOpts().FeaturesAsWritten.begin(),
1421114218
Target->getTargetOpts().FeaturesAsWritten.end());

clang/lib/CodeGen/CodeGenModule.cpp

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

clang/lib/Sema/SemaDecl.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10308,8 +10308,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1030810308
// Handle attributes.
1030910309
ProcessDeclAttributes(S, NewFD, D);
1031010310
const auto *NewTVA = NewFD->getAttr<TargetVersionAttr>();
10311-
if (NewTVA && !NewTVA->isDefaultVersion() &&
10312-
!Context.getTargetInfo().hasFeature("fmv")) {
10311+
if (Context.getTargetInfo().getTriple().isRISCV()) {
10312+
// Go thought anyway.
10313+
} else if (NewTVA && !NewTVA->isDefaultVersion() &&
10314+
!Context.getTargetInfo().hasFeature("fmv")) {
1031310315
// Don't add to scope fmv functions declarations if fmv disabled
1031410316
AddToScope = false;
1031510317
return NewFD;
@@ -11016,13 +11018,27 @@ static bool CheckMultiVersionValue(Sema &S, const FunctionDecl *FD) {
1101611018
}
1101711019

1101811020
if (TVA) {
11019-
llvm::SmallVector<StringRef, 8> Feats;
11020-
TVA->getFeatures(Feats);
11021-
for (const auto &Feat : Feats) {
11022-
if (!TargetInfo.validateCpuSupports(Feat)) {
11023-
S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
11024-
<< Feature << Feat;
11025-
return true;
11021+
if (S.getASTContext().getTargetInfo().getTriple().isRISCV()) {
11022+
ParsedTargetAttr ParseInfo =
11023+
S.getASTContext().getTargetInfo().parseTargetAttr(TVA->getName());
11024+
for (const auto &Feat : ParseInfo.Features) {
11025+
StringRef BareFeat = StringRef{Feat}.substr(1);
11026+
11027+
if (!TargetInfo.isValidFeatureName(BareFeat)) {
11028+
S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
11029+
<< Feature << BareFeat;
11030+
return true;
11031+
}
11032+
}
11033+
} else {
11034+
llvm::SmallVector<StringRef, 8> Feats;
11035+
TVA->getFeatures(Feats);
11036+
for (const auto &Feat : Feats) {
11037+
if (!TargetInfo.validateCpuSupports(Feat)) {
11038+
S.Diag(FD->getLocation(), diag::err_bad_multiversion_option)
11039+
<< Feature << Feat;
11040+
return true;
11041+
}
1102611042
}
1102711043
}
1102811044
}
@@ -11303,7 +11319,8 @@ static bool PreviousDeclsHaveMultiVersionAttribute(const FunctionDecl *FD) {
1130311319
}
1130411320

1130511321
static void patchDefaultTargetVersion(FunctionDecl *From, FunctionDecl *To) {
11306-
if (!From->getASTContext().getTargetInfo().getTriple().isAArch64())
11322+
if (!From->getASTContext().getTargetInfo().getTriple().isAArch64() &&
11323+
!From->getASTContext().getTargetInfo().getTriple().isRISCV())
1130711324
return;
1130811325

1130911326
MultiVersionKind MVKindFrom = From->getMultiVersionKind();
@@ -15491,8 +15508,10 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
1549115508
FD->setInvalidDecl();
1549215509
}
1549315510
if (const auto *Attr = FD->getAttr<TargetVersionAttr>()) {
15494-
if (!Context.getTargetInfo().hasFeature("fmv") &&
15495-
!Attr->isDefaultVersion()) {
15511+
if (Context.getTargetInfo().getTriple().isRISCV()) {
15512+
// pass thought anyway.
15513+
} else if (!Context.getTargetInfo().hasFeature("fmv") &&
15514+
!Attr->isDefaultVersion()) {
1549615515
// If function multi versioning disabled skip parsing function body
1549715516
// defined with non-default target_version attribute
1549815517
if (SkipBody)

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,45 @@ bool Sema::checkTargetVersionAttr(SourceLocation LiteralLoc, Decl *D,
30333033
enum SecondParam { None };
30343034
enum ThirdParam { Target, TargetClones, TargetVersion };
30353035
llvm::SmallVector<StringRef, 8> Features;
3036+
if (Context.getTargetInfo().getTriple().isRISCV()) {
3037+
3038+
llvm::SmallVector<StringRef, 8> AttrStrs;
3039+
AttrStr.split(AttrStrs, ";");
3040+
3041+
bool IsPriority = false;
3042+
bool IsDefault = false;
3043+
for (auto &AttrStr : AttrStrs) {
3044+
// Only support arch=+ext,... syntax.
3045+
if (AttrStr.starts_with("arch=+")) {
3046+
ParsedTargetAttr TargetAttr =
3047+
Context.getTargetInfo().parseTargetAttr(AttrStr);
3048+
3049+
if (TargetAttr.Features.empty() ||
3050+
llvm::any_of(TargetAttr.Features, [&](const StringRef Ext) {
3051+
return !RISCV().isValidFMVExtension(Ext);
3052+
}))
3053+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3054+
<< Unsupported << None << AttrStr << TargetVersion;
3055+
} else if (AttrStr.starts_with("default")) {
3056+
IsDefault = true;
3057+
} else if (AttrStr.consume_front("priority=")) {
3058+
IsPriority = true;
3059+
int Digit;
3060+
if (AttrStr.getAsInteger(0, Digit))
3061+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3062+
<< Unsupported << None << AttrStr << TargetVersion;
3063+
} else {
3064+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3065+
<< Unsupported << None << AttrStr << TargetVersion;
3066+
}
3067+
}
3068+
3069+
if (IsPriority && IsDefault)
3070+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3071+
<< Unsupported << None << AttrStr << TargetVersion;
3072+
3073+
return false;
3074+
}
30363075
AttrStr.split(Features, "+");
30373076
for (auto &CurFeature : Features) {
30383077
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)