Skip to content

Commit 7eab9b7

Browse files
vchuravyjpsamaroo
authored andcommitted
Construct constant LLVMPtr correctly (#38958)
Co-authored-by: Julian Samaroo <jpsamaroo@jpsamaroo.me> Co-authored-by: Julian Samaroo <jpsamaroo@jpsamaroo.me> (cherry picked from commit ec386bd)
1 parent 327f632 commit 7eab9b7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/intrinsics.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_data
168168
return ConstantFP::get(jl_LLVMContext,
169169
APFloat(lt->getFltSemantics(), APInt(64, data64)));
170170
}
171-
if (lt->isFloatingPointTy() || lt->isIntegerTy()) {
171+
if (lt->isFloatingPointTy() || lt->isIntegerTy() || lt->isPointerTy()) {
172172
int nb = jl_datatype_size(bt);
173173
APInt val(8 * nb, 0);
174174
void *bits = const_cast<uint64_t*>(val.getRawData());
@@ -178,6 +178,11 @@ static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_data
178178
return ConstantFP::get(jl_LLVMContext,
179179
APFloat(lt->getFltSemantics(), val));
180180
}
181+
if (lt->isPointerTy()) {
182+
Type *Ty = IntegerType::get(jl_LLVMContext, 8 * nb);
183+
Constant *addr = ConstantInt::get(Ty, val);
184+
return ConstantExpr::getIntToPtr(addr, lt);
185+
}
181186
assert(cast<IntegerType>(lt)->getBitWidth() == 8u * nb);
182187
return ConstantInt::get(lt, val);
183188
}
@@ -257,8 +262,10 @@ static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_data
257262
return ConstantVector::get(fields);
258263
if (StructType *st = dyn_cast<StructType>(lt))
259264
return ConstantStruct::get(st, fields);
260-
ArrayType *at = cast<ArrayType>(lt);
261-
return ConstantArray::get(at, fields);
265+
if (ArrayType *at = dyn_cast<ArrayType>(lt))
266+
return ConstantArray::get(at, fields);
267+
assert(false && "Unknown LLVM type");
268+
jl_unreachable();
262269
}
263270

264271
static Constant *julia_const_to_llvm(jl_codectx_t &ctx, jl_value_t *e)

test/llvmcall.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,15 @@ end
237237
# issue 34166
238238
f34166(x) = Base.llvmcall("ret i$(Sys.WORD_SIZE) %0", Int, (Int,), x)
239239
@test_throws ErrorException f34166(1)
240+
241+
# Test that codegen can construct constant LLVMPtr #38864
242+
struct MyStruct
243+
kern::UInt64
244+
ptr::Core.LLVMPtr{UInt8,1}
245+
end
246+
MyStruct(kern) = MyStruct(kern, reinterpret(Core.LLVMPtr{UInt8,1}, 0))
247+
MyStruct() = MyStruct(0)
248+
s = MyStruct()
249+
250+
@test s.kern == 0
251+
@test reinterpret(Int, s.ptr) == 0

0 commit comments

Comments
 (0)