Reputation: 25096
I am using LLVM and the llvmpy library.
My goal is to create something similar to the follow C code:
int a[] = {1, 2};
int b[] = {1, 2, 3};
int c[] = {1};
int* ptrs[] = {a, b, c};
The following is the outputted IR of the above excerpt:
%1 = alloca i32, align 4
%a = alloca [2 x i32], align 4
%b = alloca [3 x i32], align 4
%c = alloca [1 x i32], align 4
%ptrs = alloca [3 x i32*], align 16
store i32 0, i32* %1
%2 = bitcast [2 x i32]* %a to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %2, i8* bitcast ([2 x i32]* @main.a to i8*), i64 8, i32 4, i1 false)
%3 = bitcast [3 x i32]* %b to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* bitcast ([3 x i32]* @main.b to i8*), i64 12, i32 4, i1 false)
%4 = bitcast [1 x i32]* %c to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %4, i8* bitcast ([1 x i32]* @main.c to i8*), i64 4, i32 4, i1 false)
%5 = getelementptr inbounds [3 x i32*]* %ptrs, i64 0, i64 0
%6 = getelementptr inbounds [2 x i32]* %a, i32 0, i32 0
store i32* %6, i32** %5
%7 = getelementptr inbounds i32** %5, i64 1
%8 = getelementptr inbounds [3 x i32]* %b, i32 0, i32 0
store i32* %8, i32** %7
%9 = getelementptr inbounds i32** %7, i64 1
%10 = getelementptr inbounds [1 x i32]* %c, i32 0, i32 0
store i32* %10, i32** %9
%11 = load i32* %1
ret i32 %11
I believe I have understood this well enough - it simply grabs the pointers to the head of each individual array, and then uses getelementptr
to walk along the storage array and insert each pointer.
Here is what I came up with in llvmpy
:
pointers = []
for array in arrays:
# Store each of the { 1, ... } arrays in memory.
type = Type.array(Type.int(), len(arrays))
ptr = builder.alloca_array(type, Constant.int(Type.int(), len(arrays)))
builder.store(array, ptr)
# And keep track of their pointers.
indices = [ Constant.int(Type.int(), 0), Constant.int(Type.int(), 0) ]
head = builder.gep(ptr, indices)
pointers.append(head)
# Finally, construct the array to hold the pointers.
type = Type.pointer(Type.int())
array = Constant.array(type, pointers)
return array
But I receive the error:
include/llvm/Support/Casting.h:219: typename cast_retty::ret_type llvm::cast_or_null(llvm::Value *): Assertion `isa(Val) && "cast_or_null() argument of incompatible type!"' failed.
on the line containing the code:
array = Constant.array(type, pointers)
When I check the type of the pointers and the type of the array, they both seem to be int32*
. What am I doing wrong? And, is Value
a generic placeholder that needs to be cast?
Upvotes: 1
Views: 1154
Reputation: 26868
What you are doing is the equivalent of writing the following, illegal, IR:
%x = getelementptr ...
%y = getelementptr ...
%z = getelementptr ...
%array = [ i32* %x, i32* %y, i32* %z ]
Why illegal? Because you are trying to create a constant array (you're using the Constant
class - so Constant.array
is equivalent to using the []
array literal syntax above) that holds non-constant values (%x
, %y
and %z
). If you want to store these pointers in an array, you have to alloca
the array first, then store
each pointer in turn inside it.
Upvotes: 1