Skip to content

[Stream] Prevent CSE from aliasing dispatch-allocated buffers#23665

Open
jtuyls wants to merge 1 commit intoiree-org:mainfrom
jtuyls:fix/stream-dispatch-cse-aliasing
Open

[Stream] Prevent CSE from aliasing dispatch-allocated buffers#23665
jtuyls wants to merge 1 commit intoiree-org:mainfrom
jtuyls:fix/stream-dispatch-cse-aliasing

Conversation

@jtuyls
Copy link
Copy Markdown
Contributor

@jtuyls jtuyls commented Mar 5, 2026

AsyncDispatchOp was declared as Stream_PureOp (NoMemoryEffect), which let CSE merge identical dispatches that produce non-tied resource results (new allocations). When two consumers each need their own independent buffer (e.g. two ScatterElements writing into separate zero-initialized buffers), merging makes them alias the same buffer and corrupt each other.

This PR fixes it by implementing MemoryEffectsOpInterface on AsyncDispatchOp. The new getEffects() reports MemoryEffects::Allocate when any result is a non-tied resource (i.e. a fresh buffer allocation). This is conservative and prevents CSE from merging such dispatches while still allowing CSE for dispatches where all results are tied.

Copy link
Copy Markdown
Collaborator

@MaheshRavishankar MaheshRavishankar left a comment

Choose a reason for hiding this comment

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

This makes sense to me.

@hanhanW or @benvanik do you guys have any feedback here?

@@ -0,0 +1,143 @@
// Tests that two scatter ops consuming the same fill-initialized buffer each
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you move this to tests/e2e/regression/ since this is not an op test but rather testing general handling of a program.

Copy link
Copy Markdown
Contributor

@hanhanW hanhanW left a comment

Choose a reason for hiding this comment

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

This needs @benvanik's eyes because we made the flip 6 months ago:

They were for fixing duplicated globals, so I'll need to test again. We don't have in-tree tests because it was hard to add. Perhaps I can come up with a pipeline test today.

@jtuyls jtuyls force-pushed the fix/stream-dispatch-cse-aliasing branch from 0e65f7a to 9048997 Compare March 6, 2026 08:14
AsyncDispatchOp was declared as Stream_PureOp (NoMemoryEffect), which
let CSE merge identical dispatches that produce non-tied resource results
(fresh allocations). When two consumers each need their own independent
buffer (e.g. two ScatterElements writing into separate zero-initialized
buffers), merging makes them alias the same buffer and corrupt each
other.

Fix: implement MemoryEffectsOpInterface on AsyncDispatchOp. The new
getEffects() reports MemoryEffects::Allocate when any result is a
non-tied resource (i.e. a fresh buffer allocation). This prevents CSE
from merging such dispatches while still allowing CSE for dispatches
where all results are tied (pure in-place transformations).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Jorn <jorn.tuyls@gmail.com>
@jtuyls jtuyls force-pushed the fix/stream-dispatch-cse-aliasing branch from 9048997 to 0199972 Compare March 6, 2026 09:32
@bjacob bjacob removed their request for review March 24, 2026 14:22
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.

3 participants