-
Notifications
You must be signed in to change notification settings - Fork 667
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
[stream] Dedeplicate stream.yield
values from stream.async.execute
#20056
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2275,10 +2275,89 @@ struct ElideNoOpAsyncExecuteOp : public OpRewritePattern<AsyncExecuteOp> { | |
} | ||
}; | ||
|
||
struct DeduplicateYieldCmdExecuteOp : public OpRewritePattern<AsyncExecuteOp> { | ||
using OpRewritePattern::OpRewritePattern; | ||
LogicalResult matchAndRewrite(AsyncExecuteOp op, | ||
PatternRewriter &rewriter) const override { | ||
llvm::SmallVector<bool> keepYield; | ||
llvm::SmallVector<Value> yieldOperands; | ||
llvm::SmallVector<int> remapping; | ||
|
||
auto yield = | ||
cast<IREE::Stream::YieldOp>(op.getBody().front().getTerminator()); | ||
int64_t oldYieldCount = yield.getResourceOperands().size(); | ||
for (int i = 0, s = oldYieldCount; i < s; ++i) { | ||
auto operand = yield.getResourceOperands()[i]; | ||
|
||
auto find = | ||
std::find(yieldOperands.begin(), yieldOperands.end(), operand); | ||
if (find != yieldOperands.end()) { | ||
keepYield.push_back(false); | ||
remapping.push_back(find - yieldOperands.begin()); | ||
continue; | ||
} | ||
|
||
remapping.push_back(yieldOperands.size()); | ||
keepYield.push_back(true); | ||
yieldOperands.push_back(operand); | ||
} | ||
|
||
if (oldYieldCount == yieldOperands.size()) { | ||
return failure(); | ||
} | ||
|
||
llvm::SmallVector<Type> newTypes; | ||
llvm::SmallVector<Value> newResultSizes; | ||
|
||
for (int i = 0; i < oldYieldCount; ++i) { | ||
if (!keepYield[i]) | ||
continue; | ||
newTypes.push_back(op.getResults()[i].getType()); | ||
newResultSizes.push_back(op.getResultSizes()[i]); | ||
} | ||
|
||
auto newExecuteOp = rewriter.create<IREE::Stream::AsyncExecuteOp>( | ||
op.getLoc(), newTypes, newResultSizes, op.getAwaitTimepoint(), | ||
op.getResourceOperands(), op.getResourceOperandSizes(), | ||
llvm::SmallVector<int64_t>()); | ||
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. we'll need to preserve tied operands - this is stripping them all (always useful to throw in a tied operand to ensure they are not stripped, like by adding a |
||
|
||
newExecuteOp.setAffinityAttr(op.getAffinityAttr()); | ||
|
||
rewriter.inlineRegionBefore(op.getRegion(), newExecuteOp.getRegion(), | ||
newExecuteOp.getRegion().end()); | ||
|
||
llvm::SmallVector<Value> newYieldVals; | ||
llvm::SmallVector<Value> newYieldSizes; | ||
yield = cast<IREE::Stream::YieldOp>( | ||
newExecuteOp.getBody().front().getTerminator()); | ||
for (int i = 0; i < oldYieldCount; ++i) { | ||
if (!keepYield[i]) | ||
continue; | ||
newYieldVals.push_back(yield.getResourceOperands()[i]); | ||
newYieldSizes.push_back(yield.getResourceOperandSizes()[i]); | ||
} | ||
|
||
rewriter.setInsertionPoint(yield); | ||
rewriter.replaceOpWithNewOp<IREE::Stream::YieldOp>(yield, newYieldVals, | ||
newYieldSizes); | ||
|
||
llvm::SmallVector<Value> replace; | ||
for (auto i : remapping) { | ||
replace.push_back(newExecuteOp.getResult(i)); | ||
} | ||
|
||
replace.push_back(newExecuteOp.getResultTimepoint()); | ||
rewriter.replaceOp(op, replace); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void AsyncExecuteOp::getCanonicalizationPatterns(RewritePatternSet &results, | ||
MLIRContext *context) { | ||
results.insert<DeduplicateYieldCmdExecuteOp>(context); | ||
results.insert<ElideImmediateTimepointWait<AsyncExecuteOp>>(context); | ||
results.insert<ChainDependentAwaits<AsyncExecuteOp>>(context); | ||
results.insert<CloneCapturedAsyncExecuteSubviewOps>(context); | ||
|
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.
style/warnings: use consistent types - oldYieldCount is int64_t so you'd use int64_t here - but really, size() is size_t so oldYieldCount and i/s here should be size_t
(some compilers care about this more than others especially if signedness changes, but it also helps readability)