Repository navigation
[Stream] Preserve reusable transfers during copy elision - #25192
purnima-nlp wants to merge 3 commits into
Conversation
Signed-off-by: purnima <[email protected]>
45e4764 to
056416b
Compare
|
I’ve updated the commit with the required DCO sign-off. That test runs only the So this failure appears likely unrelated/flaky. |
|
| auto resultType = cast<IREE::Stream::ResourceType>(result.getType()); | ||
|
|
||
| // An explicit storage policy must remain attached to its transfer. | ||
| if (transferOp.getStorageAttr()) { |
There was a problem hiding this comment.
I think here we should again not have a change for "copy" or the default case, the only thing new we need to handle is reuse. So check here via getEffectiveStorage and keep the original behavior for copy
| // topology information to determine if they're needed. | ||
| if (!topologyAllowsElision && transferOp.getSourceAffinityAttr() != | ||
| transferOp.getResultAffinityAttr()) { | ||
| if (!topologyAllowsReuse && transferOp.getSourceAffinityAttr() != |
There was a problem hiding this comment.
NIT: Not changed here but I think we should use the inferred source affinity here and not rely only on the set affinity attributes, we infer it from the source above.
| << " + same-type transfer (true no-op); can always elide\n"); | ||
| return true; | ||
| LLVM_DEBUG(llvm::dbgs() << " + same-type transfer can share storage\n"); | ||
| return topologyAllowsReuse ? TransferElisionAction::Reuse |
There was a problem hiding this comment.
Could we also protect clone elision when retaining a reusable transfer? The transfer result having one SSA use does not prove that its storage has no other observers.
For example (schematic; X is still live, and the mutation overlaps its range):
X = produce(on=A)
Y = transfer(X, A→B)
Z = clone(Y, on=B)
changed = mutate(Z)
return X, changed
Previously, eliding the transfer replaced Y with X, exposing both uses of X; the protective clone survived. With this change, Y remains as a separate SSA value with storage=reuse. The existing isSafeToElideCloneOp shortcut at lines 950–956 sees that Y is used only by the clone and removes it. Once allocation honors reuse, the mutation then also affects the returned X.
This is an existing proof/analysis gap exposed by retaining the transfer result, rather than a historical transfer-isolation guarantee being removed. The original transfer contract permits sharing; the snapshot guarantee comes from the clone. Current lowering still copies, so this is not a demonstrated current runtime corruption.
For a scoped conservative fix, could we add a source-provenance query to ElisionAnalysis and retain the clone when a transfer may hide aliases? The existing Explorer can follow casts/tied values, region or block forwarding, and calls:
bool mayAliasThroughTransfer(Value value) {
bool foundTransfer = false;
auto traversalResult = explorer.walkDefiningOps(
value, [&](OpResult result) {
// Keep existing import ownership/borrowed-storage handling.
if (isa<IREE::Stream::TensorImportOp>(result.getOwner())) {
return WalkResult::skip();
}
if (isa<IREE::Stream::AsyncTransferOp>(result.getOwner())) {
foundTransfer = true;
return WalkResult::interrupt();
}
return WalkResult::advance();
});
return foundTransfer ||
traversalResult == TraversalResult::INCOMPLETE;
}Then in isSafeToElideCloneOp, after checking the lifetimes and before any successful elision shortcut:
if (analysis.mayAliasThroughTransfer(cloneOp.getSource())) {
LLVM_DEBUG(llvm::dbgs()
<< " - clone source may alias through a transfer; "
"cannot elide\n");
return false;
}The placement matters: guarding only hasOneUse() still leaves the SSA last-user shortcut, and the read-only shortcut can also miss mutations through aliases. Checking only the immediate defining op misses forwarded values.
Cases to cover:
- Unannotated transfer that this pass marks
reuse, followed by a clone and partial mutation, with the original source also returned. - The same pattern with explicit
storage=reuse. - A tied cast between the transfer and clone.
- Private-function forwarding between the transfer and clone.
- Incomplete provenance: preserve the clone when safety cannot be established.
I tested an equivalent prototype: the first four cases lose the clone on this PR and retain it with the guard; all five relevant existing/new test files pass, including owned-import and SCF coverage.
This deliberately retains some safe clones until a more precise alias-aware proof is available. The later optimization can legally move the clone or fuse a sole-consumer reuse transfer → clone into a copy transfer, provided snapshot preservation and the required handoff remain intact. Adding a flush alone cannot repair lost snapshot isolation.
There was a problem hiding this comment.
thanks a lot for your guidance and feedback,
Updated based on your feedback:
copy/default keeps the old elision behavior; onlyreusegets the new preservation path.- source affinity now uses the inferred value.
- clone elision is conservatively blocked when provenance may pass through a transfer.
Added coverage for explicit/topology-selected reuse, forwarding/casts, incomplete provenance, and copy behavior. Focused test and the Stream transform tests pass.
i'm grateful for learning so much from you.
Signed-off-by: purnima <[email protected]>
Signed-off-by: purnima <[email protected]>
|
I have fixed the lint failures, may we run the workflow? |
This is part 2 of #24930, which tracks the multi-device zero-copy path for Stream transfers.
Part 1 introduced the
storagepolicy onstream.async.transfer. This patch implements the next step : preventing topology-based copy elision from removing transfers that still need to represent a cross-device handoff.what used to happen was : unified memory or transparent access could cause
ElideAsyncCopiesto replace the transfer result with its source and erase thestream.async.transferentirely.Now, when topology allows the source storage to be shared safely across devices, the transfer is saved and marked with
storage = reuseinstead. This keeps the transfer result and affinity handoff explicit for later lowering while avoiding an unnecessary data copy.Explicit
storage = copy/storage = reusepolicies are preserved. Same-device no-op transfers can still be elided, while non-unified and lifetime-changing transfers remain unchanged.Tests cover : unified memory, transparent access, explicit storage policies, transfer chains, mixed topology, and inferred source affinity.