Skip to content

datadeps: NoAliasing arguments no longer share one slot - #792

Closed
AnnaLena77 wants to merge 1 commit into
JuliaParallel:masterfrom
AnnaLena77:pr/a5-noaliasing-slots
Closed

AnnaLena77 wants to merge 1 commit into
JuliaParallel:masterfrom
AnnaLena77:pr/a5-noaliasing-slots

Conversation

@AnnaLena77

Copy link
Copy Markdown
Collaborator

The Datadeps slot cache (aliased_object!) is keyed by aliasing identity, and every NoAliasing value compares equal, so the second distinct NoAliasing argument of a region was handed the first one's slot: a task received another task's value (no error, wrong data). A NoAliasing value has no aliasing to preserve, so it now gets its own copy instead of going through the cache (4 lines in src/datadeps/aliasing.jl).

Regression test test/mpi/noaliasing_args.jl (included from test/mpi/cpu.jl): a type whose aliasing is NoAliasing, one value per task, two partitions per rank, InOut buffers record which value each task saw. At 2 ranks x 2 threads (MPICH, Julia 1.13) both assertions fail on current master and pass with this change; the whole CPU MPI suite passes with it (1131 / 1134 assertions, both ranks). I could not reproduce it with Distributed workers alone (strings and a custom type with a move method did not trigger it), so the test is an MPI test.

AI disclosure: I prepared this change with the help of Claude (Anthropic) and reviewed it myself.
🤖 Generated with Claude Code

The slot cache (aliased_object!) is keyed by aliasing identity, and every
NoAliasing value is equal, so the second distinct NoAliasing argument of a
region was handed the first one's slot: the task that should have received its
own value received another one's. A NoAliasing value has no aliasing to
preserve, so it now gets its own copy instead of going through the cache.

Regression test (test/mpi/noaliasing_args.jl, added to test/mpi/cpu.jl): a type
whose aliasing is NoAliasing, one value per task, two partitions per rank,
InOut buffers record which value each task saw. On upstream master at 2 ranks x
2 threads (MPICH) both assertions fail; with this change both pass.
@jpsamaroo

Copy link
Copy Markdown
Member

I'm not sure I understand the need for this change - NoAliasing implies that the data cannot alias anywhere, ever, so whether it shares a slot with another NoAliasing object should be irrelevant. Claude Opus also indicates that it should be impossible for a NoAliasing object to enter aliased_object!, as type_may_alias should be false and thus bypass it entirely.

@AnnaLena77

AnnaLena77 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator Author

I'm not sure I understand the need for this change - NoAliasing implies that the data cannot alias anywhere, ever, so whether it shares a slot with another NoAliasing object should be irrelevant. Claude Opus also indicates that it should be impossible for a NoAliasing object to enter aliased_object!, as type_may_alias should be false and thus bypass it entirely.

Thanks for looking at this. For the built-in types you're right, String, Symbol and Type have type_may_alias == false and never get to aliased_object!.

What I ran into is a user type that says aliasing(x) = NoAliasing() but still holds mutable data, like

struct NoAliasVal; part::Int; v::Vector{Float64}; end
Dagger.aliasing(::NoAliasVal) = Dagger.NoAliasing()

type_may_alias doesn't look at aliasing, only at the field types, so for this type it's true and the argument does go through aliased_object!. There the slot cache is keyed by the aliasing info, and all NoAliasing() are equal, so two different values end up with the same key. The second one then gets the slot of the first, which holds the first value. So the task silently receives another task's data.

I don't think sharing the slot is irrelevant here, since the slot contains the value itself. NoAliasing says the objects don't share memory, not that they're the same object.

I found it in an application where each task got a different NoAliasing value in one region and one of them saw another partition's data. The test in test/mpi/noaliasing_args.jl reproduces it. It fails on current master and passes with the fix. The fix itself is just the 4-line check in aliased_object!, so these values get their own copy instead of going through the cache.

@AnnaLena77

Copy link
Copy Markdown
Collaborator Author

@jpsamaroo @fda-tome
Following up on our meeting, here is the concrete case behind this PR and a small reproducer.

Where it came from.
We use Dagger in our code running over MPI ranks. The force computation is split into partitions, and each partition produces a Vector of result tiles. Those vectors are then passed to writer tasks, one per partition, inside a single spawn_datadeps region. The tiles are private to their partition and nothing else points at them, so we declared

Dagger.aliasing(::Vector{TileOut}) = Dagger.NoAliasing()

With several partitions in flight, writer 1 got the tiles of partition 2, and writer 3 the tiles of partition 4. There was no error, the results were just wrong. Plain @spawn writers outside the region were fine, so it wasn't the data transfer.

What was going on.
TileOut holds mutable data, so type_may_alias is true and the argument goes through aliased_object!. The slot cache there is keyed by the aliasing info, and all NoAliasing() compare equal, so the second distinct value got the slot of the first one.

Reproducer. No MPI and nothing from our fork, only upstream functions:

using Dagger

struct Part
    id::Int
    data::Vector{Float64}
end
Dagger.aliasing(::Part) = Dagger.NoAliasing()

