Skip to content

Commit a88c638

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 782386f commit a88c638

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
@@ -508,6 +508,8 @@ STATIC_INLINE uint8_t JL_CONST_FUNC jl_gc_szclass_align8(unsigned sz) JL_NOTSAFE
508508
#define GC_MAX_SZCLASS (2032-sizeof(void*))
509509
static_assert(ARRAY_CACHE_ALIGN_THRESHOLD > GC_MAX_SZCLASS, "");
510510

511+
512+
// Size does NOT include the type tag!!
511513
STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, void *ty)
512514
{
513515
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ top:
9393
%pgcstack = call {}*** @julia.get_pgcstack()
9494
%ptls = call {}*** @julia.ptls_states()
9595
%ptls_i8 = bitcast {}*** %ptls to i8*
96-
; CHECK: %0 = add i64 %size, 8
97-
; TYPED: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) {} addrspace(10)* @ijl_gc_alloc_typed(i8* %ptls_i8, i64 %0, i64 12341234)
98-
; OPAQUE: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %0, i64 12341234)
96+
; TYPED: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) {} addrspace(10)* @ijl_gc_alloc_typed(i8* %ptls_i8, i64 %size, i64 12341234)
97+
; OPAQUE: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %size, i64 12341234)
9998
%v = call {} addrspace(10)* @julia.gc_alloc_bytes(i8* %ptls_i8, i64 %size, i64 12341234)
10099
%0 = bitcast {} addrspace(10)* %v to {} addrspace(10)* addrspace(10)*
101100
%1 = getelementptr {} addrspace(10)*, {} addrspace(10)* addrspace(10)* %0, i64 -1

0 commit comments

Comments
 (0)