Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

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)

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>());
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 ..., %tied) -> %tied to the end so that it's clear the indices are updated properly - updating index 0 tied operands is usually too easy :)


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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,23 @@ util.func private @ElideImmediateAsyncExecuteWaits(%arg0: !stream.resource<*>, %
util.return %0#0, %0#1 : !stream.resource<*>, !stream.timepoint
}

// -----

// CHECK-LABEL: @DedeuplicateASyncExecuteReturns
util.func private @DedeuplicateASyncExecuteReturns(%arg0: !stream.resource<*>, %arg1: index) -> (!stream.resource<*>, !stream.resource<*>, !stream.resource<*>, !stream.timepoint) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
// CHECK: %[[VAL:.+]], %[[TP:.+]] = stream.async.execute
%0:4 = stream.async.execute with(%arg0 as %arg2: !stream.resource<*>{%arg1}) -> %arg0 as !stream.resource<*>{%arg1}, %arg0 as !stream.resource<*>{%arg1}, %arg0 as !stream.resource<*>{%arg1} {
// CHECK: %[[VAL:.+]]:2 = stream.async.dispatch
%1, %2 = stream.async.dispatch @executable::@dispatch0[%c1, %c1, %c1](%arg2[%c0 to %arg1 for %arg1]) : (!stream.resource<*>{%arg1}) -> !stream.resource<*>{%arg1}, !stream.resource<*>{%arg1}
// CHECK: stream.yield %[[VAL]]#0, %[[VAL]]#1
stream.yield %1, %2, %1 : !stream.resource<*>{%arg1}, !stream.resource<*>{%arg1}, !stream.resource<*>{%arg1}
} => !stream.timepoint
util.return %0#0, %0#1, %0#2, %0#3 : !stream.resource<*>, !stream.resource<*>, !stream.resource<*>, !stream.timepoint
}


// -----

// CHECK-LABEL: @ChainAsyncExecuteWaits
Expand Down
Loading