Skip to content

[CPU] Use option to tile with scf.forall in TileRootAndFuseProducerConsumer pass - #2

Draft
AaronStGeorge wants to merge 34 commits into
stgeorge-aaron/cpu-tile-using-forall_3/4from
stgeorge-aaron/cpu-tile-using-forall_4/4
Draft

AaronStGeorge wants to merge 34 commits into
stgeorge-aaron/cpu-tile-using-forall_3/4from
stgeorge-aaron/cpu-tile-using-forall_4/4

Conversation

@AaronStGeorge

@AaronStGeorge AaronStGeorge commented Jun 3, 2025 •

Copy link
Copy Markdown
Owner

This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to use scf.forall rather than scf.for; allowing us to take advantage of some canonicalization patterns available on scf.forall. The patterns available prevent redundant stack allocations seen in iree-org#20792. Tiling parallel dimensions with scf.forall rather than scf.for could also be said to be semantically cleaner.

As part of that change, this PR uses the new option to TileRootAndFuseProducerConsumer in the mmt4d pipeline which will tile with scf.forall. As later patterns in the pipeline expect scf.for, this PR also adds pass to convert back to scf.for from scf.forall to the mmt4d pipeline.

PR 4/4 addressing iree-org#20792

@AaronStGeorge AaronStGeorge changed the title [CPU] Use option to tile with scf.forall in TileRootAndFuseProducer… [CPU] Use option to tile with scf.forall in TileRootAndFuseProducerConsumer pass Jun 4, 2025
@AaronStGeorge
AaronStGeorge force-pushed the stgeorge-aaron/cpu-tile-using-forall_3/4 branch 2 times, most recently from 1897752 to 79d21cc Compare June 4, 2025 19:06
@AaronStGeorge
AaronStGeorge force-pushed the stgeorge-aaron/cpu-tile-using-forall_4/4 branch from 5c87c21 to 2dd7c3c Compare June 4, 2025 19:24
@AaronStGeorge
AaronStGeorge force-pushed the stgeorge-aaron/cpu-tile-using-forall_4/4 branch from 2dd7c3c to 17d7f96 Compare June 11, 2025 19:28
@AaronStGeorge
AaronStGeorge force-pushed the stgeorge-aaron/cpu-tile-using-forall_3/4 branch 2 times, most recently from 292e806 to 2ddf7a9 Compare June 18, 2025 22:09
@AaronStGeorge
AaronStGeorge force-pushed the stgeorge-aaron/cpu-tile-using-forall_4/4 branch from 17d7f96 to ebc9d25 Compare June 18, 2025 22:22
Oleksandr "Alex" Zinenko and others added 20 commits June 19, 2025 16:07
This pass currently performs two rewrites:

1. *incorrectly* rewrite `atomic_rmw` for floating-point min/max, see
iree-org#21101;
  2. rewrite `reshape` into `reinterpret_cast`.

The rewrite 1 was added years ago, prior to LLVM IR supporting
floating-point atomics sufficiently and is no longer necessary. I will
remove it upstream anyway.

The rewrite 2 looks like a canonicalizaiton and it is unclear why it was
put into a separate pass, it will likely not remain there. It may have
some minor effect on performance if this rewrite is important to
simplify address computation, at which point it belongs to the set of
canonicalization patterns.

In general, I highly suspect that the usage of this pass was either
cargo-culted from existing sub-pipelines or confused with
`--expand-strided-metadata`, which is both necessary for lowering and
beneficial for address computation performance.

Signed-off-by: Alex Zinenko <[email protected]>
…-org#21121)

Extends the pattern to handle rank reducing `extract_slice`s as well
instances where the `expand_shape` contains but doesn't expand any
dynamic dimensions. This is useful to improve fusion (by cloning the
slice) and prevent the `extract_slice` from materializing to a slow
memcpy.

Closes iree-org#21015

---------

Signed-off-by: Ian Wood <[email protected]>
…21106)

