Skip to content

[Codegen][CPU] Add a VectorizeInnerTiled pattern. - #24329

Closed
bjacob wants to merge 1 commit into
mainfrom
users/bjacob/cpu-vectorize-inner-tiled
Closed

bjacob wants to merge 1 commit into
mainfrom
users/bjacob/cpu-vectorize-inner-tiled

Conversation

@bjacob

@bjacob bjacob commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

Lifts a tensor-semantics iree_codegen.inner_tiled op to vector semantics by reading each operand with vector.transfer_read, replacing the op with a vector-semantics inner_tiled, and writing each result back with vector.transfer_write. Statically-shaped operands only — the CPU codegen path drives the iteration domain to unit bounds well before this pattern runs.

This fills the gap between encoding-materialization (which produces a tensor-semantics inner_tiled) and the existing
DropInnerTiledUnitDimsPattern + LowerInnerTiledPattern (both vector-semantics-only) on the LLVM-CPU side. GPU folds the analogous read/write into its lane-distribution step (DistributeInnerTiledToLanes); CPU has no lane distribution so the vectorize step needs to be standalone.

Exposes the pattern as populateVectorizeInnerTiledPatterns from Codegen/Common/Transforms.h, plus a transform-dialect entry point apply_patterns.iree.vectorize_inner_tiled so the pattern can be exercised from a lit test.

Tested via a single-tile AVX-512 1x16x1 f32 example in TransformExtensions/test/vectorize_inner_tiled.mlir.

Progress towards #24323

@bjacob bjacob mentioned this pull request Apr 30, 2026
25 of 28 tasks
Lifts a tensor-semantics `iree_codegen.inner_tiled` op to vector
semantics by reading each operand with `vector.transfer_read`,
replacing the op with a vector-semantics `inner_tiled`, and writing
each result back with `vector.transfer_write`. Statically-shaped
operands only — the CPU codegen path drives the iteration domain to
unit bounds well before this pattern runs.

This fills the gap between encoding-materialization (which produces a
tensor-semantics inner_tiled) and the existing
`DropInnerTiledUnitDimsPattern` + `LowerInnerTiledPattern`
(both vector-semantics-only) on the LLVM-CPU side. GPU folds the
analogous read/write into its lane-distribution step
(`DistributeInnerTiledToLanes`); CPU has no lane distribution so the
vectorize step needs to be standalone.

Exposes the pattern as `populateVectorizeInnerTiledPatterns` from
`Codegen/Common/Transforms.h`, plus a transform-dialect entry point
`apply_patterns.iree.vectorize_inner_tiled` so the pattern can be
exercised from a lit test.

Tested via a single-tile AVX-512 1x16x1 f32 example in
TransformExtensions/test/vectorize_inner_tiled.mlir.

Signed-off-by: Benoit Jacob <[email protected]>

Progress towards #24323
@bjacob
bjacob force-pushed the users/bjacob/cpu-vectorize-inner-tiled branch from 0c7cc32 to 5201935 Compare April 30, 2026 19:52
@bjacob

bjacob commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator Author

The UBSan CI failure is #24331

@bjacob
bjacob marked this pull request as ready for review April 30, 2026 20:19

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is already happening in this implementation:

