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

Support cloning operand-less dispatches #20025

Open
wants to merge 3 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
25 changes: 5 additions & 20 deletions compiler/src/iree/compiler/Dialect/Stream/Analysis/Affinity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,26 +723,11 @@ void OpAffinityPVS::initializeOperation(Operation *op, DFX::Solver &solver) {
ChangeStatus OpAffinityPVS::updateOperation(Operation *op,
DFX::Solver &solver) {
StateType newState;

const bool consumesAny = llvm::any_of(
op->getOperandTypes(), +[](Type type) {
return isa<IREE::Stream::AffinityTypeInterface>(type);
});
if (consumesAny) {
for (auto operand : op->getOperands()) {
if (isa<IREE::Stream::AffinityTypeInterface>(operand.getType())) {
auto valuePVS = solver.getElementFor<ValueProducerAffinityPVS>(
*this, Position::forValue(operand), DFX::Resolution::REQUIRED);
newState ^= valuePVS;
}
}
} else {
for (auto result : op->getResults()) {
if (isa<IREE::Stream::AffinityTypeInterface>(result.getType())) {
auto valuePVS = solver.getElementFor<ValueConsumerAffinityPVS>(
*this, Position::forValue(result), DFX::Resolution::REQUIRED);
newState ^= valuePVS;
}
for (auto operand : op->getOperands()) {
if (isa<IREE::Stream::AffinityTypeInterface>(operand.getType())) {
auto valuePVS = solver.getElementFor<ValueProducerAffinityPVS>(
*this, Position::forValue(operand), DFX::Resolution::REQUIRED);
newState ^= valuePVS;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ void PartitionSet::dump(AsmState &asmState) {}
LogicalResult Partition::verify(Location loc) {
// Ensure all ops are compatible with the partition affinity.
for (auto *op : ops) {
auto streamableOp =
dyn_cast_if_present<IREE::Stream::StreamableOpInterface>(op);
bool cloned = streamableOp && streamableOp.preferCloneToConsumers();
if (auto affinityOp = dyn_cast<IREE::Stream::AffinityOpInterface>(op)) {
if (!IREE::Stream::AffinityAttr::areCompatible(
affinity, affinityOp.getAffinityAttr())) {
if (!cloned && !IREE::Stream::AffinityAttr::areCompatible(
affinity, affinityOp.getAffinityAttr())) {
return op->emitError("op affinity ")
<< affinityOp.getAffinityAttr()
<< " is not compatible with the partition affinity " << affinity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ partitionStreamableOpsReference(IREE::Stream::PartitioningConfigAttr config,
IREE::Stream::AffinityAttr affinityAttr;
if (auto affinityOp = dyn_cast<IREE::Stream::AffinityOpInterface>(op))
affinityAttr = affinityOp.getAffinityAttr();
if (!IREE::Stream::AffinityAttr::canExecuteTogether(
bool preferCloneToConsumers = streamableOp.preferCloneToConsumers();
if (!preferCloneToConsumers &&
!IREE::Stream::AffinityAttr::canExecuteTogether(
affinityAttr, builders[partitionOrdinal]->affinity))
return false;

bool preferCloneToConsumers = streamableOp.preferCloneToConsumers();
llvm::BitVector *opHazards = nullptr;
llvm::BitVector opHazardsInCandidatePartition;
if (preferCloneToConsumers) {
Expand Down
18 changes: 18 additions & 0 deletions compiler/src/iree/compiler/Dialect/Stream/IR/StreamOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,15 @@ TensorDispatchOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
getWorkload(), symbolTable);
}

bool TensorDispatchOp::preferCloneToConsumers() {
for (auto operand : getMixedOperands()) {
if (isa<Stream::ResourceType>(operand.getType())) {
return false;
}
}
return true;
}

std::pair<unsigned, unsigned>
TensorDispatchOp::getTiedOperandsIndexAndLength() {
return getODSOperandIndexAndLength(1); // $operands
Expand Down Expand Up @@ -2644,6 +2653,15 @@ static void printDispatchOperands(OpAsmPrinter &p, Operation *op,
p << ")";
}

bool AsyncDispatchOp::preferCloneToConsumers() {
for (auto operand : getResourceOperands()) {
if (isa<Stream::ResourceType>(operand.getType())) {
return false;
}
}
return true;
}

LogicalResult AsyncDispatchOp::verify() {
AsyncDispatchOp op = *this;
if (failed(verifyOpValueSizes(op, op.getResourceOperands(),
Expand Down
8 changes: 6 additions & 2 deletions compiler/src/iree/compiler/Dialect/Stream/IR/StreamOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,9 @@ def Stream_TensorDispatchOp : Stream_Op<"tensor.dispatch", [
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
Stream_AffinityOp,
Stream_TensorPhaseOp,
Stream_StreamableOp,
DeclareOpInterfaceMethods<Stream_StreamableOp, [
"preferCloneToConsumers",
]>,
Util_SizeAwareOp,
DeclareOpInterfaceMethods<Util_TiedOpInterface, [
"getTiedOperandsIndexAndLength",
Expand Down Expand Up @@ -2493,7 +2495,9 @@ def Stream_AsyncDispatchOp : Stream_Op<"async.dispatch", [
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
Stream_AffinityOp,
Stream_AsyncPhaseOp,
Stream_StreamableOp,
DeclareOpInterfaceMethods<Stream_StreamableOp, [
"preferCloneToConsumers",
]>,
DeclareOpInterfaceMethods<Stream_AsyncAccessOp, [
"getAsyncAccessRanges",
]>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ struct ExecutePartitionBuilder {
if (affinityOp.getAffinityAttr() == partition->affinity) {
affinityOp.setAffinityAttr(nullptr);
}

auto streamableOp =
dyn_cast<IREE::Stream::StreamableOpInterface>(clonedOp);
if (streamableOp && streamableOp.preferCloneToConsumers()) {
affinityOp.setAffinityAttr(nullptr);
}
}

return true;
Expand Down
Loading