record!(out::Vector{Float64}, p::Part) = (out[1] = p.id; nothing)

parts = [Part(i, fill(Float64(i), 4)) for i in 1:4]
outs  = [zeros(1) for _ in 1:4]
Dagger.spawn_datadeps() do
    for i in 1:4
        Dagger.@spawn record!(InOut(outs[i]), parts[i])
    end
end
[Int(o[1]) for o in outs]   # task i should see part i

On current master (d74b4c5, julia -t 4) this gives [1, 1, 1, 1] in every run. With this PR it gives [1, 2, 3, 4].

I'm happy to change the approach if you'd rather fix it somewhere else, for example on the type_may_alias side like in #794. The part I care about is that distinct NoAliasing values keep their own data.

@fda-tome

Copy link
Copy Markdown
Collaborator

Thanks so much! Squashed into a single commit and merged as part of #804.

@fda-tome fda-tome closed this Nov 27, 2026
fda-tome pushed a commit to AnnaLena77/Dagger_AL.jl that referenced this pull request Nov 27, 2026
…l#792)

* datadeps: NoAliasing arguments no longer share one slot
AnnaLena77 pushed a commit to AnnaLena77/Dagger_AL.jl that referenced this pull request Nov 29, 2026
On PRs JuliaParallel#792 and JuliaParallel#793, both merged essentially unchanged, reviewers
returned 6 and 7 `concern` verdicts. `concern` meant anything from
"silently loses buffers" to "a comment names wait_all imprecisely",
so the verdict table could not tell the maintainer what to look at.

Problem claims now carry `blocker`, `should-fix` or `nit`, and the
worst supported severity fixes the verdict (blocker -> fail,
should-fix -> concern, nits only -> pass). A category whose only
findings are nits now passes instead of reading as a concern. The task
rules also say that a weakness predating the change is at most a nit.

Schema version 2; reports written for version 1 must be redone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
AnnaLena77 pushed a commit to AnnaLena77/Dagger_AL.jl that referenced this pull request Nov 29, 2026
The PR JuliaParallel#792 and JuliaParallel#793 briefs repeated one defect under every category
that noticed it (the unfreed NoAliasing copies three times, the removed
in_task() guard three times), clipped each repetition at ~300
characters, and put the result in "Read these" lists of 49 and 73
lines. The maintainer had to reconstruct which items were the same
issue before deciding anything.

A consolidation step (`review.py findings`, fragments/_findings.json)
now groups problem claims by the defect they describe; submit requires
every problem claim to be in exactly one finding. A finding carries
the worst severity of its claims and says how many reviewers raised it
independently, which is evidence that it is real rather than noise.

The brief now opens with at most three blocker/should-fix findings,
each with a plain-words title and the merged lines to read; further
findings follow the verdict table, and nits are counted, not listed
(`render --nits`; folded in brief.md). Categories that are unverified
only because their measurement was never collected, and that raised
nothing, share one UNMEASURED line instead of one line each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
AnnaLena77 pushed a commit to AnnaLena77/Dagger_AL.jl that referenced this pull request Nov 29, 2026
On PR JuliaParallel#792 the maintainer's first reaction was to ask why the change
was needed at all, since NoAliasing values should never reach
aliased_object!. The review had the answer (type_may_alias looks at
field types, not at `aliasing`, so such values do reach it), but only
inside the claims backing a Necessity `pass`, where nobody reads.

Fragments may now `explain` up to two likely questions, each answer
backed by the indices of that fragment's measured or traced claims
(the validator checks), and the brief shows up to three under
"You might ask". The Necessity reviewer is told to always answer
"Why is this needed?".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
AnnaLena77 pushed a commit to AnnaLena77/Dagger_AL.jl that referenced this pull request Nov 29, 2026
Reviewers of PR JuliaParallel#793 flagged removing an `in_task()` guard from
`execute!(::MPIProcessor)` as a regression in three categories, because
`execute!` is listed in the manual and nothing says it is only called
inside a task. The maintainer had asked for that removal. Rules like
this live only in the maintainer's head or in review comments, so
every fresh reviewer rediscovers them as bugs.

The new Invariants section holds such rules, starting with that one;
the review tooling will hand it to every reviewer verbatim. Lesson 99
records why NoAliasing values still reach aliased_object! (the
maintainer's own first reading of PR JuliaParallel#792 was that they could not).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
jpsamaroo pushed a commit that referenced this pull request Nov 30, 2026
Since #792, aliased_object! returned early for NoAliasing, so the copy made
for another memory space never entered the object cache and region teardown
(which frees the cache's non-original entries) no longer freed it. And an
identity-less aliasing that is not NoAliasing itself, such as the empty
CombinedAliasing of a struct whose fields are all NoAliasing, still shared
one cache key, so a distinct argument could get another one's slot.

Add aliasing_has_identity (false for NoAliasing, wrappers of it, and empty
CombinedAliasing; dispatchable for user aliasing types). Such values bypass
the keyed lookup, and a copy made for another space is recorded under a
fresh CopyKey, so teardown frees it while originals stay untouched.

Fixes #822.
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