Issue 53034
Summary AffineScalarReplacement Broken when used within looping construct
Labels new issue
Assignees
Reporter wsmoses
    Before scalar replacement:
```
wmoses@beast:~/git/Polygeist/build (nl) $ ./bin/polygeist-opt imm.mlir -cse -canonicalize -mem2reg -cse -canonicalize -mem2reg -canonicalize -mem2reg -canonicalize -loop-restructure -affine-cfg -canonicalize -canonicalize-scf-for -canonicalize
module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-unknown-linux-gnu"}  {
  func @if_loop_1(%arg0: memref<?xi32>, %arg1: i32) -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
    %c1 = arith.constant 1 : index
    %c0 = arith.constant 0 : index
    %c0_i32 = arith.constant 0 : i32
    %c5_i32 = arith.constant 5 : i32
    %c10_i32 = arith.constant 10 : i32
    %0 = memref.alloca() : memref<1xi32>
    %1 = llvm.mlir.undef : i32
    affine.store %1, %0[0] : memref<1xi32>
    affine.store %c0_i32, %0[0] : memref<1xi32>
    %2 = arith.index_cast %arg1 : i32 to index
    scf.for %arg2 = %c0 to %2 step %c1 {
      %4 = memref.load %arg0[%arg2] : memref<?xi32>
      %5 = arith.muli %4, %c5_i32 : i32
      %6 = arith.cmpi sgt, %5, %c10_i32 : i32
      scf.if %6 {
        %7 = affine.load %0[0] : memref<1xi32>
        %8 = arith.addi %5, %7 : i32
        affine.store %8, %0[0] : memref<1xi32>
      }
    }
    %3 = affine.load %0[0] : memref<1xi32>
    return %3 : i32
  }
  ```
  
  After scalar replacement the load in the scf.if is incorrectly replaced by 0.
  
  ```
  wmoses@beast:~/git/Polygeist/build (nl) $ ./bin/polygeist-opt imm.mlir -cse -canonicalize -mem2reg -cse -canonicalize -mem2reg -canonicalize -mem2reg -canonicalize -loop-restructure -affine-cfg -canonicalize -canonicalize-scf-for -canonicalize | ../mlir-build/bin/mlir-opt -affine-scalrep
module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-unknown-linux-gnu"}  {
  func @if_loop_1(%arg0: memref<?xi32>, %arg1: i32) -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
    %c1 = arith.constant 1 : index
    %c0 = arith.constant 0 : index
    %c0_i32 = arith.constant 0 : i32
    %c5_i32 = arith.constant 5 : i32
    %c10_i32 = arith.constant 10 : i32
    %0 = memref.alloca() : memref<1xi32>
    %1 = llvm.mlir.undef : i32
    affine.store %c0_i32, %0[0] : memref<1xi32>
    %2 = arith.index_cast %arg1 : i32 to index
    scf.for %arg2 = %c0 to %2 step %c1 {
      %4 = memref.load %arg0[%arg2] : memref<?xi32>
      %5 = arith.muli %4, %c5_i32 : i32
      %6 = arith.cmpi sgt, %5, %c10_i32 : i32
      scf.if %6 {
        %7 = arith.addi %5, %c0_i32 : i32
        affine.store %7, %0[0] : memref<1xi32>
      }
    }
    %3 = affine.load %0[0] : memref<1xi32>
    return %3 : i32
  }
}
  ```
  
  cc @ftynse @bondhugula
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to