Is there a way to recover the source code variables in llvm bytecode? I've looked into debug information that llvm provides but I don't think they are stored.
Related
Normally, after compiler optimizations we get textual LLVM IR, so that we can compare the IR's before and after optimizations and reason about. In LTO, we usually input IR bitcode files to the linker (lld) and get native object files. Is there any way to get single monolithic LLVM IR (textual representation) after LTO passes? And Is there any effective way to analyze the object code to find out the optimizations other than just seeing text section of object file.
Thanks
Please tell me, if you need more information!
LTO optimizations are more or less the same that gets applied to the code during normal compilation. The difference is that the module being optimized comes from linking all the modules of a program.
So, you can just compile all your source to LLVM IR (with -flto, for instance), then link object files (which actually are bitcode files) using llvm-link and then play with optimizations running opt on this resulting bitcode. The list of passes applied during LTO stage can be seen in lib/Transforms/IPO/PassManagerBuilder.cpp, populateLTOPassManager(). There is also a nice opt option called -print-after to emit textual IR representation after applied given pass.
My question is how to set a label for instructions in LLVM optimization passes. Actually, my goal is to find a way to extract some marked LLVM IR instructions after compilation in ELF metadata of the produced executable file. What can be the most convenient way to do this? My idea was to add a label to these desired instructions in order to find them after compilation with their labels.
P.S. I am marking these instructions during an LLVM pass.
Thanks
I have some troubles wrapping my head around what LLVM actually does...
Am I right to assume that it could be used to parse mathematical expressions at runtime in a C++ program?
Right now at runtime, I'm getting the math expressions and build a C program out of it, compile it on the fly by doing system call to gcc. Then I dynamically load the .so produced by gcc and extract my eval function...
I'd like to replace this workflow by something simpler, maybe even faster...
Can LLVM help me out? Any resources out there to get me started?
You're describing using LLVM as a JIT compiler, which is absolutely possible. If you generate LLVM IR code (in memory) and hand it off to the library, it will generate machine code for you (still in memory). You can then run that code however you like.
If you want to generate LLVM IR from C code, you can also link clang as a library.
Here is a PDF I found at this answer, which has some examples of how to use LLVM as a JIT.
I have LLVM IR code in text format. What I wanna do is to be able to parse it and modify that code. Is there an API which can help in parsing the LLVM IR code? What libraries should I have in my system? At this moment I have clang compiler installed as well LLVM, as I can use commands such as llc, opt and llvm-link.
LLVM is primarily a C++ library. It has all the tools you can imagine to parse, manipulate and produce IR in both textual and bitcode (binary) formats.
To get started, take a look at the llvm::ParseIRFile function, defined in header include/llvm/Support/IRReader.h.
The best way to proceed would be to download the LLVM source code and build it, following these instructions. It's then easy to write your own code that uses the LLVM libraries.
The LLVM Core project consists of:
Compiler - converts source code to LLVM IR
VM - executes compiled IR code
How can I embed the VM to a C++ application?
The LLVM is really a collection of libraries that you can link to, so it's pretty easy to embed. More often the LLVM takes IR that you generate and compiles it directly to machine code. There is also a library available to interpret and execute IR for platforms that do not support JIT compilation.
There's a pretty good tutorial available on the LLVM website here: http://llvm.org/docs/tutorial/. I suggest that you go through that and then ask more specific questions if you have them.
Take a look at the HowToUseJIT example in LLVM.