This is a follow-up PR to address feedback on the previously landed PR
for the argmax op iree-org#21021 according to comments in
iree-org#21021 (comment):
- Adds support for a comparator region in the argmax op
- Introduces an optional index_base operand to specify the starting
index offset.

---------

Signed-off-by: Bangtian Liu <[email protected]>
Locally revert
llvm/llvm-project@e4de74b

---------

Signed-off-by: Benoit Jacob <[email protected]>
Adds "reduction" annotation for `linalg.generic` ops with reduction
loops (if the ops isn't a matmul/conv/etc) and increases the cost of
reduction ops to break ties with elementwise consumers (see second
test).

---------

Signed-off-by: Ian Wood <[email protected]>
Integrate LLVM at llvm/llvm-project@836201f.

This catches up from ~1 week ago to ~present, but in exchange for that,
we carry over the existing revert and add 2 more reverts of a pair of
related commits each:

Carried-over reverts:
*
llvm/llvm-project@e4de74b.
I don't understand it, but Rob discovered that it unblocked the previous
integrate.

New reverts:
*
llvm/llvm-project@ee070d0
and
llvm/llvm-project@a1c2a71:
after making the necessary C++ fixes to unbreak the build
(https://gist.github.com/bjacob/a132e70ea0f7ceba4554f352a45bcd1c), these
break bufferization and eliminate-empty-tensors tests that I don't
understand.
* Corresponding Bazel changes also reverted:
llvm/llvm-project@2a41350,
llvm/llvm-project@8fc20bf.
*
llvm/llvm-project@eb694b2
and
llvm/llvm-project@e33f13b
: these break `arith.trunci` with destination type `i1`, which occurs at
least in the `jit_globals.mlir` test.

Signed-off-by: Benoit Jacob <[email protected]>
…- Creating intrinsic sort routine (iree-org#21128)

This is a follow-up of iree-org#21103.

This intrinsic sorting routine will sort the MMA intrinsics by following
precedence rules:
1) K-alignment. We prefer intrinsics that can evenly divide the K
dimension of the problem.
2) M/N-alignment. We prefer intrinsics that can evenly divide the M and
N dimensions of the problem.
  3) Intrinsic with larger gemm size.
  4) Intrinsic with larger K size.

Scope of the impact is igemm and matmul problems. Other pipelines can
choose to adopt this if the sorting turns out to be useful.

### Motivation:

convolution configuration with 
> BOO_CACHE_ON=0 python boo_driver.py convbfp16 -n 16 -c 40 -H 192 -W
128 -k 40 -y 3 -x 3 -p 1 -q 1 -u 2 -v 2 -l 1 -j 1 -m conv -g 1 -F 1 -t 1
--in_layout NHWC --out_layout NHWC --fil_layout NHWC --iter 50

Since the gemmK size is 360, the best intrinsic is 32x32x8xbf16, not
16x16x16xbf16, as the former will end in aligned K. Before this PR:
`362.06 us`. After this PR: `44.74 us`. Note that due to limitation of
the tuner, this combination won't be covered by typical tuning run with
2k trials. So it is essential we bridge the gap through heuristic first
before tuner is more capable of searching through different heuristics.

---------

Signed-off-by: jerryyin <[email protected]>
Keep the same reverts as in iree-org#21148

Signed-off-by: Benoit Jacob <[email protected]>
This contains the host-side `iree_hal_amdgpu_command_buffer_t`
responsible for recording and managing the replicas of the device-side
`iree_hal_amdgpu_device_command_buffer_t` instances and the device-side
implementation for issuing and executing command buffer programs.

The comments for `iree_hal_amdgpu_device_command_buffer_t` and its
related commands document how the command buffer is represented in
device memory and what each command does. Some of the commands are not
yet wired up to host recording code as the it is skating ahead of the
HAL command buffer API. Even though unconditional branches cannot be
recorded directly they are used for segmenting large command buffers to
fit within resource limits. Conditional branches are nominally supported
but need an API (similar to indirect dispatches) to be plumbed from the
compiler to the command buffer vtable.

