Skip to content

Commit 99c0a3e

Browse files
authored
[RISCV] Enable target attribute when invoked through clang driver (#74889)
d80e46d added support for the target function attribute. However, it turns out that commit has a nasty bug/oversight. As the tests in that revision show, everything works if clang -cc1 is directly invoked. I was suprised to learn this morning that compiling with clang (i.e. the typical user workflow) did not work. The bug is that if a set of explicit negative extensions is passed to cc1 at the command line (as the clang driver always does), we were copying these negative extensions to the end of the rewritten extension list. When this was later parsed, this had the effect of turning back off any extension that the target attribute had enabled. This patch updates the logic to only propagate the features from the input which don't appear in the rewritten form in either positive or negative form. Note that this code structure is still highly suspect. In particular I'm fairly sure that mixing extension versions with this code will result in odd results. However, I figure its better to have something which mostly works than something which doesn't work at all.
1 parent e4f3ec2 commit 99c0a3e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,18 @@ bool RISCVTargetInfo::initFeatureMap(
304304

305305
// RISCVISAInfo makes implications for ISA features
306306
std::vector<std::string> ImpliedFeatures = (*ParseResult)->toFeatureVector();
307-
// Add non-ISA features like `relax` and `save-restore` back
308-
for (const std::string &Feature : NewFeaturesVec)
309-
if (!llvm::is_contained(ImpliedFeatures, Feature))
310-
ImpliedFeatures.push_back(Feature);
311307

308+
// parseFeatures normalizes the feature set by dropping any explicit
309+
// negatives, and non-extension features. We need to preserve the later
310+
// for correctness and want to preserve the former for consistency.
311+
for (auto &Feature : NewFeaturesVec) {
312+
StringRef ExtName = Feature;
313+
assert(ExtName.size() > 1 && (ExtName[0] == '+' || ExtName[0] == '-'));
314+
ExtName = ExtName.drop_front(1); // Drop '+' or '-'
315+
if (!llvm::is_contained(ImpliedFeatures, ("+" + ExtName).str()) &&
316+
!llvm::is_contained(ImpliedFeatures, ("-" + ExtName).str()))
317+
ImpliedFeatures.push_back(Feature);
318+
}
312319
return TargetInfo::initFeatureMap(Features, Diags, CPU, ImpliedFeatures);
313320
}
314321

clang/test/CodeGen/RISCV/riscv-func-attr-target.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// REQUIRES: riscv-registered-target
22
// RUN: %clang_cc1 -triple riscv64 -target-feature +zifencei -target-feature +m \
3-
// RUN: -target-feature +a -target-feature +save-restore \
3+
// RUN: -target-feature +a -target-feature +save-restore -target-feature -zbb \
4+
// RUN: -target-feature -relax -target-feature -zfa \
45
// RUN: -emit-llvm %s -o - | FileCheck %s
56

67
// CHECK-LABEL: define dso_local void @testDefault
@@ -35,12 +36,12 @@ testAttrFullArchAndAttrCpu() {}
3536
__attribute__((target("cpu=sifive-u54"))) void testAttrCpuOnly() {}
3637

3738
//.
38-
// CHECK: attributes #0 = { {{.*}}"target-features"="+64bit,+a,+m,+save-restore,+zifencei" }
39-
// CHECK: attributes #1 = { {{.*}}"target-cpu"="rocket-rv64" "target-features"="+64bit,+a,+d,+f,+m,+save-restore,+v,+zicsr,+zifencei,+zve32f,+zve32x,+zve64d,+zve64f,+zve64x,+zvl128b,+zvl32b,+zvl64b" "tune-cpu"="generic-rv64" }
40-
// CHECK: attributes #2 = { {{.*}}"target-features"="+64bit,+a,+m,+save-restore,+zbb,+zifencei" }
41-
// CHECK: attributes #3 = { {{.*}}"target-features"="+64bit,+a,+d,+experimental-zicond,+f,+m,+save-restore,+v,+zbb,+zicsr,+zifencei,+zve32f,+zve32x,+zve64d,+zve64f,+zve64x,+zvl128b,+zvl32b,+zvl64b" }
42-
// CHECK: attributes #4 = { {{.*}}"target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zbb,+zicsr,+zifencei" }
43-
// CHECK: attributes #5 = { {{.*}}"target-features"="+64bit,+m,+save-restore" }
44-
// CHECK: attributes #6 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+m,+save-restore,+zbb,+zifencei" }
45-
// CHECK: attributes #7 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+m,+save-restore" }
46-
// CHECK: attributes #8 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zicsr,+zifencei" }
39+
// CHECK: attributes #0 = { {{.*}}"target-features"="+64bit,+a,+m,+save-restore,+zifencei,-relax,-zbb,-zfa" }
40+
// CHECK: attributes #1 = { {{.*}}"target-cpu"="rocket-rv64" "target-features"="+64bit,+a,+d,+f,+m,+save-restore,+v,+zicsr,+zifencei,+zve32f,+zve32x,+zve64d,+zve64f,+zve64x,+zvl128b,+zvl32b,+zvl64b,-relax,-zbb,-zfa" "tune-cpu"="generic-rv64" }
41+
// CHECK: attributes #2 = { {{.*}}"target-features"="+64bit,+a,+m,+save-restore,+zbb,+zifencei,-relax,-zfa" }
42+
// CHECK: attributes #3 = { {{.*}}"target-features"="+64bit,+a,+d,+experimental-zicond,+f,+m,+save-restore,+v,+zbb,+zicsr,+zifencei,+zve32f,+zve32x,+zve64d,+zve64f,+zve64x,+zvl128b,+zvl32b,+zvl64b,-relax,-zfa" }
43+
// CHECK: attributes #4 = { {{.*}}"target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zbb,+zicsr,+zifencei,-relax,-zfa" }
44+
// CHECK: attributes #5 = { {{.*}}"target-features"="+64bit,+m,+save-restore,-relax,-zbb,-zfa" }
45+
// CHECK: attributes #6 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+m,+save-restore,+zbb,+zifencei,-relax,-zfa" }
46+
// CHECK: attributes #7 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+m,+save-restore,-relax,-zbb,-zfa" }
47+
// CHECK: attributes #8 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zicsr,+zifencei,-relax,-zbb,-zfa" }

0 commit comments

Comments
 (0)