Frage

I've been trying to figure out how to basically take a portion of LLVM IR Code and execute it inline in C. I would like to do be able to inline the IR code so that a virtual function call is not needed (In the same way that assembly code can be inlined using _asm{}). See the following example:

LLVM IR code to execute:

define i64 @square(i64 %x){
    %y = mul i64 %x, %x
    ret i64 %y
}

C Program:

for(i = 0; i < length; i++){
   //run LLVM IR Code here
}

I know I could read the IR code in using parseIRFile() and create a pointer to the function but that is not what I am looking for as a virtual function call is needed.

War es hilfreich?

Lösung

First of all, there are no virtual functions involved - C does not have such a concept. I think what you mean is an indirect function call, that is, getting a function pointer and invoking the function it is pointing to.

The short answer is that no, there's no straightforward way to embed LLVM IR in C and getting it to compile with your code: see this related question and my answer. As I mention there, if you want to allow the programmer to write LLVM IR and still stick with direct function calls, you should encapsulate the IR in a function and put it in a separate file.

If embedding IR is more important than performance, you'll have to go with the ParseIR() route (no need for ParseIRFile() - you can parse directly from memory).

I guess that you could introduce some script to your build process which takes a hybrid C/IR input file, splits it into two files and then compiles them together, and modifies the debug information in the IR appropriately to behave as if it was truly embedded in the C file. But that is an extremely hacky approach, and in either way that input file will not be a legal C file.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top