[llvm-dev] JIT compiler, Windows, external functions like cos

Machiel van Hooren via llvm-dev llvm-dev at lists.llvm.org
Wed Jan 2 13:31:27 PST 2019


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_cast<llvm::JITTargetAddress>(&std::sinf),
functionFlags);
runtimeSymbols[executionSession->intern("cosf")] =
llvm::JITEvaluatedSymbol(reinterpret_cast<llvm::JITTargetAddress>(&std::cosf),
functionFlags);

//Create your runtime dylib and define the absolute symbols
llvm::orc::JITDylib& runtimeLibraryDyLib =
executionSession->createJITDylib("runtimeLibrary", false);
runtimeLibraryDyLib.define(llvm::orc::absoluteSymbols(runtimeSymbols));
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:
objectLinkLayer->setAutoClaimResponsibilityForObjectSymbols(true);
objectLinkLayer->setOverrideObjectFlagsWithResponsibilityFlags(true);

I hope this helps.

Regards,

Machiel van Hooren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190102/389a32be/attachment.html>


More information about the llvm-dev mailing list