At -O1, loop fusion deletes the fill of a tensor that a later loop with branches reads
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 22/100
Research direction
Start by reproducing the wrong output at -O1 and running vx-opt on the -O0 MLIR, checking the fill of a after vx-linalg-to-affine-loops,vx-raise-loop-accesses and again after vx-fuse-and-tile-loops. The relevant code is FuseAndTileLoopsPass::reasonToSkip in src/dialect/VxLowering.cpp, which needs a check for buffer users in other blocks. Done means the program prints 64 2 at -O1 and -O2 and a regression test covers the mixed range and usize loops.
Written by the indexing model from the issue text.
Description
At -O1 and above, this program prints NaN 2. At -O0 it prints 64 2, which is right.
fn main() -> i32 {
let a = Tensor<f32, [64]>::fill(1.0);
let mut b = Tensor<f32, [64]>::fill(0.0);
for i in 0..64 {
b[i] = a[i] + 1.0;
}
let mut s = 0.0;
for i in 0usize..64usize {
s = s + a[i];
}
println!("{} {}", s, b[3]);
return 0;
}
Why
The 0..64 loop becomes an affine.for. A usize range is not an affine loop, so the second loop
stays as branches between blocks.
vx-fuse-and-tile-loops runs the upstream affine-loop-fusion. Fusion moves the fill of a into
the 0..64 loop, and gives that loop a private copy of a with one element. Then it deletes the
fill of the real a. The usize loop still reads the real a, and nothing writes it now.
vx-opt on the -O0 MLIR of the program shows this:
- After
vx-linalg-to-affine-loops,vx-raise-loop-accesses, the fill ofais there. - After
vx-fuse-and-tile-loopstoo, the realahas no store, and^bb2still loads from it.
Fusion probably looks for other users of a buffer only in the block that holds the loops. The read
in the usize loop is in another block. FuseAndTileLoopsPass::reasonToSkip in
src/dialect/VxLowering.cpp checks that no two buffers alias, but it does not check for a user in
another block.
Inlining can make the same function. Two functions that each gave the right answer, one with
0..64 loops and one with usize loops, gave NaN at -O2 when main called both on the same
tensors.
A possible fix
Skip fusion in a function where a buffer that an affine loop uses has a user in another block. A
simpler fix skips each function whose body has more than one block, but it also skips many
functions that fusion handles correctly now.
Related: #2042
- Dominant language
- Rust
- Stars
- 35
- Forks
- 5
- Avg merge
- 12h 26m
- Merged PRs (30d)
- 799
Getting set up
We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from vx-lang/Vx
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
bug testing
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
`pub struct` gives "Expected 'fn' after 'pub'": the error should say that a struct needs no `pub`Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
bug devex
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
longbridge/gpui-kit#4031 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
neuralmagic/crucible#465 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
compiler P3
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
HerringtonDarkholme/hd-lang#2162 ·
Maintainers usually reply within 1 day
-
embedded SQL: pin correlated-guard behaviour when a closed variable is reassigned inside a branchOpenpriority: low
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
jonathanong/no-mistakes#3627 ·
Maintainers usually reply within 1 day