<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=http://email.email.llvm.org/c/eJztVsuSozYU_Rq8UUGBMAYvWLTb0-tU5gNcAgRWWkiUHrGdr8-VgA5-1fTUbJJUV9lCEvdx7kM6VLK5lDvaSkWRrgknCik6cFLTngoTpC9BvA_il2ATTz-_PPVSUx2s44oS7aXybwF-65iB8TfJLx1l2s0ry3iDAlwIHuAtTNYocttMwDjMgqEcDGJ9H_WcKRTWmsJAhBQMELG_YNXTHiva_eDdZ7a5lEOoqDbK1sZC1CFpWyZoWLd3sstVqOs2hDRd747p6GVjOUXEGMUqa6hGQb7j_M8-aoghB04u0hoUpHtIAKZhDwmj4YDzGCYp9oNbJtdLN9us_RCyedIWTinBRSgKN9nMOvDuO2yDgwC_Iu_cENVRcwBQA6Cb3J-LzQFkrXgX8iRCzoQ9h52wTjHfI4d8DAqh1ooaQZFZe3BZOySBs5-BVYcBQYIVbYP0NUjfzsyB-OZcjxIuFuQ2oeohvHHzRwkC_--km9Glyz2wTM-GKkG4Mw3gFtiQ81MnXo8oZo5RLYU2RBjkNsG3aOj5Wjp-JB0_lT44yM81ILYr-eyZfPZYPnnqIHnsYYQ_Zj0inMua-IJs0VU1krkWS90xUT677oxFFuJt772MRyHSxl0HTm0qaBxkO_jvf-TpVn9M4k8aAdFlWnxpDjXcM3NrzbiRkbeFg0MauUM6SuKprVzpQdYb1oYOc-8s28n7XS9TzCVp0Nzv2W4yeRfAovuvjWWLIHrLmXcw5eKjW24K4BU3y6boB4Z0Zya9bNb_6J47Ay4FrJ3sXEforeej9bFSc4yfrc1ko1ggJE3DltDyR6DuW6P4ya5wN9N-0SRXC5ylvxKVosAEYjZzBX7h54YDYWN6vrRwSz1gT2SOFHksTPj5VBqmYaOWStHa8Mus0aDqguLo1vid1y_unWWC_BVFLiQHPfThfsTnt1xoswtXHsj0F2F_Efb_mLC_CPdfS7gPybJ-avW-tvl_jDE_Q521v69acxHALY7TpGiOtrOcrJoybbbplqyINUepypP2xLeyipdHYwbtqA-_jdR3tBUcxx4W7shMj3BQ8g_gWFgyrS2QJn7L0jhdr45lXrRbmq9bXGwq0hBcVcW2WW8bmmwLvGnyFScV5br0fYgFPSFvwl262X716whYiWOM4yTexJtkE6cRydpk3cQNKZp1nNEYkkF7wnjk7wCpupUqvcnKdo79OXC3_ucl0Zp1glIPGBAaZjgtX3x1v_svk98XHyY7Jd-pQKcjDFbDp8cJAoCPFMcaTHTI32yOpFcedukx_w2OrI9w>53034</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
AffineScalarReplacement Broken when used within looping construct
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
wsmoses
</td>
</tr>
</table>
<pre>
Before scalar replacement:
```
wmoses@beast:~/git/Polygeist/build (nl) $ ./bin/polygeist-opt imm.mlir -cse -canonicalize -mem2reg -cse -canonicalize -mem2reg -canonicalize -mem2reg -canonicalize -loop-restructure -affine-cfg -canonicalize -canonicalize-scf-for -canonicalize
module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-unknown-linux-gnu"} {
func @if_loop_1(%arg0: memref<?xi32>, %arg1: i32) -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
%c1 = arith.constant 1 : index
%c0 = arith.constant 0 : index
%c0_i32 = arith.constant 0 : i32
%c5_i32 = arith.constant 5 : i32
%c10_i32 = arith.constant 10 : i32
%0 = memref.alloca() : memref<1xi32>
%1 = llvm.mlir.undef : i32
affine.store %1, %0[0] : memref<1xi32>
affine.store %c0_i32, %0[0] : memref<1xi32>
%2 = arith.index_cast %arg1 : i32 to index
scf.for %arg2 = %c0 to %2 step %c1 {
%4 = memref.load %arg0[%arg2] : memref<?xi32>
%5 = arith.muli %4, %c5_i32 : i32
%6 = arith.cmpi sgt, %5, %c10_i32 : i32
scf.if %6 {
%7 = affine.load %0[0] : memref<1xi32>
%8 = arith.addi %5, %7 : i32
affine.store %8, %0[0] : memref<1xi32>
}
}
%3 = affine.load %0[0] : memref<1xi32>
return %3 : i32
}
```
After scalar replacement the load in the scf.if is incorrectly replaced by 0.
```
wmoses@beast:~/git/Polygeist/build (nl) $ ./bin/polygeist-opt imm.mlir -cse -canonicalize -mem2reg -cse -canonicalize -mem2reg -canonicalize -mem2reg -canonicalize -loop-restructure -affine-cfg -canonicalize -canonicalize-scf-for -canonicalize | ../mlir-build/bin/mlir-opt -affine-scalrep
module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-unknown-linux-gnu"} {
func @if_loop_1(%arg0: memref<?xi32>, %arg1: i32) -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
%c1 = arith.constant 1 : index
%c0 = arith.constant 0 : index
%c0_i32 = arith.constant 0 : i32
%c5_i32 = arith.constant 5 : i32
%c10_i32 = arith.constant 10 : i32
%0 = memref.alloca() : memref<1xi32>
%1 = llvm.mlir.undef : i32
affine.store %c0_i32, %0[0] : memref<1xi32>
%2 = arith.index_cast %arg1 : i32 to index
scf.for %arg2 = %c0 to %2 step %c1 {
%4 = memref.load %arg0[%arg2] : memref<?xi32>
%5 = arith.muli %4, %c5_i32 : i32
%6 = arith.cmpi sgt, %5, %c10_i32 : i32
scf.if %6 {
%7 = arith.addi %5, %c0_i32 : i32
affine.store %7, %0[0] : memref<1xi32>
}
}
%3 = affine.load %0[0] : memref<1xi32>
return %3 : i32
}
}
```
cc @ftynse @bondhugula
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztVstyozgU_Rq8UUGBMGAvWMRxZz01_QEuAQJrIiRKj7E9Xz9XAjL4VZ2u3sxMpSpRpMt9nPuQTirZXModbaWiSNeEE4UUHTipaU-FCdKXIN4H8UuQx9OPP556qakO1nFFifZaxbcAv3XMw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