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

[InstCombine] Infinite loop by operating on same set of instructions in worklist

Open annamthomas opened this issue 8 years ago • 8 comments

Bugzilla Link 38915
Resolution FIXED
Resolved on Sep 14, 2018 08:38
Version trunk
OS All
CC @topperc,@RKSimon,@rotateright
Fixed by commit(s) 342163

Extended Description

This test goes into infinite loop when run with InstCombine on llvm ToT:

opt -instcombine test.ll <-- hangs with rerunning IC over the same set of instructions in the worklist.

test.ll: define void @​baz(i32 %arg) { bb: switch i32 undef, label %bb4 [ i32 0, label %bb2 i32 1, label %bb3 i32 2, label %bb1 ]

bb1: ; preds = %bb ret void

bb2: ; preds = %bb unreachable

bb3: ; preds = %bb ret void

bb4: ; preds = %bb %tmp = sub i32 0, %arg %tmp5 = and i32 %tmp, 3 %tmp6 = sub i32 0, %tmp5 %tmp7 = sub i32 0, %tmp5 %tmp8 = sub i32 -2, %tmp5 %tmp9 = icmp sgt i32 %tmp6, -2147483647 %tmp10 = select i1 %tmp9, i32 %tmp6, i32 -2147483647 %tmp11 = mul i32 %tmp10, -1 %tmp12 = icmp sgt i32 %tmp7, -2147483647 %tmp13 = select i1 %tmp12, i32 %tmp7, i32 -2147483647 %tmp14 = mul i32 %tmp13, -1 %tmp15 = icmp sgt i32 %tmp11, %tmp14 %tmp16 = select i1 %tmp15, i32 %tmp11, i32 %tmp14 %tmp17 = sub i32 -1, %tmp16 %tmp18 = icmp sgt i32 %tmp17, %tmp8 %tmp19 = select i1 %tmp18, i32 %tmp17, i32 %tmp8 %tmp20 = sub i32 -1, %tmp19 %tmp21 = icmp sgt i32 %tmp20, 1 %tmp22 = select i1 %tmp21, i32 %tmp20, i32 1 %tmp23 = add i32 %tmp22, -1 %tmp24 = add i32 %tmp5, -1 %tmp25 = icmp sgt i32 %tmp24, %tmp23 br i1 %tmp25, label %bb26, label %bb27

bb26: ; preds = %bb4 unreachable

bb27: ; preds = %bb4 unreachable }

annamthomas avatar Sep 12 '18 14:09 annamthomas

Should be fixed with: https://reviews.llvm.org/rL342163

Patch includes the test from comment 7.

rotateright avatar Sep 14 '18 15:09 rotateright

This looks like the minimal test:

define i32 @​#38915 (i32 %x, i32 %y, i32 %z) { %xn = sub i32 0, %x %yn = sub i32 0, %y %c1 = icmp sgt i32 %xn, %yn %m1 = select i1 %c1, i32 %xn, i32 %yn %m1n = xor i32 %m1, -1 %c2 = icmp sgt i32 %m1n, %z %m2 = select i1 %c2, i32 %m1n, i32 %z %m2n = xor i32 %m2, -1 ret i32 %m2n }

rotateright avatar Sep 13 '18 17:09 rotateright

Not sure if this is minimal yet, but it hangs instcombine:

define i1 @​#38915 (i32 %arg, i32 %tmp10, i32 %tmp13, i32 %tmp5, i32 %tmp7, i32 %tmp8) { %tmp11 = sub i32 0, %tmp10 %tmp14 = sub i32 0, %tmp13 %tmp15 = icmp sgt i32 %tmp11, %tmp14 %tmp16 = select i1 %tmp15, i32 %tmp11, i32 %tmp14 %tmp17 = xor i32 %tmp16, -1 %tmp18 = icmp sgt i32 %tmp17, %tmp8 %tmp19 = select i1 %tmp18, i32 %tmp17, i32 %tmp8 %tmp20 = xor i32 %tmp19, -1 %tmp21 = icmp sgt i32 %tmp20, 1 %tmp22 = select i1 %tmp21, i32 %tmp20, i32 1 %tmp23 = add i32 %tmp22, -1 %tmp24 = add i32 %tmp5, -1 %tmp25 = icmp sgt i32 %tmp24, %tmp23 ret i1 %tmp25 }

rotateright avatar Sep 13 '18 15:09 rotateright

IIUC, part of https://reviews.llvm.org/D51964 will solve this. If this a recent regression, we should try to get that into the release if it's not too late?

rotateright avatar Sep 12 '18 20:09 rotateright

@​spatel: Any ideas on what maybe going on here?

