Fusion legality
Legality is semantic: no aliasing, no side effects, no reordered reduction. Multi-use producers are a profitability question, not a bar.
Three conditions, one of them arithmetic
Tile alignment. The consumer’s iteration space has to be coverable by the producer’s tile, or by a tile the producer can be given. Elementwise consumers share the output’s iteration space and fuse for free. A consumer that reduces along a tiled dimension needs the whole of that dimension in one tile, so a row-wise consumer forces the column tile to cover N rather than to be chosen for reuse.
Single use. A producer value read twice must be computed twice or stored once. Recomputing a matmul for a second consumer doubles the flops, so the fusion is legal and pointless. Single use is what makes the other two conditions sufficient rather than merely necessary.
No reduction boundary. The consumer must see a completed reduction. If the producer’s K is tiled, the consumer runs after the last K tile, which means it sits inside the tile loop and outside the K loop. That is a different transformation from op-level fusion, and it is the one that works.
One more condition that is not in the textbook list: aliasing. If the producer’s output buffer is also one of its inputs, an in-place epilogue writes values the remaining loop still reads.
The check
Run the same shape with K=1 and with K=3. A fusion that respects the boundary agrees with the unfused reference at both. One that crosses the boundary agrees at K=1 and diverges at K=3.
Things that break
Erasing the producer because it “has no other users” after fusing one consumer, when the second consumer still reads it. The result is a dangling read of a buffer that bufferization may reuse for something else, so the output is wrong in a way that depends on allocation order. Count uses before and after the pattern. The second failure is a fusion that is legal per-op and changes numerics because it split K to make the tiles fit: the accumulation order changed, and the reference check with a tight tolerance fails on a different shape each time.
Sources
- TVM: An Automated End-to-End Optimizing Compiler for Deep Learningpaper
- Halide: A Language and Compiler for Optimizing Parallelism, Locality, and Recomputation in Image Processing Pipelinespaper · PLDI 2013
- Linalg dialect rationaledocs · LLVM 23
A version records the documentation the claim was checked against, not a house pin. Confirm flags with --help on the release you installed.
Check yourself
A consumer reduces along the dimension the producer tiles. Can it fuse at that tile size?
A producer value has two consumers. What does fusing both cost?
Which arithmetic check detects fusion across a reduction boundary?