<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/128403>128403</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR][Affine] Canonicalizing a valid IR leads to invalid IR
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            bondhugula
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bondhugula
      </td>
    </tr>
</table>

<pre>
    Canonicalizing an IR like this leads to invalid IR. When the operands of the affine.apply here are composed into its users, the resulting IR becomes invalid. This IR itself should have been invalid, but we are missing the necessary verification check.

```
#map = affine_map<()[s0, s1] -> (s0 * 23 + s1)>
module {
  func.func @func1(%arg0: memref<1x23x26xi32, strided<[?, ?, ?], offset: ?>>) -> () {
    %cst = arith.constant 63.9809875 : f32
 %cst_0 = arith.constant 1.19825836E-6 : f32
    %cst_1 = arith.constant 1.18534706E-4 : f32
    %cst_2 = arith.constant 0.00226843474 : f32
 %cst_3 = arith.constant 0.00489352504 : f32
    %cst_4 = arith.constant -2.76076837E-16 : f32
    %cst_5 = arith.constant 2.00018794E-13 : f32
 %cst_6 = arith.constant -8.60467184E-11 : f32
    %cst_7 = arith.constant 5.12229725E-8 : f32
    %cst_8 = arith.constant 1.48572235E-5 : f32
 %cst_9 = arith.constant 6.37261954E-4 : f32
    %cst_10 = arith.constant 0.00489352457 : f32
    %cst_11 = arith.constant 7.99881172 : f32
 %cst_12 = arith.constant 0.000000e+00 : f32
    %alloc = memref.alloc() {alignment = 64 : i64} : memref<23x26xf32>
    affine.for %arg1 = 0 to 23 {
      affine.for %arg2 = 0 to 26 {
        %0 = math.fma %cst, %cst_4, %cst_5 : f32
        %1 = math.fma %cst, %0, %cst_6 : f32
        %2 = math.fma %cst, %1, %cst_7 : f32
        %3 = math.fma %cst, %2, %cst_8 : f32
        %4 = math.fma %cst, %3, %cst_9 : f32
        %5 = math.fma %cst, %4, %cst_10 : f32
        %6 = arith.mulf %5, %cst_11 : f32
 %7 = math.fma %cst, %cst_0, %cst_1 : f32
        %8 = math.fma %cst, %7, %cst_2 : f32
        %9 = math.fma %cst, %8, %cst_3 : f32
        %10 = arith.divf %6, %9 : f32
        affine.store %10, %alloc[%arg1, %arg2] : memref<23x26xf32>
      }
    }
    %alloc_13 = memref.alloc() {alignment = 64 : i64} : memref<1x26xf32>
    affine.for %arg1 = 0 to 1 {
 affine.for %arg2 = 0 to 23 {
        affine.for %arg3 = 0 to 26 {
 %0 = affine.apply #map()[%arg1, %arg2]
          %1 = affine.load %alloc[%0, %arg3] : memref<23x26xf32>
          %2 = affine.load %alloc_13[%arg1, %arg3] : memref<1x26xf32>
          %3 = arith.addf %1, %2 : f32
 affine.store %3, %alloc_13[%arg1, %arg3] : memref<1x26xf32>
        }
 }
    }
    return 
  }
}
```

```
bin/mlir-opt -canonicalize canonicalize.mlir 
canonicalize.mlir:40:16: error: 'affine.load' op operand cannot be used as a symbol
          %1 = affine.load %alloc[%0, %arg3] : memref<23x26xf32>
               ^
canonicalize.mlir:40:16: note: see current operation: %15 = "affine.load"(%13, %arg3, %arg1, %arg2) <{map = affine_map<(d0)[s0, s1] -> (s0 * 23 + s1, d0)>}> : (memref<23x26xf32>, index, index, index) -> f32
```


```
bin/mlir-opt -canonicalize canonicalize.mlir 
canonicalize.mlir:40:16: error: 'affine.load' op operand cannot be used as a symbol
          %1 = affine.load %alloc[%0, %arg3] : memref<23x26xf32>
               ^
canonicalize.mlir:40:16: note: see current operation: %15 = "affine.load"(%13, %arg3, %arg1, %arg2) <{map = affine_map<(d0)[s0, s1] -> (s0 * 23 + s1, d0)>}> : (memref<23x26xf32>, index, index, index) -> f32
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWE2PozgT_jXOpRRklzEfhxySdCK19L6X1kp7bDlggncARxh6u_fXrwykQ9JOZkc72tOMUDCeeurDfh53ydJafWyUWhGxIY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