Skip to content

Commit 737ae7c

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 737ae7c

File tree

5 files changed

+190
-94
lines changed

5 files changed

+190
-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: 152 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,172 @@ 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+
2359+
// Check that the target is a valid LinalgOp with correct dimensions.
2360+
DiagnosedSilenceableFailure diag =
2361+
checkStructuredOpAndDimensions(linalgOp, target->getLoc());
2362+
if (diag.isSilenceableFailure())
2363+
return diag;
2364+
2365+
for (auto &&[idx, chunkSize] : llvm::enumerate(chunkSizes)) {
2366+
2367+
if (idx > 0)
2368+
target = tail.getOperation();
2369+
2370+
if (!target)
2371+
break;
23382372

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

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

2357-
results.set(cast<OpResult>(getFirst()), first);
2358-
results.set(cast<OpResult>(getSecond()), second);
2438+
results.set(cast<OpResult>(getFirst()), first);
2439+
results.set(cast<OpResult>(getSecond()), second);
2440+
}
23592441
return DiagnosedSilenceableFailure::success();
23602442
}
23612443

23622444
void SplitOp::getEffects(
23632445
SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
23642446
consumesHandle(getTarget(), effects);
2365-
if (getDynamicSplitPoint())
2366-
onlyReadsHandle(getDynamicSplitPoint(), effects);
2447+
if (getDynamicChunkSizes())
2448+
onlyReadsHandle(getDynamicChunkSizes(), effects);
23672449
producesHandle(getResults(), effects);
23682450
modifiesPayload(effects);
23692451
}
23702452

23712453
ParseResult SplitOp::parse(OpAsmParser &parser, OperationState &result) {
2372-
OpAsmParser::UnresolvedOperand target, dynamicSplitPoint;
2373-
IntegerAttr staticSplitPoint;
2454+
OpAsmParser::UnresolvedOperand target, dynamicChunkSizes;
2455+
IntegerAttr staticChunkSizes;
23742456
if (parser.parseOperand(target) || parser.parseKeyword("after"))
23752457
return failure();
23762458

23772459
OptionalParseResult dynamicPointParseResult =
2378-
parser.parseOptionalOperand(dynamicSplitPoint);
2460+
parser.parseOptionalOperand(dynamicChunkSizes);
23792461
if (!dynamicPointParseResult.has_value()) {
2380-
int64_t staticSplitPointValue;
2381-
if (failed(parser.parseInteger(staticSplitPointValue)))
2462+
int64_t staticChunkSizesValue;
2463+
if (failed(parser.parseInteger(staticChunkSizesValue)))
23822464
return failure();
23832465

2384-
staticSplitPoint =
2385-
parser.getBuilder().getI64IntegerAttr(staticSplitPointValue);
2466+
staticChunkSizes =
2467+
parser.getBuilder().getI64IntegerAttr(staticChunkSizesValue);
23862468
}
23872469

23882470
Type targetType;
@@ -2392,43 +2474,43 @@ ParseResult SplitOp::parse(OpAsmParser &parser, OperationState &result) {
23922474
return failure();
23932475
}
23942476
if (dynamicPointParseResult.has_value()) {
2395-
Type splitPointType;
2477+
Type ChunkSizesType;
23962478
if (failed(*dynamicPointParseResult) || parser.parseComma() ||
2397-
parser.parseType(splitPointType) ||
2398-
parser.resolveOperand(dynamicSplitPoint, splitPointType,
2479+
parser.parseType(ChunkSizesType) ||
2480+
parser.resolveOperand(dynamicChunkSizes, ChunkSizesType,
23992481
result.operands)) {
24002482
return failure();
24012483
}
24022484

2403-
staticSplitPoint =
2485+
staticChunkSizes =
24042486
parser.getBuilder().getI64IntegerAttr(ShapedType::kDynamic);
24052487
}
24062488

24072489
result.addAttribute(
2408-
SplitOp::getStaticSplitPointAttrName(result.name).getValue(),
2409-
staticSplitPoint);
2490+
SplitOp::getStaticChunkSizesAttrName(result.name).getValue(),
2491+
staticChunkSizes);
24102492
result.addTypes({targetType, targetType});
24112493
return success();
24122494
}
24132495

24142496
void SplitOp::print(OpAsmPrinter &printer) {
24152497
printer << " " << getTarget() << " after ";
2416-
int64_t staticSplitSize = static_cast<int64_t>(getStaticSplitPoint());
2417-
if (staticSplitSize != ShapedType::kDynamic)
2418-
printer << staticSplitSize;
2498+
int64_t staticChunkSize = static_cast<int64_t>(getStaticChunkSizes());
2499+
if (staticChunkSize != ShapedType::kDynamic)
2500+
printer << staticChunkSize;
24192501
else
2420-
printer << getDynamicSplitPoint();
2502+
printer << getDynamicChunkSizes();
24212503
printer << " ";
24222504
printer.printOptionalAttrDict(getOperation()->getAttrs(),
2423-
{getStaticSplitPointAttrName()});
2505+
{getStaticChunkSizesAttrName()});
24242506
printer << " : " << getTarget().getType();
2425-
if (staticSplitSize == ShapedType::kDynamic)
2426-
printer << ", " << getDynamicSplitPoint().getType();
2507+
if (staticChunkSize == ShapedType::kDynamic)
2508+
printer << ", " << getDynamicChunkSizes().getType();
24272509
}
24282510

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

0 commit comments

Comments
 (0)