llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Assertion `isa<Argument>(Val) && "Unknown live-in to the entry block"' failed

Open wjristow opened this issue 9 years ago • 6 comments

Bugzilla Link 33364
Resolution FIXED
Resolved on Jun 21, 2017 17:53
Version trunk
OS Windows NT
CC @annamthomas,@gregbedwell,@rgal,@rotateright

Extended Description

Our internal testing hit a compile-time assertion failure with optimization on trunk:

Using: clang version 5.0.0 (trunk 304995) Target: x86_64-unknown-linux-gnu Thread model: posix

$ cat test.cpp // ====================================================== typedef char char2 attribute((ext_vector_type(2))); char2 a, b; int c; int foo() { char2 d = {1}; char2 f = a; for (int g = 0; g < c; ++g) d = b; char2 h = d <= 0; char2 i = f & h; return i[0] + i[1]; } // ====================================================== $

It fails at -O1 or higher:

$ clang++ -c -O1 -c test.cpp clang++: /home/warren/Upstream/llvm/lib/Analysis/LazyValueInfo.cpp:890: bool {anonymous}::LazyValueInfoImpl::solveBlockValueNonLocal({anonymous}::LVILatticeVal&, llvm::Value*, llvm::BasicBlock*): Assertion `isa(Val) && "Unknown live-in to the entry block"' failed. ... $

We've bisected it to r298845 ( http://llvm.org/viewvc/llvm-project?view=revision&revision=298845 ):

~~ [InstCombine] Avoid incorrect folding of select into phi nodes when incoming element is a vector type

Summary: We are incorrectly folding selects into phi nodes when the incoming value of a phi node is a constant vector. This optimization is done in FoldOpIntoPhi when the select condition is a phi node with constant incoming values. Without the fix, we are miscompiling (i.e. incorrectly folding the select into the phi node) when the vector contains non-zero elements. This patch fixes the miscompile and we will correctly fold based on the select vector operand (see added test cases).

Reviewers: majnemer, sanjoy, spatel

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31189 ~~

wjristow avatar Jun 08 '17 18:06 wjristow

Thanks Anna for the quick response. I see that your fix landed at:

https://reviews.llvm.org/rL305591

It fixes the test-case I reported here, as well as a second similar failure I had encountered. So I'm resolving this.

wjristow avatar Jun 22 '17 00:06 wjristow

Fix up for review: https://reviews.llvm.org/D34162

For completeness, confirmed that with the fix, the code produces the correct result. The result with versus without instcombine is 24 (which is correct on manual inspection).

Run with opt -instcombine and pipe result to lli

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1 @​globalA = global <2 x i8> <i8 42, i8 24>, align 2 @​globalB = global <2 x i8> zeroinitializer, align 2

define i32 @​quux(i1 %cond) { bb: %loadA = load <2 x i8>, <2 x i8>* @​globalA, align 2 br i1 %cond, label %bb13_crit_edge, label %bb13

bb13_crit_edge: ; preds = %bb15 %loadB = load <2 x i8>, <2 x i8>* @​globalB, align 2 br label %bb13

bb13: ; preds = %bb13_crit_edge, %bb %phi = phi <2 x i8> [ %loadB, %bb13_crit_edge ], [ <i8 1, i8 0>, %bb ] %tmp23 = icmp slt <2 x i8> %phi, <i8 1, i8 1> %sel = select <2 x i1> %tmp23, <2 x i8> %loadA, <2 x i8> zeroinitializer %tmp30 = extractelement <2 x i8> %sel, i32 0 %tmp31 = sext i8 %tmp30 to i32 %tmp33 = extractelement <2 x i8> %sel, i32 1 %tmp34 = sext i8 %tmp33 to i32 %tmp35 = add nsw i32 %tmp31, %tmp34 ret i32 %tmp35 } define i32 @​main() #​1 { %result = call i32 @​quux(i1 false) %re = call i32 (i8*, ...) @​printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %result) #​2 ret i32 0 }

declare i32 @​printf(i8*, ...)

annamthomas avatar Jun 13 '17 20:06 annamthomas

just FYI: without this change, there's no assertion failure, but the code generated is incorrect. It is triggering exactly the miscompile fixed by r298845. ... I'll put up a fix for this.

Thanks for the explanation. That's great to hear.

wjristow avatar Jun 13 '17 15:06 wjristow

just FYI: without this change, there's no assertion failure, but the code generated is incorrect. It is triggering exactly the miscompile fixed by r298845.

The issue is that in some cases during recursive calls of instcombine, we can have an incorrect insertion point for the select instruction created in foldOpIntoPhi. I'll put up a fix for this.

annamthomas avatar Jun 13 '17 15:06 annamthomas

The LVI assertion is just a side effect of dominator information being incorrect.

Narrowed it down to an IR which fails IR verifier when run with instcombine:

opt -instcombine test.ll We get an instruction does not dominate uses error. I'll take a look at this tomorrow.

test.ll: @​global = global <2 x i8> zeroinitializer, align 2 @​global.1 = global <2 x i8> zeroinitializer, align 2 @​global.2 = global i32 0, align 4

define i32 @​quux() local_unnamed_addr #​0 { bb: %tmp7 = load <2 x i8>, <2 x i8>* @​global, align 2, !tbaa !​2 %tmp113 = load i32, i32* @​global.2, align 4, !tbaa !​5 %tmp124 = icmp slt i32 0, %tmp113 br i1 %tmp124, label %bb15.lr.ph, label %bb13

bb15.lr.ph: ; preds = %bb %tmp11 = load i32, i32* @​global.2, align 4, !tbaa !​5 br label %bb15

bb9.bb13_crit_edge: ; preds = %bb15 %tmp16.le = load <2 x i8>, <2 x i8>* @​global.1, align 2, !tbaa !​2 br label %bb13

bb13: ; preds = %bb9.bb13_crit_edge, %bb %tmp.0.lcssa = phi <2 x i8> [ %tmp16.le, %bb9.bb13_crit_edge ], [ <i8 1, i8 0>, %bb ] %tmp23 = icmp slt <2 x i8> %tmp.0.lcssa, <i8 1, i8 1> %tmp28 = select <2 x i1> %tmp23, <2 x i8> %tmp7, <2 x i8> zeroinitializer %tmp30 = extractelement <2 x i8> %tmp28, i32 0 %tmp31 = sext i8 %tmp30 to i32 %tmp33 = extractelement <2 x i8> %tmp28, i32 1 %tmp34 = sext i8 %tmp33 to i32 %tmp35 = add nsw i32 %tmp31, %tmp34 ret i32 %tmp35

bb15: ; preds = %bb15.lr.ph, %bb15 %tmp2.05 = phi i32 [ 0, %bb15.lr.ph ], [ %tmp19, %bb15 ] %tmp19 = add nsw i32 %tmp2.05, 1 %tmp12 = icmp slt i32 %tmp19, %tmp11 br i1 %tmp12, label %bb15, label %bb9.bb13_crit_edge }

!​0 = !{i32 1, !"PIC Level", i32 2} !​1 = !{!"Apple LLVM version 8.0.0 (clang-800.0.42.1)"} !​2 = !{#3, !​3, i64 0} !​3 = !{!"omnipotent char", !​4, i64 0} !​4 = !{!"Simple C++ TBAA"} !​5 = !{#6, !​6, i64 0} !​6 = !{!"int", !​3, i64 0}

annamthomas avatar Jun 13 '17 02:06 annamthomas

assigned to @annamthomas

wjristow avatar Jun 08 '17 18:06 wjristow