Skip to content

Commit 67ec874

Browse files
committed
llc: Change behavior of -mattr with existing attribute
Append this to the existing target-features attribute on the function. Some flags ignore existing attributes, and some overwrite them. Move towards consistently respecting existing attributes if present. Since target features act as a state machine on their own, append to the function attribute. The backend default added feature list, function attributes, and -mattr will all be appended together, and the later features can individually toggle the earlier settings.
1 parent 4bafcec commit 67ec874

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

llvm/include/llvm/CodeGen/CommandFlags.inc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,19 @@ setFunctionAttributes(StringRef CPU, StringRef Features, Function &F) {
387387

388388
if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
389389
NewAttrs.addAttribute("target-cpu", CPU);
390-
if (!Features.empty())
391-
NewAttrs.addAttribute("target-features", Features);
390+
if (!Features.empty()) {
391+
// Append the command line features to any that are already on the function.
392+
StringRef OldFeatures
393+
= F.getFnAttribute("target-features").getValueAsString();
394+
if (OldFeatures.empty())
395+
NewAttrs.addAttribute("target-features", Features);
396+
else {
397+
SmallString<256> Appended(OldFeatures);
398+
Appended.push_back(',');
399+
Appended.append(Features);
400+
NewAttrs.addAttribute("target-features", Appended);
401+
}
402+
}
392403
if (FramePointerUsage.getNumOccurrences() > 0) {
393404
if (FramePointerUsage == llvm::FramePointer::All)
394405
NewAttrs.addAttribute("frame-pointer", "all");

llvm/test/CodeGen/WebAssembly/target-features.ll

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
1010
target triple = "wasm32-unknown-unknown"
1111

12-
define void @foo(i32* %p1, float %f2) #0 {
12+
define void @fn_atomics(i32* %p1, float %f2) #0 {
1313
%a = atomicrmw min i32* undef, i32 42 seq_cst
1414
%v = fptoui float %f2 to i32
1515
store i32 %v, i32* %p1
1616
ret void
1717
}
1818

19-
define void @bar(i32* %p1, float %f2) #1 {
19+
define void @fn_nontrapping_fptoint(i32* %p1, float %f2) #1 {
2020
%a = atomicrmw min i32* undef, i32 42 seq_cst
2121
%v = fptoui float %f2 to i32
2222
store i32 %v, i32* %p1
@@ -26,32 +26,27 @@ define void @bar(i32* %p1, float %f2) #1 {
2626
attributes #0 = { "target-features"="+atomics" }
2727
attributes #1 = { "target-features"="+nontrapping-fptoint" }
2828

29-
30-
; CHECK-LABEL: foo:
29+
; CHECK-LABEL: fn_atomics:
3130

3231
; Expanded atomicrmw min
3332
; ATTRS: loop
34-
; ATTRS: i32.atomic.rmw.cmpxchg
35-
; SIMD128-NOT: i32.atomic.rmw.cmpxchg
33+
; CHECK: i32.atomic.rmw.cmpxchg
3634
; ATTRS: end_loop
3735

3836
; nontrapping fptoint
39-
; ATTRS: i32.trunc_sat_f32_u
40-
; SIMD128-NOT: i32.trunc_sat_f32_u
37+
; CHECK: i32.trunc_sat_f32_u
4138
; ATTRS: i32.store
4239

43-
; `bar` should be the same as `foo`
44-
; CHECK-LABEL: bar:
40+
; `fn_nontrapping_fptoint` should be the same as `fn_atomics`
41+
; CHECK-LABEL: fn_nontrapping_fptoint:
4542

4643
; Expanded atomicrmw min
4744
; ATTRS: loop
48-
; ATTRS: i32.atomic.rmw.cmpxchg
49-
; SIMD128-NOT: i32.atomic.rmw.cmpxchg
45+
; CHECK: i32.atomic.rmw.cmpxchg
5046
; ATTRS: end_loop
5147

5248
; nontrapping fptoint
53-
; ATTRS: i32.trunc_sat_f32_u
54-
; SIMD128-NOT: i32.trunc_sat_f32_u
49+
; CHECK: i32.trunc_sat_f32_u
5550
; ATTRS: i32.store
5651

5752
; CHECK-LABEL: .custom_section.target_features,"",@
@@ -65,12 +60,15 @@ attributes #1 = { "target-features"="+nontrapping-fptoint" }
6560
; ATTRS-NEXT: .int8 19
6661
; ATTRS-NEXT: .ascii "nontrapping-fptoint"
6762

68-
; -atomics, +simd128
69-
; SIMD128-NEXT: .int8 2
70-
; SIMD128-NEXT: .int8 45
63+
; +atomics, +simd128
64+
; SIMD128-NEXT: .int8 3
65+
; SIMD128-NEXT: .int8 43
7166
; SIMD128-NEXT: .int8 7
7267
; SIMD128-NEXT: .ascii "atomics"
7368
; SIMD128-NEXT: .int8 43
69+
; SIMD128-NEXT: .int8 19
70+
; SIMD128-NEXT: .ascii "nontrapping-fptoint"
71+
; SIMD128-NEXT: .int8 43
7472
; SIMD128-NEXT: .int8 7
7573
; SIMD128-NEXT: .ascii "simd128"
7674

llvm/test/Other/opt-override-mcpu-mattr.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
; target-cpu and target-features using command line options -mcpu and
55
; -mattr.
66

7-
; CHECK: attributes #0 = { nounwind readnone ssp uwtable "target-cpu"="broadwell" "target-features"="+avx2" "use-soft-float"="false" }
8-
; CHECK: attributes #1 = { nounwind readnone ssp uwtable "target-cpu"="core2" "target-features"="+avx2" "use-soft-float"="false" }
7+
; CHECK: attributes #0 = { nounwind readnone ssp uwtable "target-cpu"="broadwell" "target-features"="+ssse3,+cx16,+sse,+sse2,+sse3,+avx2" "use-soft-float"="false" }
8+
; CHECK: attributes #1 = { nounwind readnone ssp uwtable "target-cpu"="core2" "target-features"="+ssse3,+cx16,+sse,+sse2,+sse3,+avx2" "use-soft-float"="false" }
99

1010
define i32 @no_target_cpu() #0 {
1111
entry:

0 commit comments

Comments
 (0)