aleks-tmb wrote:

@VigneshwarJ 
Hi, before your MR was reverted, our downstream testing detected an issue.
Below is a minimal reproducer:

```
define i32 @main(i1 %cond) {
entry:
  %a = load i32, ptr addrspace(1) null, align 8
  br label %loop0

loop0:                                         
  %iv0 = phi i32 [ 0, %entry ], [ %iv0.next, %loop0 ]
  %iv0.next = add i32 %iv0, 1
  br i1 %cond, label %mid, label %loop0

mid:
  %add = add i32 %a, %iv0
  ret i32 %add
}
```
Command to reproduce:
```
bin/opt -passes='print<scalar-evolution>,loop-mssa(licm)' -verify-scev test.ll
```
Output:
```
Printing analysis 'Scalar Evolution Analysis' for function 'main':
Classifying expressions for: @main
  %a = load i32, ptr addrspace(1) null, align 8
  -->  %a U: full-set S: full-set
  %iv0 = phi i32 [ 0, %entry ], [ %iv0.next, %loop0 ]
  -->  {0,+,1}<%loop0> U: full-set S: full-set          Exits: <<Unknown>>      
  LoopDispositions: { %loop0: Computable }
  %iv0.next = add i32 %iv0, 1
  -->  {1,+,1}<%loop0> U: full-set S: full-set          Exits: <<Unknown>>      
  LoopDispositions: { %loop0: Computable }
  %add = add i32 %a, %iv0
  -->  {%a,+,1}<%loop0> U: full-set S: full-set
Determining loop execution counts for: @main
Loop %loop0: Unpredictable backedge-taken count.
Loop %loop0: Unpredictable constant max backedge-taken count. 
Loop %loop0: Unpredictable symbolic max backedge-taken count. 
opt: 
/remote-home/apopov/old_work/llvm-project/llvm/lib/Analysis/ScalarEvolution.cpp:3706:
 const llvm::SCEV* 
llvm::ScalarEvolution::getAddRecExpr(llvm::SmallVectorImpl<const llvm::SCEV*>&, 
const llvm::Loop*, llvm::SCEV::NoWrapFlags): Assertion 
`isAvailableAtLoopEntry(Op, L) && "SCEVAddRecExpr operand is not available at 
loop entry!"' failed.
```

The root cause of the issue is that after sinking load `%a` to the loop exit, 
the SCEV `{%a,+,1}<%loop0>` became invalid, since `%a` is no longer available 
at the `%loop0` entry.

A possible fix is to forget the SCEV after load sinking
```cpp
    ...
    moveInstructionBefore(I, ExitBlock->getFirstInsertionPt(), *SafetyInfo,
                          MSSAU, SE, MemorySSA::Beginning);
    if (SE)
      SE->forgetValue(&I);
```


https://github.com/llvm/llvm-project/pull/157559
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to