0

I know the title isn't good, but i don't know any other way of saying it.

Let's say i have an array:

%1 = alloca [2 x i32] 

In LLVM-C, how would i get the type, or in this case, the i32 from %1? the reason i need the variable is to use a 'store' instruction, and i need to convert [2 x i32] to i32*

here is an example:

LLVMTypeRef arrty = LLVMArrayType(LLVMIntTypeInContext(context, 32), 2); LLVMValueRef alloca = LLVMBuildAlloca(builder, arrty, ""); LLVMValueRef gep = LLVMBuildGEP2( builder, LLVMGetAllocatedType(alloca), alloca, [tmp], 1, "" ); LLVMValueRef x = LLVMConstInt(LLVMIntTypeInContext(context, 32), 2, 0); LLVMBuildStore(builder, x, gep); // gep type is [2 x i32]*, i want i32* 

this would generate to:

%1 = alloca [2 x i32] %2 = gep [2 x i32], [2 x i32]* %1, i32 1 store i32 2, [2 x i32]* %2 

but i want it to generate:

%1 = alloca [2 x i32] %2 = gep [2 x i32], [2 x i32]* %1, i32 0, i32 1 store i32 2, i32* %2 

Sorry if the example is bad, i had to translate from rust to C.

2
  • What do you need the type for? Why don't you know the type already? Please provide a minimal reproducible example of what you have and of as much as you can fake of what you are expecting to do, once you have the type information. Also please double check the "C" tag, I consider myself quite fluent in C and it seems that the LLVM-C your are referring to, and its syntax, is not suitably represented by using the C tag. Commented Aug 9, 2022 at 6:03
  • The general answer to most LLVM-C questions here is ① find the answer for LLVM's native C++ interface ② find the LLVM-C wrapper function that calls that C++ ③ call that wrapper. Good luck. Commented Aug 9, 2022 at 6:50

1 Answer 1

0

I fixed it, the problem was that in the GEP inst, instead of compiling it like this:

%1 = alloca [2 x i32] %2 = gep [2 x i32], [2 x i32]* %1, i32 0, i32 1 store i32 2, i32* %2 

It compiled to this:

%1 = alloca [2 x i32] %2 = gep [2 x i32], [2 x i32]* %1, i32 1 store i32 2, [2 x i32]* %2 

So to fix this in C, we would do this:

LLVMValueRef x = LLVMConstInt(LLVMIntTypeInContext(context, 32), 0, 0); LLVMValueRef idx = LLVMConstInt(LLVMIntTypeInContext(context, 32), 2, 0); LLVMValueRef gep = LLVMBuildGEP2( builder, LLVMGetAllocatedType(alloca), alloca, [x, y], // Fix 2, "" ); 
Sign up to request clarification or add additional context in comments.

Comments