Tom Leese
Tom Leese

Reputation: 19699

Define new type in LLVM

In a very simple LLVM IR program, I can do

%MyStruct = type { i32 }
%MyInt = type i32

declare void @main(%MyStruct, %MyInt)

However, I cannot work out how to produce the same %MyStruct = type { i32 } or %MyInt = type i32 statement using the LLVM C++ API. I've been scouring the docs for the past few days and the only thing I've found which is close to what I want is manually defining a new type as mentioned here: http://llvm.org/docs/ExtendingLLVM.html#adding-a-new-type

However, I would like a way to define and use new types using the LLVM C++ API, as I can with functions, etc.

The only other way I can think of doing this is keeping a separate record of my custom types and their LLVM primitive base type, and when declaring functions, use the primitive base type; but I didn't want to start doing that if there was a quicker way built into LLVM itself (as I discovered by looking at some LLVM IR examples and producing the code above).

Edit: I have done some more looking around, and I've found a reference to a function which appears to be exactly what I need called addTypeName in http://llvm.org/docs/ProgrammersManual.html and also in http://llvm.org/doxygen/Module_8h-source.html, however this appears to be all there is as I cannot see any reference to addTypeName in the actual documentation and I cannot compile a program using addTypeName as it claims the method does not exist.

Upvotes: 4

Views: 3035

Answers (1)

Anton Korobeynikov
Anton Korobeynikov

Reputation: 9324

The easiest way for you is to use cpp backend (llc -march=cpp) - given the IR it will generate the sequence of C++ API calls necessary to reproduce the IR.

Upvotes: 4

Related Questions