[GlobalOptimization] change pass ordering so img2col convolutions in quantized conv models will result in integer math (#25099)

Addresses https://github.com/iree-org/iree/issues/25034

Move im2col after the late QDQ conversion and remove the redundant early
QDQ pass, so biased QDQ convolutions convert to integer matmuls after
bias detachment and transpose normalization. Move input demotion
afterward and extend it to generic contractions, preserving f32
accumulation.

Four changes make the single conversion hold up:

- Unit-dim folding shrinks a 1x1 dequantize to rank 0 and leaves an
`expand_shape` between it and the matmul. Reshape fusion by expansion
now handles an empty reassociation for ops without loops, for every op
it supports and in both directions. Only operands that must span every
loop (inits, and the value operand of quantize/dequantize) gain the new
unit dims; scalar parameters stay broadcasts.
- A quantized convolution with a weight zero point needs the sum of
every input window. With QDQ conversion before im2col, that sum
re-gathered every window in its own dispatch and cost about as much as
the im2col itself. The window sum now reads like a ones-filter
convolution, and `ConvertConvToIm2Col` rewrites a convolution without
output-channel reuse when its gather is identical to that of another
rewritten convolution. CSE merges the gathers and the sum becomes a row
reduction over the shared im2col result. Unshared pooling and depthwise
convolutions, dynamic gather shapes and plain matmul sums are
unaffected.
- With im2col after generalization and unit-dim folding, a strided
convolution with a unit window is left with an input access such as `(c,
oh * 2, ow * 2)`, which no longer passes the convolution interface
check. im2col skipped it, and the strided access kept the op from being
data-tiled (the regression reported in review for strided 1x1 shortcut
convolutions). `ConvertConvToIm2Col` now matches on the indexing maps:
an input, a filter and an output, with filter and output maps that are
projected permutations and an input map that is not one but is a sum of
loops scaled by positive constants. Plain contractions, named matmuls
and elementwise ops are left alone; the output-channel reuse and
shared-gather rules are unchanged.
- Unit-dim folding skips named contractions, so the generic that the QDQ
conversion creates from an M = 1 matmul kept its unit loop when
contractions are not generalized. Unit-dim folding now runs on the
function again after the QDQ conversion.

The demotion pass classifies by the contraction and convolution
interfaces and does one rewrite. The multiply-accumulate body check
stays because the convolution interface only inspects indexing maps;
without it a mul+max pooling-like generic would be demoted.

Pipeline regression tests require integer im2col and i8×i8→i32
contractions for biased NHWC/NCHW convolutions, that the window sum
reads the shared gather, and cover generic demotion.

Performance on Apple M4 with llvm-cpu, data tiling and all ukernels,
`--task_topology_performance_level=performance`, median of medians.
NCHW, C_in 16, 28x28 input, C_out 32, activation scale 0.125 and zero
point −1, per-channel weight scale 0.0625 and zero point 2. "Float" is
this branch with `--iree-global-opt-enable-qdq-to-integer-math=false`.

| Case | 1 worker integer | 1 worker float | 4 workers integer | 4
workers float |
|---|---:|---:|---:|---:|
| 3x3 symmetric | 0.103 ms | 0.117 ms | 0.057 ms | 0.062 ms |
| 3x3 activation zero point only | 0.095 ms | 0.118 ms | 0.052 ms |
0.063 ms |
| 3x3 weight zero point only | 0.063 ms | 0.119 ms | 0.042 ms | 0.070 ms
|
| 3x3 both zero points | 0.065 ms | 0.116 ms | 0.041 ms | 0.063 ms |
| 5x5 both zero points | 0.125 ms | 0.241 ms | 0.060 ms | 0.098 ms |
| 7x7 both zero points | 0.197 ms | 0.369 ms | 0.081 ms | 0.194 ms |
| matmul 676x144x32, both zero points | 0.056 ms | 0.149 ms | 0.045 ms |
0.074 ms |

The gather merges for NCHW and NHWC with stride 2, dilation 2 and batch
2. The integer output is bit-identical to the float path on the tested
inputs; the scales are powers of two, so this does not exercise
rounding.

### Comparison against main

`main` at b4f03e34f7 against this branch, same machine and runtime flags
as above, default task topology restricted to the 4 performance cores,
median of 5 repetitions. All cases use
`--iree-global-opt-use-im2col-for-convs`; "DT" adds
`--iree-opt-data-tiling`, "DT + UK" also adds
`--iree-llvmcpu-enable-ukernels=all`. Shortcut cases are 1x1 QDQ
convolutions with bias and relu, written as channels in → out, input
height, stride.

| QDQ case | No DT: main → PR | DT: main → PR | DT + UK: main → PR |
|---|---:|---:|---:|
| shortcut 256 → 512, 56x56, stride 1 | 14.9 → 14.9 ms | 2.35 → 0.79 ms
| 2.38 → 0.79 ms |
| shortcut 512 → 1024, 28x28, stride 1 | 15.9 → 15.9 ms | 2.15 → 0.66 ms
| 2.15 → 0.66 ms |
| shortcut 1024 → 2048, 14x14, stride 1 | 14.2 → 14.8 ms | 2.11 → 0.55
ms | 2.13 → 0.55 ms |
| shortcut 256 → 512, 56x56, stride 2 | 3.99 → 4.26 ms | 0.67 → 0.22 ms
| 0.68 → 0.22 ms |
| shortcut 512 → 1024, 28x28, stride 2 | 5.70 → 6.01 ms | 0.71 → 0.20 ms
| 0.71 → 0.20 ms |
| shortcut 1024 → 2048, 14x14, stride 2 | 4.01 → 4.35 ms | 0.70 → 0.16
ms | 0.71 → 0.16 ms |
| NHWC 1x1, 512 → 1024, 28x28, stride 2 | 0.71 → 1.23 ms | 0.70 → 0.19
ms | 0.70 → 0.19 ms |
| NCHW 3x3, 64 → 64, 56x56, stride 1, pad 1 | 5.20 → 5.56 ms | 0.92 →
0.52 ms | 0.92 → 0.54 ms |

With data tiling these cases take 0.23x to 0.59x of the time on `main`,
because they now run as `i8 x i8 -> i32` matmuls. Without data tiling
the integer matmul is not faster than the float one: most cases are
within 8%, and the NHWC 1x1 stride 2 case is 1.7x slower.

The run covered 237 case and configuration pairs (quantized and float
convolutions, shortcuts, linear layers, each with and without im2col,
data tiling and ukernels). 192 compile to byte-identical modules on both
sides. The other 45 are the 8 QDQ cases above and 7 float convolutions
with bias, in the three im2col configurations; the float ones are within
noise of `main`. No case loses data tiling. The full ResNet-50 from the
review has not been re-measured.

Compiled with:

```sh
build/tools/iree-compile input.mlir \
  --iree-hal-target-backends=llvm-cpu \
  --iree-llvmcpu-target-cpu=host \
  --iree-global-opt-use-im2col-for-convs \
  --iree-opt-data-tiling \
  --iree-llvmcpu-enable-ukernels=all \
  -o model.vmfb
```

Assisted by Codex

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Florian Walbroel <walbroel@roofline.ai>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
17 files changed
tree: a141427c600318164bfb9547e9e4275965ac20df
  1. .github/
  2. build_tools/
  3. compiler/
  4. docs/
  5. experimental/
  6. integrations/
  7. lib/
  8. llvm-external-projects/
  9. runtime/
  10. samples/
  11. tests/
  12. third_party/
  13. tools/
  14. .bazel_to_cmake.cfg.py
  15. .bazelignore
  16. .bazelrc
  17. .bazelversion
  18. .clang-format
  19. .git-blame-ignore-revs
  20. .gitattributes
  21. .gitignore
  22. .gitmodules
  23. .pre-commit-config.yaml
  24. .yamllint.yml
  25. AUTHORS
  26. BUILD.bazel
  27. CITATION.cff
  28. CMakeLists.txt
  29. configure_bazel.py
  30. CONTRIBUTING.md
  31. GOVERNANCE.md
  32. LICENSE
  33. MAINTAINERS.md
  34. MODULE.bazel
  35. README.md
  36. RELEASING.md
README.md

IREE: Intermediate Representation Execution Environment

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.

IREE Discord Status pre-commit OpenSSF Best Practices

Project news

Project status

Release status

Releases notes are published on GitHub releases.

PackageRelease status
GitHub release (stable)GitHub Release
GitHub release (nightly)GitHub Release
iree-base-compilerPyPI version
iree-base-runtimePyPI version

For more details on the release process, see https://iree.dev/developers/general/release-management/.

Build status

CI PkgCI

Nightly build status

Operating systemBuild status
LinuxCI - Linux arm64 clang
macOSCI - macOS arm64 clang

For the full list of workflows see https://iree.dev/developers/general/github-actions/.

Communication channels

Related project channels

  • MLIR topic within LLVM Discourse: IREE is enabled by and heavily relies on MLIR. IREE sometimes is referred to in certain MLIR discussions. Useful if you are also interested in MLIR evolution.

Architecture overview

IREE Architecture IREE Architecture

See our website for more information.

Presentations and talks

Community meeting recordings: IREE YouTube channel

DateTitleRecordingSlides
2025-06-10Data-Tiling in IREE: Achieving High Performance Through Compiler Design (AsiaLLVM)recordingslides
2025-05-17Introduction to GPU architecture and IREE's GPU CodeGen Pipelinerecordingslides
2025-02-12The Long Tail of AI: SPIR-V in IREE and MLIR (Vulkanised)recordingslides
2024-10-01Unveiling the Inner Workings of IREE: An MLIR-Based Compiler for Diverse Hardwarerecording
2021-06-09IREE Runtime Design Tech Talkrecordingslides
2020-08-20IREE CodeGen (MLIR Open Design Meeting)recordingslides
2020-03-18Interactive HAL IR Walkthroughrecording
2020-01-31End-to-end MLIR Workflow in IREE (MLIR Open Design Meeting)recordingslides

License

IREE is licensed under the terms of the Apache 2.0 License with LLVM Exceptions. See LICENSE for more information.