Skip to content

Commit 02ef996

Browse files
committed
[RISCV] Prevent __builtin_riscv_orc_b_64 from being compiled RV32 target.
The backend can't handle this and will throw a fatal error from type legalization. It's easy enough to fix that for this intrinsic by just splitting the IR intrinsic since it works on individual bytes. There will be other intrinsics in the future that would be harder to support through splitting, for example grev, gorc, and shfl. Those would require a compare and a select be inserted to check the MSB of their control input. This patch adds support for preventing this in the frontend with a nice diagnostic. Reviewed By: frasercrmck Differential Revision: https://reviews.llvm.org/D99984
1 parent 5f0ac1e commit 02ef996

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

clang/include/clang/Basic/BuiltinsRISCV.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
// Zbb extension
2121
TARGET_BUILTIN(__builtin_riscv_orc_b_32, "ZiZi", "nc", "experimental-zbb")
22-
TARGET_BUILTIN(__builtin_riscv_orc_b_64, "WiWi", "nc", "experimental-zbb")
22+
TARGET_BUILTIN(__builtin_riscv_orc_b_64, "WiWi", "nc", "experimental-zbb,64bit")
2323

2424
// Zbc extension
2525
TARGET_BUILTIN(__builtin_riscv_clmul, "LiLiLi", "nc", "experimental-zbc")

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,24 @@ ArrayRef<Builtin::Info> RISCVTargetInfo::getTargetBuiltins() const {
239239
Builtin::FirstTSBuiltin);
240240
}
241241

242+
bool RISCVTargetInfo::initFeatureMap(
243+
llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
244+
const std::vector<std::string> &FeaturesVec) const {
245+
246+
if (getTriple().getArch() == llvm::Triple::riscv64)
247+
Features["64bit"] = true;
248+
249+
return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
250+
}
251+
242252
/// Return true if has this feature, need to sync with handleTargetFeatures.
243253
bool RISCVTargetInfo::hasFeature(StringRef Feature) const {
244254
bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64;
245255
return llvm::StringSwitch<bool>(Feature)
246256
.Case("riscv", true)
247257
.Case("riscv32", !Is64Bit)
248258
.Case("riscv64", Is64Bit)
259+
.Case("64bit", Is64Bit)
249260
.Case("m", HasM)
250261
.Case("a", HasA)
251262
.Case("f", HasF)

clang/lib/Basic/Targets/RISCV.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class RISCVTargetInfo : public TargetInfo {
9999

100100
std::string convertConstraint(const char *&Constraint) const override;
101101

102+
bool
103+
initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
104+
StringRef CPU,
105+
const std::vector<std::string> &FeaturesVec) const override;
106+
102107
bool hasFeature(StringRef Feature) const override;
103108

104109
bool handleTargetFeatures(std::vector<std::string> &Features,

clang/lib/Sema/SemaChecking.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,12 +3422,18 @@ bool Sema::CheckRISCVBuiltinFunctionCall(const TargetInfo &TI,
34223422
Features.split(ReqFeatures, ',');
34233423

34243424
// Check if each required feature is included
3425-
for (auto &I : ReqFeatures) {
3426-
if (TI.hasFeature(I))
3425+
for (StringRef F : ReqFeatures) {
3426+
if (TI.hasFeature(F))
34273427
continue;
3428+
3429+
// If the feature is 64bit, alter the string so it will print better in
3430+
// the diagnostic.
3431+
if (F == "64bit")
3432+
F = "RV64";
3433+
34283434
// Convert features like "zbr" and "experimental-zbr" to "Zbr".
3429-
I.consume_front("experimental-");
3430-
std::string FeatureStr = I.str();
3435+
F.consume_front("experimental-");
3436+
std::string FeatureStr = F.str();
34313437
FeatureStr[0] = std::toupper(FeatureStr[0]);
34323438

34333439
// Error message
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2+
// RUN: %clang_cc1 -triple riscv32 -target-feature +experimental-zbb -verify %s -o -
3+
4+
int orc_b_64(int a) {
5+
return __builtin_riscv_orc_b_64(a); // expected-error {{builtin requires 'RV64' extension support to be enabled}}
6+
}

0 commit comments

Comments
 (0)