Repository navigation
Description
(AI-generated).
Summary
On the LLVMCPU backend, a data-tiling encoding relayout dispatch (the
set_encoding pack/expand_shape/transpose chain) lowers to a scalar loop
instead of vectorizing. This is a measurable perf cost: for an i8 data-tiled
matmul it accounts for the entire residual gap to the hand-written mmt4d
ukernel (~7% of total runtime, K-independent — i.e. pure relayout overhead).
The shared GenericVectorization pass does vectorize the equivalent dispatch
on GPU. The divergence is not in the pass — it is structural, and is described
precisely below.
Background
A non-row-major encoding swizzle materializes as pack → expand_shape →
transpose. The intervening tensor.expand_shape does not implement
TilingInterface, so it blocks producer fusion and strands an untiled,
whole-tensor pack intermediate (this is #24483). The fix
(mirroring the GPU config pipeline) is to fold the relayout chain into a single
iree_linalg_ext.map_store / map_load before tiling — a scatter/gather with
no intermediate buffer.
With that done, the encode dispatch is a single map_store, but it lowers to
scalar LinalgExtToLoops rather than vectorizing.
Root cause
MapStoreOpVectorizationModel::isVectorizable
(compiler/src/iree/compiler/Codegen/Interfaces/VectorizableOpInterface.cpp)
has a hard precondition:
ShapedType inputType = mapStoreOp.getInputType();
if (!inputType.hasStaticShape())
return false;Its vectorize() only emits an unmasked, full-tile vector.transfer_read, so
a static input shape is genuinely required.
A map_store iterates its input. So it only vectorizes when that input is
already statically tiled. The encoding relayout's input is the un-padded
source tensor — an arbitrary tensor<?x?xT> with no static tile structure —
so any tile carved out of it (tensor.extract_slice with
affine.min(remaining, tileSize) extents) is again tensor<?x?xT>, dynamically
shaped. isVectorizable rejects it, and it scalarizes.
Why this is CPU-specific
The same map_store vectorizes on GPU. GPU distributes the relayout dispatch
across a static thread/workgroup tile shape (only the grid size is dynamic),
so each map_store's input tile is statically shaped → hasStaticShape()
holds. CPU TileAndDistributeToWorkgroupsUsingForallOp represents the partial
boundary tile as a dynamically-typed extract_slice, so the per-workgroup
map_store input is tensor<?x?xT>.
GenericVectorization itself is shared and not CPU/GPU-specific; CPU and GPU
tiling simply feed the same model differently-shaped inputs.
The structural fix: drive the relayout from the padded side
In data-tiling, the packed/encoded side of the relayout is padded and
tiled to the encoding's tile shape by construction — its inner tile dims
(e.g. 16x16, here a 4x4x4x4 swizzle) are static, and there are never
incomplete tiles. The dynamic, un-padded side is the row-major source.
map_store (scatter) is rooted on / iterates its input (the un-padded
source). The structurally correct op for an encoding relayout is map_load
(gather), which is rooted on / iterates its result (the padded packed
tensor). Tiling a map_load keeps the static inner tile dims static and
confines all dynamism to the outer tile-count loop — so it tiles into complete
static tiles, exactly like the GPU case.
This was validated end-to-end: switching the CPU config pipeline from
CombineResultLayoutTransformation (→ map_store) to
CombineSourceLayoutTransformation (→ map_load) makes the encode dispatch a
map_load chain that compiles cleanly and is numerically correct for
f32/f16/i8/i16 on dynamic shapes.
What's still missing (the actual work)
map_load is not faster yet, because the vectorization/lowering
infrastructure for it does not exist:
- No
MapLoadOpVectorizationModel.VectorizableOpInterface.cpponly has
MapStoreOpVectorizationModel. Amap_loadmodel should check the
post-tiling tile shape — which, unlikemap_store, is static for an
encoding relayout — so the static-shape precondition would pass. - No lowering for a vectorized
map_load.DecomposeMapStorePassonly
handlesmap_store; there is noDecomposeMapLoadand no other path that
turns a vectorizedmap_loadinto vector ops. setRootConfig/ pipeline routing forMapLoadOp— routemap_load
dispatches to a vectorizing pipeline and tile them so the inner tile dims
stay static.foldReshapeIntoMapLoadshould fold per reassociation group (the
map_storecounterpart was changed to do this to avoid a per-element
dynamic integer division — see the per-group reshape-folding change).
Proposed resolution
Add a MapLoadOpVectorizationModel plus a way to lower a vectorized
map_load, then route CPU encoding-relayout dispatches through
CombineSourceLayoutTransformation (→ map_load) instead of
CombineResultLayoutTransformation (→ map_store). This makes a CPU encoding
relayout vectorize the same way it does on GPU, closing the residual data-tiled
matmul perf gap.
Impact / motivation
Concrete data point — i8 data-tiled matmul, 4096×4096, AMD Zen 4, inner_tiled
codegen vs the hand-written mmt4d ukernel: after fixing #24483 and making the
relayout index math division-free, inner_tiled is at ~46.9 ms vs the
ukernel's ~43.6 ms. The ~7% residual is exactly this scalar encode dispatch
(confirmed K-independent by sweeping K). Vectorizing it should close the gap.
Activity