Skip to content

Commit c56603f

Browse files
committed
AtomicExpand: Allow incrementally legalizing atomicrmw
If a lowering changed control flow, resume the legalization loop at the first newly inserted block. This will allow incrementally legalizing atomicrmw and cmpxchg. The AArch64 test might be a bugfix. Previously it would lower the vector FP case as a cmpxchg loop, but cmpxchgs get lowered but previously weren't. Maybe it shouldn't be reporting cmpxchg for the expand type in the first place though.
1 parent f0ef1d3 commit c56603f

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

llvm/lib/CodeGen/AtomicExpandPass.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,30 @@ bool AtomicExpandImpl::run(Function &F, const TargetMachine *TM) {
351351

352352
bool MadeChange = false;
353353

354-
SmallVector<Instruction *, 1> AtomicInsts;
355-
356-
// Changing control-flow while iterating through it is a bad idea, so gather a
357-
// list of all atomic instructions before we start.
358-
for (Instruction &I : instructions(F))
359-
if (I.isAtomic() && !isa<FenceInst>(&I))
360-
AtomicInsts.push_back(&I);
361-
362-
for (auto *I : AtomicInsts) {
363-
if (processAtomicInstr(I))
364-
MadeChange = true;
354+
for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE;) {
355+
BasicBlock *BB = &*BBI;
356+
++BBI;
357+
358+
BasicBlock::iterator Next;
359+
360+
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
361+
I = Next) {
362+
Instruction &Inst = *I;
363+
Next = std::next(I);
364+
365+
if (processAtomicInstr(&Inst)) {
366+
MadeChange = true;
367+
368+
// Detect control flow change and resume iteration from the original
369+
// block to inspect any newly inserted blocks. This allows incremental
370+
// legalizaton of atomicrmw and cmpxchg.
371+
if (BB != Next->getParent()) {
372+
BBI = BB->getIterator();
373+
BBE = F.end();
374+
break;
375+
}
376+
}
377+
}
365378
}
366379

367380
return MadeChange;

