-
Notifications
You must be signed in to change notification settings - Fork 621
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
[compiler] strip execution context affinities in const eval #18663
base: main
Are you sure you want to change the base?
Changes from 2 commits
865a8bd
5a294e4
5b0910f
f032251
8b0fc95
bbd1d15
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 |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
|
||
#include "iree/compiler/ConstEval/Passes.h" | ||
#include "iree/compiler/ConstEval/Runtime.h" | ||
#include "iree/compiler/Dialect/Flow/IR/FlowOps.h" | ||
#include "iree/compiler/Dialect/HAL/Target/TargetOptions.h" | ||
#include "iree/compiler/Dialect/Stream/IR/StreamTypes.h" | ||
#include "iree/compiler/Dialect/Util/Analysis/Constant/ConstExpr.h" | ||
#include "iree/compiler/Dialect/Util/Analysis/Constant/OpOracle.h" | ||
#include "iree/compiler/Dialect/Util/IR/UtilOps.h" | ||
|
@@ -21,7 +23,9 @@ | |
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/IR/IRMapping.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/IR/SymbolTable.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
#include <cstdlib> | ||
|
||
|
@@ -448,6 +452,53 @@ static LogicalResult cloneUsedObjects(FunctionOpInterface funcOp, | |
return success(); | ||
} | ||
|
||
struct StripFlowTensorTransferPattern | ||
: public OpRewritePattern<IREE::Flow::TensorTransferOp> { | ||
using OpRewritePattern::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(IREE::Flow::TensorTransferOp op, | ||
PatternRewriter &rewriter) const override { | ||
rewriter.replaceAllUsesWith(op.getResult(), op.getOperand()); | ||
rewriter.eraseOp(op); | ||
return success(); | ||
} | ||
}; | ||
|
||
// If an op that implements AffinityOpInterface has an optional stream affinity | ||
// attribute, remove it. | ||
struct StripStreamAffinityOptionalAttributePattern | ||
: public OpInterfaceRewritePattern<IREE::Stream::AffinityOpInterface> { | ||
using OpInterfaceRewritePattern::OpInterfaceRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(IREE::Stream::AffinityOpInterface op, | ||
PatternRewriter &rewriter) const override { | ||
// Shouldn't we reject ops for which `op.requiresAffinity() == true`? | ||
// For example there are a lot of ops in the Flow dialect that | ||
// have this property, but do they really require an affinity? | ||
// See | ||
// compiler/src/iree/compiler/ExternalInterfaces/StreamExternalModels.cpp | ||
if (op.getAffinityAttr() == nullptr) { | ||
return failure(); | ||
} | ||
op.setAffinityAttr(nullptr); | ||
return success(); | ||
} | ||
}; | ||
|
||
// Remove device/queue affinities for the IR. | ||
// E.g. remove `flow.tensor.transfer` ops. | ||
static LogicalResult stripExecutionContextAffinities(ModuleOp moduleOp) { | ||
RewritePatternSet patterns(moduleOp->getContext()); | ||
patterns.add<StripFlowTensorTransferPattern, | ||
StripStreamAffinityOptionalAttributePattern>( | ||
moduleOp.getContext()); | ||
if (failed(applyPatternsAndFoldGreedily(moduleOp, std::move(patterns)))) { | ||
return emitError(moduleOp->getLoc()) | ||
<< "Stripping execution context affinities failed"; | ||
} | ||
return success(); | ||
} | ||
|
||
class ProgramBuilder { | ||
public: | ||
ProgramBuilder(ModuleOp sourceModuleOp, | ||
|
@@ -831,6 +882,10 @@ class JitGlobalsPass final : public impl::JitGlobalsPassBase<JitGlobalsPass> { | |
programBuilder.getTargetModule()->erase(); | ||
return; | ||
} | ||
if (failed(stripExecutionContextAffinities( | ||
programBuilder.getTargetModule()))) { | ||
return signalPassFailure(); | ||
} | ||
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. Seems like this code could instead go in 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. I moved it there. |
||
|
||
std::optional<llvm::Timer> compileTimer; | ||
if (debugEnabled) { | ||
|
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.
if you're confused when you're writing this then someone coming across this in the future will be doubly confused
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.
I removed the comment. I was not sure if removing this attribute when it is required may result in an illegal operation.
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.
I guess since we are attaching the interface to Flow ops, having the attribute removed, must not lead to illegality.