runtime pass

Dear All

I need to create a pass that is triggered to run at a specific point during runtime. Also, this pass needs to have access to object file and to be able to modify the original IR. Any idea how to make such pass?


By “runtime”, are you referring to the runtime of opt? If so, I assume your question is about how to make sure certain passes run before your pass runs, so that their results are available to your pass. The “Writing an LLVM Pass” doc has a nice section on the interaction between passes:

Alternatively, if by “runtime”, you are referring to the runtime of the application under analysis, then that’s trickier. LLVM is designed to run analysis and transformation passes at compilation time, not runtime. So it depends on what you’re trying to do. If you’re trying to profile the application to, e.g., gather values of certain variables, that’s certainly possible by first writing a transformation pass that instruments the IR with calls at certain points in the target application.


First, thanks for your help.

Actually, I mean the second one, runtime of application. I’ve already made some instrumentation in the given IR, but only to add some extra code and change some variables. Now, if I want to instrument it to call some function that is able to parse stackmap section in the object file and change the given IR itself, is that possible?

By specific point, do you mean:

  • a specific instruction as identified by some means, e.g if (operaton == add && operand1 == 42 && operand2 == 17) { … insert special code … }

  • a specific location in the code e.g. if (ProgramCounter == 123456) { … insert special code here … }

  • some instruction in a specific function e.g. if (function == MyFunc && instructionNumber == 4) { … insert special code … }

  • some combination of the above?

I’m not sure I can help much, but calling the “runtime” of the application is really just a case of adding a function (declaration, not definition) with the right prototype and calling it, and then call the function.

This is how my compiler does “create a function”:

And here is an example of a function that creates the function prototype and calls the a function:

It will then be up to the linker to do the right thing to call the actual function (and if you try to call a function that doesn’t exist, it will fail to link…)