<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58419>58419</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Hit an assertion in Affine patterns when running canonicalizer
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir:affine,
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hanhanW
</td>
</tr>
</table>
<pre>
I hit an assertion when running canonicalizer. It's broken in one of affine.apply canonicalization patterns. It shouldn't crash given it's a canonicalization pattern.
To repro: `mlir-opt --canonicalize a.mlir`
```mlir
#map0 = affine_map<(d0) -> (d0 mod 2)>
#map1 = affine_map<(d0, d1)[s0] -> ((d0 floordiv 2) * s0 + d1 floordiv 2)>
#map2 = affine_map<()[s0, s1, s2] -> (s0 * s1 + s2)>
#map3 = affine_map<()[s0, s1] -> (s0 floordiv s1)>
#map4 = affine_map<()[s0, s1] -> (s0 + s1)>
module {
func.func @dynamic_unpack_simple_dispatch_0(%arg0: memref<?xi32>, %arg1: memref<?xi32>, %arg2: index, %arg3: index, %arg4: index, %arg5: index, %arg6: index) {
%c0 = arith.constant 0 : index
%c4 = arith.constant 4 : index
%c2 = arith.constant 2 : index
%c1 = arith.constant 1 : index
scf.for %arg7 = %c0 to %arg5 step %c1 {
scf.for %arg8 = %c0 to %arg6 step %c1 {
%0 = affine.apply #map0(%arg7)
%1 = affine.apply #map0(%arg8)
%2 = affine.apply #map1(%arg7, %arg8)[%arg3]
%3 = affine.apply #map2()[%2, %c2, %0]
%4 = affine.apply #map2()[%3, %c2, %1]
%5 = affine.apply #map3()[%arg2, %c4]
%6 = affine.apply #map4()[%4, %5]
%7 = memref.load %arg0[%6] : memref<?xi32>
%8 = affine.apply #map2()[%arg7, %arg6, %arg8]
memref.store %7, %arg1[%8] : memref<?xi32>
}
}
return
}
}
```
Error logs:
```
llvm-project/mlir/lib/IR/AffineExpr.cpp:978: auto getSemiAffineExprFromFlatForm(ArrayRef<int64_t>, unsigned int, unsigned int, ArrayRef<mlir::AffineExpr>, mlir::MLIRContext *)::(anonymous class)::operator()(std::pair<unsigned int, int>, int64_t, mlir::AffineExpr) const: Assertion `!llvm::is_contained(indices, index) && "Key is already present in indices vector and overwriting will " "happen in `indexToExprMap` and `coefficients`!"' failed.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVkuPozgQ_jXkYgWBeebAIf2ItjUzl96R9hg5YIJ3jI1s093ZX79lAwnJkJkeiUBsV331cL0OsjoVL6hhBhGBiNZUGSYFem-oQKoXgokjKomQgpWEs_-o8tGL8XCm0UHJH0DEBJKCIlkjUtdMUJ90HT_NeYhD7IgxVAlt-ZFuZM8rATgGlYroBh3ZmwUboMlddt8LnrxgO7y_S6Rop6QXbZGXBi1nai07g9brucaI-PYECOa8dukedzZs4aglXYC86Gm0ZQ9rL3r0cF4FHt6gtRc9I7dCrawQhj3YmXOHd7kfURVa-uRBB17ydMYa4GouparYm8OE7S3SoAh-AKbrsxt5eFHeWQ5I1aF747lMhw0iQidCL-BGn8G9RjyrqcOf8eI_x3OqXUGBz3tOkZc9DGuE6l6Uvn0hLw6qkyAtK_e96Ej5Y69Z23G6r5iG2CmbfeAEJkQdAxswLW0Vra0e0e6DRdhKATUGivC3FNhSMFHRj8tetLAXL-wlC3vpbG8zNxFZgnKMSsVM45dSaEOEQXZzYrqijpeo43vUeIka36MOl6jDJWpd1n4t1Whg5vgGW4ycPIG0od2EOzf6lj1fYk9_we60nSfzWJjGLD8HQ2Yj7JYv_D1fvsSH7_GFc3nTnY_xPwVP8vQTXnQPD5-zx0odIcvpT7CEFX8KK7rFCpewkntY0RzLpckIFy_BpPdg4jlMPGIkSxBDWA2p6nNJKjTluONNbVG5l8y3WPmnPHR9h-n8Om_1G9XSRirqlJ1XGAeWf1I_L5tBXy0UNb0S0_p8dPkzNbp593tWCjKLy6MG2YttcVhy_tauocP-S0vozDvXK_GOswO8X17htXX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