[Codegen][GlobalOpt] Detach non-splat constant inits (#24605)
A constant used directly as the `outs` (init/destination) of a
`linalg.matmul` or convolution e.g. a fully-connected/conv bias imported
from TFLite via TOSA survives into dispatch formation. It is then cloned
into the workgroup on the written (output) side and bufferizes as a
read-only global, which fails workgroup-distribution verification with
the following error:
```
'linalg.generic' op write affecting operations on global resources are restricted to workgroup distributed contexts
'func.func' op failed on workgroup distribution verification
```
This kind of IR was seen in TFLite -> TOSA conversion flows and and the
IR below is an example of that:
```mlir
util.func public @main(%a: tensor<1x1xf32>, %w: tensor<1x9xf32>) -> tensor<1x9xf32> {
%bias = arith.constant dense<[[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0]]> : tensor<1x9xf32>
%0 = linalg.matmul ins(%a, %w : tensor<1x1xf32>, tensor<1x9xf32>)
outs(%bias : tensor<1x9xf32>) -> tensor<1x9xf32>
util.return %0 : tensor<1x9xf32>
}
```
The chosen solution is to use is the `DetachElementwiseFromNamedOps`
pattern which exists to keep non-writable inits out of dispatches. It
only covers two of our three relevant cases. The pass will
1. Take a non-const init and detach it into a zero-fill plus a trailing
add
2. Take a splat constant init and convert it to a fill.
Our use-case of a non-splat constant init would not be handled. Here we
extend the elementwise detach pattern so a non-splat constant init is
detached the same way a computed init is. A fresh zero-fill is created
and the constant is added back as a read-only input. This form of IR can
be then handled by codegen.
After the fix the reproducer compiles and the bias survives read-only:
```
%bias = arith.constant ... : tensor<1x9xf32>
%z = linalg.fill ins(%c0 : f32) outs(%empty : tensor<1x9xf32>) -> tensor<1x9xf32>
%mm = linalg.matmul ins(%a, %w) outs(%z) -> tensor<1x9xf32>
%0 = linalg.generic ins(%mm, %bias) outs(%z) { ... } // %mm + %bias
```
---
_Code Examples and Tests Assisted-by Claude_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 | |
| 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.