<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/81601>81601</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Affine fusion] Uninitialized memory region is accessed after loops fusion
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
m-ly4
</td>
</tr>
</table>
<pre>
IIUC, affine-loop-fusion pass doesn't take into account memory accesses in regions inside of non affine-for ops. In some cases it leads to removing operations that are necessary for another piece of code.
Example (reduced to minimal reproducer from real code):
```
func.func @func() {
%cst = arith.constant 1 : i32
%alloc = memref.alloc() : memref<16xi32>
affine.for %arg0 = 0 to 16 {
affine.store %cst, %alloc[%arg0] : memref<16xi32>
}
affine.for %arg0 = 0 to 16 {
%1 = affine.load %alloc[%arg0] : memref<16xi32>
}
%0 = arith.cmpi eq, %cst, %cst : i32
scf.if %0 {
affine.for %arg0 = 0 to 16 {
%1 = affine.load %alloc[%arg0] : memref<16xi32>
}
}
return
}
```
command: `mlir-opt -pass-pipeline='builtin.module(func.func(affine-loop-fusion))' example.mlir`
Output:
```
module {
func.func @func() {
%alloc = memref.alloc() : memref<1xi32>
%c1_i32 = arith.constant 1 : i32
%alloc_0 = memref.alloc() : memref<16xi32>
affine.for %arg0 = 0 to 16 {
affine.store %c1_i32, %alloc[0] : memref<1xi32>
%1 = affine.load %alloc[0] : memref<1xi32>
}
%0 = arith.cmpi eq, %c1_i32, %c1_i32 : i32
scf.if %0 {
affine.for %arg0 = 0 to 16 {
%1 = affine.load %alloc_0[%arg0] : memref<16xi32>
}
}
return
}
}
```
So, `alloc_0` becomes uninitialized, but still used by the code after fused loop.
Even if I use `affine.if` operation (like on example below) the behavior is the same:
```
#set0 = affine_set<(d0) : (1 == 0)>
func.func @func() {
%cst = arith.constant 1 : i32
%alloc = memref.alloc() : memref<16xi32>
affine.for %arg0 = 0 to 16 {
affine.store %cst, %alloc[%arg0] : memref<16xi32>
}
affine.for %arg0 = 0 to 16 {
%1 = affine.load %alloc[%arg0] : memref<16xi32>
}
%0 = arith.index_cast %cst : i32 to index
affine.if #set0(%0) {
affine.for %arg0 = 0 to 16 {
%1 = affine.load %alloc[%arg0] : memref<16xi32>
}
}
return
}
```
hash of last llvm-project's commit: c230138011cbf07ad7caf9d256ae9d0c5032a974
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk1v6ygX_jVkcxQLQxw7iyx6m1upq3fx6q4rDMcJczF4APe28-tHYCdN2lulnS_NSFFsMJyPh_PwHBGC3lvELam-kGq3EGM8OL_tl-Z5tWidet7e33-7JewWRNdpi0vj3LDsxqCdhUGEAMphsITVEaL4jqBtdCCkdKON0GPv_HMaYggYQFvwuNfOptegFYLrwDp7NN45D24IBdxbCK5HkCJvi2BQqADRgcfePWq7BzegFzHbigcRQXgEi8mR8M-QLAnr4gE9DBpl9iSdwoLQHaE3X59EPxgEwhqPapSokvFeW90LAx4H79Ksh867HjwKk3cTtiH8ZjJB1nT-5WE3WlmkPyArmp6ENYRtgNRfpgUAhFUyRCB8B8LreCiksyEKG6EEwm9AczYvJawSxjiZ1_bYe-yKPHE0ym_macJvy_VT2sm_Hv1MaBYJg2TI72m2Q1OK5fo8otPaEJ3HOcB02scAUllMJki1u-KW1Lv59TMBEFaVEyTTJuOE-pPu03Z6DnM_aMBf57xeMpwO4xx3CLIrdDcb-AlOH0rpL0zqMq3Tq8c4ejvX4XH2VUFK1_fCqmSfrGlvtF-6IcIysXY56AGNtkj4jrC6HbWJ2ha9U6NBwppTMRPWvCV-YkH61YATjYpk_eT4f2McxvgeTyYf54BdZ87n6PAWQlbJ8kFz9hHqTeuz9Qf6x_j32Wp5w8Ec7Csa_qRYLt1eq7mrBi6L7RqLzmM8ofsKyHfZ9FmErjDqgX6CU5c5ng_OWXX26T2C_d_l9Nf0GMOaQovS9RhgtNrqqIXRv6FKy9oxQojaGBgDKmif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