[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.
This commit is contained in:
Piyou Chen 2024-09-09 15:07:39 +08:00 committed by GitHub
parent 9347b66cfc
commit 022b3c27e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 5 deletions

View File

@ -388,7 +388,7 @@ static void handleFullArchString(StringRef FullArchStr,
FullArchStr, /* EnableExperimentalExtension */ true);
if (llvm::errorToBool(RII.takeError())) {
// Forward the invalid FullArchStr.
Features.push_back("+" + FullArchStr.str());
Features.push_back(FullArchStr.str());
} else {
// Append a full list of features, including any negative extensions so that
// we override the CPU's features.
@ -478,3 +478,7 @@ bool RISCVTargetInfo::validateCpuSupports(StringRef Feature) const {
// __riscv_feature_bits structure.
return -1 != llvm::RISCVISAInfo::getRISCVFeaturesBitsInfo(Feature).second;
}
bool RISCVTargetInfo::isValidFeatureName(StringRef Name) const {
return llvm::RISCVISAInfo::isSupportedExtensionFeature(Name);
}

View File

@ -130,6 +130,7 @@ public:
bool supportsCpuSupports() const override { return getTriple().isOSLinux(); }
bool supportsCpuInit() const override { return getTriple().isOSLinux(); }
bool validateCpuSupports(StringRef Feature) const override;
bool isValidFeatureName(StringRef Name) const override;
};
class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
public:

View File

@ -2993,10 +2993,17 @@ bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
<< Unknown << Tune << ParsedAttrs.Tune << Target;
if (Context.getTargetInfo().getTriple().isRISCV() &&
ParsedAttrs.Duplicate != "")
return Diag(LiteralLoc, diag::err_duplicate_target_attribute)
<< Duplicate << None << ParsedAttrs.Duplicate << Target;
if (Context.getTargetInfo().getTriple().isRISCV()) {
if (ParsedAttrs.Duplicate != "")
return Diag(LiteralLoc, diag::err_duplicate_target_attribute)
<< Duplicate << None << ParsedAttrs.Duplicate << Target;
for (const auto &Feature : ParsedAttrs.Features) {
StringRef CurFeature = Feature;
if (!CurFeature.starts_with('+') && !CurFeature.starts_with('-'))
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
<< Unsupported << None << AttrStr << Target;
}
}
if (ParsedAttrs.Duplicate != "")
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)

View File

@ -4,3 +4,15 @@
int __attribute__((target("arch=rv64g"))) foo(void) { return 0; }
//expected-error@+1 {{redefinition of 'foo'}}
int __attribute__((target("arch=rv64gc"))) foo(void) { return 0; }
//expected-warning@+1 {{unsupported 'notafeature' in the 'target' attribute string; 'target' attribute ignored}}
int __attribute__((target("arch=+notafeature"))) UnsupportFeature(void) { return 0; }
//expected-warning@+1 {{unsupported 'notafeature' in the 'target' attribute string; 'target' attribute ignored}}
int __attribute__((target("arch=-notafeature"))) UnsupportNegativeFeature(void) { return 0; }
//expected-warning@+1 {{unsupported 'arch=+zba,zbb' in the 'target' attribute string; 'target' attribute ignored}}
int __attribute__((target("arch=+zba,zbb"))) WithoutPlus(void) { return 0; }
//expected-warning@+1 {{unsupported 'arch=zba' in the 'target' attribute string; 'target' attribute ignored}}
int __attribute__((target("arch=zba"))) WithoutPlus2(void) { return 0; }