Skip to content

Only zero the non-GC-pointer bytes of Julia shadow allocations - #3881

Draft
vchuravy wants to merge 1 commit into
mainfrom
vc/julia-split-zeroing-shadow
Draft

vchuravy wants to merge 1 commit into
mainfrom
vc/julia-split-zeroing-shadow

Conversation

@vchuravy

@vchuravy vchuravy commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Follows #3426 (merged).

With #3426, the shadow of a julia.gc_alloc_obj carries the primal's julia.gc_alloc_ptr_offsets bundle. Julia's late-gc-lowering (julia#60924, Julia 1.13.4 and 1.14) then nulls the shadow's GC pointer fields right after the allocation. So Enzyme's memset only has to zero the bytes between them, such as the Float64 of a Tuple{Float64,Any}.

Change. zeroKnownAllocation (LibraryFuncs.h) takes a skipJuliaGCPointers flag. The reverse-mode and loop-rematerialized shadow allocations set it. With the flag, getJuliaNonGCPointerRanges computes the non-pointer byte ranges from the bundle, and one memset is emitted per range.

Nothing in Enzyme now writes the shadow's GC pointer fields before late-gc-lowering does. So the shadow must no longer keep the allockind("alloc,uninitialized") it copies from a fully initialized primal, or LLVM could fold a load of such a field to undef. The new dropUninitializedAllocKind removes it on the forward, reverse and loop-rematerialized shadows. Marking the shadow zeroed would be wrong instead: LLVM would then drop Enzyme's memset of the other bytes as redundant.

The whole shadow is still zeroed when:

  • the allocation has no ptr_offsets bundle, including older Julia and julia.gc_alloc_zeroinit;
  • its size or an offset is not constant, or an offset is outside the object;
  • the allocation is enzyme_fromstack, since an alloca replacement drops the bundle. Such allocations come from Enzyme's own CreateAllocation and have no bundle anyway, so this check is a safeguard;
  • a shadow inside a loop is re-zeroed on each iteration (loopShadowZeroInits).

Open points:

  • In ReverseModePrimal/Combined, Enzyme.jl's shadow_alloc_rewrite atomically stores nothing into the shadow's GC slots before this memset. With the full memset the slots ended up null; now they keep nothing. Primal fields that are never set (#undef) would then have a nothing shadow. Skip GC-field null stores when Julia zeroes the allocation itself Enzyme.jl#3766 skips those stores when the allocation carries a zeroing bundle. Julia's own zeroing at allocation is what they were standing in for. So this PR should land together with, or after, that one.
  • An object made only of GC pointers gets no memset from Enzyme. If Julia's AllocOpt moves it to the stack, the GC frame setup nulls it, as it would for the primal.

Tests. ReverseMode/julia_gc_alloc_zeroing_bundles.ll checks the memsets for a pointer at 0, a pointer at 8 (Tuple{Float64,Any}), and two adjacent pointers given out of order. Both tests check that the shadow drops uninitialized.

Locally, against LLVM 20, the lit suite has no new failures. On Julia 1.13.4, with libEnzyme built from this branch, these Enzyme.jl test files pass: basic, array, typeunstable, applyiter, mixedapplyiter, absint, sugar, rules/inactive_kwrules, runtime_calls, typetree, threads, callconv, core/abi and make_zero.

🤖 Generated with Claude Code

@gbaraldi

Copy link
Copy Markdown

I had written the same change before seeing this (oscardssmith#1, on top of #3632), and a review of it turned up something that applies here. It is safe here only because of the last commit of #3426, so it seems worth writing down.

Julia's AllocOpt drops a zeroing memset that starts mid-object. When splitOnStack rewrites a memset slot by slot, it starts at find_slot(offset), which returns the last slot at or before the memset's start, and then breaks if that slot ends before the memset begins (v1.13.4, llvm-alloc-opt.cpp#L1184-L1187; the same loop is on master). Slots only exist for fields that are read, so the slot before the memset can be a different field from the first one the memset covers, and the later slots are never reached.

Take a 24-byte object with a GC pointer at 0 that is loaded, a double at 8 that is never read, and an active double at 16. The memset this PR emits is [8, 24). find_slot(8) returns the pointer slot, which ends at 8, so the loop exits and the slot at 16 gets no zeroing. A memset of [0, 24) is rewritten correctly.

What happens next depends on the allocation kind:

  • With allockind("alloc,uninitialized") on the shadow, which it had while it copied the primal's attributes, nothing else zeroes that slot and the reverse pass accumulates into an undefined double.
  • With plain allockind("alloc"), as after 69b4bdc in Keep Julia allocation zeroing bundles on shadow allocations #3426, initializeAlloca zeroes every slot itself and the lost memset does not matter. I checked this on the same IR.

So this PR depends on that commit, not just on the bundles from #3426. A test that pins the shadow's allocation kind next to the partial memset, or a comment where the memset is split, would keep the two from drifting apart.

I reproduced this at the pass level only: AllocOptPass from Julia 1.13.4 through LLVM.jl on the IR below. I did not get a Julia function to hit it end to end.

IR for AllocOpt
declare noalias nonnull ptr addrspace(10) @julia.gc_alloc_obj(ptr, i64, ptr addrspace(10)) #0
declare ptr @julia.get_pgcstack()
declare void @llvm.memset.p10.i64(ptr addrspace(10), i8, i64, i1)

define double @f(ptr addrspace(10) %v, double %x) {
top:
  %pgc = call ptr @julia.get_pgcstack()
  %ct = getelementptr inbounds i8, ptr %pgc, i64 -152
  %obj = call noalias nonnull align 8 dereferenceable(24) ptr addrspace(10) @julia.gc_alloc_obj(ptr %ct, i64 24, ptr addrspace(10) addrspacecast (ptr inttoptr (i64 1234 to ptr) to ptr addrspace(10))) #1 [ "julia.gc_alloc_ptr_offsets"(i64 0) ]
  %o11 = addrspacecast ptr addrspace(10) %obj to ptr addrspace(11)
  %p8 = getelementptr inbounds i8, ptr addrspace(10) %obj, i64 8
  call void @llvm.memset.p10.i64(ptr addrspace(10) %p8, i8 0, i64 16, i1 false)
  store atomic ptr addrspace(10) %v, ptr addrspace(11) %o11 release, align 8
  %pb = getelementptr inbounds i8, ptr addrspace(11) %o11, i64 16
  %old = load double, ptr addrspace(11) %pb, align 8
  %new = fadd double %old, %x
  store double %new, ptr addrspace(11) %pb, align 8
  %r = load double, ptr addrspace(11) %pb, align 8
  %vv = load atomic ptr addrspace(10), ptr addrspace(11) %o11 unordered, align 8
  %c = icmp eq ptr addrspace(10) %vv, null
  %s = select i1 %c, double 0.0, double %r
  ret double %s
}
attributes #0 = { allockind("alloc") }
attributes #1 = { allockind("alloc,uninitialized") }

After AllocOpt the object is one alloca double that is loaded with nothing stored to it first. Changing #1 to #0 on the call, or the memset to [0, 24), gives a memset on the slot.

Two smaller things:

A shadow of a julia.gc_alloc_obj carries the primal's
julia.gc_alloc_ptr_offsets bundle, so Julia's late-gc-lowering nulls its GC
pointer fields right after the allocation. Enzyme only needs to zero the
bytes between them, such as the Float64 of a Tuple{Float64,Any}.

This applies when the allocation has a constant size and constant offsets
within it. Otherwise, for julia.gc_alloc_zeroinit, an allocation that may
become an alloca (enzyme_fromstack), and the re-zeroing of a shadow inside a
loop, the whole shadow is still zeroed.

Nothing in Enzyme now writes the shadow's GC pointer fields before
late-gc-lowering does, so the shadow must not keep the
allockind("alloc,uninitialized") it copies from a fully initialized primal:
LLVM could fold a load of such a field to undef. Drop it on the forward,
reverse and loop rematerialized shadows. "zeroed" would be wrong instead:
LLVM would then drop Enzyme's memset of the other bytes as redundant.

Assisted-by: Claude Code (Opus 5.5)
@vchuravy
vchuravy force-pushed the vc/julia-split-zeroing-shadow branch from c25e551 to 49cb4c9 Compare November 15, 2026 12:46

This branch has not been deployed

No deployments
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