I could narrow down to the pattern we're trying to transform here: diff --git a/lib/Transforms/InstCombine/InstCombineSelect.cpp b/lib/Transforms/InstCombine/InstCombineSelect.cpp index e710c486706..0ccc680a23f 100644 --- a/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1164,7 +1164,7 @@ Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner, // This transform is performance neutral if we can elide at least one xor from // the set of three operands, since we'll be tacking on an xor at the very // end.

  • if (SelectPatternResult::isMinOrMax(SPF1) &&
  • if (false && SelectPatternResult::isMinOrMax(SPF1) && SelectPatternResult::isMinOrMax(SPF2) && IsFreeOrProfitableToInvert(A, NotA, ElidesXor) && IsFreeOrProfitableToInvert(B, NotB, ElidesXor) &&

folding SPF of SPF seems to be the issue, but it could be that a wrong values are identified for LHS and RHS?

Couple of prints before the call to foldSPFofSPF show this: SI: %tmp20 = select i1 %1, i32 %0, i32 %tmp16 RHS: %tmp16 = select i1 %tmp15, i32 %tmp11, i32 %tmp14 LHS2: %tmp11 = sub i32 0, %tmp10 RHS2: %tmp14 = sub i32 0, %tmp13 SPF23

SI: %tmp20 = select i1 %1, i32 %0, i32 %tmp16 LHS: %tmp16 = select i1 %tmp15, i32 %tmp11, i32 %tmp14 RHS: %0 = add nuw nsw i32 %tmp5, 1 LHS2: %tmp11 = sub i32 0, %tmp10 RHS2: %tmp14 = sub i32 0, %tmp13

Note that for the same outer select, at one time the RHS is %tmp16, but during the next call to matchSelectPattern, %tmp16 becomes the LHS.

Value *LHS2, *RHS2; ~ if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor) {

  •     errs() << "SI: " << SI << "  LHS:  " << *LHS << "  RHS: " << *RHS << "  LHS2: " << *LHS2
    
  •            << " RHS2: " << *RHS2 << "  SPF2" <<  SPF2 << "\n";
        if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
                                          SI, SPF, RHS))
          return R;
    

~ }

  •   if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor) {
    
  •     errs() << "SI: " << SI << "  RHS:  " << *RHS << "  LHS2: " << *LHS2
    
  •            << " RHS2: " << *RHS2 <<  "  SPF2" <<  SPF2 << "\n";
        if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
                                          SI, SPF, LHS))
          return R;
    
  •   }
    

annamthomas avatar Sep 12 '18 16:09 annamthomas

Narrowed down the problem to the min/max transform in InstCombineSelect:

With this hack, we do not go into inf loop in IC:

--- a/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1868,7 +1868,7 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { return I; }

  • if (SPF) {
  • if (false && SPF) { // MAX(MAX(a, b), a) -> MAX(a, b) // MIN(MIN(a, b), a) -> MIN(a, b) // MAX(MIN(a, b), a) -> a

annamthomas avatar Sep 12 '18 15:09 annamthomas

it looks like Select InstCombine transform is the bad one here. Returning nullptr on visitSelect allows IC to finish successfully.

annamthomas avatar Sep 12 '18 14:09 annamthomas

-debug-only=instcombine

INSTCOMBINE ITERATION #​2 on baz IC: ADDING: 27 instrs to worklist IC: Visiting: switch i32 undef, label %bb4 [ i32 0, label %bb2 i32 1, label %bb3 i32 2, label %bb1 ] IC: Visiting: ret void IC: Visiting: ret void IC: Visiting: unreachable IC: Visiting: %tmp = sub i32 0, %arg IC: Visiting: %tmp5 = and i32 %tmp, 3 IC: Visiting: %tmp6 = sub nsw i32 0, %tmp5 IC: Visiting: %tmp7 = sub nsw i32 0, %tmp5 IC: Visiting: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: Visiting: %tmp9 = icmp sgt i32 %tmp6, -2147483647 IC: Visiting: %tmp10 = select i1 %tmp9, i32 %tmp6, i32 -2147483647 IC: Visiting: %tmp11 = sub i32 0, %tmp10 IC: Visiting: %tmp12 = icmp sgt i32 %tmp7, -2147483647 IC: Visiting: %tmp13 = select i1 %tmp12, i32 %tmp7, i32 -2147483647 IC: Visiting: %tmp14 = sub i32 0, %tmp13 IC: Visiting: %0 = icmp sgt i32 %tmp11, %tmp14 IC: Visiting: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: Visiting: %2 = xor i32 %1, -1 IC: Visiting: %3 = icmp slt i32 %tmp8, %2 IC: Visiting: %4 = select i1 %3, i32 %2, i32 %tmp8 IC: Visiting: %5 = xor i32 %4, -1 IC: ADD: %5 = xor i32 %tmp8, -1 IC: ADD: %6 = icmp slt i32 %1, %5 IC: Old = %7 = xor i32 %4, -1 New = = select i1 %6, i32 %1, i32 %5 IC: ADD: = select i1 %6, i32 %1, i32 %5 IC: ERASE %8 = xor i32 %4, -1 IC: ADD: %4 = select i1 %3, i32 %2, i32 %tmp8 IC: DCE: %4 = select i1 %3, i32 %2, i32 %tmp8 IC: ERASE %4 = select i1 %3, i32 %2, i32 %tmp8 IC: ADD: %3 = icmp slt i32 %tmp8, %2 IC: ADD: %2 = xor i32 %1, -1 IC: ADD: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: Visiting: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: Visiting: %2 = xor i32 %1, -1 IC: DCE: %3 = icmp slt i32 %tmp8, %2 IC: ERASE %3 = icmp slt i32 %tmp8, %2 IC: ADD: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: ADD: %2 = xor i32 %1, -1 IC: DCE: %2 = xor i32 %1, -1 IC: ERASE %2 = xor i32 %1, -1 IC: ADD: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: Visiting: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: Visiting: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: Visiting: %4 = select i1 %3, i32 %1, i32 %2 IC: ADD: %4 = xor i32 %tmp11, -1 IC: ADD: %5 = xor i32 %tmp14, -1 IC: ADD: %6 = icmp slt i32 %4, %5 IC: ADD: %7 = select i1 %6, i32 %4, i32 %5 IC: ADD: %8 = icmp sgt i32 %7, %tmp8 IC: ADD: %9 = select i1 %8, i32 %7, i32 %tmp8 IC: ADD: %10 = xor i32 %9, -1 IC: Replacing %11 = select i1 %3, i32 %1, i32 %2 with %10 = xor i32 %9, -1 IC: Mod = %4 = select i1 %3, i32 %1, i32 %2 New = %11 = select i1 %3, i32 %1, i32 %2 IC: ERASE %11 = select i1 %3, i32 %1, i32 %2 IC: ADD: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: Visiting: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: Visiting: %10 = xor i32 %9, -1 IC: Visiting: %9 = select i1 %8, i32 %7, i32 %tmp8 IC: Visiting: %8 = icmp sgt i32 %7, %tmp8 IC: Visiting: %7 = select i1 %6, i32 %4, i32 %5 IC: ADD: %7 = icmp sgt i32 %tmp11, %tmp14 IC: ADD: %8 = select i1 %7, i32 %tmp11, i32 %tmp14 IC: Old = %9 = select i1 %6, i32 %4, i32 %5 New = = xor i32 %8, -1 IC: ADD: %10 = icmp sgt i32 , %tmp8 IC: ADD: %11 = select i1 %10, i32 , i32 %tmp8 IC: ADD: = xor i32 %8, -1 IC: ERASE %10 = select i1 %6, i32 %4, i32 %5 IC: Visiting: %9 = xor i32 %8, -1 IC: Visiting: %11 = select i1 %10, i32 %9, i32 %tmp8 IC: Visiting: %10 = icmp sgt i32 %9, %tmp8 IC: Visiting: %8 = select i1 %7, i32 %tmp11, i32 %tmp14 IC: Visiting: %7 = icmp sgt i32 %tmp11, %tmp14 IC: DCE: %6 = icmp slt i32 %4, %5 IC: ERASE %6 = icmp slt i32 %4, %5 IC: DCE: %5 = xor i32 %tmp14, -1 IC: ERASE %5 = xor i32 %tmp14, -1 IC: ADD: %tmp14 = sub i32 0, %tmp13 IC: Visiting: %tmp14 = sub i32 0, %tmp13 IC: DCE: %4 = xor i32 %tmp11, -1 IC: ERASE %4 = xor i32 %tmp11, -1 IC: ADD: %tmp11 = sub i32 0, %tmp10 IC: Visiting: %tmp11 = sub i32 0, %tmp10 IC: DCE: %3 = icmp slt i32 %1, %2 IC: ERASE %3 = icmp slt i32 %1, %2 IC: ADD: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: DCE: %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: ERASE %1 = select i1 %0, i32 %tmp11, i32 %tmp14 IC: ADD: %0 = icmp sgt i32 %tmp11, %tmp14 IC: ADD: %tmp11 = sub i32 0, %tmp10 IC: Visiting: %tmp11 = sub i32 0, %tmp10 IC: DCE: %0 = icmp sgt i32 %tmp11, %tmp14 IC: ERASE %0 = icmp sgt i32 %tmp11, %tmp14 IC: ADD: %tmp11 = sub i32 0, %tmp10 IC: ADD: %tmp14 = sub i32 0, %tmp13 IC: Visiting: %tmp14 = sub i32 0, %tmp13 IC: Visiting: %tmp11 = sub i32 0, %tmp10 IC: DCE: %0 = xor i32 %tmp8, -1 IC: ERASE %0 = xor i32 %tmp8, -1 IC: ADD: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: Visiting: %tmp8 = sub nuw nsw i32 -2, %tmp5 IC: Visiting: %tmp21 = icmp sgt i32 %5, 1 IC: Visiting: %tmp22 = select i1 %tmp21, i32 %5, i32 1 IC: Visiting: %tmp25 = icmp sgt i32 %tmp5, %tmp22 IC: Visiting: br i1 %tmp25, label %bb26, label %bb27 IC: Visiting: unreachable IC: Visiting: unreachable

INSTCOMBINE ITERATION #​3 on baz IC: ADDING: 27 instrs to worklist <-- same set of instructions added to worklist again. And this keeps repeating.

annamthomas avatar Sep 12 '18 14:09 annamthomas