llvm/test/CodeGen/AArch64/atomicrmw-fadd-fp-vector.ll

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@ define <2 x half> @test_atomicrmw_fadd_v2f16_align4(ptr addrspace(1) %ptr, <2 x
88
; NOLSE-NEXT: fcvtl v1.4s, v0.4h
99
; NOLSE-NEXT: ldr s0, [x0]
1010
; NOLSE-NEXT: b .LBB0_2
11-
; NOLSE-NEXT: .LBB0_1: // %atomicrmw.start
11+
; NOLSE-NEXT: .LBB0_1: // %cmpxchg.nostore
1212
; NOLSE-NEXT: // in Loop: Header=BB0_2 Depth=1
13-
; NOLSE-NEXT: fmov s0, w10
14-
; NOLSE-NEXT: cmp w10, w9
15-
; NOLSE-NEXT: b.eq .LBB0_5
13+
; NOLSE-NEXT: mov w9, wzr
14+
; NOLSE-NEXT: clrex
15+
; NOLSE-NEXT: fmov s0, w8
16+
; NOLSE-NEXT: cbnz w9, .LBB0_6
1617
; NOLSE-NEXT: .LBB0_2: // %atomicrmw.start
1718
; NOLSE-NEXT: // =>This Loop Header: Depth=1
1819
; NOLSE-NEXT: // Child Loop BB0_3 Depth 2
1920
; NOLSE-NEXT: fcvtl v2.4s, v0.4h
20-
; NOLSE-NEXT: fmov w9, s0
21+
; NOLSE-NEXT: fmov w10, s0
2122
; NOLSE-NEXT: fadd v2.4s, v2.4s, v1.4s
2223
; NOLSE-NEXT: fcvtn v2.4h, v2.4s
23-
; NOLSE-NEXT: fmov w8, s2
24-
; NOLSE-NEXT: .LBB0_3: // %atomicrmw.start
24+
; NOLSE-NEXT: fmov w9, s2
25+
; NOLSE-NEXT: .LBB0_3: // %cmpxchg.start
2526
; NOLSE-NEXT: // Parent Loop BB0_2 Depth=1
2627
; NOLSE-NEXT: // => This Inner Loop Header: Depth=2
27-
; NOLSE-NEXT: ldaxr w10, [x0]
28-
; NOLSE-NEXT: cmp w10, w9
28+
; NOLSE-NEXT: ldaxr w8, [x0]
29+
; NOLSE-NEXT: cmp w8, w10
2930
; NOLSE-NEXT: b.ne .LBB0_1
30-
; NOLSE-NEXT: // %bb.4: // %atomicrmw.start
31+
; NOLSE-NEXT: // %bb.4: // %cmpxchg.trystore
3132
; NOLSE-NEXT: // in Loop: Header=BB0_3 Depth=2
32-
; NOLSE-NEXT: stlxr wzr, w8, [x0]
33-
; NOLSE-NEXT: cbnz wzr, .LBB0_3
34-
; NOLSE-NEXT: b .LBB0_1
35-
; NOLSE-NEXT: .LBB0_5: // %atomicrmw.end
33+
; NOLSE-NEXT: stlxr w11, w9, [x0]
34+
; NOLSE-NEXT: cbnz w11, .LBB0_3
35+
; NOLSE-NEXT: // %bb.5: // in Loop: Header=BB0_2 Depth=1
36+
; NOLSE-NEXT: mov w9, #1 // =0x1
37+
; NOLSE-NEXT: fmov s0, w8
38+
; NOLSE-NEXT: cbz w9, .LBB0_2
39+
; NOLSE-NEXT: .LBB0_6: // %atomicrmw.end
3640
; NOLSE-NEXT: // kill: def $d0 killed $d0 killed $q0
3741
; NOLSE-NEXT: ret
3842
;
@@ -64,29 +68,33 @@ define <2 x float> @test_atomicrmw_fadd_v2f32_align8(ptr addrspace(1) %ptr, <2 x
6468
; NOLSE: // %bb.0:
6569
; NOLSE-NEXT: ldr d1, [x0]
6670
; NOLSE-NEXT: b .LBB1_2
67-
; NOLSE-NEXT: .LBB1_1: // %atomicrmw.start
71+
; NOLSE-NEXT: .LBB1_1: // %cmpxchg.nostore
6872
; NOLSE-NEXT: // in Loop: Header=BB1_2 Depth=1
69-
; NOLSE-NEXT: fmov d1, x10
70-
; NOLSE-NEXT: cmp x10, x9
71-
; NOLSE-NEXT: b.eq .LBB1_5
73+
; NOLSE-NEXT: mov w9, wzr
74+
; NOLSE-NEXT: clrex
75+
; NOLSE-NEXT: fmov d1, x8
76+
; NOLSE-NEXT: cbnz w9, .LBB1_6
7277
; NOLSE-NEXT: .LBB1_2: // %atomicrmw.start
7378
; NOLSE-NEXT: // =>This Loop Header: Depth=1
7479
; NOLSE-NEXT: // Child Loop BB1_3 Depth 2
7580
; NOLSE-NEXT: fadd v2.2s, v1.2s, v0.2s
76-
; NOLSE-NEXT: fmov x9, d1
77-
; NOLSE-NEXT: fmov x8, d2
78-
; NOLSE-NEXT: .LBB1_3: // %atomicrmw.start
81+
; NOLSE-NEXT: fmov x10, d1
82+
; NOLSE-NEXT: fmov x9, d2
83+
; NOLSE-NEXT: .LBB1_3: // %cmpxchg.start
7984
; NOLSE-NEXT: // Parent Loop BB1_2 Depth=1
8085
; NOLSE-NEXT: // => This Inner Loop Header: Depth=2
81-
; NOLSE-NEXT: ldaxr x10, [x0]
82-
; NOLSE-NEXT: cmp x10, x9
86+
; NOLSE-NEXT: ldaxr x8, [x0]
87+
; NOLSE-NEXT: cmp x8, x10
8388
; NOLSE-NEXT: b.ne .LBB1_1
84-
; NOLSE-NEXT: // %bb.4: // %atomicrmw.start
89+
; NOLSE-NEXT: // %bb.4: // %cmpxchg.trystore
8590
; NOLSE-NEXT: // in Loop: Header=BB1_3 Depth=2
86-
; NOLSE-NEXT: stlxr wzr, x8, [x0]
87-
; NOLSE-NEXT: cbnz wzr, .LBB1_3
88-
; NOLSE-NEXT: b .LBB1_1
89-
; NOLSE-NEXT: .LBB1_5: // %atomicrmw.end
91+
; NOLSE-NEXT: stlxr w11, x9, [x0]
92+
; NOLSE-NEXT: cbnz w11, .LBB1_3
93+
; NOLSE-NEXT: // %bb.5: // in Loop: Header=BB1_2 Depth=1
94+
; NOLSE-NEXT: mov w9, #1 // =0x1
95+
; NOLSE-NEXT: fmov d1, x8
96+
; NOLSE-NEXT: cbz w9, .LBB1_2
97+
; NOLSE-NEXT: .LBB1_6: // %atomicrmw.end
9098
; NOLSE-NEXT: fmov d0, d1
9199
; NOLSE-NEXT: ret
92100
;

0 commit comments

Comments
 (0)