Issue 237581
Summary [MLIR][Affine] `affine-data-copy-generate` hoists a region symbol outside its SSA scope
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the official LLVM/MLIR 23.1.4 optimized Linux build.

```mlir
module {
  func.func @test(%arg0: memref<1xi32>) {
    affine.for %iv = 0 to 1 {
 %zero = arith.constant 0 : index
      %index = affine.linearize_index [%zero] by (1) : index
      %value = affine.load %arg0[%index] : memref<1xi32>
    }
    return
  }
}
```

The input verifies:

```console
$ mlir-opt reduced.mlir --verify-each -o /dev/null
```

The data-copy pass rejects the same verifier-valid input:

```console
$ mlir-opt reduced.mlir --affine-data-copy-generate -o /dev/null
reduced.mlir:6:16: error: operand #0 does not dominate this use
 %value = affine.load %arg0[%index] : memref<1xi32>
 ^
reduced.mlir:5:16: note: operand defined here (op in a child region)
 %index = affine.linearize_index [%zero] by (1) : index
 ^
```

With `--verify-each=false`, we see that the first invalid output contains an `affine.apply` before the loop that uses the loop-local linearization result:

```mlir
%3 = "affine.apply"(%8) ...
...
"affine.for"() ... ({
  %8 = "affine.linearize_index"(%2) ...
```

Expected behavior is for the pass to produce verifier-valid IR, leave the access unpromoted, or keep/rematerialize its index in a dominating scope.

## LLM-Suggested Cause

`findHighestBlockForPlacement` checks whether the memref definition and enclosing loop IVs permit hoisting, but it does not check the SSA scopes of other memory-region symbols. The source contains a TODO for this exact missing check. The loop-local `affine.linearize_index` result is treated as an invariant region symbol, and `generateCopy` uses it when materializing bounds before the loop.

Current main at [`24ea9d257831ee5d6f827511b35ddb09036859b8`](https://github.com/llvm/llvm-project/blob/24ea9d257831ee5d6f827511b35ddb09036859b8/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp#L1822-L1829) retains the same TODO and placement logic.

## LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to