Skip to content

Commit c3042fb

Browse files
committed
[MLIR] Add support for multiway split in SplitOp
Add functionality that enables SplitOp to do a multiway split of a traget op along a given dimension. With multiway attribute, SplitOp takes a list of chunk sizes and applies it to a single target along the given dimension to generate multiple structured ops extracted from the target.
1 parent a6155b6 commit c3042fb

File tree

5 files changed

+189
-94
lines changed

5 files changed

+189
-94
lines changed

mlir/include/mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,29 +1396,43 @@ def SplitOp : Op<Transform_Dialect, "structured.split",
13961396
DeclareOpInterfaceMethods<TransformOpInterface>,
13971397
ReportTrackingListenerFailuresOpTrait]> {
13981398
let description = [{
1399-
Indicates that the given `target` op should be split into two complementary
1399+
Splits the given `target` op into two or more complementary
14001400
parts, which combined cover the entire iteration domain of the original op.
14011401
The split is performed along the iteration space dimension provided as
1402-
attribute. In case of dimension overflow, the transformation fails. The
1403-
split is performed at the dimension iterator value specified as either the
1404-
static split point attribute when it is known at transform IR construction
1405-
time or as the handle to an operation producing a single index-typed value
1406-
when it is computed by payload IR. In the latter case, the static split
1402+
chunk size attribute specifying the size of the lower part; the remaining
1403+
range in the iteration space is assigned as the upper part. In case of
1404+
dimension overflow, the transformation fails. The split is performed at the
1405+
dimension iterator value specified as either the static chunk size
1406+
attribute when it is known at transform IR construction time or
1407+
as the handle to an operation producing a single index-typed value
1408+
when it is computed by payload IR. In the latter case, the chunk size
14071409
point must be set to `ShapedType::kDynamic` and the dynamic size handle
14081410
must point to as many value-producing operations as there are structured
14091411
operations pointed to by the target handle.
14101412

1411-
The operation consumes the target handle, but preserves the split point
1412-
handle if provided. It produces two new handles pointing to the two parts
1413-
of the structured op after splitting, in the same order as the target
1414-
operand, with the first handle corresponding to the part with lower
1415-
iteration space indices.
1413+
The operation consumes the target handle, but preserves the chunk size
1414+
handle if provided. Without the `multiway` attribute, it produces two
1415+
new handles pointing to the two parts of the structured op after splitting,
1416+
in the same order as the target operand, with the first handle
1417+
corresponding to the part with lower iteration space indices.
1418+
1419+
Multiway split mode is enabled by specifying the `multiway` attribute.
1420+
In this mode a single `target` op is split into multiple parts covering
1421+
the iteration space of the specified dimension. `static_chunk_sizes` and
1422+
`dynamic_chunk_sizes` in this case is a list of chunk sizes that the given
1423+
dimension should be split into. With `multiway` it produces two handles;
1424+
the first handle is a list of the multiple parts of the structured op
1425+
after splitting, where the target dimensions for each linalg op in the
1426+
list corresponds to the chunk sizes specfied in the input split list.
1427+
If the chunk sizes do not cover the entire iteration space, the leftover
1428+
chunk is the last payload in the first handle. The second handle is empty.
14161429
}];
14171430

14181431
let arguments = (ins TransformHandleTypeInterface:$target,
14191432
I64Attr:$dimension,
1420-
Optional<TransformAnyParamTypeOrAnyHandle>:$dynamic_split_point,
1421-
I64Attr:$static_split_point);
1433+
Optional<TransformAnyParamTypeOrAnyHandle>:$dynamic_chunk_sizes,
1434+
I64Attr:$static_chunk_sizes,
1435+
UnitAttr:$multiway);
14221436
let results = (outs TransformHandleTypeInterface:$first,
14231437
TransformHandleTypeInterface:$second);
14241438
let hasCustomAssemblyFormat = 1;

mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp

