Skip to content

Commit 022b3c2

Browse files
authored
[Clang][RISCV] Recognize unsupport target feature by supporting isValidFeatureName (#106495)
This patch makes unsupported target attributes emit a warning and ignore the target attribute during semantic checks. The changes include: 1. Adding the RISCVTargetInfo::isValidFeatureName function. 2. Rejecting non-full-arch strings in the handleFullArchString function. 3. Adding test cases to demonstrate the warning behavior.
1 parent 9347b66 commit 022b3c2

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static void handleFullArchString(StringRef FullArchStr,
388388
FullArchStr, /* EnableExperimentalExtension */ true);
389389
if (llvm::errorToBool(RII.takeError())) {
390390
// Forward the invalid FullArchStr.
391-
Features.push_back("+" + FullArchStr.str());
391+
Features.push_back(FullArchStr.str());
392392
} else {
393393
// Append a full list of features, including any negative extensions so that
394394
// we override the CPU's features.
@@ -478,3 +478,7 @@ bool RISCVTargetInfo::validateCpuSupports(StringRef Feature) const {
478478
// __riscv_feature_bits structure.
479479
return -1 != llvm::RISCVISAInfo::getRISCVFeaturesBitsInfo(Feature).second;
480480
}
481+
482+
bool RISCVTargetInfo::isValidFeatureName(StringRef Name) const {
483+
return llvm::RISCVISAInfo::isSupportedExtensionFeature(Name);
484+
}

clang/lib/Basic/Targets/RISCV.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class RISCVTargetInfo : public TargetInfo {
130130
bool supportsCpuSupports() const override { return getTriple().isOSLinux(); }
131131
bool supportsCpuInit() const override { return getTriple().isOSLinux(); }
132132
bool validateCpuSupports(StringRef Feature) const override;
133+
bool isValidFeatureName(StringRef Name) const override;
133134
};
134135
class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
135136
public:

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,10 +2993,17 @@ bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
29932993
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
29942994
<< Unknown << Tune << ParsedAttrs.Tune << Target;
29952995

2996-
if (Context.getTargetInfo().getTriple().isRISCV() &&
2997-
ParsedAttrs.Duplicate != "")
2998-
return Diag(LiteralLoc, diag::err_duplicate_target_attribute)
2999-
<< Duplicate << None << ParsedAttrs.Duplicate << Target;
2996+
if (Context.getTargetInfo().getTriple().isRISCV()) {
2997+
if (ParsedAttrs.Duplicate != "")
2998+
return Diag(LiteralLoc, diag::err_duplicate_target_attribute)
2999+
<< Duplicate << None << ParsedAttrs.Duplicate << Target;
3000+
for (const auto &Feature : ParsedAttrs.Features) {
3001+
StringRef CurFeature = Feature;
3002+
if (!CurFeature.starts_with('+') && !CurFeature.starts_with('-'))
3003+
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3004+
<< Unsupported << None << AttrStr << Target;
3005+
}
3006+
}
30003007

30013008
if (ParsedAttrs.Duplicate != "")
30023009
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)

clang/test/Sema/attr-target-riscv.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@
44
int __attribute__((target("arch=rv64g"))) foo(void) { return 0; }
55
//expected-error@+1 {{redefinition of 'foo'}}
66
int __attribute__((target("arch=rv64gc"))) foo(void) { return 0; }
7+
8+
//expected-warning@+1 {{unsupported 'notafeature' in the 'target' attribute string; 'target' attribute ignored}}
9+
int __attribute__((target("arch=+notafeature"))) UnsupportFeature(void) { return 0; }
10+
11+
//expected-warning@+1 {{unsupported 'notafeature' in the 'target' attribute string; 'target' attribute ignored}}
12+
int __attribute__((target("arch=-notafeature"))) UnsupportNegativeFeature(void) { return 0; }
13+
14+
//expected-warning@+1 {{unsupported 'arch=+zba,zbb' in the 'target' attribute string; 'target' attribute ignored}}
15+
int __attribute__((target("arch=+zba,zbb"))) WithoutPlus(void) { return 0; }
16+
17+
//expected-warning@+1 {{unsupported 'arch=zba' in the 'target' attribute string; 'target' attribute ignored}}
18+
int __attribute__((target("arch=zba"))) WithoutPlus2(void) { return 0; }

0 commit comments

Comments
 (0)