diff --git a/src/intrinsics.cpp b/src/intrinsics.cpp index b5160bbc39c08..d5b826b1b817e 100644 --- a/src/intrinsics.cpp +++ b/src/intrinsics.cpp @@ -168,7 +168,7 @@ static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_data return ConstantFP::get(jl_LLVMContext, APFloat(lt->getFltSemantics(), APInt(64, data64))); } - if (lt->isFloatingPointTy() || lt->isIntegerTy()) { + if (lt->isFloatingPointTy() || lt->isIntegerTy() || lt->isPointerTy()) { int nb = jl_datatype_size(bt); APInt val(8 * nb, 0); void *bits = const_cast(val.getRawData()); @@ -178,6 +178,11 @@ static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_data return ConstantFP::get(jl_LLVMContext, APFloat(lt->getFltSemantics(), val)); } + if (lt->isPointerTy()) { + Type *Ty = IntegerType::get(jl_LLVMContext, 8 * nb); + Constant *addr = ConstantInt::get(Ty, val); + return ConstantExpr::getIntToPtr(addr, lt); + } assert(cast(lt)->getBitWidth() == 8u * nb); return ConstantInt::get(lt, val); } @@ -257,8 +262,10 @@ static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_data return ConstantVector::get(fields); if (StructType *st = dyn_cast(lt)) return ConstantStruct::get(st, fields); - ArrayType *at = cast(lt); - return ConstantArray::get(at, fields); + if (ArrayType *at = dyn_cast(lt)) + return ConstantArray::get(at, fields); + assert(false && "Unknown LLVM type"); + jl_unreachable(); } static Constant *julia_const_to_llvm(jl_codectx_t &ctx, jl_value_t *e) diff --git a/test/llvmcall.jl b/test/llvmcall.jl index 25951757b9e9d..b7f78205ec856 100644 --- a/test/llvmcall.jl +++ b/test/llvmcall.jl @@ -237,3 +237,15 @@ end # issue 34166 f34166(x) = Base.llvmcall("ret i$(Sys.WORD_SIZE) %0", Int, (Int,), x) @test_throws ErrorException f34166(1) + +# Test that codegen can construct constant LLVMPtr #38864 +struct MyStruct + kern::UInt64 + ptr::Core.LLVMPtr{UInt8,1} +end +MyStruct(kern) = MyStruct(kern, reinterpret(Core.LLVMPtr{UInt8,1}, 0)) +MyStruct() = MyStruct(0) +s = MyStruct() + +@test s.kern == 0 +@test reinterpret(Int, s.ptr) == 0