/// External model for IREE::Codegen::InnerTiledOp. Reads tensor operands into
/// vectors, creates a vector-semantic InnerTiledOp, and writes results back.
struct InnerTiledOpVectorizationModel
: VectorizableOpInterface::ExternalModel<InnerTiledOpVectorizationModel,
IREE::Codegen::InnerTiledOp> {
bool isVectorizable(Operation *op, ArrayRef<int64_t> vectorSizes,
ArrayRef<bool> scalableDims,
DictionaryAttr options) const {
auto tiledOp = cast<IREE::Codegen::InnerTiledOp>(op);
if (!tiledOp.hasTensorSemantics()) {
return false;
}
SmallVector<int64_t> loopRanges;
tiledOp.getIterationBounds(loopRanges);
// If vector sizes are provided (from tile size analysis or config),
// dynamic outer shapes are fine - they'll be masked during vectorization.
// However, vector sizes must be >= the static outer dimension sizes.
if (!vectorSizes.empty()) {
return succeeded(
vector::isValidMaskedInputVector(loopRanges, vectorSizes));
}
// Without vector sizes, require static outer shapes.
return ShapedType::isStaticShape(loopRanges);
}
FailureOr<SmallVector<Value>> vectorize(Operation *op, RewriterBase &rewriter,
ArrayRef<int64_t> vectorSizes,
ArrayRef<bool> scalableDims,
DictionaryAttr options) const {
auto tiledOp = cast<IREE::Codegen::InnerTiledOp>(op);
OpBuilder::InsertionGuard g(rewriter);
rewriter.setInsertionPoint(tiledOp);
Location loc = tiledOp.getLoc();
SmallVector<ShapedType> argTypes = tiledOp.getOperandShapedTypes();
SmallVector<AffineMap> indexingMaps = tiledOp.getIndexingMapsArray();
// If no vector sizes are provided, use static loop ranges and the inBounds
// attribute instead of masking.
bool needsMasking = true;
if (vectorSizes.empty()) {
SmallVector<int64_t> loopRanges;
tiledOp.getIterationBounds(loopRanges);
assert(ShapedType::isStaticShape(loopRanges) &&
"unable to infer vector sizes");
vectorSizes = loopRanges;
needsMasking = false;
}
// Construct the zero padding value for each operand. Ideally, we'd need the
// InnerTile interface to return the padding value to use. If it is not
// provided, ub::Poison is a better choice. Zero was chosen because the op
// was designed for matmul, and zero padding is the most common case.
SmallVector<Value> padValues =
llvm::map_to_vector(argTypes, [&](ShapedType argType) -> Value {
return arith::ConstantOp::create(
rewriter, loc, rewriter.getZeroAttr(argType.getElementType()));
});
// Compute the read shape for each operand.
SmallVector<SmallVector<int64_t>> readShapes;
for (auto [i, argType] : llvm::enumerate(argTypes)) {
if (!needsMasking) {
readShapes.push_back(llvm::to_vector(argType.getShape()));
continue;
}
// Outer dimensions come from vector sizes via the indexing map, inner
// dimensions are static.
SmallVector<int64_t> readShape;
AffineMap map = indexingMaps[i];
for (AffineExpr expr : map.getResults()) {
auto dimExpr = cast<AffineDimExpr>(expr);
readShape.push_back(vectorSizes[dimExpr.getPosition()]);
}
ArrayRef<int64_t> innerShape = tiledOp.getOperandInnerShape(i);
readShape.append(innerShape.begin(), innerShape.end());
readShapes.push_back(std::move(readShape));
}
// Read each operand into a vector, with masking if needed.
SmallVector<Value> newOperands(tiledOp->getOperands());
for (auto [operand, readShape, padValue] :
llvm::zip_equal(newOperands, readShapes, padValues)) {
operand = vector::createReadOrMaskedRead(
rewriter, loc, operand, readShape, padValue,
/*useInBoundsInsteadOfMasking=*/!needsMasking);
}
auto newTiledOp = IREE::Codegen::InnerTiledOp::create(
rewriter, loc,
ValueRange{newOperands}.take_front(tiledOp.getNumInputs()),
ValueRange{newOperands}.take_back(tiledOp.getNumOutputs()),
tiledOp.getIndexingMaps(), tiledOp.getIteratorTypes(),
tiledOp.getKind(), tiledOp.getSemantics());
// Write results back to tensor, with masking if needed.
SmallVector<Value> results;
for (auto [result, dest] :
llvm::zip_equal(newTiledOp.getResults(), tiledOp.getOutputs())) {
Operation *write = vector::createWriteOrMaskedWrite(
rewriter, loc, result, dest, /*writeIndices=*/{},
/*useInBoundsInsteadOfMasking=*/!needsMasking);
results.push_back(write->getResult(0));
}
return results;
}
};
Can you check?

@sommerlukas we are reaching to the state that your vector size analysis requires inner tiles soon. We now allow inner tiles being dynamic for future SVE/SME/RISC-V work.

@hanhanW

hanhanW commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Corresponding PRs:

@sommerlukas

Copy link
Copy Markdown
Contributor

@sommerlukas we are reaching to the state that your vector size analysis requires inner tiles soon. We now allow inner tiles being dynamic for future SVE/SME/RISC-V work.

Thanks for the heads-up, happy to chat about how this can be supported. Do we also need to track scalable flags then?

The pack/unpack operations probably already carry size information for the inner tiles. How do we determine the mapping for inner dimensions, as they're not part of the maps?

@hanhanW

hanhanW commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

@sommerlukas we are reaching to the state that your vector size analysis requires inner tiles soon. We now allow inner tiles being dynamic for future SVE/SME/RISC-V work.

Thanks for the heads-up, happy to chat about how this can be supported. Do we also need to track scalable flags then?

The pack/unpack operations probably already carry size information for the inner tiles. How do we determine the mapping for inner dimensions, as they're not part of the maps?

It will be more clear after #24311 is fixed.

The rough idea in my mind is using either ValueBound or extended IntRange analysis to get the tiling size, then you can propagate the information.

@bjacob bjacob closed this May 1, 2026
@bjacob
bjacob deleted the users/bjacob/cpu-vectorize-inner-tiled branch May 1, 2026 15:17
@bjacob

bjacob commented May 1, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @hanhanW for catching this! Closed.

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.

3 participants