Skip to content

Commit 9062f71

Browse files
[ControlHeightReduction] Drop lifetime annotations where necessary
ControlHeightReduction will duplicate some blocks and insert phi nodes in exit blocks of regions that it operates on for any live values. This includes allocas. Having a lifetime annotation refer to a phi node was made illegal in 92c55a3, which causes the verifier to fail after CHR. There are some cases where we might not need to drop lifetime annotations (usually because we do not need the phi to begin with), but drop all annotations for now to be conservative. I believe this also ends up being a correctness issue on top of the assertion because the lifetime annotation in some cases would get shifted to after the store, making the store UB when it was not before. Fixes #159621.
1 parent 8cfbace commit 9062f71

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,12 @@ static void insertTrivialPHIs(CHRScope *Scope,
15921592
TrivialPHIs.insert(PN);
15931593
CHR_DEBUG(dbgs() << "Insert phi " << *PN << "\n");
15941594
for (Instruction *UI : Users) {
1595+
// Drop lifetime annotations as it is illegal for them to refer to a
1596+
// phi node.
1597+
if (UI->isLifetimeStartOrEnd()) {
1598+
UI->eraseFromParent();
1599+
continue;
1600+
}
15951601
for (unsigned J = 0, NumOps = UI->getNumOperands(); J < NumOps; ++J) {
15961602
if (UI->getOperand(J) == &I) {
15971603
UI->setOperand(J, PN);

llvm/test/Transforms/PGOProfile/chr.ll

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,65 @@ bb6:
26782678
ret void
26792679
}
26802680

2681+
; Test that when we create phi nodes for allocas, we drop lifetime annotations
2682+
; that would otherwise be illegally referring to a phi node.
2683+
declare void @baz(i64)
2684+
declare void @llvm.lifetime.start.p0(ptr captures(none))
2685+
define void @test_chr_with_lifetimes(ptr %i) !prof !14 {
2686+
; CHECK-LABEL: @test_chr_with_lifetimes(
2687+
; CHECK-NEXT: entry:
2688+
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[I:%.*]], align 4
2689+
; CHECK-NEXT: [[DOTFR:%.*]] = freeze i32 [[TMP0]]
2690+
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[DOTFR]], 0
2691+
; CHECK-NEXT: br i1 [[DOTNOT]], label [[ENTRY_SPLIT_NONCHR:%.*]], label [[ENTRY_SPLIT:%.*]], !prof [[PROF21:![0-9]+]]
2692+
; CHECK: entry.split:
2693+
; CHECK-NEXT: [[TEST:%.*]] = alloca i32, align 8
2694+
; CHECK-NEXT: call void @baz(i64 0)
2695+
; CHECK-NEXT: call void @foo()
2696+
; CHECK-NEXT: br label [[BB1:%.*]]
2697+
; CHECK: entry.split.nonchr:
2698+
; CHECK-NEXT: [[TEST_NONCHR:%.*]] = alloca i32, align 8
2699+
; CHECK-NEXT: call void @baz(i64 4)
2700+
; CHECK-NEXT: br label [[BB1]]
2701+
; CHECK: bb1:
2702+
; CHECK-NEXT: [[TMP1:%.*]] = phi ptr [ [[TEST]], [[ENTRY_SPLIT]] ], [ [[TEST_NONCHR]], [[ENTRY_SPLIT_NONCHR]] ]
2703+
; CHECK-NEXT: store ptr [[TMP1]], ptr [[I]], align 8
2704+
; CHECK-NEXT: br label [[BB2:%.*]]
2705+
; CHECK: bb2:
2706+
; CHECK-NEXT: [[TMP2:%.*]] = phi ptr [ [[TMP3:%.*]], [[BB2]] ], [ null, [[BB1]] ]
2707+
; CHECK-NEXT: [[TMP3]] = getelementptr i8, ptr [[TMP2]], i64 24
2708+
; CHECK-NEXT: [[TMP4:%.*]] = icmp eq ptr [[TMP2]], [[I]]
2709+
; CHECK-NEXT: br i1 [[TMP4]], label [[BB3:%.*]], label [[BB2]]
2710+
; CHECK: bb3:
2711+
; CHECK-NEXT: ret void
2712+
;
2713+
entry:
2714+
%1 = load i32, ptr %i
2715+
%2 = icmp eq i32 %1, 0
2716+
%3 = select i1 %2, i64 4, i64 0, !prof !15
2717+
%test = alloca i32, align 8
2718+
call void @baz(i64 %3)
2719+
br i1 %2, label %bb1, label %bb0, !prof !15
2720+
2721+
bb0:
2722+
call void @foo()
2723+
br label %bb1
2724+
2725+
bb1:
2726+
call void @llvm.lifetime.start.p0(ptr %test)
2727+
store ptr %test, ptr %i, align 8
2728+
br label %bb2
2729+
2730+
bb2:
2731+
%4 = phi ptr [ %5, %bb2 ], [ null, %bb1 ]
2732+
%5 = getelementptr i8, ptr %4, i64 24
2733+
%6 = icmp eq ptr %4, %i
2734+
br i1 %6, label %bb3, label %bb2
2735+
2736+
bb3:
2737+
ret void
2738+
}
2739+
26812740

26822741
!llvm.module.flags = !{!0}
26832742
!0 = !{i32 1, !"ProfileSummary", !1}

0 commit comments

Comments
 (0)