Skip to content

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

Description

@jpsamaroo

Since #792 (merged in #804), Datadeps no longer frees the copies it makes of NoAliasing arguments when a region ends. The copies stay allocated until garbage collection reclaims them, which can take a long time for device memory or for buffers on remote workers or MPI ranks.

What happens

#792 added an early return to aliased_object! so that distinct NoAliasing values no longer share one cached slot (src/datadeps/aliasing.jl):

(ainfo isa NoAliasing || (ainfo isa AliasingWrapper && ainfo.inner isa NoAliasing)) && return f(x)::Chunk

When the argument has to move to another memory space, f(x) makes a new chunk there (move_rewrap → tochunk(libc_backed(move(...)), to_proc, to_space)). Because of the early return, that copy is never recorded in the aliased-object cache.

At the end of a region, Datadeps frees the copies it made by walking the cache (src/datadeps/queue.jl, the loop over obj_cache.values that spawns unsafe_free! for every non-original entry). A copy that never entered the cache is never freed there.

Before #792, the first NoAliasing copy was cached (wrongly shared with later values, which was the bug #792 fixed) and was freed at region end. So this lifetime change came with #792. It is not pre-existing.

Related: one identity-less case still collides

The default aliasing(x::T) skips NoAliasing fields. For a struct whose only aliasing-relevant fields are NoAliasing, it returns an empty CombinedAliasing, and all empty CombinedAliasing values compare and hash equal (src/memory-spaces.jl). Given #792's test type NoAliasVal, a wrapper such as struct Outer; x::NoAliasVal; end still passes type_may_alias and still hits one shared cache key. A distinct Outer argument can therefore still receive another one's slot.

Possible fix

Both cases come from using the aliasing value as the cache key when it carries no identity. Keying such values by object identity (or giving AbstractAliasing a dispatchable "has no identity" predicate, which an empty memory_spans also satisfies) would keep each value's slot separate and register the copy, so region teardown frees it again. It would also cover user-defined identity-less aliasing types.

How this was found

By reading the code during a review of #792; not yet reproduced by running it. A reproducer would move two distinct NoAliasing values across memory spaces in one spawn_datadeps region (for example to a second Distributed worker or a GPU) and check that their destination chunks are freed when the region ends.

Activity

  1. jpsamaroo commented on Nov 28, 2026

    @jpsamaroo
    MemberAuthor

    Correction on scope: this matters less widely than the description implies.

    Datadeps' region-end free is unsafe_free!(chunk), which unwraps the chunk and frees the value only if it is a type Dagger knows how to release (src/memory-spaces.jl):

    • a CPU Array with an isbits element type (Datadeps' move_rewrap copies these into Libc-backed memory via libc_backed, so they can be freed eagerly);
    • a GPU array (CuArray, ROCArray, MtlArray, oneArray, CLArray, through the GPU extensions).

    For every other value, unsafe_free!(x) = nothing. That includes a struct like #792's test type NoAliasVal (a struct holding a Vector), so for such values nothing was ever freed eagerly, before or after #792.

    So the lost eager free affects only users who declare aliasing(...) = NoAliasing() on an isbits-element Array type or a GPU array type, for example Dagger.aliasing(::Vector{TileOut}) = Dagger.NoAliasing() with an isbits TileOut. For those, cross-space copies now wait for GC; for everything else this part of the issue changes nothing. The second point (an empty CombinedAliasing still sharing one slot) is about correctness, not memory, and stands as written.

  2. AnnaLena77 commented on Nov 28, 2026

    @AnnaLena77
    Collaborator

    Thanks for the careful read, and you are right on both points: the lost eager free is a regression from #792, and the empty CombinedAliasing case still shares one slot.

    One thing that may help with the scope: the case in your correction, Dagger.aliasing(::Vector{TileOut}) = Dagger.NoAliasing() with an isbits TileOut, is exactly what one of our applications does, so we do hit it (the copies of those tile vectors now wait for GC).

    I opened #833, which keys such values by identity and registers the copy, so that the region teardown frees it again and distinct identity-less values (including the Outer wrapper) keep separate slots. It comes with a test that moves distinct NoAliasing values and Outer wrappers to another worker and checks that each task sees its own value and that all copies are freed when the region ends.

  3. added a commit that references this issue on Nov 30, 2026
    a9d3a0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions