Skip to content

Commit ded0b28

Browse files
authored
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
1 parent 320366b commit ded0b28

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

src/julia_internal.h

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

497+
498+
// Size does NOT include the type tag!!
497499
STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, void *ty)
498500
{
499501
jl_value_t *v;

src/llvm-final-gc-lowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void FinalLowerGC::lowerGCAllocBytes(CallInst *target, Function &F)
211211
}
212212
} else {
213213
auto size = builder.CreateZExtOrTrunc(target->getArgOperand(1), T_size);
214-
size = builder.CreateAdd(size, ConstantInt::get(T_size, sizeof(void*)));
214+
// allocTypedFunc does not include the type tag in the allocation size!
215215
newI = builder.CreateCall(allocTypedFunc, { ptls, size, type });
216216
derefBytes = sizeof(void*);
217217
}

test/llvmpasses/final-lower-gc.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ top:
7272
%pgcstack = call {}*** @julia.get_pgcstack()
7373
%ptls = call {}*** @julia.ptls_states()
7474
%ptls_i8 = bitcast {}*** %ptls to i8*
75-
; CHECK: %0 = add i64 %size, 8
76-
; OPAQUE: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %0, i64 12341234)
75+
; OPAQUE: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %size, i64 12341234)
7776
%v = call {} addrspace(10)* @julia.gc_alloc_bytes(i8* %ptls_i8, i64 %size, i64 12341234)
7877
%0 = bitcast {} addrspace(10)* %v to {} addrspace(10)* addrspace(10)*
7978
%1 = getelementptr {} addrspace(10)*, {} addrspace(10)* addrspace(10)* %0, i64 -1

0 commit comments

Comments
 (0)