llvm.org GIT mirror llvm / 130bebd
[ORC] Add findSymbolIn() wrapper to C bindings, take #2. Re-appply r333147, reverted in r333152 due to a pre-existing bug. As D47308 has been merged in r333206, the OSX issue should now be resolved. In many cases JIT users will know in which module a symbol resides. Avoiding to search other modules can be more efficient. It also allows to handle duplicate symbol names between modules. Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D44889 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333215 91177308-0d34-0410-b5e6-96231b3b80d8 Andres Freund 1 year, 3 months ago
4 changed file(s) with 59 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
140140 const char *SymbolName);
141141
142142 /**
143 * Get symbol address from JIT instance, searching only the specified
144 * handle.
145 */
146 LLVMOrcErrorCode LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack,
147 LLVMOrcTargetAddress *RetAddr,
148 LLVMOrcModuleHandle H,
149 const char *SymbolName);
150
151 /**
143152 * Dispose of an ORC JIT stack.
144153 */
145154 LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack);
111111 return J.findSymbolAddress(*RetAddr, SymbolName, true);
112112 }
113113
114 LLVMOrcErrorCode LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack,
115 LLVMOrcTargetAddress *RetAddr,
116 LLVMOrcModuleHandle H,
117 const char *SymbolName) {
118 OrcCBindingsStack &J = *unwrap(JITStack);
119 return J.findSymbolAddressIn(*RetAddr, H, SymbolName, true);
120 }
121
114122 LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
115123 auto *J = unwrap(JITStack);
116124 auto Err = J->shutdown();
379379
380380 JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
381381 bool ExportedSymbolsOnly) {
382 assert(KeyLayers.count(K) && "looking up symbol in unknown module");
382383 return KeyLayers[K]->findSymbolIn(K, mangle(Name), ExportedSymbolsOnly);
383384 }
384385
402403 return LLVMOrcErrSuccess;
403404 }
404405
406 LLVMOrcErrorCode findSymbolAddressIn(JITTargetAddress &RetAddr,
407 orc::VModuleKey K,
408 const std::string &Name,
409 bool ExportedSymbolsOnly) {
410 RetAddr = 0;
411 if (auto Sym = findSymbolIn(K, Name, ExportedSymbolsOnly)) {
412 // Successful lookup, non-null symbol:
413 if (auto AddrOrErr = Sym.getAddress()) {
414 RetAddr = *AddrOrErr;
415 return LLVMOrcErrSuccess;
416 } else
417 return mapError(AddrOrErr.takeError());
418 } else if (auto Err = Sym.takeError()) {
419 // Lookup failure - report error.
420 return mapError(std::move(Err));
421 }
422 // Otherwise we had a successful lookup but got a null result. We already
423 // set RetAddr to '0' above, so just return success.
424 return LLVMOrcErrSuccess;
425 }
426
405427 const std::string &getErrorMessage() const { return ErrMsg; }
406428
407429 private:
9797
9898 LLVMOrcModuleHandle H;
9999 LLVMOrcAddEagerlyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
100 LLVMOrcTargetAddress MainAddr;
101 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
102 MainFnTy MainFn = (MainFnTy)MainAddr;
103 int Result = MainFn();
104 EXPECT_EQ(Result, 42)
105 << "Eagerly JIT'd code did not return expected result";
100
101 // get symbol address searching the entire stack
102 {
103 LLVMOrcTargetAddress MainAddr;
104 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
105 MainFnTy MainFn = (MainFnTy)MainAddr;
106 int Result = MainFn();
107 EXPECT_EQ(Result, 42)
108 << "Eagerly JIT'd code did not return expected result";
109 }
110
111 // and then just searching a single handle
112 {
113 LLVMOrcTargetAddress MainAddr;
114 LLVMOrcGetSymbolAddressIn(JIT, &MainAddr, H, "main");
115 MainFnTy MainFn = (MainFnTy)MainAddr;
116 int Result = MainFn();
117 EXPECT_EQ(Result, 42)
118 << "Eagerly JIT'd code did not return expected result";
119 }
106120
107121 LLVMOrcRemoveModule(JIT, H);
108122