Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create LLVM structure value?

I'm trying to create an LLVM value of a structure type. I'm using LLVM-C interface and find a function:

LLVMValueRef    LLVMConstStruct (LLVMValueRef *ConstantVals, unsigned Count, LLVMBool Packed)

This works fine if all members are constant value created by LLVMConstXXX(), it will generate code like:

store { i32, i32, i32 } { i32 1, i32 2, i32 3 }, { i32, i32, i32 }* %17, align 4

But the problem is if the member is not constant, it will generate things like:

%0 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
store { i32, i32, i32 } { i32 1, i32 %0, i32 3 }, { i32, i32, i32 }* %17, align 4

And when I send this piece of LLVM code to NVVM (Nvidia PTX backend), it says:

module 0 (27, 39): parse error: invalid use of function-local name

So, I don't know if this struct value creating is a correct. What I need is a value, not an allocated memory.

Anyone has idea?

Regards, Xiang.

like image 270
Xiang Zhang Avatar asked Apr 03 '13 11:04

Xiang Zhang


People also ask

What is LLVM value?

LLVM Value Representation. This is a very important LLVM class. It is the base class of all values computed by a program that may be used as operands to other values. Value is the super class of other important classes such as Instruction and Function.

What is i1 in LLVM?

i1: This is a 1-bit integer, i.e., a boolean value. This is the type returned by comparison instructions in LLVM, and it is the type that you must use for the condition of conditional branches.

Why is LLVM so good?

Each library supports a particular component in a typical compiler pipeline (lexing, parsing, optimizations of a particular type, machine code generation for a particular architecture, etc.). What makes it so popular is that its modular design allows its functionality to be adapted and reused very easily.


1 Answers

A constant struct is a kind of literal that - loyal to its name - may only contain other constants, not general values. The correct way to generate that struct, then, is via insertvalue. In your example above, it should look like this:

%0 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%1 = insertvalue {i32, i32, i32} {i32 1, i32 undef, i32 3}, i32 %0, 1
store { i32, i32, i32 } %1, { i32, i32, i32 }* %17, align 4
like image 171
Oak Avatar answered Oct 30 '22 21:10

Oak