#include "llvm/ADT/StringMap.h"
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
#include "llvm/ExecutionEngine/Orc/EPCIndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "../ExampleModules.h"
#include <future>
using namespace llvm;
using namespace llvm::orc;
ExitOnError ExitOnErr;
const llvm::StringRef FooMod =
R"(
declare i32 @return1()
define i32 @foo_body() {
entry:
%0 = call i32 @return1()
ret i32 %0
}
)";
const llvm::StringRef BarMod =
R"(
declare i32 @return2()
define i32 @bar_body() {
entry:
%0 = call i32 @return2()
ret i32 %0
}
)";
const llvm::StringRef MainMod =
R"(
define i32 @entry(i32 %argc) {
entry:
%and = and i32 %argc, 1
%tobool = icmp eq i32 %and, 0
br i1 %tobool, label %if.end, label %if.then
if.then: ; preds = %entry
%call = tail call i32 @foo() #2
br label %return
if.end: ; preds = %entry
%call1 = tail call i32 @bar() #2
br label %return
return: ; preds = %if.end, %if.then
%retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.end ]
ret i32 %retval.0
}
declare i32 @foo()
declare i32 @bar()
)";
extern "C" int32_t return1() { return 1; }
extern "C" int32_t return2() { return 2; }
static void *reenter(void *Ctx, void *TrampolineAddr) {
std::promise<void *> LandingAddressP;
auto LandingAddressF = LandingAddressP.get_future();
auto *EPCIU = static_cast<EPCIndirectionUtils *>(Ctx);
EPCIU->getLazyCallThroughManager().resolveTrampolineLandingAddress(
pointerToJITTargetAddress(TrampolineAddr),
[&](JITTargetAddress LandingAddress) {
LandingAddressP.set_value(
jitTargetAddressToPointer<void *>(LandingAddress));
});
return LandingAddressF.get();
}
static void reportErrorAndExit() {
errs() << "Unable to lazily compile function. Exiting.\n";
exit(1);
}
cl::list<std::string> InputArgv(cl::Positional,
cl::desc("<program arguments>..."));
int main(int argc, char *argv[]) {
InitLLVM X(argc, argv);
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
cl::ParseCommandLineOptions(argc, argv, "LLJITWithLazyReexports");
ExitOnErr.setBanner(std::string(argv[0]) + ": ");
auto EPC = ExitOnErr(SelfExecutorProcessControl::Create());
auto J = ExitOnErr(
LLJITBuilder().setExecutorProcessControl(std::move(EPC)).create());
J->getIRTransformLayer().setTransform(
[](ThreadSafeModule TSM,
const MaterializationResponsibility &R) -> Expected<ThreadSafeModule> {
TSM.withModuleDo([](Module &M) { dbgs() << "---Compiling---\n" << M; });
return std::move(TSM); });
auto EPCIU = ExitOnErr(EPCIndirectionUtils::Create(
J->getExecutionSession().getExecutorProcessControl()));
ExitOnErr(EPCIU->writeResolverBlock(pointerToJITTargetAddress(&reenter),
pointerToJITTargetAddress(EPCIU.get())));
EPCIU->createLazyCallThroughManager(
J->getExecutionSession(), pointerToJITTargetAddress(&reportErrorAndExit));
auto ISM = EPCIU->createIndirectStubsManager();
J->getMainJITDylib().addGenerator(
ExitOnErr(EPCDynamicLibrarySearchGenerator::GetForTargetProcess(
J->getExecutionSession())));
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(FooMod, "foo-mod"))));
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(BarMod, "bar-mod"))));
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(MainMod, "main-mod"))));
MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout());
SymbolAliasMap ReExports(
{{Mangle("foo"),
{Mangle("foo_body"),
JITSymbolFlags::Exported | JITSymbolFlags::Callable}},
{Mangle("bar"),
{Mangle("bar_body"),
JITSymbolFlags::Exported | JITSymbolFlags::Callable}}});
ExitOnErr(J->getMainJITDylib().define(
lazyReexports(EPCIU->getLazyCallThroughManager(), *ISM,
J->getMainJITDylib(), std::move(ReExports))));
dbgs() << "---Session state---\n";
J->getExecutionSession().dump(dbgs());
dbgs() << "\n";
auto EntryAddr = ExitOnErr(J->lookup("entry"));
auto *Entry = EntryAddr.toPtr<int(int)>();
int Result = Entry(argc);
outs() << "---Result---\n"
<< "entry(" << argc << ") = " << Result << "\n";
ExitOnErr(EPCIU->cleanup());
return 0;
}