Skip to content

[Clang][RISCV] Recognize unsupport target feature by supporting isValidFeatureName #106495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions clang/lib/Basic/Targets/RISCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class RISCVTargetInfo : public TargetInfo {
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:
Expand Down
15 changes: 11 additions & 4 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Sema/attr-target-riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Loading