Issue 134291
Summary [mlir] -remove-dead-values generates invalid MLIR for affine.for with iter_args, which executes only once.
Labels mlir
Assignees
Reporter yyong120
    To reproduce: `mlir-opt -remove-dead-values test.mlir`

`test.mlir`:
```
func.func @test_affine_for() {
    %c1_i32 = arith.constant 1 : i32
    %alloca = memref.alloca() : memref<16xi32>
 %0 = affine.for %arg0 = 0 to 16 step 64 iter_args(%arg1 = %c1_i32) -> (i32) {
        %1 = arith.addi %arg1, %c1_i32 : i32
        memref.store %1, %alloca[%arg0] : memref<16xi32>
        affine.yield %1 : i32
    }
 return
}
```

The generated `result.mlir`:
```
func.func @test_for() {
    %c1_i32 = arith.constant 1 : i32
    %alloca = memref.alloca() : memref<16xi32>
    affine.for %arg0 = 0 to 16 step 64 iter_args(%arg1 = %c1_i32) -> () {
        %0 = arith.addi %arg1, %c1_i32 : i32
 memref.store %0, %alloca[%arg0] : memref<16xi32>
        affine.yield
 }
    return
}
```
Run `mlir-opt --verify-diagnostics result.mlir`, the error is

```
result_1.mlir:4:5: error: unexpected error: custom op 'affine.for' mismatch between the number of loop-carried values and results
 affine.for %arg0 = 0 to 16 step 64 iter_args(%arg1 = %c1_i32) -> () {
 ^
```

This issue happens when an `affine.for` only executes once. Since the loop-carried arguments (`iter_args`) will not be updated, remove-dead-values pass will remove the return values of `affine.yield`. However, `iter_args` are still left in the for loop, which leads to an invalid mlir.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to