LLVM Discussion Forums

How to build llc individually?

I’m trying to generate the object file while building my IR module. I found the tool llc, but actually it was an executable file which I can’t call it from my llvm program directly.

Later I pull the llc.cpp file from the llvm source tree, along with the cmakelist.txt. But when I finally fix some trivial undefined code and comile it, It says lots of function not found.
Just like

initializeCore’ was not declared in this scope
   76 |     initializeCore(*Registry);
      |     ^~~~~~~~~~~~~~
odegen.cpp:77:5: error: ‘initializeCodeGen’ was not declared in this scope
   77 |     initializeCodeGen(*Registry);
      |     ^~~~~~~~~~~~~~~~~
codegen.cpp:78:5: error: ‘initializeLoopStrengthReducePass’ was not declared in this scope
   78 |     initializeLoopStrengthReducePass(*Registry);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
codegen.cpp:79:5: error: ‘initializeLowerIntrinsicsPass’ was not declared in this scope
   79 |     initializeLowerIntrinsicsPass(*Registry);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...

I’ve comment the below header because I can’t find it when I compile out of tree.

llvm/CodeGen/CommandFlags.inc

the complete header file and the cmake is pasted
header : https://paste.ubuntu.com/p/tx4MY8QnBB/
cmakelist https://paste.ubuntu.com/p/7mTytDzzwp/

Alternatively, extract the llc.cpp file and compile it out of tree is not a good idea.
I wonder whether I could compile the llc as a library file for me to call the function directly, like .so on linux. If possible, is there any idea to modify the original llc cmakelists?

Thanks in advance.

Depending on what you want to achieve, you could call llc using something like popen(3).

Edit: If you want to do it in a less hacky way, a good starting point would be to look at how the object file generation is handled in LLVMTargetMachineEmit from the C API. In my experience, the C API often strips down the most difficult parts of the infrastructure that may be spread all over the place in the main repository.

Thanks a lot. Diving into the LLVMTargetMachineEmit related API, I finally find the addPassesToEmitFile C API works.