Lines changed: 151 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,13 +2269,26 @@ SplitOp::apply(transform::TransformRewriter &rewriter,
22692269
// Collect the dynamic split points if provided.
22702270
SmallVector<Operation *> payload =
22712271
llvm::to_vector(state.getPayloadOps(getTarget()));
2272-
SmallVector<OpFoldResult> splitPoints;
2273-
splitPoints.reserve(payload.size());
2274-
if (getDynamicSplitPoint()) {
2272+
2273+
bool isMultiwaySplit = getMultiway();
2274+
2275+
if (isMultiwaySplit && !llvm::hasSingleElement(payload)) {
2276+
return mlir::emitSilenceableFailure(getLoc())
2277+
<< "requires exactly one target when "
2278+
"multiway split is enabled (got "
2279+
<< llvm::range_size(payload) << ")";
2280+
}
2281+
2282+
SmallVector<OpFoldResult> chunkSizes;
2283+
2284+
if (!isMultiwaySplit)
2285+
chunkSizes.reserve(payload.size());
2286+
2287+
if (getDynamicChunkSizes()) {
22752288
auto diag = DiagnosedSilenceableFailure::success();
2276-
if (isa<TransformHandleTypeInterface>(getDynamicSplitPoint().getType())) {
2277-
splitPoints = llvm::to_vector(llvm::map_range(
2278-
state.getPayloadOps(getDynamicSplitPoint()), [&](Operation *op) {
2289+
if (isa<TransformHandleTypeInterface>(getDynamicChunkSizes().getType())) {
2290+
chunkSizes = llvm::to_vector(llvm::map_range(
2291+
state.getPayloadOps(getDynamicChunkSizes()), [&](Operation *op) {
22792292
if (op->getNumResults() != 1 ||
22802293
!op->getResult(0).getType().isIndex()) {
22812294
diag = emitSilenceableError()
@@ -2286,103 +2299,171 @@ SplitOp::apply(transform::TransformRewriter &rewriter,
22862299
return OpFoldResult(op->getResult(0));
22872300
}));
22882301
} else {
2289-
splitPoints = llvm::to_vector(
2290-
llvm::map_range(state.getParams(getDynamicSplitPoint()),
2302+
chunkSizes = llvm::to_vector(
2303+
llvm::map_range(state.getParams(getDynamicChunkSizes()),
22912304
[](Attribute attr) { return OpFoldResult(attr); }));
22922305
}
22932306
if (diag.isSilenceableFailure())
22942307
return diag;
22952308

2296-
if (splitPoints.size() != payload.size()) {
2309+
// For multiway split, a single payload is expected to have multiple
2310+
// split points.
2311+
if (!isMultiwaySplit && chunkSizes.size() != payload.size()) {
22972312
return emitDefiniteFailure()
22982313
<< "expected the dynamic split point handle to point to as "
22992314
"many operations ("
2300-
<< splitPoints.size() << ") as the target handle ("
2315+
<< chunkSizes.size() << ") as the target handle ("
23012316
<< payload.size() << ")";
23022317
}
23032318
} else {
2304-
splitPoints.resize(payload.size(),
2305-
rewriter.getIndexAttr(getStaticSplitPoint()));
2319+
chunkSizes.resize(payload.size(),
2320+
rewriter.getIndexAttr(getStaticChunkSizes()));
23062321
}
23072322

2308-
// Split each target operation.
2309-
SmallVector<Operation *> first, second;
2310-
Operation *noSecondPart = nullptr;
2311-
for (const auto &pair : llvm::zip(payload, splitPoints)) {
2312-
Operation *target = std::get<0>(pair);
2313-
auto linalgOp = dyn_cast<LinalgOp>(target);
2323+
auto checkStructuredOpAndDimensions =
2324+
[&](LinalgOp linalgOp, Location loc) -> DiagnosedSilenceableFailure {
23142325
if (!linalgOp) {
23152326
auto diag = emitSilenceableError() << "only applies to structured ops";
2316-
diag.attachNote(target->getLoc()) << "target op";
2327+
diag.attachNote(loc) << "target op";
23172328
return diag;
23182329
}
23192330

23202331
if (getDimension() >= linalgOp.getNumLoops()) {
23212332
auto diag = emitSilenceableError() << "dimension " << getDimension()
2322-
<< " does not exist in target op";
2323-
diag.attachNote(target->getLoc()) << "target op";
2333+
<< " does not exist in target op";
2334+
diag.attachNote(loc) << "target op";
23242335
return diag;
23252336
}
2337+
return DiagnosedSilenceableFailure::success();
2338+
};
23262339

2327-
rewriter.setInsertionPoint(linalgOp);
2328-
std::tie(first.emplace_back(), second.emplace_back()) = linalg::splitOp(
2329-
rewriter, cast<TilingInterface>(linalgOp.getOperation()),
2330-
getDimension(), std::get<1>(pair));
2331-
2332-
// Propagate errors.
2333-
if (!first.back() && !second.back()) {
2340+
auto checkFailureInSplitting =
2341+
[&](bool hasFailed, Location loc) -> DiagnosedSilenceableFailure {
2342+
if (hasFailed) {
23342343
auto diag = emitDefiniteFailure() << "internal failure in splitting";
2335-
diag.attachNote(target->getLoc()) << "target op";
2344+
diag.attachNote(loc) << "target op";
23362345
return diag;
23372346
}
2347+
return DiagnosedSilenceableFailure::success();
2348+
};
2349+
2350+
if (isMultiwaySplit) {
2351+
2352+
// Split a single target operation at multiple points.
2353+
SmallVector<Operation *> opList;
2354+
TilingInterface head, tail;
2355+
Operation *target = payload.front();
2356+
2357+
LinalgOp linalgOp = dyn_cast<LinalgOp>(target);
2358+
DiagnosedSilenceableFailure diag =
2359+
checkStructuredOpAndDimensions(linalgOp, target->getLoc());
2360+
2361+
if (diag.isSilenceableFailure())
2362+
return diag;
2363+
2364+
for (auto &&[idx, chunkSize] : llvm::enumerate(chunkSizes)) {
2365+
2366+
if (idx > 0)
2367+
target = tail.getOperation();
2368+
2369+
if (!target)
2370+
break;
23382371

2339-
// Do not add null second parts.
2340-
if (!second.back()) {
2341-
noSecondPart = target;
2342-
second.pop_back();
2372+
linalgOp = cast<LinalgOp>(target);
2373+
2374+
rewriter.setInsertionPoint(linalgOp);
2375+
std::tie(head, tail) = linalg::splitOp(
2376+
rewriter, cast<TilingInterface>(linalgOp.getOperation()),
2377+
getDimension(), chunkSize);
2378+
2379+
// Propagate errors.
2380+
DiagnosedSilenceableFailure diag =
2381+
checkFailureInSplitting(!head && !tail, target->getLoc());
2382+
if (diag.isDefiniteFailure())
2383+
return diag;
2384+
2385+
opList.push_back(head.getOperation());
23432386
}
2344-
}
23452387

2346-
if (second.size() != first.size() && !second.empty()) {
2347-
auto diag = emitSilenceableError()
2348-
<< "splitting does not produce the second part for a subset "
2349-
"of targets";
2350-
diag.attachNote() << "expected splitting to produce the second part of all "
2351-
"or none of the targets";
2352-
diag.attachNote(noSecondPart->getLoc())
2353-
<< "first target with no second part";
2354-
return diag;
2355-
}
2388+
// Append any leftover parts to the end of the result list.
2389+
if (tail)
2390+
opList.push_back(tail);
2391+
results.set(cast<OpResult>(getFirst()), opList);
2392+
results.set(cast<OpResult>(getSecond()), {});
2393+
2394+
} else {
2395+
// Split each target operation.
2396+
SmallVector<Operation *> first, second;
2397+
Operation *noSecondPart = nullptr;
2398+
for (const auto &pair : llvm::zip(payload, chunkSizes)) {
2399+
Operation *target = std::get<0>(pair);
2400+
LinalgOp linalgOp = dyn_cast<LinalgOp>(target);
2401+
DiagnosedSilenceableFailure diag =
2402+
checkStructuredOpAndDimensions(linalgOp, target->getLoc());
2403+
2404+
if (diag.isSilenceableFailure())
2405+
return diag;
2406+
2407+
rewriter.setInsertionPoint(linalgOp);
2408+
std::tie(first.emplace_back(), second.emplace_back()) = linalg::splitOp(
2409+
rewriter, cast<TilingInterface>(linalgOp.getOperation()),
2410+
getDimension(), std::get<1>(pair));
2411+
2412+
// Propagate errors.
2413+
DiagnosedSilenceableFailure diagSplit = checkFailureInSplitting(
2414+
!first.back() && !second.back(), target->getLoc());
2415+
if (diagSplit.isDefiniteFailure())
2416+
return diag;
2417+
2418+
// Do not add null second parts.
2419+
if (!second.back()) {
2420+
noSecondPart = target;
2421+
second.pop_back();
2422+
}
2423+
}
2424+
2425+
if (second.size() != first.size() && !second.empty()) {
2426+
auto diag = emitSilenceableError()
2427+
<< "splitting does not produce the second part for a subset "
2428+
"of targets";
2429+
diag.attachNote()
2430+
<< "expected splitting to produce the second part of all "
2431+
"or none of the targets";
2432+
diag.attachNote(noSecondPart->getLoc())
2433+
<< "first target with no second part";
2434+
return diag;
2435+
}
23562436

2357-
results.set(cast<OpResult>(getFirst()), first);
2358-
results.set(cast<OpResult>(getSecond()), second);
2437+
results.set(cast<OpResult>(getFirst()), first);
2438+
results.set(cast<OpResult>(getSecond()), second);
2439+
}
23592440
return DiagnosedSilenceableFailure::success();
23602441
}
23612442

23622443
void SplitOp::getEffects(
23632444
SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
23642445
consumesHandle(getTarget(), effects);
2365-
if (getDynamicSplitPoint())
2366-
onlyReadsHandle(getDynamicSplitPoint(), effects);
2446+
if (getDynamicChunkSizes())
2447+
onlyReadsHandle(getDynamicChunkSizes(), effects);
23672448
producesHandle(getResults(), effects);
23682449
modifiesPayload(effects);
23692450
}
23702451

23712452
ParseResult SplitOp::parse(OpAsmParser &parser, OperationState &result) {
2372-
OpAsmParser::UnresolvedOperand target, dynamicSplitPoint;
2373-
IntegerAttr staticSplitPoint;
2453+
OpAsmParser::UnresolvedOperand target, dynamicChunkSizes;
2454+
IntegerAttr staticChunkSizes;
23742455
if (parser.parseOperand(target) || parser.parseKeyword("after"))
23752456
return failure();
23762457

23772458
OptionalParseResult dynamicPointParseResult =
2378-
parser.parseOptionalOperand(dynamicSplitPoint);
2459+
parser.parseOptionalOperand(dynamicChunkSizes);
23792460
if (!dynamicPointParseResult.has_value()) {
2380-
int64_t staticSplitPointValue;
2381-
if (failed(parser.parseInteger(staticSplitPointValue)))
2461+
int64_t staticChunkSizesValue;
2462+
if (failed(parser.parseInteger(staticChunkSizesValue)))
23822463
return failure();
23832464

2384-
staticSplitPoint =
2385-
parser.getBuilder().getI64IntegerAttr(staticSplitPointValue);
2465+
staticChunkSizes =
2466+
parser.getBuilder().getI64IntegerAttr(staticChunkSizesValue);
23862467
}
23872468

23882469
Type targetType;
@@ -2392,43 +2473,43 @@ ParseResult SplitOp::parse(OpAsmParser &parser, OperationState &result) {
23922473
return failure();
23932474
}
23942475
if (dynamicPointParseResult.has_value()) {
2395-
Type splitPointType;
2476+
Type ChunkSizesType;
23962477
if (failed(*dynamicPointParseResult) || parser.parseComma() ||
2397-
parser.parseType(splitPointType) ||
2398-
parser.resolveOperand(dynamicSplitPoint, splitPointType,
2478+
parser.parseType(ChunkSizesType) ||
2479+
parser.resolveOperand(dynamicChunkSizes, ChunkSizesType,
23992480
result.operands)) {
24002481
return failure();
24012482
}
24022483

2403-
staticSplitPoint =
2484+
staticChunkSizes =
24042485
parser.getBuilder().getI64IntegerAttr(ShapedType::kDynamic);
24052486
}
24062487

24072488
result.addAttribute(
2408-
SplitOp::getStaticSplitPointAttrName(result.name).getValue(),
2409-
staticSplitPoint);
2489+
SplitOp::getStaticChunkSizesAttrName(result.name).getValue(),
2490+
staticChunkSizes);
24102491
result.addTypes({targetType, targetType});
24112492
return success();
24122493
}
24132494

24142495
void SplitOp::print(OpAsmPrinter &printer) {
24152496
printer << " " << getTarget() << " after ";
2416-
int64_t staticSplitSize = static_cast<int64_t>(getStaticSplitPoint());
2417-
if (staticSplitSize != ShapedType::kDynamic)
2418-
printer << staticSplitSize;
2497+
int64_t staticChunkSize = static_cast<int64_t>(getStaticChunkSizes());
2498+
if (staticChunkSize != ShapedType::kDynamic)
2499+
printer << staticChunkSize;
24192500
else
2420-
printer << getDynamicSplitPoint();
2501+
printer << getDynamicChunkSizes();
24212502
printer << " ";
24222503
printer.printOptionalAttrDict(getOperation()->getAttrs(),
2423-
{getStaticSplitPointAttrName()});
2504+
{getStaticChunkSizesAttrName()});
24242505
printer << " : " << getTarget().getType();
2425-
if (staticSplitSize == ShapedType::kDynamic)
2426-
printer << ", " << getDynamicSplitPoint().getType();
2506+
if (staticChunkSize == ShapedType::kDynamic)
2507+
printer << ", " << getDynamicChunkSizes().getType();
24272508
}
24282509

24292510
LogicalResult SplitOp::verify() {
2430-
if ((static_cast<int64_t>(getStaticSplitPoint()) != ShapedType::kDynamic) ^
2431-
(getDynamicSplitPoint() == nullptr)) {
2511+
if ((static_cast<int64_t>(getStaticChunkSizes()) != ShapedType::kDynamic) ^
2512+
(getDynamicChunkSizes() == nullptr)) {
24322513
return emitOpError() << "expects either a dynamic or a static split "
24332514
"point to be provided";
24342515
}

0 commit comments

Comments
 (0)