Skip to content

Datadeps: free copies of identity-less arguments again, and give each its own slot - #833

Merged
jpsamaroo merged 5 commits into
JuliaParallel:masterfrom
AnnaLena77:pr/f1-noaliasing-free
Nov 30, 2026
Merged

jpsamaroo merged 5 commits into
JuliaParallel:masterfrom
AnnaLena77:pr/f1-noaliasing-free

Conversation

@AnnaLena77

Copy link
Copy Markdown
Collaborator

Category 1 (bug fix), regression repair: This fixes two defects in how Datadeps' aliased-object cache treats arguments whose aliasing carries no identity. It changes no public API.

Fixes #822. This is a regression of our own PR #792 (merged in #804), and I am sorry for it.

Problem

#792 made aliased_object! return early for NoAliasing, so that two distinct NoAliasing arguments no longer share one cache slot. Two consequences, both reported in #822:

  1. The copy made for another memory space never entered the cache. Region teardown frees the copies by walking the cache, so it no longer freed them. This matters only where unsafe_free! does something (isbits-element CPU arrays moved through libc_backed, and GPU arrays), as noted in the discussion on the issue; those copies now wait for GC.
  2. An identity-less aliasing that is not NoAliasing itself still shared one cache key. The default aliasing of struct Outer; x::NoAliasVal; end is an empty CombinedAliasing, and all of those compare and hash equal, so a distinct Outer argument could receive another one's slot (wrong data, no error).

Change

  • New predicate aliasing_has_identity(::AbstractAliasing): true by default, false for NoAliasing, forwarded by AliasingWrapper, and for CombinedAliasing true only if some sub-aliasing has identity (so an empty one has none). User-defined aliasing types can add a method.
  • aliased_object! sends identity-less values to a separate path. It does not look anything up by aliasing. It calls the mover, and if the result is a new copy in another space (not the argument itself, and not data already in the destination space) it records it under a fresh CopyKey (a counter on the cache store). Copies are therefore freed at region end like every other copy, originals are never freed, and distinct values never share a slot. The same object used twice in a region still shares its copy, because the slot is already memoized per argument and space before the cache is reached.
  • resolve_pending! does not add derived entries for CopyKey copies, since nothing looks them up by aliasing.
  • No new switches or options. The MPI path needs no extra hook: the new cache function unwraps the store locally when its owner is this process, as the existing MPI methods do, and key ids come from the store, which sees the same sequence of calls on every rank.

Testing

