Repository navigation
Conversation
|
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 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 What happens next depends on the allocation kind:
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: IR for AllocOptdeclare 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 Two smaller things:
|
69b4bdc to
19327f8
Compare
216676f to
c25e551
Compare
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)
c25e551 to
49cb4c9
Compare
Follows #3426 (merged).
With #3426, the shadow of a
julia.gc_alloc_objcarries the primal'sjulia.gc_alloc_ptr_offsetsbundle. 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 theFloat64of aTuple{Float64,Any}.Change.
zeroKnownAllocation(LibraryFuncs.h) takes askipJuliaGCPointersflag. The reverse-mode and loop-rematerialized shadow allocations set it. With the flag,getJuliaNonGCPointerRangescomputes 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 newdropUninitializedAllocKindremoves it on the forward, reverse and loop-rematerialized shadows. Marking the shadowzeroedwould be wrong instead: LLVM would then drop Enzyme's memset of the other bytes as redundant.The whole shadow is still zeroed when:
ptr_offsetsbundle, including older Julia andjulia.gc_alloc_zeroinit;enzyme_fromstack, since an alloca replacement drops the bundle. Such allocations come from Enzyme's ownCreateAllocationand have no bundle anyway, so this check is a safeguard;loopShadowZeroInits).Open points:
ReverseModePrimal/Combined, Enzyme.jl'sshadow_alloc_rewriteatomically storesnothinginto the shadow's GC slots before this memset. With the full memset the slots ended up null; now they keepnothing. Primal fields that are never set (#undef) would then have anothingshadow. 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.Tests.
ReverseMode/julia_gc_alloc_zeroing_bundles.llchecks 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 dropsuninitialized.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/abiandmake_zero.🤖 Generated with Claude Code