How can I make the LLVM IR of a function available to my program?

user2333829

I'm working on a library which I'd like certain introspection features to be available. Let's say I'm compiling with clang, so I have access to libtooling or whatever.

What I'd like specifically is for someone to be able to view the LLVM IR of an already-compiled function as part of the program. I know that, when compiling, I can use -emit-llvm to get the IR. But that saves it to a file. What I'd like is for the LLVM IR to be embedded in and retrievable from the program itself -- e.g. my_function_object.llvm_ir()

Is such a thing possible? Thanks!

Theodoros Chatzigiannakis

You're basically trying to have reflection to your program. Reflection requires the existence of metadata in your binary. This doesn't exist out of the box in LLVM, as far as I know.

To achieve an effect like this, you could create a global key-value dictionary in your program, exposed via an exported function - something like IRInstruction* retrieve_llvm_ir_stream(char* name).

This dictionary would map some kind of identifier (for example, the exported name) of a given function to an in-memory array that represents the IR stream of that function (each instruction represented as a custom IRInstruction struct, for example). The types and functions of the representation format (like the custom IRInstruction struct) will have to be included in your source.

At the step of the IR generation, this dictionary will be empty. Immediately after the IR generation step, you'll need to add a custom build step: open the IR file and populate the dictionary with the data - for each exported function of your program, inject its name as a key to the dictionary and its IR stream as a value. The IR stream would be generated from the definitions of your functions, as read by your custom build tool (which would leverage the LLVM API to read the generated IR and convert it to your format).

Then, proceed to the assembler and linker as before.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I find the function pointers for tests from the LLVM IR code of a Rust program?

How can I make my program faster?

How can I get the LLVM IR dump from XLA in TensorFlow?

How can I create an executable from LLVM ir?

How can I make my webserver running on android publically available

How can I make my textfield fill the available vertical space?

How do I make LLVM opt output an IR file when given an IR file?

Adding a function call in my IR code in llvm

Why is my LLVM IR program crashing when i print a variable only 1 time but not 2?

How can I get the list of function calls that are performed in each function of a program, from the intermediate representation of LLVM?

How to make clang compile to llvm IR

How can I make a flow diagram of my program

How can i make my program reactive in swift with RxSwift

How can I make my program run on startup by adding it to the registry?

How can I make specific file extension open in my program

How can I make that my program does not crash?

How can I make this API call from my Nodejs program?

How to call C++ function in LLVM IR?

How can I use function or loop to simplify my program?

How can I program the function keys on my MacBook?

How can I make my tftp server visible/available on my local network?

How can I update global variable value in LLVM IR using IRBuilder?

How can I make my function accept different types of input?

How can I make a def function with my "if" statements?

How can I make my function a public selector?

How can I make a function in Javascript global to my html

how can i make my function inverse in r?

How can I make my function work for any number?

How can I make my IsItAHoliday function more efficient?