What I ran, locally:

  • New testset in test/datadeps.jl ("Identity-less aliasing: own slot per value, copies freed"), run as a standalone script with two Distributed workers: three distinct NoAliasing values and three distinct Outer wrappers are moved to another worker in one spawn_datadeps region; each task sees its own value, and the number of unsafe_free! tasks in the logs equals the number of copies (it was 3 of 6 without the fix's registration). Plus unit checks for the predicate. It passes.
  • Mutation checks: removing the cache registration makes the free-count assertions fail (3 == 6); making empty CombinedAliasing count as having identity again makes the Outer data checks fail ([3,3,3] instead of [1,2,3]).
  • datadeps: NoAliasing arguments no longer share one slot #792's MPI test (test/mpi/noaliasing_args.jl): passes on MPICH, 2 ranks x 2 threads (7 assertions).
  • --test mutation --test memory-spaces at -p 1 -t 2 (71 and 22 pass), and the full --test datadeps at -p 2 -t 2 (2142 pass, 2 known broken).

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

Since JuliaParallel#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 JuliaParallel#822.
jpsamaroo and others added 2 commits November 28, 2026 21:20
…d its slot

Review finding 1 on PR JuliaParallel#833 (blocker): Copies of NoAliasing arguments are freed at region end without waiting for the tasks reading them (use-after-free)
Requested: Make the region-end free of each CopyKey copy wait for every task that used that slot, by porting PR JuliaParallel#834's per-slot user list (UnkeyedSlot / add_unkeyed_user! / free_unkeyed_copies!, branch jps/review/pr792 commit af59cd7) or an equivalent wait attached to the CopyKey free. Keep it rank-uniform under MPI.

The region-end free loops take a buffer's syncdeps from dependency
tracking, which orders nothing on bare NoAliasing (get_write_deps!
returns at once). So the unsafe_free! of a CopyKey copy waited for
nothing and ran while the copy's readers were still running.

This ports JuliaParallel#834's per-slot user list onto this PR's CopyKey design,
rather than adding a second free loop:
- generate_slot! attributes each CopyKey copy made while building a slot
  to that slot (a leaf argument's slot, or a wrapper slot's children), by
  scanning the cache keys appended during the call.
- add_reader!, which every task touching a slot passes through (writers
  and copy tasks included), records the task as a user of such a slot.
- Both planners' existing free loops add the slot's users to a CopyKey
  copy's syncdeps (push_copy_users_syncdeps!). The hierarchical planner's
  shared-owner syncdep, the free-once check and the sorted order still
  apply. A CopyKey copy with no recorded slot is an error, not a free
  with no syncdeps.

CopyKey ids, slots and users are all recorded in planning order, which
is the same on every rank, so the free syncdeps are rank-uniform.

The bookkeeping (UnkeyedSlots) is created lazily, on a region's first
CopyKey copy. DataDepsState holds its (always local) cache store
directly, so generate_slot! checks it without a MemPool lookup. The
common path pays a length read and a short isa scan per slot, and a
`nothing` check per reader. One region over plain Vectors (8 InOut and
7 two-In tasks; -t1, 15 warmup regions, minimum of 12, 3 fresh
processes, Julia 1.12.6): 4600-4602 allocations / 175,440-175,632 bytes
with this commit, 4695-4697 / 178,224-178,416 on the PR head. No
increase.

Verified:
- The next commit's test fails in all 6 slow-read rounds (both planners)
  without this commit, and passes 12/12 with it.
- --test datadeps (1555 pass, 2 broken) and --test allocations (25/25)
  pass, Julia 1.13.1, 3 workers.
- The 2-rank MPI CPU suite (test/mpi/cpu.jl, including
  test/mpi/noaliasing_args.jl) passes: 1143 and 1146 per rank.
- A 2-rank MPI probe with uniformity checks on (a NoAliasing isbits
  Vector built on rank 0, read by two slow tasks on rank 1, both planners,
  3 rounds each) sees every copy allocated throughout its reads and freed
  after the region.

Written by an AI agent on jpsamaroo's review feedback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
Review finding 2 on PR JuliaParallel#833 (should-fix): The new free test cannot detect an early free: it counts free tasks for payloads whose free does nothing
Requested: As reported: make the free test detect an early free, using a payload whose free is observable (Libc-backed isbits array) read by a deliberately slow task on another worker; it must fail without the F1 fix.

The existing free test counts unsafe_free! tasks for plain-struct
payloads, whose free is a no-op, and its tasks read without waiting. So
it passes while the free runs under the reader.

The new testset's payload has an observable free: a Vector of an isbits
struct declared NoAliasing. It is built on the driver and read (In) by
two tasks on another worker. Each task checks
Dagger.is_libc_allocated before and after a 0.5 s sleep. Datadeps'
copy on the worker is Libc-backed, so an early unsafe_free! makes
is_libc_allocated false. After the region, the test checks that every
copy was freed. It runs under both planners, 3 rounds each.

Verified (Julia 1.12.6, 3 workers, only this testset): with the
previous commit's src changes reverted locally (not committed), the
reader assertion fails in all 6 rounds (6 passed, 6 failed). With them,
12/12 pass. It also passes in the full --test datadeps run.

Written by an AI agent on jpsamaroo's review feedback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.46835% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.28%. Comparing base (d3bd866) to head (6ea5f7b).
⚠️ Report is 12 commits behind head on master.

Files with missing lines Patch % Lines
src/datadeps/aliasing.jl 97.40% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #833      +/-   ##
==========================================
+ Coverage   63.91%   64.28%   +0.37%     
==========================================
  Files         126      126              
  Lines       20119    20153      +34     
==========================================
+ Hits        12859    12956      +97     
+ Misses       7260     7197      -63     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Use master's ownership check before recording an identity-less slot for
region-end freeing. Keep both sets of Datadeps tests and exercise a
cross-space identity move alongside a genuine copy.

Written by an AI agent on jpsamaroo's review feedback.
Keep the regression tests for identity-less copy ownership and lifetime
alongside the wrapper and sparse-container tests added by JuliaParallel#840.

Written by an AI agent on jpsamaroo's review feedback.
@jpsamaroo
jpsamaroo merged commit b720747 into JuliaParallel:master Nov 30, 2026
51 of 54 checks passed
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.

Datadeps: copies of NoAliasing arguments are no longer freed at region end (since #792)

2 participants