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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Affine For Op invalid IR: verifying symbols in lower/upper bound operands
        </td>
    </tr>

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

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

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

<pre>
    Hey, mlir complains that the following IR is invalid:

```
func.func @mul_gf256(%x: i64) -> (i64) {
    %0 = arith.constant 0 : i64
    %1 = arith.constant 2 : i64
    %3 = arith.index_cast %1 : i64 to index
    %2:1 = affine.for %4 = 0 to 2 iter_args(%5 = %0) -> (i64) {
      %6 = arith.addi %5, %x : i64
      %7 = arith.constant 2 : i64
      %9 = arith.index_cast %7 : i64 to index
      affine.for %10 = 0 to %9 {
        %11 = arith.constant 1 : i64 
      }
      affine.yield %6 : i64
    }
 func.return %2 : i64
}
```

with the error:
```
 error: 'affine.for' op operand cannot be used as a symbol
      affine.for %10 = 0 to %9 {
 ^
/usr/local/google/home/asraa/git/heir/out.mlir:9:7: note: see current operation: 
"affine.for"(%6) <{lowerBoundMap = affine_map<() -> (0)>, operandSegmentSizes = array<i32: 0, 1, 0>, step = 1 : index, upperBoundMap = affine_map<()[s0] -> (s0)>}> ({
^bb0(%arg4: index):
  %7 = "arith.constant"() <{value = 1 : i64}> : () -> i64
  "affine.yield"() : () -> ()
}) : (index) -> ()
```

Both for loops use an index cast constant as their upper bound. The outer loop is declared valid since the upper bound operand is a valid symbol since it's a top-level value. The inner loop's operand is not a top-level value, so we check that it's defining operation is pure and that all of the operands to the defining op are valid symbols. But that fails since the operand to `index_cast` is not an index type.

See the code here:
https://github.com/llvm/llvm-project/blob/f8ba0df8781a9f2d0b54fcd6b06a47a06234a4c4/mlir/lib/Dialect/Affine/IR/AffineOps.cpp#L422

Would it make sense to validate this IR by skipping that index check for this particular case?

Why we need this: We're writing a python frontend for our MLIR project, and the interpreted code has assignments to constants, and then we index_cast those constants when using them for range arguments. We can't simplify the IR text before this unfortunately.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVk1v2zgQ_TX0ZVCDpr7sgw9OHGMDtCiQLtBjQEkji1uaFEgqqffXL4aUbSVtd7eAYFnUm-G8xzekpPfqaBC3rLhjxX4hx9Bbt5XeSbmobXve_oFnJu7hpJWDxp4GLZXxEHoZIPQIndXavipzhMcnUB6UeZFatSzbMR6vkk8X33WjaZb0Ayznp1E_HztRlEysmSi-s2wHqsyZ2MAHlj0AE-vpkVV3jO8AAJgoOLBsD9Kp0C8ba3yQJgANpugrbvUznPgRl81wyrT4_bmRPlwyRDAEC_HVLUqwbDfN0HXK4LKzjsbzOMYpQoAK6J6lO_rEsIjviMK_coz5y1lVsm0VjRW0DKTUOxIxoPo_dCNy80vC1a8IwzuWK36jmTLOqk_q_1T-m6K3gqr9-0nOCnV7EeHNaiVsdJHDMDoTl2KGSoi55RjfvarQR6uic9ZNxpxBruPARHUjykQFdgA7oJOmhUYaYwPUCKPHFqQHCf58qq3-TZFY8UAFiMPoHRMHbRupmTgcrT1qZOLQ2xPdYgfSuAo0iIrAdgxLakSW7TYs21VUtLEB6e4RoRmdQxNS0UFZE1nRbGJOTCRDltF42T2r7rR9RXdnR9N-ksPM188nORCC8DfPkoFZ9kB-nOT5gscTmvBF_Y1-Wnknzyy7Vxm1CnDCruiHT4E-YJpoMkW0m7iHcRj-uxJW3HnOiv21In8pqdpPI0ltVjzUNU90pTvms6k2yQmz7iGV3hj2otRFpRepR5wXXeaXCaN7biJdTHsTPrp6nvBtwMQrWfj2_lLre9Bbh9_Z0APZTls7eDIoSJN4QmzuawdK2rlRuSQz1KTzEv7sEewYMCWgXbzFRkuHLcTNHLwyDcYemsVdW0NRL0zA2BETnpxb0btghw8aX1BDVDBNqIyZJoyoWTLqsx-ComcsvCI0PTbf0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