Skip to content

Apply in-place rewrites only if there are no other uses - #304

Closed
simeonschaub wants to merge 2 commits into
JuliaGPU:mainfrom
simeonschaub:sds/fix_inplace_rewrite
Closed

Apply in-place rewrites only if there are no other uses#304
simeonschaub wants to merge 2 commits into
JuliaGPU:mainfrom
simeonschaub:sds/fix_inplace_rewrite

Conversation

@simeonschaub

Copy link
Copy Markdown
Member

Previously, the inplace rewrite rule would miscompile code like:

c = ct.arange(n)
mask = c .≤ lim
tile = ct.gather(a, c; mask)

since the gather would now see c after it has been rewritten

simeonschaub and others added 2 commits August 29, 2026 12:42
An inplace=true rewrite rule modifies the matched ops' operands instead
of building new instructions. That mutation is only sound when the match
is the single user of every op it mutates; any other user silently sees
the rewritten value.

The one such rule, the nested comparison strength reduction
cmpi(addi(a, addi(b, 1)), y, <=) -> cmpi(addi(a, b), y, <), miscompiles
kernels in which the 1-based index tile feeds both a mask comparison and
a gather or scatter: the shared addi chain is rewritten to 0-based for
the comparison while the gather's lowering still subtracts 1 from it,
shifting every gathered element down by one. Whether the bug fires
depends on worklist order: when the algebra cancellation
subi(addi(a, addi(b, c)), c) -> addi(a, b) consumes the gather's
subtraction first, the comparison is left as the chain's only user and
the mutation happens to be sound.

The driver now checks use counts before applying a rule in-place and
falls back to the standard mode -- fresh ops, only the root replaced --
when a sub-op that would be mutated is shared. The rewrite fires in both
cases; in-place application remains a worklist-efficiency detail. A
FileCheck regression test asserts that the strength-reduced mask
operand of a masked gather is never decremented again.

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
@maleadt

maleadt commented Aug 31, 2026

Copy link
Copy Markdown
Member

Let's just get rid of inplace mode, it doesn't seem necessary anymore, and there's issues beyond the one fixed here.

@simeonschaub

Copy link
Copy Markdown
Member Author

Yes, that was my first thought as well, but from reading the comments it looked to me like there were good reasons to use inplace there. If we can get rid of it after all, all the better

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