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

[IndVarSimplify] Stale SCEV results transforms a terminating loop into infinite loop.

Open fraglantia opened this issue 2 months ago • 5 comments

; test.ll
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

define i32 @pre_inc_i8(ptr %ptr, i1 %always_false) {
entry:
  br label %for.cond

for.cond:
  %iv = phi i8 [ -128, %entry ], [ %iv.inc, %always_taken ] 
  %iv2 = phi i8 [ 0, %entry ], [ %iv2.inc, %always_taken ]
  store i8 %iv, ptr %ptr         ; poison store
  %cmp = icmp slt i8 %iv, 20
  br i1 %cmp, label %for.body, label %for.end

for.body:
  br i1 false, label %never_taken, label %always_taken

never_taken:
  store i8 %iv2, ptr %ptr
  br label %always_taken

always_taken:
  %iv.inc = add nsw i8 %iv, 1          ; poison here
  %iv2.inc = add nuw nsw i8 %iv2, 1    ; poison here
  br label %for.cond

for.end:
  ret i32 0
}

The loop runs %iv from -128 to 20, i.e. 148 iterations, and terminates. %iv and %iv2 do become poison in the iteration but they are only ever used as stores.

With the following pipeline, an infinite loop is introduced.

$ opt -S -passes='function(loop(indvars),loop(indvars))' test.ll

Reproducer: https://godbolt.org/z/6P11zffz9

define i32 @pre_inc_i8(ptr %ptr, i1 %always_false) {
entry:
  br label %for.cond

for.cond:
  %iv = phi i8 [ -128, %entry ], [ %iv.inc, %always_taken ]
  %iv2 = phi i8 [ 0, %entry ], [ %iv2.inc, %always_taken ]
  store i8 %iv, ptr %ptr, align 1
  br i1 true, label %for.body, label %for.end

for.body:
  br i1 false, label %never_taken, label %always_taken

never_taken:
  store i8 %iv2, ptr %ptr, align 1
  br label %always_taken

always_taken:
  %iv.inc = add nsw i8 %iv, 1
  %iv2.inc = add nuw i8 %iv2, 1
  br label %for.cond ; infinite loop

for.end:
  ret i32 0
}

The issue seem to have come from stale SCEV as invalidating it fixes the infinite loop.

$ opt -S -passes='function(loop(indvars),loop(indvars))' test.ll
$ opt -S -passes='function(loop(indvars),invalidate<scalar-evolution>,loop(indvars))' test.ll
  for.cond:
    %iv = phi i8 [ -128, %entry ], [ %iv.inc, %always_taken ]
    %iv2 = phi i8 [ 0, %entry ], [ %iv2.inc, %always_taken ]
    store i8 %iv, ptr %ptr, align 1
- ; with a fresh ScalarEvolution:
-   %exitcond = icmp ne i8 %iv2, -108
-   br i1 %exitcond, label %for.body, label %for.end
+ ; with the preserved ScalarEvolution:
+   br i1 true, label %for.body, label %for.end

fraglantia avatar Sep 10 '26 01:09 fraglantia

working on this

msolk avatar Oct 12 '26 06:10 msolk

@fraglantia the shared reproducer (https://godbolt.org/z/6P11zffz9) is compiling successfully. Kindly check once.

msolk avatar Oct 12 '26 07:10 msolk

@fraglantia the shared reproducer (https://godbolt.org/z/6P11zffz9) is compiling successfully. Kindly check once.

It is compiling successfully, but incorrectly. This is a miscompilation issue (wrong transformation). The input has a terminating loop, but after the optimization it becomes an infinite loop.

fraglantia avatar Oct 12 '26 10:10 fraglantia

LFTR leaves a stale nsw on {0,+,1}

for.cond:
  %iv  = phi i8 [ -128, %entry ], [ %iv.inc, %always_taken ]
  %iv2 = phi i8 [ 0, %entry ], [ %iv2.inc, %always_taken ] ; unit stride from 0, LFTR picks this
  %cmp = icmp slt i8 %iv, 20                               ; exits at %iv == 20
  br i1 %cmp, label %for.body, label %for.end              ; backedge-taken count 148 == i8 -108

always_taken:
  %iv.inc  = add nsw i8 %iv, 1
  %iv2.inc = add nuw nsw i8 %iv2, 1                        ; nsw is valid here; post-inc is not

After LFTR. %iv.inc is untouched. The loop still exits: nuw allows the signed wrap 127 + 1.

for.cond:
  %iv2 = phi i8 [ 0, %entry ], [ %iv2.inc, %always_taken ] ; cached {0,+,1}<nuw><nsw>, range [0, 128)
  %exitcond = icmp ne i8 %iv2, -108                        ; -108 is outside that range
  br i1 %exitcond, label %for.body, label %for.end

always_taken:
  %iv.inc  = add nsw i8 %iv, 1
  %iv2.inc = add nuw i8 %iv2, 1                            ; nsw dropped to match the post-inc recurrence

{0,+,1}<nuw><nsw> was built while %iv2.inc still had nsw. The node is uniqued by start, step, and loop. setNoWrapFlags only adds flags. forgetValue / forgetLoop / forgetAllLoops drop the value map and the exit count, not those flags. The next getSCEV(%iv2) returns the same node.

The second indvars run folds the compare in eliminateIVComparison:

for.cond:
  br i1 true, label %for.body, label %for.end              ; %for.end unreachable

always_taken:
  %iv2.inc = add nuw i8 %iv2, 1
  br label %for.cond                                       ; infinite

A fresh ScalarEvolution on the LFTR'd IR is {0,+,1}<nuw> and keeps icmp ne %iv2, -108.

cc: @nikic

msolk avatar Oct 12 '26 14:10 msolk

From a 10s look, this is probably a duplicate of https://github.com/llvm/llvm-project/issues/175729.

nikic avatar Oct 12 '26 16:10 nikic