From 8337efc5f11931c155ecb96ac98dcac5d8086e35 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Fri, 10 Feb 2023 15:03:49 -0500 Subject: [PATCH 1/4] use gc alloc instead of alloc typed --- src/llvm-final-gc-lowering.cpp | 10 ++++++---- src/llvm-pass-helpers.cpp | 21 ++++++++++++++++++++- src/llvm-pass-helpers.h | 3 +++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/llvm-final-gc-lowering.cpp b/src/llvm-final-gc-lowering.cpp index e4e8ff69ee2da..f5c22fc97924d 100644 --- a/src/llvm-final-gc-lowering.cpp +++ b/src/llvm-final-gc-lowering.cpp @@ -49,6 +49,7 @@ struct FinalLowerGC: private JuliaPassContext { Function *poolAllocFunc; Function *bigAllocFunc; Function *allocTypedFunc; + Function *allocFunc; Instruction *pgcstack; // Lowers a `julia.new_gc_frame` intrinsic. @@ -236,7 +237,7 @@ Value *FinalLowerGC::lowerGCAllocBytes(CallInst *target, Function &F) } else { auto size = builder.CreateZExtOrTrunc(target->getArgOperand(1), getSizeTy(F.getContext())); size = builder.CreateAdd(size, ConstantInt::get(getSizeTy(F.getContext()), sizeof(void*))); - newI = builder.CreateCall(allocTypedFunc, { ptls, size, ConstantPointerNull::get(Type::getInt8PtrTy(F.getContext())) }); + newI = builder.CreateCall(allocFunc, { ptls, size, ConstantPointerNull::get(Type::getInt8PtrTy(F.getContext())) }); derefAttr = Attribute::getWithDereferenceableBytes(F.getContext(), sizeof(void*)); } newI->setAttributes(newI->getCalledFunction()->getAttributes()); @@ -254,8 +255,9 @@ bool FinalLowerGC::doInitialization(Module &M) { poolAllocFunc = getOrDeclare(jl_well_known::GCPoolAlloc); bigAllocFunc = getOrDeclare(jl_well_known::GCBigAlloc); allocTypedFunc = getOrDeclare(jl_well_known::GCAllocTyped); + allocFunc = getOrDeclare(jl_well_known::GCAlloc); - GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocTypedFunc}; + GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocTypedFunc, allocFunc}; unsigned j = 0; for (unsigned i = 0; i < sizeof(functionList) / sizeof(void*); i++) { if (!functionList[i]) @@ -271,8 +273,8 @@ bool FinalLowerGC::doInitialization(Module &M) { bool FinalLowerGC::doFinalization(Module &M) { - GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocTypedFunc}; - queueRootFunc = poolAllocFunc = bigAllocFunc = allocTypedFunc = nullptr; + GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocTypedFunc, allocFunc}; + queueRootFunc = poolAllocFunc = bigAllocFunc = allocTypedFunc = allocFunc = nullptr; auto used = M.getGlobalVariable("llvm.compiler.used"); if (!used) return false; diff --git a/src/llvm-pass-helpers.cpp b/src/llvm-pass-helpers.cpp index e69a7d32bda9b..f7f63be5f8d39 100644 --- a/src/llvm-pass-helpers.cpp +++ b/src/llvm-pass-helpers.cpp @@ -230,7 +230,8 @@ namespace jl_well_known { static const char *GC_POOL_ALLOC_NAME = XSTR(jl_gc_pool_alloc); static const char *GC_QUEUE_ROOT_NAME = XSTR(jl_gc_queue_root); static const char *GC_ALLOC_TYPED_NAME = XSTR(jl_gc_alloc_typed); - + static const char *GC_ALLOC_NAME = XSTR(jl_gc_alloc); + using jl_intrinsics::addGCAllocAttributes; const WellKnownFunctionDescription GCBigAlloc( @@ -295,4 +296,22 @@ namespace jl_well_known { allocTypedFunc->addFnAttr(Attribute::getWithAllocSizeArgs(context.getLLVMContext(), 1, None)); return addGCAllocAttributes(allocTypedFunc, context.getLLVMContext()); }); + + const WellKnownFunctionDescription GCAlloc( + GC_ALLOC_NAME, + [](const JuliaPassContext &context) { + auto allocFunc = Function::Create( + FunctionType::get( + context.T_prjlvalue, + { Type::getInt8PtrTy(context.getLLVMContext()), + sizeof(size_t) == sizeof(uint32_t) ? + Type::getInt32Ty(context.getLLVMContext()) : + Type::getInt64Ty(context.getLLVMContext()), + Type::getInt8PtrTy(context.getLLVMContext()) }, + false), + Function::ExternalLinkage, + GC_ALLOC_NAME); + allocFunc->addFnAttr(Attribute::getWithAllocSizeArgs(context.getLLVMContext(), 1, None)); + return addGCAllocAttributes(allocFunc, context.getLLVMContext()); + }); } diff --git a/src/llvm-pass-helpers.h b/src/llvm-pass-helpers.h index 3388e6d485181..d8c46baa7b915 100644 --- a/src/llvm-pass-helpers.h +++ b/src/llvm-pass-helpers.h @@ -152,6 +152,9 @@ namespace jl_well_known { // `jl_gc_alloc_typed`: allocates bytes. extern const WellKnownFunctionDescription GCAllocTyped; + + // `jl_gc_alloc`: allocates bytes. + extern const WellKnownFunctionDescription GCAlloc; } #endif From bc07e52a15fe02f575050930ea8f9ea2dd009885 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Fri, 10 Feb 2023 16:26:42 -0500 Subject: [PATCH 2/4] cleanup --- src/llvm-final-gc-lowering.cpp | 8 +++----- src/llvm-pass-helpers.cpp | 19 ------------------- src/llvm-pass-helpers.h | 3 --- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/src/llvm-final-gc-lowering.cpp b/src/llvm-final-gc-lowering.cpp index f5c22fc97924d..a4f8cbbda1820 100644 --- a/src/llvm-final-gc-lowering.cpp +++ b/src/llvm-final-gc-lowering.cpp @@ -48,7 +48,6 @@ struct FinalLowerGC: private JuliaPassContext { Function *queueRootFunc; Function *poolAllocFunc; Function *bigAllocFunc; - Function *allocTypedFunc; Function *allocFunc; Instruction *pgcstack; @@ -254,10 +253,9 @@ bool FinalLowerGC::doInitialization(Module &M) { queueRootFunc = getOrDeclare(jl_well_known::GCQueueRoot); poolAllocFunc = getOrDeclare(jl_well_known::GCPoolAlloc); bigAllocFunc = getOrDeclare(jl_well_known::GCBigAlloc); - allocTypedFunc = getOrDeclare(jl_well_known::GCAllocTyped); allocFunc = getOrDeclare(jl_well_known::GCAlloc); - GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocTypedFunc, allocFunc}; + GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocFunc}; unsigned j = 0; for (unsigned i = 0; i < sizeof(functionList) / sizeof(void*); i++) { if (!functionList[i]) @@ -273,8 +271,8 @@ bool FinalLowerGC::doInitialization(Module &M) { bool FinalLowerGC::doFinalization(Module &M) { - GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocTypedFunc, allocFunc}; - queueRootFunc = poolAllocFunc = bigAllocFunc = allocTypedFunc = allocFunc = nullptr; + GlobalValue *functionList[] = {queueRootFunc, poolAllocFunc, bigAllocFunc, allocFunc}; + queueRootFunc = poolAllocFunc = bigAllocFunc = allocFunc = nullptr; auto used = M.getGlobalVariable("llvm.compiler.used"); if (!used) return false; diff --git a/src/llvm-pass-helpers.cpp b/src/llvm-pass-helpers.cpp index f7f63be5f8d39..77d19339aae3a 100644 --- a/src/llvm-pass-helpers.cpp +++ b/src/llvm-pass-helpers.cpp @@ -229,7 +229,6 @@ namespace jl_well_known { static const char *GC_BIG_ALLOC_NAME = XSTR(jl_gc_big_alloc); static const char *GC_POOL_ALLOC_NAME = XSTR(jl_gc_pool_alloc); static const char *GC_QUEUE_ROOT_NAME = XSTR(jl_gc_queue_root); - static const char *GC_ALLOC_TYPED_NAME = XSTR(jl_gc_alloc_typed); static const char *GC_ALLOC_NAME = XSTR(jl_gc_alloc); using jl_intrinsics::addGCAllocAttributes; @@ -279,24 +278,6 @@ namespace jl_well_known { return func; }); - const WellKnownFunctionDescription GCAllocTyped( - GC_ALLOC_TYPED_NAME, - [](const JuliaPassContext &context) { - auto allocTypedFunc = Function::Create( - FunctionType::get( - context.T_prjlvalue, - { Type::getInt8PtrTy(context.getLLVMContext()), - sizeof(size_t) == sizeof(uint32_t) ? - Type::getInt32Ty(context.getLLVMContext()) : - Type::getInt64Ty(context.getLLVMContext()), - Type::getInt8PtrTy(context.getLLVMContext()) }, - false), - Function::ExternalLinkage, - GC_ALLOC_TYPED_NAME); - allocTypedFunc->addFnAttr(Attribute::getWithAllocSizeArgs(context.getLLVMContext(), 1, None)); - return addGCAllocAttributes(allocTypedFunc, context.getLLVMContext()); - }); - const WellKnownFunctionDescription GCAlloc( GC_ALLOC_NAME, [](const JuliaPassContext &context) { diff --git a/src/llvm-pass-helpers.h b/src/llvm-pass-helpers.h index d8c46baa7b915..bae820ffee2cb 100644 --- a/src/llvm-pass-helpers.h +++ b/src/llvm-pass-helpers.h @@ -150,9 +150,6 @@ namespace jl_well_known { // `jl_gc_queue_root`: queues a GC root. extern const WellKnownFunctionDescription GCQueueRoot; - // `jl_gc_alloc_typed`: allocates bytes. - extern const WellKnownFunctionDescription GCAllocTyped; - // `jl_gc_alloc`: allocates bytes. extern const WellKnownFunctionDescription GCAlloc; } From 4e319f8d9e2e23c5d7c5004ba534cd048aea4ff3 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Sun, 12 Feb 2023 18:04:42 -0500 Subject: [PATCH 3/4] Remove trailing whitespace --- src/llvm-pass-helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llvm-pass-helpers.cpp b/src/llvm-pass-helpers.cpp index 77d19339aae3a..9c00f70d61b87 100644 --- a/src/llvm-pass-helpers.cpp +++ b/src/llvm-pass-helpers.cpp @@ -230,7 +230,7 @@ namespace jl_well_known { static const char *GC_POOL_ALLOC_NAME = XSTR(jl_gc_pool_alloc); static const char *GC_QUEUE_ROOT_NAME = XSTR(jl_gc_queue_root); static const char *GC_ALLOC_NAME = XSTR(jl_gc_alloc); - + using jl_intrinsics::addGCAllocAttributes; const WellKnownFunctionDescription GCBigAlloc( From 19cafe4a2074205dbac6f3550d20e4a23ce21a44 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 12 Feb 2023 20:28:03 -0500 Subject: [PATCH 4/4] Add jl_gc_alloc to exported functions --- src/jl_exported_funcs.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/jl_exported_funcs.inc b/src/jl_exported_funcs.inc index c475184573faa..eae2ceda63112 100644 --- a/src/jl_exported_funcs.inc +++ b/src/jl_exported_funcs.inc @@ -152,6 +152,7 @@ XX(jl_gc_add_finalizer_th) \ XX(jl_gc_add_ptr_finalizer) \ XX(jl_gc_add_quiescent) \ + XX(jl_gc_alloc) \ XX(jl_gc_allocobj) \ XX(jl_gc_alloc_0w) \ XX(jl_gc_alloc_1w) \