Skip to content

[Stream] Preserve reusable transfers during copy elision - #25192

Open
purnima-nlp wants to merge 3 commits into
iree-org:mainfrom
purnima-nlp:stream-transfer-elision-semantics
Open

purnima-nlp wants to merge 3 commits into
iree-org:mainfrom
purnima-nlp:stream-transfer-elision-semantics

Conversation

@purnima-nlp

Copy link
Copy Markdown
Contributor

This is part 2 of #24930, which tracks the multi-device zero-copy path for Stream transfers.

Part 1 introduced the storage policy on stream.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 ElideAsyncCopies to replace the transfer result with its source and erase the stream.async.transfer entirely.

Now, when topology allows the source storage to be shared safely across devices, the transfer is saved and marked with storage = reuse instead. This keeps the transfer result and affinity handoff explicit for later lowering while avoiding an unnecessary data copy.
Explicit storage = copy / storage = reuse policies 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.

@purnima-nlp
purnima-nlp force-pushed the stream-transfer-elision-semantics branch from 45e4764 to 056416b Compare November 4, 2026 14:31
@purnima-nlp

Copy link
Copy Markdown
Contributor Author

I’ve updated the commit with the required DCO sign-off.
Could someone please rerun the failed Windows CI check?
The previous windows_x64_msvc run failed only on:
iree/compiler/Codegen/LLVMCPU/test/tile_and_fuse_producer_consumer_anchoring_root_op.mlir.test

That test runs only the iree-llvmcpu-tile-and-fuse-producer-consumer pass plus CSE and does not exercise the Stream copy-elision path changed by this PR. The Stream tests modified by this PR also passed in that Windows run.

So this failure appears likely unrelated/flaky.

@purnima-nlp

purnima-nlp commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

May we run the test suit again, In previous some checks got cancelled. Infra issue I think

auto resultType = cast<IREE::Stream::ResourceType>(result.getType());

// An explicit storage policy must remain attached to its transfer.
if (transferOp.getStorageAttr()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() !=

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot for your guidance and feedback,
Updated based on your feedback:

  • copy/default keeps the old elision behavior; only reuse gets 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.

@purnima-nlp

purnima-nlp commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

I have fixed the lint failures, may we run the workflow?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants