Skip to content

Keep Julia allocation zeroing bundles on shadow allocations - #3426

Merged
vchuravy merged 5 commits into
mainfrom
vc/julia-alloc-zeroing-bundles
Nov 15, 2026
Merged

vchuravy merged 5 commits into
mainfrom
vc/julia-alloc-zeroing-bundles

Conversation

@vchuravy

@vchuravy vchuravy commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Requested by Valentin

Needed for EnzymeAD/Enzyme.jl#4051. Follow-up: #3881.

Before: Julia 1.13.4 backported julia#60924, which 1.14 has had since 1.14.0-DEV.1697. Julia allocation calls now carry julia.gc_alloc_ptr_offsets, julia.gc_alloc_zeroinit or julia.gc_alloc_zeroinit_indirect operand bundles. Enzyme aborts with "unsupported tag" when it meets one in usedInRooting or getInvertedBundles. This breaks every Enzyme.jl test run on Julia 1.13.4. For example, Enzyme.gradient(Forward, Const(x -> sum([x][1])), [1.0, 2.0]) aborts with unsupported tag julia.gc_alloc_zeroinit. The reverse-mode shadow of a julia.gc_alloc_obj was also created without any bundles, so only the memset after the allocation covered its GC pointer fields.

After: these bundles pass through like gc-transition. Every shadow allocation of julia.gc_alloc_obj carries the primal's zeroing bundles, so late-gc-lowering nulls its GC pointer fields right after the allocation.

julia#60924 moved the null stores out of codegen because LLVM may sink or shorten them past a safepoint. The memset that Enzyme puts on a reverse-mode shadow has the same risk: later shadow stores overwrite the fields, and DSE can shorten it. The memset stays because the bundles only null the GC pointer fields. The rest of the shadow, such as the Float64 of a Tuple{Float64,Any}, must still be zeroed. #3881 narrows the memset to those bytes.

How: a new GradientUtils::getAllocationZeroingBundles returns the zeroing bundles of the original call, remapped, and looked up in loop rematerialization. The two places that build a shadow allocation without bundles now pass them: CallDerivatives.cpp and the loop rematerialization in GradientUtils.cpp. The tags are literal names, so older Julia, which never emits them, sees no change.

Companion Enzyme.jl PR: EnzymeAD/Enzyme.jl#3766 drops the explicit GC-field stores in shadow_alloc_rewrite for allocations that carry one of these bundles. It is not required for this PR.

Testing:

  • Lit tests ReverseMode/julia_gc_alloc_zeroing_bundles.ll and ForwardMode/julia_gc_alloc_zeroing_bundles.ll check the copied bundles and the full memset, including a Tuple{Float64,Any} layout (a pointer at offset 8). Against LLVM 20, the lit suite has the same results as main.
  • Julia 1.13.4 with libEnzyme built from this branch:
    • #4051's example and other gradients work in forward and reverse mode: push! into a vector of vectors, and a Tuple{Float64,Any}.
    • 13 of the 14 Enzyme.jl test files that aborted on 1.13.4 CI now pass: basic, array, typeunstable, applyiter, mixedapplyiter, absint, sugar, rules/inactive_kwrules, runtime_calls, typetree, threads, callconv, core/abi.
    • The 14th, advanced, segfaults in "Broadcast noalias" (bc1_loss_function). It does so on 1.13.2 with the released Enzyme_jll 0.0.308 too, so it is unrelated to this PR.
  • An earlier revision was also run against the Enzyme.jl test files on Julia master dda42805ec (LLVM 22, assertions), with the same per-file results as an earlier prototype.

🤖 Generated with Claude Code

Comment thread enzyme/Enzyme/CallDerivatives.cpp Resolved
Since julia#60924 (1.14.0-DEV.1697), Julia codegen no longer emits null
stores for the GC pointer fields of a new object. It attaches operand
bundles to the allocation call instead, and late-gc-lowering zeroes the
fields right after it lowers the allocation:

  julia.gc_alloc_ptr_offsets(i64 off...)        on julia.gc_alloc_obj
  julia.gc_alloc_zeroinit(i64 off, i64 size)    on julia.gc_alloc_obj
  julia.gc_alloc_zeroinit_indirect(i64 off, i64 size)
                                    on jl_alloc_genericmemory_unchecked

Accept these bundles in usedInRooting and getInvertedBundles, so that
calls that carry them no longer hit "unsupported tag". Also copy them
onto the shadow allocations that Enzyme creates for julia.gc_alloc_obj
in reverse mode and in loop rematerialization. Those calls had no
bundles before, so the GC pointer fields of the shadow were only
covered by the memset after the allocation, which LLVM may shorten or
remove when later stores overwrite the fields.

Assisted-by: Claude Code (Opus 5.5)
Assisted-by: Claude Code (Opus 5.5)
@vchuravy
vchuravy force-pushed the vc/julia-alloc-zeroing-bundles branch from de29010 to 9f71d38 Compare November 14, 2026 18:14
@vchuravy
vchuravy requested a review from a team November 14, 2026 18:14
@wsmoses
wsmoses requested a review from gbaraldi November 14, 2026 18:27
Julia's julia.gc_alloc_ptr_offsets bundle only nulls the GC pointer fields,
so the shadow still needs Enzyme's full memset to zero the double at offset 0.

Assisted-by: Claude Code (Opus 5.5)
wsmoses and others added 2 commits November 14, 2026 15:13
On LLVM 16 the cache and tape allocations keep the names of
CallInst::CreateMalloc (%malloccall...), simplifycfg names the select
%p instead of %spec.select, and the extractvalues of the tape and the
frees of the tape caches come in the other order. Match those values by
FileCheck variable and the reordered pairs with CHECK-DAG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015RAaewsVwF5wyRBfErBEEr
@vchuravy
vchuravy merged commit ea4cdcd into main Nov 15, 2026
48 checks passed
@vchuravy
vchuravy deleted the vc/julia-alloc-zeroing-bundles branch November 15, 2026 12:34
@vchuravy

Copy link
Copy Markdown
Member Author

Fixes EnzymeAD/Enzyme.jl#4051

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