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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] `shape.dim` verifier checks for UB instead of propagating the error or being truly UB
        </td>
    </tr>

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

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

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

<pre>
    The description of `shape.dim` reads:
```
If the index is error or out-of-bound then it returns an invalid size if the return type carries error information else the behavior is undefined.
```

However, in practice the verifier will reject this kind of construct as invalid, which as @joker-eph pointed out in https://github.com/llvm/llvm-project/issues/60295 is an anti-pattern as it makes the compiler non-composable.

In other words, unlike what the description says, there's no error propagation happening in the type or UB being generated.

I didn't fix that as part of https://github.com/llvm/llvm-project/commit/4bc2357c3de268b2b50ad0ff9c2c040329b75375 because I didn't know how to carry the error as part of the return type.
I'd need some guidance on that.

To reproduce:
Run `mlir-opt -canonicalize` on:
```
func.func @dim(%arg : tensor<1x?xf32>) -> index {
  %c2 = arith.constant 2 : index
  %add = arith.addi %c2, %c2 : index
  %dim = shape.dim %arg, %add : tensor<1x?xf32>, index -> index
  return %dim : index
}
```

Result:
```
error: 'shape.dim' op index is out of range
  %dim = shape.dim %arg, %add : tensor<1x?xf32>, index -> index
         ^
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VV2PozgQ_DXOSysRMQHCQx7mK7p5Xe3-AGM30Btoc7aZzOyvP9lkMtndm5NOJ90IMQHsclW7q6y8p44RD6K4F8XjSs2ht-7wp7aDHRsMq8aat8PXHsGg146mQJbBtiDKzPdqwo2hUZQZOFTGi_xOZI8iuxNldrnS43MLoUcgNvgK5AGdsw7iNYe1bdeNndnEIQwUwGGYHXtQDMQvaiADnn4g0IKyfIbwNiFo5RzhOyBxa92oEkUcPKbhDfbqhe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