IREE's HAL is intended to optimize for precompiled command buffers that
can amortize the recording cost - this implementation leans that
direction and should be optimal for meaningful (non-benchmark) programs
originating from anything targeting CUDA graphs. There are some
provisions for small command buffers but the intent is that
single-command buffers end up routed towards device queue operations
much earlier.

Since the device-side queue scheduler does not yet exist the two methods
required (`iree_hal_amdgpu_device_queue_scheduler_*`) are stubbed out to
indicate where they'll be used in the future. The API may change in
subsequent changes that implement the behavior.
…e-org#21164)

Also improves the implementation of mask layout inference for
transfer_gather operations.

---------

Co-authored-by: Quinn Dawkins <[email protected]>
)

Pytorch ignores byte_offset at least in some circumstances, meaning we
end up with the wrong data getting output.

This solves the issue by baking the byte_offset directly into the
returned pointer.

See:
https://github.com/dmlc/dlpack/blob/7f393bbb86a0ddd71fde3e700fc2affa5cdce72d/include/dlpack/dlpack.h#L225

Signed-off-by: Andrew Woloszyn <[email protected]>
Allows vector types for the map_scatter input operand, and adds a
trivial vectorization pass, which converts the input of map_scatter ops
to vector operands with a vector.transfer_read.

This vectorization lowering is not complete, because the map_scatter op
in this form will not be able to lower into other operations. The final
lowering will happen as a decomposition after the map_scatter op has
been both vectorized and bufferized, and the map_scatter op will be
decomposed into a vector.scatter op. The reason this decomposition will
need to happen after bufferization, is that the vector.scatter op
requires a memref base. The decomposition pattern of the vectorized
map_scatter will come as a follow-up PR.

Eventually, the vectorized map_scatter op could instead be decomposed
into an `iree_vector_ext.transfer_scatter` op, but since the op does not
exist yet, the current plan is to use `vector.scatter` first.

---------

Signed-off-by: Max Dawkins <[email protected]>
…g#21160)

Towards collapsing the padding encoding materialization into the generic
encoding materialization: iree-org#20160.

I don't think the intermediate extract and insert slices are needed and
removing them will streamline the implementation with the generic one,
so that we can later combine them into a single implementation in
follow-up work.

Signed-off-by: Jorn Tuyls <[email protected]>
This folding is not possible in general because C * (X udiv C) may
clamp, but if a `util.assume.int` indicates that X is divisible by C, we
can fold away the divide and multiply.
Keeping the reverts in: iree-org#21148

One update change to the existing revert because the
`BufferizationTypeInterfaces.cpp` has been reverted:
iree-org/llvm-project@d1f7d62

---------

Signed-off-by: Alan Li <[email protected]>
Signed-off-by: MaheshRavishankar <[email protected]>
Co-authored-by: MaheshRavishankar <[email protected]>
…rg#21174)

This [discord
thread](https://discord.com/channels/689900678990135345/1385756730796933360)
is only the latest iteration of a recurring hard-to-diagnose bug that
occurs when LLVM is improperly configured. The CMake changes here help
diagnose that early. This PR also fixes an instance of that incorrect
usage in our own JSON configuration file (thanks Scott for finding it).

---------

Signed-off-by: Benoit Jacob <[email protected]>
…iree-org#21144)

As discussed in iree-org#20742, the
resolvers should end with `_resolver` suffix, and the encoding is
redundant for encoding type attributes. The revision updates the names
for testing purpose encodings.

Resolver changes:
- iree_encoding.identity_encoding -> iree_encoding.identity_resolver
- iree_encoding.unsupported_encoding ->
iree_encoding.unsupported_resolver
- iree_encoding.unspecialized_encoding ->
iree_encoding.specialization_resolver

Encoding type attribute changes:
- iree_encoding.specialized_encoding -> iree_encoding.specialized
- iree_encoding.unknown_encoding -> iree_encoding.unknown
- iree_encoding.testing_encoding -> iree_encoding.testing 

The revision also fixes a misuse for `flow.tensor.encode` roundtrip
tests in `flow_ops.mlir`, which should use encoding type attributes but
not resolvers.

It is a step towards iree-org#20742

---------

Signed-off-by: hanhanW <[email protected]>
…ree-org#20764)

transfer_gather is distributed just like transfer_read on non gathered
dimensions and like vector.gather on gathered dimensions.
Max191 and others added 13 commits June 24, 2025 09:30
…1136)

