Inlining native functions during JIT compilation

Let’s say I’m using LLVM to JIT compile a function. Inside that function I make a call to a runtime method in a currently loaded library. Is there any way to get LLVM to inline that function call?

As an example, let’s say my jitted function calls memcpy, can I get memcpy’s body inlined somehow? Or can that only be done via lto and aot compilation?


Timothy Baldridge

llvm is capable of inlining some of the basic functions like that during some of the optimization passes. How are you compiling?

As long as the JIT knows about the IR for the functions, you should be able to use the standard inlining passes. For standard c routines (memcpy,etc..) this knowledge is built in. For language specific routines, you'll have to provide the IR.


If all your IR is in a single module, and you’re willing to reoptimize the entire module every time you want to do inlining, you can use the existing machinery pretty easily.

But unless those are both true, the situation is more complicated: my understanding is the inlining machinery (at least for non-builtins, not sure how those work) assumes that 1) you’re inlining functions from the same module, and 2) you’re optimizing an entire module at a time. I ended up writing a custom inlining FunctionPass: I initialize an InlineCostAnalysis object (which ends up being quite hacky since it expects to be part of a passmanager even though that’s not what we want here), use that to determine when to inline, call llvm::InlineFunction() when it says I should, and then use a custom ValueMaterializer to remap references from objects in the other module (ex global variables, function declarations, etc) to the correct references in the current module.