Skip to content

Commit 48e140b

Browse files
gbaraldivchuravy
authored andcommitted
Do not add type tag size to the alloc_typed lowering for GC allocations (#54837)
Enzyme.jl hit an issue where, in a dynamically typed allocation of size `GC_MAX_SZCLASS`, because we mistakenly added they type tag size to the allocation, the runtime disagreed if this was a pool allocation or a big allocation. Causing a crash in the GC (cherry picked from commit ded0b28)
1 parent 82aa263 commit 48e140b

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

src/julia_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ STATIC_INLINE uint8_t JL_CONST_FUNC jl_gc_szclass_align8(unsigned sz) JL_NOTSAFE
463463
#define GC_MAX_SZCLASS (2032-sizeof(void*))
464464
static_assert(ARRAY_CACHE_ALIGN_THRESHOLD > GC_MAX_SZCLASS, "");
465465

466+
467+
// Size does NOT include the type tag!!
466468
STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, void *ty)
467469
{
468470
jl_value_t *v;

src/llvm-final-gc-lowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Value *FinalLowerGC::lowerGCAllocBytes(CallInst *target, Function &F)
214214
}
215215
} else {
216216
auto size = builder.CreateZExtOrTrunc(target->getArgOperand(1), T_size);
217-
size = builder.CreateAdd(size, ConstantInt::get(T_size, sizeof(void*)));
217+
// allocTypedFunc does not include the type tag in the allocation size!
218218
newI = builder.CreateCall(allocTypedFunc, { ptls, size, ConstantPointerNull::get(Type::getInt8PtrTy(F.getContext())) });
219219
derefAttr = Attribute::getWithDereferenceableBytes(F.getContext(), sizeof(void*));
220220
}

test/llvmpasses/final-lower-gc.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ top:
9595
%pgcstack = call {}*** @julia.get_pgcstack()
9696
%ptls = call {}*** @julia.ptls_states()
9797
%ptls_i8 = bitcast {}*** %ptls to i8*
98-
; CHECK: %0 = add i64 %size, 8
99-
; TYPED: %v = call noalias nonnull dereferenceable(8) {} addrspace(10)* @ijl_gc_alloc_typed(i8* %ptls_i8, i64 %0, i8* null)
100-
; OPAQUE: %v = call noalias nonnull dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %0, ptr null)
98+
; TYPED: %v = call noalias nonnull dereferenceable(8) {} addrspace(10)* @ijl_gc_alloc_typed(i8* %ptls_i8, i64 %size, i8* null)
99+
; OPAQUE: %v = call noalias nonnull dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %size, ptr null)
101100
%v = call {} addrspace(10)* @julia.gc_alloc_bytes(i8* %ptls_i8, i64 %size)
102101
%0 = bitcast {} addrspace(10)* %v to {} addrspace(10)* addrspace(10)*
103102
%1 = getelementptr {} addrspace(10)*, {} addrspace(10)* addrspace(10)* %0, i64 -1

0 commit comments

Comments
 (0)