JIT compiler, Windows, external functions like cos

Hi Marcus,

I ran into the same/similar issue a while ago (and posted the same question here) but since I got no response I eventually came up with the following solution.

Create a JITDylib containing absolute addresses to all your runtime functions like cosf, sinf, etc and then link that JITDylib to each new JITDylib that needs those functions.
This can be done by defining some absolute symbols as follows:

llvm::JITSymbolFlags functionFlags;
functionFlags |= llvm::JITSymbolFlags::Callable;
functionFlags |= llvm::JITSymbolFlags::Exported;
functionFlags |= llvm::JITSymbolFlags::Absolute;

llvm::orc::SymbolMap runtimeSymbols;
//This will map the “sinf” symbol to the absolute address of std::sinf
//Do this for every runtime library symbol that you need.
runtimeSymbols[executionSession->intern(“sinf”)] = llvm::JITEvaluatedSymbol(reinterpret_castllvm::JITTargetAddress(&std::sinf), functionFlags);
runtimeSymbols[executionSession->intern(“cosf”)] = llvm::JITEvaluatedSymbol(reinterpret_castllvm::JITTargetAddress(&std::cosf), functionFlags);

//Create your runtime dylib and define the absolute symbols
llvm::orc::JITDylib& runtimeLibraryDyLib = executionSession->createJITDylib(“runtimeLibrary”, false);

Then on the JITDylib that you are building:

dylib.addToSearchOrder(runtimeLibraryDyLib, false);

Another crash/issue you may run into on Windows that is related to your question is described in this bug report (there is a workaround): https://bugs.llvm.org/show_bug.cgi?id=40074

You may already be doing this, but on Windows you also need to enable some workarounds on your RTDyldObjectLinkingLayer in order for your compiled function symbols to be found at all:

I hope this helps.


Machiel van Hooren

Hi Machiel, thanks for sharing your solution and the workarounds for RTDyldObjectLinkingLayer on Windows!

As in the other “Undefined symbols” thread, you might want to have a look at symbol generators. They offer an easy way to emit additional symbols to a Dylib’s symbol table. Two examples for this in ORCv2 are:

  • DynamicLibrarySearchGenerator in llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
  • ReexportsGenerator in llvm/include/llvm/ExecutionEngine/Orc/Core.h

Of course, depending on your use-case a common runtime Dylib can be a suitable solution too.