Skip to content

Commit

Permalink
Construct constant LLVMPtr correctly (JuliaLang#38958)
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Samaroo <[email protected]>

Co-authored-by: Julian Samaroo <[email protected]>
  • Loading branch information
vchuravy and jpsamaroo committed Dec 25, 2020
1 parent 4d7289b commit ec386bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t*>(val.getRawData());
Expand All @@ -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<IntegerType>(lt)->getBitWidth() == 8u * nb);
return ConstantInt::get(lt, val);
}
Expand Down Expand Up @@ -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<StructType>(lt))
return ConstantStruct::get(st, fields);
ArrayType *at = cast<ArrayType>(lt);
return ConstantArray::get(at, fields);
if (ArrayType *at = dyn_cast<ArrayType>(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)
Expand Down
12 changes: 12 additions & 0 deletions test/llvmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ec386bd

Please sign in to comment.