At -O1, loop fusion deletes the fill of a tensor that a later loop with branches reads

Open
#3,395 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
compilers

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

bug

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 of a is there.
  • After vx-fuse-and-tile-loops too, the real a has no store, and ^bb2 still 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vx-lang/Vx

All issues in vx-lang/Vx

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.