[MLIR] Missed Optimization: `--gpu-eliminate-barriers` fails to eliminate redundant barrier across `memref.alloca_scope`
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
`gpu-eliminate-barriers` appears to miss a redundant barrier after `memref.alloca_scope`. The inner barrier already synchronizes the writes to `%a` with both subsequent reads, so the outer barrier should be removed.
When collecting effects, `collectEffects` skips the nested barrier and includes earlier writes, creating a spurious conflict across the outer barrier and resulting in a missed optimization.
### Reproducer
**input.mlir**
```mlir
module {
func.func @nested_barrier(
%a: memref<32xi32> {llvm.noalias},
%out: memref<32xi32> {llvm.noalias}) {
%c1 = arith.constant 1 : index
%c32 = arith.constant 32 : index
%c31 = arith.constant 31 : index
gpu.launch
blocks(%bx, %by, %bz) in (%gx = %c1, %gy = %c1, %gz = %c1)
threads(%t, %ty, %tz) in (%sx = %c32, %sy = %c1, %sz = %c1) {
%peer = arith.subi %c31, %t : index
%v = arith.index_cast %t : index to i32
memref.store %v, %a[%t] : memref<32xi32>
%x = memref.alloca_scope -> (i32) {
gpu.barrier // Required.
%r = memref.load %a[%peer] : memref<32xi32>
memref.alloca_scope.return %r : i32
}
gpu.barrier // Expected to be removed.
%y = memref.load %a[%peer] : memref<32xi32>
%sum = arith.addi %x, %y : i32
memref.store %sum, %out[%t] : memref<32xi32>
gpu.terminator
}
return
}
}
```
**Command**
```bash
mlir-opt input.mlir --pass-pipeline='builtin.module(func.func(gpu-eliminate-barriers))'
```
Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25
Contributor guide
Research direction
Start by running the provided input.mlir with mlir-opt and the gpu-eliminate-barriers pass pipeline to reproduce the missed optimization. Read the pass implementation around collectEffects and verify that the outer gpu.barrier is removed while the inner barrier remains; the reproduced output should show that result.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100