Skip to content

Commit

Permalink
Do not add type tag size to the alloc_typed lowering for GC allocat…
Browse files Browse the repository at this point in the history
…ions (#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)
  • Loading branch information
gbaraldi authored and vchuravy committed Jun 23, 2024
1 parent 782386f commit a88c638
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ STATIC_INLINE uint8_t JL_CONST_FUNC jl_gc_szclass_align8(unsigned sz) JL_NOTSAFE
#define GC_MAX_SZCLASS (2032-sizeof(void*))
static_assert(ARRAY_CACHE_ALIGN_THRESHOLD > GC_MAX_SZCLASS, "");


// Size does NOT include the type tag!!
STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, void *ty)
{
jl_value_t *v;
Expand Down
2 changes: 1 addition & 1 deletion src/llvm-final-gc-lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void FinalLowerGC::lowerGCAllocBytes(CallInst *target, Function &F)
}
} else {
auto size = builder.CreateZExtOrTrunc(target->getArgOperand(1), T_size);
size = builder.CreateAdd(size, ConstantInt::get(T_size, sizeof(void*)));
// allocTypedFunc does not include the type tag in the allocation size!
newI = builder.CreateCall(allocTypedFunc, { ptls, size, type });
derefBytes = sizeof(void*);
}
Expand Down
5 changes: 2 additions & 3 deletions test/llvmpasses/final-lower-gc.ll
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ top:
%pgcstack = call {}*** @julia.get_pgcstack()
%ptls = call {}*** @julia.ptls_states()
%ptls_i8 = bitcast {}*** %ptls to i8*
; CHECK: %0 = add i64 %size, 8
; TYPED: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) {} addrspace(10)* @ijl_gc_alloc_typed(i8* %ptls_i8, i64 %0, i64 12341234)
; OPAQUE: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %0, i64 12341234)
; TYPED: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) {} addrspace(10)* @ijl_gc_alloc_typed(i8* %ptls_i8, i64 %size, i64 12341234)
; OPAQUE: %v = call noalias nonnull align {{[0-9]+}} dereferenceable(8) ptr addrspace(10) @ijl_gc_alloc_typed(ptr %ptls_i8, i64 %size, i64 12341234)
%v = call {} addrspace(10)* @julia.gc_alloc_bytes(i8* %ptls_i8, i64 %size, i64 12341234)
%0 = bitcast {} addrspace(10)* %v to {} addrspace(10)* addrspace(10)*
%1 = getelementptr {} addrspace(10)*, {} addrspace(10)* addrspace(10)* %0, i64 -1
Expand Down

0 comments on commit a88c638

Please sign in to comment.