From b1393cefafca984a6818a2b835576453426cd438 Mon Sep 17 00:00:00 2001 From: William Moses Date: Mon, 23 Dec 2024 00:29:55 -0500 Subject: [PATCH 1/3] Enable getting non-boxed LLVM type from Julia Type The current method :jl_type_to_llvm takes a pointer-to-bool parameter isboxed, which if non-null, is set to true if the Julia Type requires boxing. However, one may want to know the julia type without boxing and there is no mechanism for doing so. If the isboxed parameter is null and cannot be set, we can return the LLVM struct inside the jlvaluet as a way of returning the information. Users like LLVM.jl, GPUCompiler.jl, and ClangCompiler.jl always request the isboxed parameter set to a non-null value and would not be affected: https://github.com/maleadt/LLVM.jl/blob/ff15977666b313c25e0b66302fa789898e00279e/src/interop/base.jl#L74C25-L74C40 https://github.com/maleadt/GPUCompiler.jl/blob/8fb9b50d85df93183aba852c59dd7ae8e58c0abc/src/driver.jl#L65 https://github.com/Gnimuc/ClangCompiler.jl/blob/0e503fe94dff1b6965a6b43d9527258ba0c9593c/src/utils.jl#L2 and an old commit of staticcompiler.jl (https://github.com/el-oso/StaticCompiler.jl/blob/145ef4ad788acb0dc2928ded53827636fdf37563/src/utils.jl#L5). There are other users of this function on github --- src/cgutils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cgutils.cpp b/src/cgutils.cpp index 98c5627578b80..11076b7146d95 100644 --- a/src/cgutils.cpp +++ b/src/cgutils.cpp @@ -640,7 +640,7 @@ static Type *_julia_type_to_llvm(jl_codegen_params_t *ctx, LLVMContext &ctxt, jl if (isboxed) *isboxed = false; if (jt == (jl_value_t*)jl_bottom_type || jt == (jl_value_t*)jl_typeofbottom_type || jt == (jl_value_t*)jl_typeofbottom_type->super) return getVoidTy(ctxt); - if (jl_is_concrete_immutable(jt)) { + if (jl_is_concrete_immutable(jt) || !isboxed) { if (jl_datatype_nbits(jt) == 0) return getVoidTy(ctxt); Type *t = _julia_struct_to_llvm(ctx, ctxt, jt, isboxed); From 09925ffe2aaabadaca8c1a8fb5145f29ce9ec0f5 Mon Sep 17 00:00:00 2001 From: William Moses Date: Mon, 23 Dec 2024 01:13:04 -0500 Subject: [PATCH 2/3] Update cgutils.cpp --- src/cgutils.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cgutils.cpp b/src/cgutils.cpp index 11076b7146d95..153bc2fe054c0 100644 --- a/src/cgutils.cpp +++ b/src/cgutils.cpp @@ -634,13 +634,13 @@ static unsigned convert_struct_offset(jl_codectx_t &ctx, Type *lty, unsigned byt static Type *_julia_struct_to_llvm(jl_codegen_params_t *ctx, LLVMContext &ctxt, jl_value_t *jt, bool *isboxed, bool llvmcall=false); -static Type *_julia_type_to_llvm(jl_codegen_params_t *ctx, LLVMContext &ctxt, jl_value_t *jt, bool *isboxed) +static Type *_julia_type_to_llvm(jl_codegen_params_t *ctx, LLVMContext &ctxt, jl_value_t *jt, bool *isboxed, bool no_boxing) { // this function converts a Julia Type into the equivalent LLVM type if (isboxed) *isboxed = false; if (jt == (jl_value_t*)jl_bottom_type || jt == (jl_value_t*)jl_typeofbottom_type || jt == (jl_value_t*)jl_typeofbottom_type->super) return getVoidTy(ctxt); - if (jl_is_concrete_immutable(jt) || !isboxed) { + if (jl_is_concrete_immutable(jt) || no_boxing) { if (jl_datatype_nbits(jt) == 0) return getVoidTy(ctxt); Type *t = _julia_struct_to_llvm(ctx, ctxt, jt, isboxed); @@ -653,13 +653,13 @@ static Type *_julia_type_to_llvm(jl_codegen_params_t *ctx, LLVMContext &ctxt, jl static Type *julia_type_to_llvm(jl_codectx_t &ctx, jl_value_t *jt, bool *isboxed) { - return _julia_type_to_llvm(&ctx.emission_context, ctx.builder.getContext(), jt, isboxed); + return _julia_type_to_llvm(&ctx.emission_context, ctx.builder.getContext(), jt, isboxed, false); } extern "C" JL_DLLEXPORT_CODEGEN Type *jl_type_to_llvm_impl(jl_value_t *jt, LLVMContextRef ctxt, bool *isboxed) { - return _julia_type_to_llvm(NULL, *unwrap(ctxt), jt, isboxed); + return _julia_type_to_llvm(NULL, *unwrap(ctxt), jt, isboxed, isboxed == nullptr); } From 51e84494f160357f61b616bfb3ce6317ba396669 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 23 Dec 2024 10:54:31 -0500 Subject: [PATCH 3/3] export jl_struct_to_llvm --- src/cgutils.cpp | 9 ++++++++- src/codegen-stubs.c | 2 ++ src/jl_exported_funcs.inc | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cgutils.cpp b/src/cgutils.cpp index 153bc2fe054c0..b377e33a2260c 100644 --- a/src/cgutils.cpp +++ b/src/cgutils.cpp @@ -659,7 +659,14 @@ static Type *julia_type_to_llvm(jl_codectx_t &ctx, jl_value_t *jt, bool *isboxed extern "C" JL_DLLEXPORT_CODEGEN Type *jl_type_to_llvm_impl(jl_value_t *jt, LLVMContextRef ctxt, bool *isboxed) { - return _julia_type_to_llvm(NULL, *unwrap(ctxt), jt, isboxed, isboxed == nullptr); + return _julia_type_to_llvm(NULL, *unwrap(ctxt), jt, isboxed, false); +} + + +extern "C" JL_DLLEXPORT_CODEGEN +Type *jl_struct_to_llvm_impl(jl_value_t *jt, LLVMContextRef ctxt, bool *isboxed) +{ + return _julia_type_to_llvm(NULL, *unwrap(ctxt), jt, isboxed, true); } diff --git a/src/codegen-stubs.c b/src/codegen-stubs.c index 5e243ddda28c9..1c0c22d801b3f 100644 --- a/src/codegen-stubs.c +++ b/src/codegen-stubs.c @@ -114,6 +114,8 @@ JL_DLLEXPORT LLVMOrcThreadSafeModuleRef jl_get_llvm_module_fallback(void *native JL_DLLEXPORT void *jl_type_to_llvm_fallback(jl_value_t *jt, LLVMContextRef llvmctxt, bool_t *isboxed) UNAVAILABLE +JL_DLLEXPORT void *jl_struct_to_llvm_fallback(jl_value_t *jt, LLVMContextRef llvmctxt, bool_t *isboxed) UNAVAILABLE + JL_DLLEXPORT jl_value_t *jl_get_libllvm_fallback(void) JL_NOTSAFEPOINT { return jl_nothing; diff --git a/src/jl_exported_funcs.inc b/src/jl_exported_funcs.inc index cb48cf6f9962c..2ac971300f520 100644 --- a/src/jl_exported_funcs.inc +++ b/src/jl_exported_funcs.inc @@ -544,6 +544,7 @@ YY(jl_dump_fptr_asm) \ YY(jl_emit_native) \ YY(jl_get_function_id) \ + YY(jl_struct_to_llvm) \ YY(jl_type_to_llvm) \ YY(jl_getUnwindInfo) \ YY(jl_get_libllvm) \