-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[mlir] Fix consumer fusion for producer with multiple results #125915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pashu123
wants to merge
1
commit into
llvm:main
Choose a base branch
from
pashu123:cons_fuse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1949,6 +1949,60 @@ getUntiledConsumerFromSlice(RewriterBase &rewriter, Operation *sliceOp) { | |
} | ||
} | ||
|
||
// If the producer of the operand is a loopLikeOp, then finds the last | ||
// insertSlice/parallelInsertSlice in the producer op that uses the block | ||
// argument corresponding to the operand. | ||
static FailureOr<Operation *> | ||
getSliceOpFromConsumerOperand(OpOperand &operand) { | ||
|
||
OpResult producerResult = dyn_cast<OpResult>(operand.get()); | ||
if (!producerResult) | ||
return failure(); | ||
|
||
LoopLikeOpInterface loopLikeOp = | ||
dyn_cast<LoopLikeOpInterface>(producerResult.getOwner()); | ||
if (!loopLikeOp) | ||
return failure(); | ||
|
||
// Obtain the BlockArgument correponding to the result. | ||
BlockArgument bbArg = | ||
loopLikeOp.getRegionIterArgs()[producerResult.getResultNumber()]; | ||
|
||
// Finally return the operation corresponding to the yielded value. | ||
// Also check whether it's an InsertSliceOp. | ||
if (dyn_cast<scf::ForOp>(producerResult.getOwner())) { | ||
OpOperand *yieldVal = loopLikeOp.getTiedLoopYieldedValue(bbArg); | ||
Operation *lastOp = dyn_cast<OpResult>(yieldVal->get()).getOwner(); | ||
auto isInsertSliceOp = isa<tensor::InsertSliceOp>(lastOp); | ||
if (!isInsertSliceOp) { | ||
return failure(); | ||
} | ||
return lastOp; | ||
} | ||
|
||
auto forallOp = dyn_cast<scf::ForallOp>(producerResult.getOwner()); | ||
if (!forallOp) | ||
return failure(); | ||
|
||
// Iterate over the terminator operation of the forallOp to find the last | ||
// parallelInsertSliceOp that uses the blockArgument. | ||
Operation *lastOp = nullptr; | ||
forallOp.getTerminator()->walk([&](tensor::ParallelInsertSliceOp op) { | ||
for (mlir::Value operand : op->getOperands()) { | ||
if (auto maybeBlockArg = dyn_cast<BlockArgument>(operand)) { | ||
if (maybeBlockArg == bbArg) { | ||
lastOp = op; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
if (!lastOp) | ||
return failure(); | ||
|
||
return lastOp; | ||
} | ||
|
||
/// Implementation of fusing consumer of a single slice by computing the | ||
/// slice of the consumer in-place for scf loop. | ||
FailureOr<scf::SCFFuseConsumerOfSliceResult> | ||
|
@@ -1979,6 +2033,26 @@ mlir::scf::tileAndFuseConsumerOfSlice(RewriterBase &rewriter, | |
consumerOp, "consumer op's operand doesn't seem to be an OpResult"); | ||
} | ||
|
||
SmallVector<OpOperand *> potentialOperands = {*maybeConsumerOpOperand}; | ||
SmallVector<unsigned> potentialOperandResultNos = { | ||
consumerOpOperand->getOperandNumber()}; | ||
SmallVector<Operation *> potentialSliceOps = {candidateSliceOp}; | ||
|
||
// 1b. Get all the other operands of the consumer op and their corresponding | ||
// slice ops. In the case of the consumer using multiple results | ||
// from the producer, we need to update every operand. | ||
for (OpOperand &otherOperand : consumerOp->getOpOperands()) { | ||
if (&otherOperand == *maybeConsumerOpOperand) | ||
continue; | ||
auto maybePotentialSlice = getSliceOpFromConsumerOperand(otherOperand); | ||
if (failed(maybePotentialSlice)) { | ||
continue; | ||
} | ||
potentialSliceOps.push_back(*maybePotentialSlice); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to check right here that the producer of |
||
potentialOperands.push_back(&otherOperand); | ||
potentialOperandResultNos.push_back(otherOperand.getOperandNumber()); | ||
} | ||
|
||
// There are two possible cases regarding `oldLoopOp` here: | ||
// 1. single `scf.forall` or `scf.for`. | ||
// 2. inner-most `scf.for` insider nest `scf.loop` structure, where the | ||
|
@@ -2037,43 +2111,64 @@ mlir::scf::tileAndFuseConsumerOfSlice(RewriterBase &rewriter, | |
// tensor.insert_slice. In the scf.for case this is a clone of the | ||
// candidateSliceOp whereas in the scf.forall case this is created from the | ||
// operands of tensor.parallel_insert_slice. | ||
tensor::InsertSliceOp clonedInsertSliceOp; | ||
|
||
SmallVector<tensor::InsertSliceOp> allClonedInsertSliceOps; | ||
|
||
scf::ForallOp newForallOp; | ||
if (auto sliceOp = | ||
dyn_cast<tensor::ParallelInsertSliceOp>(candidateSliceOp)) { | ||
auto newForallOp = cast<scf::ForallOp>(innerMostLoop.getOperation()); | ||
rewriter.setInsertionPoint(newForallOp.getTerminator()); | ||
clonedInsertSliceOp = rewriter.create<tensor::InsertSliceOp>( | ||
loc, sliceOp.getSource(), sliceOp.getDest(), sliceOp.getMixedOffsets(), | ||
sliceOp.getMixedSizes(), sliceOp.getMixedStrides()); | ||
} else { | ||
rewriter.setInsertionPoint(candidateSliceOp); | ||
clonedInsertSliceOp = | ||
cast<tensor::InsertSliceOp>(rewriter.clone(*candidateSliceOp)); | ||
rewriter.setInsertionPoint(potentialSliceOps.back()); | ||
} | ||
|
||
for (auto *candidateSliceOp : potentialSliceOps) { | ||
if (auto sliceOp = | ||
dyn_cast<tensor::ParallelInsertSliceOp>(candidateSliceOp)) { | ||
allClonedInsertSliceOps.push_back(rewriter.create<tensor::InsertSliceOp>( | ||
loc, sliceOp.getSource(), sliceOp.getDest(), | ||
sliceOp.getMixedOffsets(), sliceOp.getMixedSizes(), | ||
sliceOp.getMixedStrides())); | ||
} else { | ||
allClonedInsertSliceOps.push_back( | ||
cast<tensor::InsertSliceOp>(rewriter.clone(*candidateSliceOp))); | ||
} | ||
} | ||
|
||
// 5.a. Clone consumer op. | ||
auto clonedConsumerOp = cast<TilingInterface>(rewriter.clone(*consumerOp)); | ||
|
||
// 5.b. Replace all uses of the loop result with the result of the cloned | ||
// tensor.insert_slice. | ||
OpOperand &operandToReplace = clonedConsumerOp->getOpOperand(operandNumber); | ||
rewriter.modifyOpInPlace(clonedConsumerOp, [&]() { | ||
operandToReplace.set(clonedInsertSliceOp.getResult()); | ||
}); | ||
for (const auto &it : llvm::enumerate(allClonedInsertSliceOps)) { | ||
OpOperand &operandToReplace = | ||
clonedConsumerOp->getOpOperand(potentialOperandResultNos[it.index()]); | ||
rewriter.modifyOpInPlace(clonedConsumerOp, [&]() { | ||
operandToReplace.set(it.value().getResult()); | ||
}); | ||
} | ||
|
||
// 6. Perform tiling of the cloned consumer and replace the operand at | ||
// `operandNumber` with the source of the cloned tensor.insert_slice op. | ||
auto ossSliceOp = | ||
cast<OffsetSizeAndStrideOpInterface>(clonedInsertSliceOp.getOperation()); | ||
auto ossSliceOp = cast<OffsetSizeAndStrideOpInterface>( | ||
allClonedInsertSliceOps.front().getOperation()); | ||
FailureOr<TilingResult> tileAndFuseResult = | ||
tensor::replaceInsertSliceWithTiledConsumer( | ||
rewriter, ossSliceOp, clonedConsumerOp->getOpOperand(operandNumber)); | ||
|
||
if (failed(tileAndFuseResult)) { | ||
return failure(); | ||
} | ||
|
||
auto tiledConsumerOp = cast<TilingInterface>(tileAndFuseResult->tiledOps[0]); | ||
rewriter.replaceAllUsesWith(tiledConsumerOp->getOperand(operandNumber), | ||
clonedInsertSliceOp.getSource()); | ||
|
||
// 6b. Update the tiled consumer op with the new operands. | ||
for (const auto &it : llvm::enumerate(allClonedInsertSliceOps)) { | ||
rewriter.replaceAllUsesWith( | ||
tiledConsumerOp->getOperand(potentialOperandResultNos[it.index()]), | ||
it.value().getSource()); | ||
} | ||
|
||
// 7. Reconstruct [nested] loop with new inits. | ||
YieldTiledValuesFn newYieldValuesFn = | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave some comments as to what this is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment below actually is fine. Move that up above these.
Also instead of a
SmallVector<OpOperand *>
and aSmallVector<Operation *>
for the slices, just make aSmallVector<std::tuple<OpOperand *, Operation *>>