[LLVMCPU] avoid fusing invariant reduction producers (#25201)
Keep invariant tensor Linalg reductions outside reduction tile loops,
instead of fusing it with the consumer inside the loop, which would
repeat the same computation for each consumer tile. This avoids for
example recomputing whole-row maxima during tiled softmax sums.
IR excerpt of softmax without change (max inside the loop)
```
%15 = tensor.empty(%14) : tensor<?xf32>
%16 = linalg.fill {lowering_config = #iree_cpu.lowering_config<vector_common_parallel = [4]>} ins(%cst : f32) outs(%15 : tensor<?xf32>) -> tensor<?xf32>
%17 = scf.for %arg4 = %c0 to %6 step %c4 iter_args(%arg5 = %16) -> (tensor<?xf32>) {
...
%27 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%extracted_slice_6 : tensor<?x?xf32>) outs(%26 : tensor<?xf32>) attrs = {lowering_config = #iree_cpu.lowering_config<vector_common_parallel = [4, 0], vector_reduction = [0, 4]>} {
^bb0(%in: f32, %out: f32):
%29 = arith.maxnumf %in, %out : f32
linalg.yield %29 : f32
} -> tensor<?xf32>
%extracted_slice_7 = tensor.extract_slice %arg5[0] [%14] [1] : tensor<?xf32> to tensor<?xf32>
%28 = linalg.generic {...} ins(%extracted_slice_5, %27 : tensor<?x?xf32>, tensor<?xf32>) outs(%extracted_slice_7 : tensor<?xf32>) attrs = {...} {
^bb0(%in: f32, %in_8: f32, %out: f32):
%29 = arith.subf %in, %in_8 : f32
%30 = math.exp %29 : f32
%31 = arith.addf %30, %out : f32
linalg.yield %31 : f32
} -> tensor<?xf32>
%inserted_slice = tensor.insert_slice %28 into %arg5[0] [%14] [1] : tensor<?xf32> into tensor<?xf32>
scf.yield %inserted_slice : tensor<?xf32>
}
```
With the change:
```
%15 = affine.apply affine_map<()[s0, s1] -> (s0 + s1)>()[%arg2, %arg0]
%extracted_slice_2 = tensor.extract_slice %9[%15, 0] [%14, %6] [1, 1] : tensor<?x?xf32> to tensor<?x?xf32>
%16 = tensor.empty(%14) : tensor<?xf32>
%17 = linalg.fill {lowering_config = #iree_cpu.lowering_config<vector_common_parallel = [4]>} ins(%cst_0 : f32) outs(%16 : tensor<?xf32>) -> tensor<?xf32>
%18 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%extracted_slice_2 : tensor<?x?xf32>) outs(%17 : tensor<?xf32>) attrs = {lowering_config = #iree_cpu.lowering_config<vector_common_parallel = [4, 0], vector_reduction = [0, 4]>} {
^bb0(%in: f32, %out: f32):
%26 = arith.maxnumf %in, %out : f32
linalg.yield %26 : f32
} -> tensor<?xf32>
%19 = tensor.empty(%14) : tensor<?xf32>
%20 = linalg.fill {lowering_config = #iree_cpu.lowering_config<vector_common_parallel = [4]>} ins(%cst : f32) outs(%19 : tensor<?xf32>) -> tensor<?xf32>
%21 = scf.for %arg4 = %c0 to %6 step %c4 iter_args(%arg5 = %20) -> (tensor<?xf32>) {
...
%28 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%extracted_slice_6, %extracted_slice_7 : tensor<?x?xf32>, tensor<?xf32>) outs(%extracted_slice_8 : tensor<?xf32>) attrs = {lowering_config = #iree_cpu.lowering_config<distribution = [32, 0], vector_common_parallel = [4, 0], vector_reduction = [0, 4]>} {
^bb0(%in: f32, %in_9: f32, %out: f32):
%29 = arith.subf %in, %in_9 : f32
%30 = math.exp %29 : f32
%31 = arith.addf %30, %out : f32
linalg.yield %31 : f32
} -> tensor<?xf32>
%inserted_slice = tensor.insert_slice %28 into %arg5[0] [%14] [1] : tensor<?xf32> into tensor<?xf32>
scf.yield %inserted_slice : tensor<?xf32>
}
```
LICM does not move the max out of the loop because it only runs after
the max computation has been tiled, at which point LICM cannot prove
invariance anymore.
Assisted-by: OpenAI Codex
---------
Signed-off-by: Jakob Knauer <knauer@roofline.ai>
IREE (Intermediate Representation Execution Environment, pronounced as “eerie”) is an MLIR-based end-to-end compiler and runtime that lowers Machine Learning (ML) models to a unified IR that scales up to meet the needs of the datacenter and down to satisfy the constraints and special considerations of mobile and edge deployments.
See our website for project details, user guides, and instructions on building from source.
Releases notes are published on GitHub releases.
| Package | Release status |
|---|---|
| GitHub release (stable) | |
| GitHub release (nightly) | |
iree-base-compiler | |
iree-base-runtime |
For more details on the release process, see https://iree.dev/developers/general/release-management/.
| Operating system | Build status |
|---|---|
| Linux | |
| macOS |
For the full list of workflows see https://iree.dev/developers/general/github-actions/.
See our website for more information.
Community meeting recordings: IREE YouTube channel
| Date | Title | Recording | Slides |
|---|---|---|---|
| 2025-06-10 | Data-Tiling in IREE: Achieving High Performance Through Compiler Design (AsiaLLVM) | recording | slides |
| 2025-05-17 | Introduction to GPU architecture and IREE's GPU CodeGen Pipeline | recording | slides |
| 2025-02-12 | The Long Tail of AI: SPIR-V in IREE and MLIR (Vulkanised) | recording | slides |
| 2024-10-01 | Unveiling the Inner Workings of IREE: An MLIR-Based Compiler for Diverse Hardware | recording | |
| 2021-06-09 | IREE Runtime Design Tech Talk | recording | slides |
| 2020-08-20 | IREE CodeGen (MLIR Open Design Meeting) | recording | slides |
| 2020-03-18 | Interactive HAL IR Walkthrough | recording | |
| 2020-01-31 | End-to-end MLIR Workflow in IREE (MLIR Open Design Meeting) | recording | slides |
IREE is licensed under the terms of the Apache 2.0 License with LLVM Exceptions. See LICENSE for more information.