Skip to content

Commit 85eeba4

Browse files
committed
[ConstantHoisting] Add back ptr->ptr bitcast to avoid assertion failure
In commit a7ee80f a ptr->ptr bitcast was removed. But that seem to cause "Expected an cast instruction!" assertions later in that pass. This patch will add back the bitcast again. This was a bit unexpected since there is no bitcast added after creating the Add instruction in the else clause, but I guess there is something special with the GetElementPtr scenario which makes this bitcast needed to avoid such asserts. This patch is also adding a reproducer for llvm#52689 that started to fail due to hitting the above mentioned assert. Now it should end up hitting the assertion failure from llvm#52689 again.
1 parent a3f2751 commit 85eeba4

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
763763
// Constant being rebased is a ConstantExpr.
764764
Mat = GetElementPtrInst::Create(Type::getInt8Ty(*Ctx), Base, Adj->Offset,
765765
"mat_gep", Adj->MatInsertPt);
766+
// Hide it behind a bitcast.
767+
Mat = new BitCastInst(Mat, Adj->Ty, "mat_bitcast", Adj->MatInsertPt);
766768
} else
767769
// Constant being rebased is a ConstantInt.
768770
Mat = BinaryOperator::Create(Instruction::Add, Base, Adj->Offset,

llvm/test/Transforms/ConstantHoisting/AArch64/const-hoist-gep.ll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ define dso_local void @zot() {
2222
; CHECK-NEXT: [[CONST:%.*]] = bitcast ptr getelementptr inbounds ([[TMP0:%.*]], ptr @global, i32 0, i32 4, i32 0, i32 0) to ptr
2323
; CHECK-NEXT: store i32 undef, ptr [[CONST]], align 4
2424
; CHECK-NEXT: [[MAT_GEP:%.*]] = getelementptr i8, ptr [[CONST]], i32 4
25-
; CHECK-NEXT: store i32 undef, ptr [[MAT_GEP]], align 4
25+
; CHECK-NEXT: [[MAT_BITCAST:%.*]] = bitcast ptr [[MAT_GEP]] to ptr
26+
; CHECK-NEXT: store i32 undef, ptr [[MAT_BITCAST]], align 4
2627
; CHECK-NEXT: [[MAT_GEP1:%.*]] = getelementptr i8, ptr [[CONST]], i32 160
27-
; CHECK-NEXT: store i32 undef, ptr [[MAT_GEP1]], align 4
28-
; CHECK-NEXT: [[MAT_GEP2:%.*]] = getelementptr i8, ptr [[CONST]], i32 164
29-
; CHECK-NEXT: store i32 undef, ptr [[MAT_GEP2]], align 4
28+
; CHECK-NEXT: [[MAT_BITCAST2:%.*]] = bitcast ptr [[MAT_GEP1]] to ptr
29+
; CHECK-NEXT: store i32 undef, ptr [[MAT_BITCAST2]], align 4
30+
; CHECK-NEXT: [[MAT_GEP3:%.*]] = getelementptr i8, ptr [[CONST]], i32 164
31+
; CHECK-NEXT: [[MAT_BITCAST4:%.*]] = bitcast ptr [[MAT_GEP3]] to ptr
32+
; CHECK-NEXT: store i32 undef, ptr [[MAT_BITCAST4]], align 4
3033
; CHECK-NEXT: ret void
3134
;
3235
bb:

llvm/test/Transforms/ConstantHoisting/ARM/const-hoist-gep-overindexing.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ define void @test_inbounds() {
1414
; CHECK-NEXT: [[CONST:%.*]] = bitcast ptr getelementptr inbounds ([[TMP0:%.*]], ptr @global, i32 0, i32 1, i32 0) to ptr
1515
; CHECK-NEXT: store i16 undef, ptr [[CONST]], align 2
1616
; CHECK-NEXT: [[MAT_GEP:%.*]] = getelementptr i8, ptr [[CONST]], i32 2
17-
; CHECK-NEXT: store i16 undef, ptr [[MAT_GEP]], align 2
17+
; CHECK-NEXT: [[MAT_BITCAST:%.*]] = bitcast ptr [[MAT_GEP]] to ptr
18+
; CHECK-NEXT: store i16 undef, ptr [[MAT_BITCAST]], align 2
1819
; CHECK-NEXT: [[MAT_GEP1:%.*]] = getelementptr i8, ptr [[CONST]], i32 20
19-
; CHECK-NEXT: store i16 undef, ptr [[MAT_GEP1]], align 2
20+
; CHECK-NEXT: [[MAT_BITCAST2:%.*]] = bitcast ptr [[MAT_GEP1]] to ptr
21+
; CHECK-NEXT: store i16 undef, ptr [[MAT_BITCAST2]], align 2
2022
; CHECK-NEXT: ret void
2123
;
2224
bb:

llvm/test/Transforms/ConstantHoisting/ARM/const-hoist-gep.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ define dso_local void @zot() {
2424
; CHECK-NEXT: [[CONST:%.*]] = bitcast ptr getelementptr inbounds ([[TMP0]], ptr @global, i32 0, i32 4, i32 0, i32 0) to ptr
2525
; CHECK-NEXT: store i32 undef, ptr [[CONST]], align 4
2626
; CHECK-NEXT: [[MAT_GEP:%.*]] = getelementptr i8, ptr [[CONST]], i32 4
27-
; CHECK-NEXT: store i32 undef, ptr [[MAT_GEP]], align 4
27+
; CHECK-NEXT: [[MAT_BITCAST:%.*]] = bitcast ptr [[MAT_GEP]] to ptr
28+
; CHECK-NEXT: store i32 undef, ptr [[MAT_BITCAST]], align 4
2829
; CHECK-NEXT: store i32 undef, ptr [[CONST1]], align 4
2930
; CHECK-NEXT: [[MAT_GEP2:%.*]] = getelementptr i8, ptr [[CONST1]], i32 4
30-
; CHECK-NEXT: store i32 undef, ptr [[MAT_GEP2]], align 4
31+
; CHECK-NEXT: [[MAT_BITCAST3:%.*]] = bitcast ptr [[MAT_GEP2]] to ptr
32+
; CHECK-NEXT: store i32 undef, ptr [[MAT_BITCAST3]], align 4
3133
; CHECK-NEXT: ret void
3234
;
3335
bb:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: not --crash opt -passes='consthoist' -S -o - -consthoist-gep=1 -mtriple=x86_64-unknown-linux-gnu < %s 2>&1 | FileCheck %s
2+
3+
; REQUIRES: asserts
4+
5+
; This is a reproducer for https://github.com/llvm/llvm-project/issues/52689
6+
;
7+
; opt: ../lib/Transforms/Scalar/ConstantHoisting.cpp:919: bool llvm::ConstantHoistingPass::emitBaseConstants(llvm::GlobalVariable *): Assertion `UsesNum == (ReBasesNum + NotRebasedNum) && "Not all uses are rebased"' failed.
8+
9+
; CHECK: Assertion `UsesNum == (ReBasesNum + NotRebasedNum) && "Not all uses are rebased"' failed.
10+
11+
12+
@g_77 = external global [5 x i32]
13+
14+
define internal ptr @func_29(i1 %p1, i1 %p2, ptr %p3) {
15+
entry:
16+
br i1 %p1, label %crit_edge, label %if.else3089
17+
18+
crit_edge: ; preds = %entry
19+
br label %for.cond1063
20+
21+
for.cond1063: ; preds = %cleanup1660, %crit_edge
22+
%l_323.sroa.0.0 = phi ptr [ getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 3), %cleanup1660 ], [ null, %crit_edge ]
23+
%l_323.sroa.2.0 = phi ptr [ getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 3), %cleanup1660 ], [ null, %crit_edge ]
24+
br i1 %p2, label %cleanup1660.thread, label %cleanup1674
25+
26+
cleanup1660.thread: ; preds = %for.cond1063
27+
br label %cleanup1674
28+
29+
cleanup1660: ; No predecessors!
30+
br label %for.cond1063
31+
32+
cleanup1674: ; preds = %cleanup1660.thread, %for.cond1063
33+
store ptr getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 1), ptr %p3, align 1
34+
ret ptr null
35+
36+
if.else3089: ; preds = %entry
37+
store ptr getelementptr inbounds ([5 x i32], ptr @g_77, i32 0, i32 1), ptr %p3, align 1
38+
ret ptr null
39+
}

0 commit comments

Comments
 (0)