Skip to content

Commit

Permalink
[ORC] Return ExecutorAddrs rather than JITEvaluatedSymbols from LLJIT…
Browse files Browse the repository at this point in the history
…::lookup.

Clients don't care about linkage, and ExecutorAddr is much more ergonomic.
  • Loading branch information
lhames committed May 5, 2022
1 parent b226894 commit 16dcbb5
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Interpreter/IncrementalExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ IncrementalExecutor::getSymbolAddress(llvm::StringRef Name,

if (!Sym)
return Sym.takeError();
return Sym->getAddress();
return Sym->getValue();
}

} // end namespace clang
4 changes: 2 additions & 2 deletions llvm/examples/HowToUseLLJIT/HowToUseLLJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ int main(int argc, char *argv[]) {
ExitOnErr(J->addIRModule(std::move(M)));

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto Add1Sym = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
auto Add1Addr = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = Add1Addr.toPtr<int(int)>();

int Result = Add1(42);
outs() << "add1(42) = " << Result << "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ int main(int argc, char *argv[]) {
ExitOnErr(J->addIRModule(std::move(M)));

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto Add1Sym = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
auto Add1Addr = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = Add1Addr.toPtr<int(int)>();

int Result = Add1(42);
outs() << "add1(42) = " << Result << "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char *argv[]) {
auto PrintSymbol = [&](StringRef Name) {
dbgs() << Name << " = ";
if (auto Sym = J->lookup(JD, Name))
dbgs() << formatv("{0:x}\n", Sym->getAddress());
dbgs() << *Sym;
else
dbgs() << "error: " << toString(Sym.takeError()) << "\n";
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ int main(int argc, char *argv[]) {
ExitOnErr(J->addIRModule(std::move(M)));

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto Add1Sym = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
auto Add1Addr = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = Add1Addr.toPtr<int(int)>();

int Result = Add1(42);
outs() << "add1(42) = " << Result << "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ int main(int argc, char *argv[]) {
// arguments passed.

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto EntrySym = ExitOnErr(J->lookup("entry"));
auto *Entry = (int (*)(int))EntrySym.getAddress();
auto EntryAddr = ExitOnErr(J->lookup("entry"));
auto *Entry = EntryAddr.toPtr<int(int)>();

int Result = Entry(argc);
outs() << "---Result---\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ int main(int argc, char *argv[]) {

// Look up the entry point, cast it to a C main function pointer, then use
// runAsMain to call it.
auto EntrySym = ExitOnErr(J->lookup(EntryPointName));
auto EntryFn =
jitTargetAddressToFunction<int (*)(int, char *[])>(EntrySym.getAddress());
auto EntryAddr = ExitOnErr(J->lookup(EntryPointName));
auto EntryFn = EntryAddr.toPtr<int(int, char *[])>();

return runAsMain(EntryFn, InputArgv, StringRef(InputFiles.front()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ int main(int argc, char *argv[]) {
// arguments passed.

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto EntrySym = ExitOnErr(J->lookup("entry"));
auto *Entry = (int (*)(int))EntrySym.getAddress();
auto EntryAddr = ExitOnErr(J->lookup("entry"));
auto *Entry = EntryAddr.toPtr<int(int)>();

int Result = Entry(argc);
outs() << "---Result---\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ void runJITWithCache(ObjectCache &ObjCache) {
ExitOnErr(J->addIRModule(std::move(M)));

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto Add1Sym = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
auto Add1Addr = ExitOnErr(J->lookup("add1"));
int (*Add1)(int) = Add1Addr.toPtr<int(int)>();

int Result = Add1(42);
outs() << "add1(42) = " << Result << "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ int main(int argc, char *argv[]) {
}

// Look up the JIT'd function, cast it to a function pointer, then call it.
auto EntrySym = ExitOnErr(J->lookup(EntryPointName));
auto *Entry = (int (*)())EntrySym.getAddress();
auto EntryAddr = ExitOnErr(J->lookup(EntryPointName));
auto *Entry = EntryAddr.toPtr<int()>();

int Result = Entry();
outs() << "---Result---\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ int main(int argc, char *argv[]) {
ExitOnErr(J->addIRModule(ExitOnErr(parseExampleModule(MainMod, "MainMod"))));

// (4) Look up the JIT'd function and call it.
auto EntrySym = ExitOnErr(J->lookup("entry"));
auto *Entry = (int (*)())EntrySym.getAddress();
auto EntryAddr = ExitOnErr(J->lookup("entry"));
auto *Entry = EntryAddr.toPtr<int()>();

int Result = Entry();
outs() << "--- Result ---\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ int main(int argc, char *argv[]) {
// The example uses a non-lazy JIT for simplicity. Thus, looking up the main
// function will materialize all reachable code. It also triggers debug
// registration in the remote target process.
JITEvaluatedSymbol MainFn = ExitOnErr(J->lookup("main"));
auto MainAddr = ExitOnErr(J->lookup("main"));

outs() << "Running: main(";
int Pos = 0;
Expand All @@ -238,10 +238,9 @@ int main(int argc, char *argv[]) {
// the debugger attached to the target, it should be possible to inspect the
// JITed code as if it was compiled statically.
{
JITTargetAddress MainFnAddr = MainFn.getAddress();
ExecutorProcessControl &EPC =
J->getExecutionSession().getExecutorProcessControl();
int Result = ExitOnErr(EPC.runAsMain(ExecutorAddr(MainFnAddr), ActualArgv));
int Result = ExitOnErr(EPC.runAsMain(MainAddr, ActualArgv));
outs() << "Exit code: " << Result << "\n";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,10 @@ int main(int Argc, char *Argv[]) {
}

// (5) Look up and run the JIT'd function.
auto MainSym = ExitOnErr(J->lookup(MainFunctionName));
auto MainAddr = ExitOnErr(J->lookup(MainFunctionName));

using MainFnPtr = int (*)(int, char *[]);
MainFnPtr MainFunction =
jitTargetAddressToFunction<MainFnPtr>(MainSym.getAddress());
auto *MainFunction = MainAddr.toPtr<MainFnPtr>();

int Result = runAsMain(MainFunction, {}, MainModulePath);
outs() << "'" << MainFunctionName << "' finished with exit code: " << Result
Expand Down
18 changes: 9 additions & 9 deletions llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,30 @@ class LLJIT {

/// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
/// look up symbols based on their IR name use the lookup function instead).
Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD,
SymbolStringPtr Name);
Expected<ExecutorAddr> lookupLinkerMangled(JITDylib &JD,
SymbolStringPtr Name);

/// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
/// look up symbols based on their IR name use the lookup function instead).
Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD,
StringRef Name) {
Expected<ExecutorAddr> lookupLinkerMangled(JITDylib &JD,
StringRef Name) {
return lookupLinkerMangled(JD, ES->intern(Name));
}

/// Look up a symbol in the main JITDylib by the symbol's linker-mangled name
/// (to look up symbols based on their IR name use the lookup function
/// instead).
Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) {
Expected<ExecutorAddr> lookupLinkerMangled(StringRef Name) {
return lookupLinkerMangled(*Main, Name);
}

/// Look up a symbol in JITDylib JD based on its IR symbol name.
Expected<JITEvaluatedSymbol> lookup(JITDylib &JD, StringRef UnmangledName) {
Expected<ExecutorAddr> lookup(JITDylib &JD, StringRef UnmangledName) {
return lookupLinkerMangled(JD, mangle(UnmangledName));
}

/// Look up a symbol in the main JITDylib based on its IR symbol name.
Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) {
Expected<ExecutorAddr> lookup(StringRef UnmangledName) {
return lookup(*Main, UnmangledName);
}

Expand Down Expand Up @@ -401,7 +401,7 @@ class LLLazyJITBuilderState : public LLJITBuilderState {
std::function<std::unique_ptr<IndirectStubsManager>()>;

Triple TT;
JITTargetAddress LazyCompileFailureAddr = 0;
ExecutorAddr LazyCompileFailureAddr;
std::unique_ptr<LazyCallThroughManager> LCTMgr;
IndirectStubsManagerBuilderFunction ISMBuilder;

Expand All @@ -415,7 +415,7 @@ class LLLazyJITBuilderSetters
/// Set the address in the target address to call if a lazy compile fails.
///
/// If this method is not called then the value will default to 0.
SetterImpl &setLazyCompileFailureAddr(JITTargetAddress Addr) {
SetterImpl &setLazyCompileFailureAddr(ExecutorAddr Addr) {
this->impl().LazyCompileFailureAddr = Addr;
return this->impl();
}
Expand Down
14 changes: 9 additions & 5 deletions llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,14 @@ Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj));
}

Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD,
SymbolStringPtr Name) {
return ES->lookup(
makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols), Name);
Expected<ExecutorAddr> LLJIT::lookupLinkerMangled(JITDylib &JD,
SymbolStringPtr Name) {
if (auto Sym = ES->lookup(
makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols),
Name))
return ExecutorAddr(Sym->getAddress());
else
return Sym.takeError();
}

Expected<std::unique_ptr<ObjectLayer>>
Expand Down Expand Up @@ -897,7 +901,7 @@ LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
LCTMgr = std::move(S.LCTMgr);
else {
if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
S.TT, *ES, S.LazyCompileFailureAddr))
S.TT, *ES, S.LazyCompileFailureAddr.getValue()))
LCTMgr = std::move(*LCTMgrOrErr);
else {
Err = LCTMgrOrErr.takeError();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J,
return wrap(Sym.takeError());
}

*Result = Sym->getAddress();
*Result = Sym->getValue();
return LLVMErrorSuccess;
}

Expand Down
12 changes: 5 additions & 7 deletions llvm/tools/lli/lli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ int runOrcJIT(const char *ProgName) {
}

Builder.setLazyCompileFailureAddr(
pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
orc::ExecutorAddr::fromPtr(exitOnLazyCallThroughFailure));
Builder.setNumCompileThreads(LazyJITCompileThreads);

// If the object cache is enabled then set a custom compile function
Expand Down Expand Up @@ -1049,23 +1049,21 @@ int runOrcJIT(const char *ProgName) {
for (auto &ThreadEntryPoint : ThreadEntryPoints) {
auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
typedef void (*EntryPointPtr)();
auto EntryPoint =
reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress()));
auto EntryPoint = EntryPointSym.toPtr<EntryPointPtr>();
AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
}

// Resolve and run the main function.
JITEvaluatedSymbol MainSym = ExitOnErr(J->lookup(EntryFunc));
auto MainAddr = ExitOnErr(J->lookup(EntryFunc));
int Result;

if (EPC) {
// ExecutorProcessControl-based execution with JITLink.
Result = ExitOnErr(
EPC->runAsMain(orc::ExecutorAddr(MainSym.getAddress()), InputArgv));
Result = ExitOnErr(EPC->runAsMain(MainAddr, InputArgv));
} else {
// Manual in-process execution with RuntimeDyld.
using MainFnTy = int(int, char *[]);
auto MainFn = jitTargetAddressToFunction<MainFnTy *>(MainSym.getAddress());
auto MainFn = MainAddr.toPtr<MainFnTy *>();
Result = orc::runAsMain(MainFn, InputArgv, StringRef(InputFile));
}

Expand Down

0 comments on commit 16dcbb5

Please sign in to comment.