Skip to content

Commit aceff1f

Browse files
committed
[MachineLICM] Don't allow hoisting invariant loads across mem barrier. (llvm#116987)
The improvements in 63917e1 / llvm#70796 do not check for memory barriers/unmodelled sideeffects, which means we may incorrectly hoist loads across memory barriers. Fix this by checking any machine instruction in the loop is a load-fold barrier. PR: llvm#116987 (cherry picked from commit ef102b4)
1 parent 06a77c5 commit aceff1f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

llvm/lib/CodeGen/MachineLICM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ void MachineLICMBase::InitializeLoadsHoistableLoops() {
14741474
if (!AllowedToHoistLoads[Loop])
14751475
continue;
14761476
for (auto &MI : *MBB) {
1477-
if (!MI.mayStore() && !MI.isCall() &&
1477+
if (!MI.isLoadFoldBarrier() && !MI.mayStore() && !MI.isCall() &&
14781478
!(MI.mayLoad() && MI.hasOrderedMemoryRef()))
14791479
continue;
14801480
for (MachineLoop *L = Loop; L != nullptr; L = L->getParentLoop())

llvm/test/CodeGen/AArch64/machine-licm-hoist-load.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,35 @@ for.exit: ; preds = %for.body
497497
ret i64 %spec.select
498498
}
499499

500+
@a = external local_unnamed_addr global i32, align 4
501+
502+
; Make sure the load is not hoisted out of the loop across memory barriers.
503+
define i32 @load_between_memory_barriers() {
504+
; CHECK-LABEL: load_between_memory_barriers:
505+
; CHECK: // %bb.0:
506+
; CHECK-NEXT: adrp x8, :got:a
507+
; CHECK-NEXT: ldr x8, [x8, :got_lo12:a]
508+
; CHECK-NEXT: .LBB8_1: // %loop
509+
; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
510+
; CHECK-NEXT: //MEMBARRIER
511+
; CHECK-NEXT: ldr w0, [x8]
512+
; CHECK-NEXT: //MEMBARRIER
513+
; CHECK-NEXT: cbz w0, .LBB8_1
514+
; CHECK-NEXT: // %bb.2: // %exit
515+
; CHECK-NEXT: ret
516+
br label %loop
517+
518+
loop:
519+
fence syncscope("singlethread") acq_rel
520+
%l = load i32, ptr @a, align 4
521+
fence syncscope("singlethread") acq_rel
522+
%c = icmp eq i32 %l, 0
523+
br i1 %c, label %loop, label %exit
524+
525+
exit:
526+
ret i32 %l
527+
}
528+
500529
declare i32 @bcmp(ptr, ptr, i64)
501530
declare i32 @memcmp(ptr, ptr, i64)
502531
declare void @func()

llvm/test/CodeGen/Mips/lcb5.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ if.end: ; preds = %if.then, %entry
186186
}
187187

188188
; ci: .ent z3
189-
; ci: bteqz $BB6_3
189+
; ci: bteqz $BB6_2
190190
; ci: .end z3
191191

192192
; Function Attrs: nounwind optsize
@@ -210,7 +210,7 @@ if.end: ; preds = %if.then, %entry
210210

211211
; ci: .ent z4
212212
; ci: btnez $BB7_1 # 16 bit inst
213-
; ci: jal $BB7_3 # branch
213+
; ci: jal $BB7_2 # branch
214214
; ci: nop
215215
; ci: $BB7_1:
216216
; ci: .p2align 2

0 commit comments

Comments
 (0)