Adds support for `iree_codegen.store_to_buffer` and
`iree_codegen.load_from_buffer` in
`ConvertToDestinationPassingStylePass`. This is necessary for enabling
early bufferization and layout transformation combination in data tiling
e2e tests, because scalar dispatches will go down the
LLVMGPUBaseLowering pipeline, which uses this pass.

---------

Signed-off-by: Max Dawkins <[email protected]>
… with side-effecting ops (iree-org#21166)

Fission should be skipped when there are side-effecting operations in
the loop, besides the targeted `vector.transfer_{read,write}` ops. The
fixes a bug where other side-effecting ops get erased during loop
splitting.
…n. (iree-org#21146)

- iree_encoding.pad_encoding_layout -> iree_encoding.padding 
- iree_gpu.gpu_pad_layout -> iree_gpu.gpu_padding_resolver
- iree_cpu.cpu_encoding_layout -> iree_cpu.cpu_encoding_resolver
- iree_cpu.vmvx_encoding_layout -> iree_cpu.vmvx_encoding_resolver
- iree_gpu.gpu_encoding_layout -> iree_gpu.gpu_encoding_resolver

It is a step towards iree-org#20742

---------

Signed-off-by: hanhanW <[email protected]>
…ree-org#21001)

Adds support for arch/sku specific PDL patterns to be loaded for finding
and annotating ops with ranges to specialize for. This is a stopgap for
a longer term solution where configuration logic uses range information
directly and returns the set of ranges to specialize for itself.

Today, IREE::Codegen::BlockDynamicDims uses the divisibility info to
split out static dims that kernel config uses, meaning this has to
happen before this pass.

PDL modules aren't ideal for this use case because pattern rewrite sets
require ownership over the module, but is lighter weight than transform
dialect for this use case (basic matching + annotation). The reason for
IR based patterns is two-fold

1. Arch/SKU specific ranges are often fixed to a few static best cases,
meaning hand writing the ranges to annotate is fewer characters than
emitting it with C++.
2. Adding support for new architectures is much faster to copy-paste an
existing file and update the values than extending C++ switch
statements.
The revision adds a high-level summary for the dialect, and fixes the
interface descriptions.

Fixes iree-org#20742

Signed-off-by: hanhanW <[email protected]>
The mix of affine + arith operations generated at Flow and earlier
hinders folding/canonicalization opportunities, so it's best for us to
pick one to use. This picks arith over affine since it is easier to go
from affine -> arith rather than the other way, and it composes better
with dialects like Util.
On top of iree-org#21106.

This PR Adds `generateScalarImplementation` for ArgCompareOp, defining
the scalar behavior within the innermost loop during loop lowering.

The lowering performs the following: 
- Loads the current best value and corresponding index (accumulator)
from the output tensor.
- Loads the candidate value at the current loop indices.
- Invokes the provided comparator region (T a, T b) -> i1 to compare
values.
- Selects the new best value and index based on the comparison result.
- Supports an optional index_base operand, which is added to the
computed index before storing.

Next, will add tiling support.

---------

Signed-off-by: Bangtian Liu <[email protected]>
…g with `scf.forall` (iree-org#21009)

This PR is part of a larger change moving
`LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimensions
to use `scf.forall` rather than `scf.for`; allowing us to take advantage
of some canonicalization patterns available on scf.forall. The patterns
available prevent redundant stack allocations seen in
iree-org#20792. Tiling parallel
dimensions with `scf.forall` rather than `scf.for` could also be said to
be semantically cleaner.

As part of that change, this PR intrudes an option to
`TileRootAndFuseProducerConsumer` for tiling with `scf.forall`.

PR 3/4 addressing iree-org#20792

---------

Co-authored-by: Prashant Kumar <[email protected]>
Co-authored-by: Han-Chung Wang <[email protected]>
…rg#21141)

This commit adds a descriptor attribute - #iree_gpu.scaled_mma_layout -
for MMAs that takes "scale" operands and tests for its lowering from
DistributeInnerTiledToLanes and afterwards.

This descriptor takes four input operands - the lleft-hand side of the
matrix, the LHS scales, the right-hand side, and the RHS scales, in that
order. The operation has one accumulator.

Scaled MMAs are, like MMA intrinsics, represented by an enum, which
litss out the available intrinsics (currenvtly, we only have AMD's
MI-350). However, unlike with MMA intrinsics, the input and output types
are given in the scaled_mma_layout attribute itself. This is because
these scaled MMAs support arbitrary pairs of left- and right-hand side
types from {f4E2M1FN, f6E3M2FN, f6E3M2FN, f8E4M3FN, f8E5M2}, so listing
out all the combinations would take up a lot of space and produce a
large number of nearly-identical cases in the descriotor logic.

This inner tiled operation treats the k values that have distinct scales
and the "block" (the B (currently always 32) values that share a common
scale) as separate reduction dimensions. This means that the scale
operands, in effect, don't have a block dimension. This also means that
models where the block size is larger than 32 can be supported by tiling
across the scale dimension.

The following work is not yet implemented:
- ContretizeMmaShapes has not yet been generalized to arbitrary inner
tiled ops. This will be needed to correctly lower the 32x32x64
intrinsic, which has an outer dimension on the accumulator.
- Lowering configuration logic and PackToIntrinsics have not been
addressed

Note that this PR does contain a few code changes. One is declaring the
vector unrolling logic to be safe for non-MMA inner_tiled ops. The other
is a bug fix to basisFromSizesStrides(), which used an incorrect
tiebreaking strategy for multiple dimensions that had the same stride
(now it's ties broken by the length of the dimension and _then_ its
position in the list).

---------

Co-authored-by: Quinn Dawkins <[email protected]>
iree-org#21142)

This pass generalizes ContretizeMmaShapes to
ExpandUndistributedInnerTiles, allowing it to work on the 32x32x64
scaled MFMA intrinsic and thus clearing the way for lowering this
operation from a packed generic.

---------

Co-authored-by: Jakub Kuderski <[email protected]>
Co-authored-by: Quinn Dawkins <[email protected]>
…-org#21170)

This PR introduces a new Python binding `get_attention_op_detail` for
the function
`mlir::iree_compiler::IREE::LinalgExt::AttentionOpDetail::get(
QMap, KMap, VMap, OMap)`, which infers the logical structure of an
attention operation (e.g., batch/m/k1/k2/n/domain_rank) from its affine
indexing maps.
 
 This PR will enable tuner to reason about attention ops more easily.

---------

Signed-off-by: Bangtian Liu <[email protected]>
…Consumer pass

This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to use scf.forall rather than scf.for; allowing us to take advantage of some canonicalization patterns available on scf.forall. The patterns available prevent redundant stack allocations seen in iree-org#20792. Tiling parallel dimensions with scf.forall rather than scf.for could also be said to be semantically cleaner.

As part of that change, this PR uses the new option to TileRootAndFuseProducerConsumer in the mmt4d pipeline which will tile with scf.forall. As later patterns in the pipeline expect `scf.for`, this PR also adds pass to convert back to `scf.for` from `scf.forall` to the mmt4d pipeline.

PR 4/4 addressing iree-org#20792
@AaronStGeorge
AaronStGeorge force-pushed the stgeorge-aaron/cpu-tile-using-forall_4/4 branch from ebc9d25 to 0c367aa Compare June 26, 2025 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.