llvm.org GIT mirror llvm / 35ca32d
[ORC] Start migrating ORC layers to use the new ORC Core.h APIs. In particular this patch switches RTDyldObjectLinkingLayer to use orc::SymbolResolver and threads the requried changse (ExecutionSession references and VModuleKeys) through the existing layer APIs. The purpose of the new resolver interface is to improve query performance and better support parallelism, both in JIT'd code and within the compiler itself. The most visibile change is switch of the <Layer>::addModule signatures from: Expected<Handle> addModule(std::shared_ptr<ModuleType> Mod, std::shared_ptr<JITSymbolResolver> Resolver) to: Expected<Handle> addModule(VModuleKey K, std::shared_ptr<ModuleType> Mod); Typical usage of addModule will now look like: auto K = ES.allocateVModuleKey(); Resolvers[K] = createSymbolResolver(...); Layer.addModule(K, std::move(Mod)); See the BuildingAJIT tutorial code for example usage. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324405 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 1 year, 8 months ago
30 changed file(s) with 824 addition(s) and 445 deletion(s). Raw diff Collapse all Expand all
33 ExecutionEngine
44 InstCombine
55 Object
6 OrcJIT
67 RuntimeDyld
78 ScalarOpts
89 Support
3737
3838 class KaleidoscopeJIT {
3939 private:
40 SymbolStringPool SSP;
41 ExecutionSession ES;
42 std::shared_ptr Resolver;
4043 std::unique_ptr TM;
4144 const DataLayout DL;
4245 RTDyldObjectLinkingLayer ObjectLayer;
4649 using ModuleHandle = decltype(CompileLayer)::ModuleHandleT;
4750
4851 KaleidoscopeJIT()
49 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
50 ObjectLayer([]() { return std::make_shared(); }),
52 : ES(SSP),
53 Resolver(createLegacyLookupResolver(
54 [this](const std::string &Name) -> JITSymbol {
55 if (auto Sym = CompileLayer.findSymbol(Name, false))
56 return Sym;
57 else if (auto Err = Sym.takeError())
58 return std::move(Err);
59 if (auto SymAddr =
60 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
61 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
62 return nullptr;
63 },
64 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
65 TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
66 ObjectLayer(
67 ES,
68 [](VModuleKey) { return std::make_shared(); },
69 [this](VModuleKey K) { return Resolver; }),
5170 CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
5271 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
5372 }
5574 TargetMachine &getTargetMachine() { return *TM; }
5675
5776 ModuleHandle addModule(std::unique_ptr M) {
58 // Build our symbol resolver:
59 // Lambda 1: Look back into the JIT itself to find symbols that are part of
60 // the same "logical dylib".
61 // Lambda 2: Search for external symbols in the host process.
62 auto Resolver = createLambdaResolver(
63 [&](const std::string &Name) {
64 if (auto Sym = CompileLayer.findSymbol(Name, false))
65 return Sym;
66 return JITSymbol(nullptr);
67 },
68 [](const std::string &Name) {
69 if (auto SymAddr =
70 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
71 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
72 return JITSymbol(nullptr);
73 });
74
75 // Add the set to the JIT with the resolver we created above and a newly
76 // created SectionMemoryManager.
77 return cantFail(CompileLayer.addModule(std::move(M),
78 std::move(Resolver)));
77 // Add the module to the JIT with a new VModuleKey.
78 return cantFail(CompileLayer.addModule(ES.allocateVModule(), std::move(M)));
7979 }
8080
8181 JITSymbol findSymbol(const std::string Name) {
33 ExecutionEngine
44 InstCombine
55 Object
6 OrcJIT
67 RuntimeDyld
78 ScalarOpts
89 Support
4141
4242 class KaleidoscopeJIT {
4343 private:
44 SymbolStringPool SSP;
45 ExecutionSession ES;
46 std::shared_ptr Resolver;
4447 std::unique_ptr TM;
4548 const DataLayout DL;
4649 RTDyldObjectLinkingLayer ObjectLayer;
5558 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
5659
5760 KaleidoscopeJIT()
58 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
59 ObjectLayer([]() { return std::make_shared(); }),
61 : ES(SSP),
62 Resolver(createLegacyLookupResolver(
63 [this](const std::string &Name) -> JITSymbol {
64 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
65 return Sym;
66 else if (auto Err = Sym.takeError())
67 return std::move(Err);
68 if (auto SymAddr =
69 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
70 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
71 return nullptr;
72 },
73 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
74 TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
75 ObjectLayer(
76 ES,
77 [](VModuleKey) { return std::make_shared(); },
78 [this](VModuleKey K) { return Resolver; }),
6079 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
61 OptimizeLayer(CompileLayer,
62 [this](std::shared_ptr M) {
63 return optimizeModule(std::move(M));
64 }) {
80 OptimizeLayer(CompileLayer, [this](std::shared_ptr M) {
81 return optimizeModule(std::move(M));
82 }) {
6583 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
6684 }
6785
6886 TargetMachine &getTargetMachine() { return *TM; }
6987
7088 ModuleHandle addModule(std::unique_ptr M) {
71 // Build our symbol resolver:
72 // Lambda 1: Look back into the JIT itself to find symbols that are part of
73 // the same "logical dylib".
74 // Lambda 2: Search for external symbols in the host process.
75 auto Resolver = createLambdaResolver(
76 [&](const std::string &Name) {
77 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
78 return Sym;
79 return JITSymbol(nullptr);
80 },
81 [](const std::string &Name) {
82 if (auto SymAddr =
83 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
84 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
85 return JITSymbol(nullptr);
86 });
87
88 // Add the set to the JIT with the resolver we created above and a newly
89 // created SectionMemoryManager.
90 return cantFail(OptimizeLayer.addModule(std::move(M),
91 std::move(Resolver)));
89 // Add the module to the JIT with a new VModuleKey.
90 return cantFail(
91 OptimizeLayer.addModule(ES.allocateVModule(), std::move(M)));
9292 }
9393
9494 JITSymbol findSymbol(const std::string Name) {
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/ExecutionEngine.h"
1818 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
20 #include "llvm/ExecutionEngine/RuntimeDyld.h"
21 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2219 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
2320 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
2421 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2522 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2623 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
2724 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
26 #include "llvm/ExecutionEngine/RuntimeDyld.h"
27 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2828 #include "llvm/IR/DataLayout.h"
2929 #include "llvm/IR/LegacyPassManager.h"
3030 #include "llvm/IR/Mangler.h"
3434 #include "llvm/Transforms/Scalar.h"
3535 #include "llvm/Transforms/Scalar/GVN.h"
3636 #include
37 #include
3738 #include
3839 #include
3940 #include
4445
4546 class KaleidoscopeJIT {
4647 private:
48 SymbolStringPool SSP;
49 ExecutionSession ES;
50 std::map> Resolvers;
4751 std::unique_ptr TM;
4852 const DataLayout DL;
4953 RTDyldObjectLinkingLayer ObjectLayer;
6165 using ModuleHandle = decltype(CODLayer)::ModuleHandleT;
6266
6367 KaleidoscopeJIT()
64 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
65 ObjectLayer([]() { return std::make_shared(); }),
68 : ES(SSP), TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
69 ObjectLayer(
70 ES,
71 [](VModuleKey) { return std::make_shared(); },
72 [&](orc::VModuleKey K) { return Resolvers[K]; }),
6673 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
6774 OptimizeLayer(CompileLayer,
6875 [this](std::shared_ptr M) {
7077 }),
7178 CompileCallbackManager(
7279 orc::createLocalCompileCallbackManager(TM->getTargetTriple(), 0)),
73 CODLayer(OptimizeLayer,
74 [](Function &F) { return std::set({&F}); },
80 CODLayer(ES, OptimizeLayer,
81 [&](orc::VModuleKey K) { return Resolvers[K]; },
82 [&](orc::VModuleKey K, std::shared_ptr R) {
83 Resolvers[K] = std::move(R);
84 },
85 [](Function &F) { return std::set({&F}); },
7586 *CompileCallbackManager,
7687 orc::createLocalIndirectStubsManagerBuilder(
77 TM->getTargetTriple())) {
88 TM->getTargetTriple())) {
7889 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
7990 }
8091
8192 TargetMachine &getTargetMachine() { return *TM; }
8293
8394 ModuleHandle addModule(std::unique_ptr M) {
84 // Build our symbol resolver:
85 // Lambda 1: Look back into the JIT itself to find symbols that are part of
86 // the same "logical dylib".
87 // Lambda 2: Search for external symbols in the host process.
88 auto Resolver = createLambdaResolver(
89 [&](const std::string &Name) {
90 if (auto Sym = CODLayer.findSymbol(Name, false))
95 // Create a new VModuleKey.
96 VModuleKey K = ES.allocateVModule();
97
98 // Build a resolver and associate it with the new key.
99 Resolvers[K] = createLegacyLookupResolver(
100 [this](const std::string &Name) -> JITSymbol {
101 if (auto Sym = CompileLayer.findSymbol(Name, false))
91102 return Sym;
92 return JITSymbol(nullptr);
103 else if (auto Err = Sym.takeError())
104 return std::move(Err);
105 if (auto SymAddr =
106 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
107 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
108 return nullptr;
93109 },
94 [](const std::string &Name) {
95 if (auto SymAddr =
96 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
97 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
98 return JITSymbol(nullptr);
99 });
110 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); });
100111
101 // Add the set to the JIT with the resolver we created above and a newly
102 // created SectionMemoryManager.
103 return cantFail(CODLayer.addModule(std::move(M), std::move(Resolver)));
112 // Add the module to the JIT with the new key.
113 return cantFail(CODLayer.addModule(K, std::move(M)));
104114 }
105115
106116 JITSymbol findSymbol(const std::string Name) {
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/ExecutionEngine.h"
1818 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
20 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
21 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
22 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
23 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
24 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
1925 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2026 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
21 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
23 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
24 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
25 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
26 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
2727 #include "llvm/IR/DataLayout.h"
2828 #include "llvm/IR/LegacyPassManager.h"
2929 #include "llvm/IR/Mangler.h"
3636 #include
3737 #include
3838 #include
39 #include
3940 #include
4041 #include
4142 #include
7071
7172 class KaleidoscopeJIT {
7273 private:
74 SymbolStringPool SSP;
75 ExecutionSession ES;
76 std::shared_ptr Resolver;
7377 std::unique_ptr TM;
7478 const DataLayout DL;
7579 RTDyldObjectLinkingLayer ObjectLayer;
8791 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
8892
8993 KaleidoscopeJIT()
90 : TM(EngineBuilder().selectTarget()),
91 DL(TM->createDataLayout()),
92 ObjectLayer([]() { return std::make_shared(); }),
94 : ES(SSP),
95 Resolver(createLegacyLookupResolver(
96 [this](const std::string &Name) -> JITSymbol {
97 if (auto Sym = IndirectStubsMgr->findStub(Name, false))
98 return Sym;
99 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
100 return Sym;
101 else if (auto Err = Sym.takeError())
102 return std::move(Err);
103 if (auto SymAddr =
104 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
105 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
106 return nullptr;
107 },
108 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
109 TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
110 ObjectLayer(
111 ES,
112 [](VModuleKey) { return std::make_shared(); },
113 [&](VModuleKey K) { return Resolver; }),
93114 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
94115 OptimizeLayer(CompileLayer,
95116 [this](std::shared_ptr M) {
106127 TargetMachine &getTargetMachine() { return *TM; }
107128
108129 ModuleHandle addModule(std::unique_ptr M) {
109 // Build our symbol resolver:
110 // Lambda 1: Look back into the JIT itself to find symbols that are part of
111 // the same "logical dylib".
112 // Lambda 2: Search for external symbols in the host process.
113 auto Resolver = createLambdaResolver(
114 [&](const std::string &Name) {
115 if (auto Sym = IndirectStubsMgr->findStub(Name, false))
116 return Sym;
117 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
118 return Sym;
119 return JITSymbol(nullptr);
120 },
121 [](const std::string &Name) {
122 if (auto SymAddr =
123 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
124 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
125 return JITSymbol(nullptr);
126 });
127
128 // Add the set to the JIT with the resolver we created above and a newly
129 // created SectionMemoryManager.
130 return cantFail(OptimizeLayer.addModule(std::move(M),
131 std::move(Resolver)));
130 // Add the module to the JIT with a new VModuleKey.
131 return cantFail(
132 OptimizeLayer.addModule(ES.allocateVModule(), std::move(M)));
132133 }
133134
134135 Error addFunctionAST(std::unique_ptr FnAST) {
1414 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
1515
1616 #include "RemoteJITUtils.h"
17 #include "llvm/ADT/STLExtras.h"
1718 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/STLExtras.h"
1919 #include "llvm/ADT/Triple.h"
2020 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2121 #include "llvm/ExecutionEngine/JITSymbol.h"
2222 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
23 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
2423 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2524 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
25 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
2626 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
27 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
2728 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
28 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
2929 #include "llvm/IR/DataLayout.h"
3030 #include "llvm/IR/LegacyPassManager.h"
3131 #include "llvm/IR/Mangler.h"
3838 #include
3939 #include
4040 #include
41 #include
4142 #include
4243 #include
4344 #include
7576
7677 class KaleidoscopeJIT {
7778 private:
79 SymbolStringPool SSP;
80 ExecutionSession ES;
81 std::shared_ptr Resolver;
7882 std::unique_ptr TM;
7983 const DataLayout DL;
8084 RTDyldObjectLinkingLayer ObjectLayer;
9397 using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
9498
9599 KaleidoscopeJIT(MyRemote &Remote)
96 : TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "",
100 : ES(SSP),
101 Resolver(createLegacyLookupResolver(
102 [this](const std::string &Name) -> JITSymbol {
103 if (auto Sym = IndirectStubsMgr->findStub(Name, false))
104 return Sym;
105 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
106 return Sym;
107 else if (auto Err = Sym.takeError())
108 return std::move(Err);
109 if (auto Addr = cantFail(this->Remote.getSymbolAddress(Name)))
110 return JITSymbol(Addr, JITSymbolFlags::Exported);
111 return nullptr;
112 },
113 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
114 TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "",
97115 "", SmallVector())),
98116 DL(TM->createDataLayout()),
99 ObjectLayer([&Remote]() {
100 return cantFail(Remote.createRemoteMemoryManager());
101 }),
117 ObjectLayer(ES,
118 [&Remote](VModuleKey) {
119 return cantFail(Remote.createRemoteMemoryManager());
120 },
121 [this](VModuleKey) { return Resolver; }),
102122 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
103123 OptimizeLayer(CompileLayer,
104124 [this](std::shared_ptr M) {
119139 TargetMachine &getTargetMachine() { return *TM; }
120140
121141 ModuleHandle addModule(std::unique_ptr M) {
122 // Build our symbol resolver:
123 // Lambda 1: Look back into the JIT itself to find symbols that are part of
124 // the same "logical dylib".
125 // Lambda 2: Search for external symbols in the host process.
126 auto Resolver = createLambdaResolver(
127 [&](const std::string &Name) {
128 if (auto Sym = IndirectStubsMgr->findStub(Name, false))
129 return Sym;
130 if (auto Sym = OptimizeLayer.findSymbol(Name, false))
131 return Sym;
132 return JITSymbol(nullptr);
133 },
134 [&](const std::string &Name) {
135 if (auto AddrOrErr = Remote.getSymbolAddress(Name))
136 return JITSymbol(*AddrOrErr, JITSymbolFlags::Exported);
137 else {
138 logAllUnhandledErrors(AddrOrErr.takeError(), errs(),
139 "Error resolving remote symbol:");
140 exit(1);
141 }
142 return JITSymbol(nullptr);
143 });
144
145 // Add the set to the JIT with the resolver we created above and a newly
146 // created SectionMemoryManager.
147 return cantFail(OptimizeLayer.addModule(std::move(M),
148 std::move(Resolver)));
142 // Add the module with a new VModuleKey.
143 return cantFail(
144 OptimizeLayer.addModule(ES.allocateVModule(), std::move(M)));
149145 }
150146
151147 Error addFunctionAST(std::unique_ptr FnAST) {
33 ExecutionEngine
44 InstCombine
55 Object
6 OrcJIT
67 RuntimeDyld
78 ScalarOpts
89 Support
33 ExecutionEngine
44 InstCombine
55 Object
6 OrcJIT
67 RuntimeDyld
78 ScalarOpts
89 Support
33 ExecutionEngine
44 InstCombine
55 Object
6 OrcJIT
67 RuntimeDyld
78 ScalarOpts
89 Support
33 ExecutionEngine
44 InstCombine
55 Object
6 OrcJIT
67 RuntimeDyld
78 ScalarOpts
89 Support
1313 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
1414 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
1515
16 #include "llvm/ADT/STLExtras.h"
1617 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/ADT/STLExtras.h"
1818 #include "llvm/ExecutionEngine/ExecutionEngine.h"
1919 #include "llvm/ExecutionEngine/JITSymbol.h"
20 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
21 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2220 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
2321 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2422 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
2523 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
25 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
2626 #include "llvm/IR/DataLayout.h"
2727 #include "llvm/IR/Mangler.h"
2828 #include "llvm/Support/DynamicLibrary.h"
2929 #include "llvm/Support/raw_ostream.h"
3030 #include "llvm/Target/TargetMachine.h"
3131 #include
32 #include
3233 #include
3334 #include
3435 #include
4344 using ModuleHandleT = CompileLayerT::ModuleHandleT;
4445
4546 KaleidoscopeJIT()
46 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
47 ObjectLayer([]() { return std::make_shared(); }),
47 : ES(SSP),
48 Resolver(createLegacyLookupResolver(
49 [this](const std::string &Name) {
50 return ObjectLayer.findSymbol(Name, true);
51 },
52 [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
53 TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
54 ObjectLayer(
55 ES,
56 [](VModuleKey) { return std::make_shared(); },
57 [this](VModuleKey) { return Resolver; }),
4858 CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
4959 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
5060 }
5262 TargetMachine &getTargetMachine() { return *TM; }
5363
5464 ModuleHandleT addModule(std::unique_ptr M) {
55 // We need a memory manager to allocate memory and resolve symbols for this
56 // new module. Create one that resolves symbols by looking back into the
57 // JIT.
58 auto Resolver = createLambdaResolver(
59 [&](const std::string &Name) {
60 if (auto Sym = findMangledSymbol(Name))
61 return Sym;
62 return JITSymbol(nullptr);
63 },
64 [](const std::string &S) { return nullptr; });
65 auto H = cantFail(CompileLayer.addModule(std::move(M),
66 std::move(Resolver)));
67
65 auto H =
66 cantFail(CompileLayer.addModule(ES.allocateVModule(), std::move(M)));
6867 ModuleHandles.push_back(H);
6968 return H;
7069 }
126125 return nullptr;
127126 }
128127
128 SymbolStringPool SSP;
129 ExecutionSession ES;
130 std::shared_ptr Resolver;
129131 std::unique_ptr TM;
130132 const DataLayout DL;
131133 ObjLayerT ObjectLayer;
1919 #include "llvm/ADT/StringRef.h"
2020 #include "llvm/ADT/Twine.h"
2121 #include "llvm/ExecutionEngine/JITSymbol.h"
22 #include "llvm/ExecutionEngine/Orc/Core.h"
2223 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
2324 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
2425 #include "llvm/ExecutionEngine/Orc/OrcError.h"
137138 };
138139
139140 struct LogicalDylib {
140 using SymbolResolverFtor = std::function;
141
142141 struct SourceModuleEntry {
143142 std::shared_ptr SourceMod;
144143 std::set StubsToClone;
182181 return Error::success();
183182 }
184183
185 std::shared_ptr ExternalSymbolResolver;
184 VModuleKey K;
185 std::shared_ptr BackingResolver;
186186 std::unique_ptr StubsMgr;
187187 StaticGlobalRenamer StaticRenamer;
188188 SourceModulesList SourceModules;
203203 using IndirectStubsManagerBuilderT =
204204 std::function()>;
205205
206 using SymbolResolverGetter =
207 std::function(VModuleKey K)>;
208
209 using SymbolResolverSetter =
210 std::function R)>;
211
206212 /// @brief Construct a compile-on-demand layer instance.
207 CompileOnDemandLayer(BaseLayerT &BaseLayer, PartitioningFtor Partition,
213 CompileOnDemandLayer(ExecutionSession &ES, BaseLayerT &BaseLayer,
214 SymbolResolverGetter GetSymbolResolver,
215 SymbolResolverSetter SetSymbolResolver,
216 PartitioningFtor Partition,
208217 CompileCallbackMgrT &CallbackMgr,
209218 IndirectStubsManagerBuilderT CreateIndirectStubsManager,
210219 bool CloneStubsIntoPartitions = true)
211 : BaseLayer(BaseLayer), Partition(std::move(Partition)),
212 CompileCallbackMgr(CallbackMgr),
220 : ES(ES), BaseLayer(BaseLayer),
221 GetSymbolResolver(std::move(GetSymbolResolver)),
222 SetSymbolResolver(std::move(SetSymbolResolver)),
223 Partition(std::move(Partition)), CompileCallbackMgr(CallbackMgr),
213224 CreateIndirectStubsManager(std::move(CreateIndirectStubsManager)),
214225 CloneStubsIntoPartitions(CloneStubsIntoPartitions) {}
215226
220231 }
221232
222233 /// @brief Add a module to the compile-on-demand layer.
223 Expected
224 addModule(std::shared_ptr M,
225 std::shared_ptr Resolver) {
234 Expected addModule(VModuleKey K, std::shared_ptr M) {
226235
227236 LogicalDylibs.push_back(LogicalDylib());
228237 auto &LD = LogicalDylibs.back();
229 LD.ExternalSymbolResolver = std::move(Resolver);
238 LD.K = std::move(K);
230239 LD.StubsMgr = CreateIndirectStubsManager();
231
232 // Process each of the modules in this module set.
240 LD.BackingResolver = GetSymbolResolver(LD.K);
241
233242 if (auto Err = addLogicalModule(LD, std::move(M)))
234243 return std::move(Err);
235244
453462 return MaterializerErrors;
454463
455464 // Build a resolver for the globals module and add it to the base layer.
456 auto GVsResolver = createLambdaResolver(
457 [this, &LD](const std::string &Name) -> JITSymbol {
458 if (auto Sym = LD.StubsMgr->findStub(Name, false))
459 return Sym;
460 if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
461 return Sym;
462 else if (auto Err = Sym.takeError())
463 return std::move(Err);
464 return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
465 auto LegacyLookup = [this, &LD](const std::string &Name) -> JITSymbol {
466 if (auto Sym = LD.StubsMgr->findStub(Name, false))
467 return Sym;
468 else if (auto Err = Sym.takeError())
469 return std::move(Err);
470
471 if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
472 return Sym;
473 else if (auto Err = Sym.takeError())
474 return std::move(Err);
475
476 return nullptr;
477 };
478
479 auto GVsResolver = createSymbolResolver(
480 [this, &LD, LegacyLookup](SymbolFlagsMap &SymbolFlags,
481 const SymbolNameSet &Symbols) {
482 auto NotFoundViaLegacyLookup =
483 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup);
484
485 if (!NotFoundViaLegacyLookup) {
486 logAllUnhandledErrors(NotFoundViaLegacyLookup.takeError(), errs(),
487 "CODLayer/GVsResolver flags lookup failed: ");
488 SymbolFlags.clear();
489 return SymbolNameSet();
490 }
491
492 return LD.BackingResolver->lookupFlags(SymbolFlags,
493 *NotFoundViaLegacyLookup);
465494 },
466 [&LD](const std::string &Name) {
467 return LD.ExternalSymbolResolver->findSymbol(Name);
495 [&LD, LegacyLookup](AsynchronousSymbolQuery &Query,
496 SymbolNameSet Symbols) {
497 auto NotFoundViaLegacyLookup =
498 lookupWithLegacyFn(Query, Symbols, LegacyLookup);
499 return LD.BackingResolver->lookup(Query, NotFoundViaLegacyLookup);
468500 });
469501
470 if (auto GVsHOrErr =
471 BaseLayer.addModule(std::move(GVsM), std::move(GVsResolver)))
502 SetSymbolResolver(LD.K, std::move(GVsResolver));
503
504 if (auto GVsHOrErr = BaseLayer.addModule(LD.K, std::move(GVsM)))
472505 LD.BaseLayerHandles.push_back(*GVsHOrErr);
473506 else
474507 return GVsHOrErr.takeError();
595628 for (auto *F : Part)
596629 moveFunctionBody(*F, VMap, &Materializer);
597630
631 auto K = ES.allocateVModule();
632
633 auto LegacyLookup = [this, &LD](const std::string &Name) -> JITSymbol {
634 return LD.findSymbol(BaseLayer, Name, false);
635 };
636
598637 // Create memory manager and symbol resolver.
599 auto Resolver = createLambdaResolver(
600 [this, &LD](const std::string &Name) -> JITSymbol {
601 if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
602 return Sym;
603 else if (auto Err = Sym.takeError())
604 return std::move(Err);
605 return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
638 auto Resolver = createSymbolResolver(
639 [this, &LD, LegacyLookup](SymbolFlagsMap &SymbolFlags,
640 const SymbolNameSet &Symbols) {
641 auto NotFoundViaLegacyLookup =
642 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup);
643 if (!NotFoundViaLegacyLookup) {
644 logAllUnhandledErrors(NotFoundViaLegacyLookup.takeError(), errs(),
645 "CODLayer/SubResolver flags lookup failed: ");
646 SymbolFlags.clear();
647 return SymbolNameSet();
648 }
649 return LD.BackingResolver->lookupFlags(SymbolFlags,
650 *NotFoundViaLegacyLookup);
606651 },
607 [&LD](const std::string &Name) {
608 return LD.ExternalSymbolResolver->findSymbol(Name);
652 [&LD, LegacyLookup](AsynchronousSymbolQuery &Q, SymbolNameSet Symbols) {
653 auto NotFoundViaLegacyLookup =
654 lookupWithLegacyFn(Q, Symbols, LegacyLookup);
655 return LD.BackingResolver->lookup(Q,
656 std::move(NotFoundViaLegacyLookup));
609657 });
610
611 return BaseLayer.addModule(std::move(M), std::move(Resolver));
612 }
613
658 SetSymbolResolver(K, std::move(Resolver));
659
660 return BaseLayer.addModule(std::move(K), std::move(M));
661 }
662
663 ExecutionSession &ES;
614664 BaseLayerT &BaseLayer;
665 SymbolResolverGetter GetSymbolResolver;
666 SymbolResolverSetter SetSymbolResolver;
615667 PartitioningFtor Partition;
616668 CompileCallbackMgrT &CompileCallbackMgr;
617669 IndirectStubsManagerBuilderT CreateIndirectStubsManager;
103103 Error runViaLayer(JITLayerT &JITLayer) const {
104104 using CtorDtorTy = void (*)();
105105
106 for (const auto &CtorDtorName : CtorDtorNames)
106 for (const auto &CtorDtorName : CtorDtorNames) {
107 dbgs() << "Searching for ctor/dtor: " << CtorDtorName << "...";
107108 if (auto CtorDtorSym = JITLayer.findSymbolIn(H, CtorDtorName, false)) {
109 dbgs() << " found symbol...";
108110 if (auto AddrOrErr = CtorDtorSym.getAddress()) {
111 dbgs() << " at addr " << format("0x%016x", *AddrOrErr) << "\n";
109112 CtorDtorTy CtorDtor =
110113 reinterpret_cast(static_cast(*AddrOrErr));
111114 CtorDtor();
112 } else
115 } else {
116 dbgs() << " failed materialization!\n";
113117 return AddrOrErr.takeError();
118 }
114119 } else {
120 dbgs() << " failed to find symbol...";
115121 if (auto Err = CtorDtorSym.takeError())
116122 return Err;
117123 else
118124 return make_error(CtorDtorName);
119125 }
126 }
120127 return Error::success();
121128 }
122129
1515
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/ExecutionEngine/Orc/Core.h"
1819 #include "llvm/Support/Error.h"
1920 #include
2021 #include
4950 /// along with the given memory manager and symbol resolver.
5051 ///
5152 /// @return A handle for the added module.
52 Expected
53 addModule(std::shared_ptr M,
54 std::shared_ptr Resolver) {
53 Expected addModule(VModuleKey K, std::shared_ptr M) {
5554 using CompileResult = decltype(Compile(*M));
5655 auto Obj = std::make_shared(Compile(*M));
57 return BaseLayer.addObject(std::move(Obj), std::move(Resolver));
56 return BaseLayer.addObject(std::move(K), std::move(Obj));
5857 }
5958
6059 /// @brief Remove the module associated with the handle H.
1414 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
1515
1616 #include "llvm/ExecutionEngine/JITSymbol.h"
17 #include "llvm/ExecutionEngine/Orc/Core.h"
1718 #include
1819 #include
1920
4142 /// the layer below, along with the memory manager and symbol resolver.
4243 ///
4344 /// @return A handle for the added modules.
44 Expected
45 addModule(std::shared_ptr M,
46 std::shared_ptr Resolver) {
47 return BaseLayer.addModule(Transform(std::move(M)), std::move(Resolver));
45 Expected addModule(VModuleKey K, std::shared_ptr M) {
46 return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
4847 }
4948
5049 /// @brief Remove the module associated with the handle H.
1717 #include "llvm/ADT/StringMap.h"
1818 #include "llvm/ADT/StringRef.h"
1919 #include "llvm/ExecutionEngine/JITSymbol.h"
20 #include "llvm/ExecutionEngine/Orc/Core.h"
2021 #include "llvm/IR/GlobalValue.h"
2122 #include "llvm/IR/Mangler.h"
2223 #include "llvm/IR/Module.h"
4546 private:
4647 class EmissionDeferredModule {
4748 public:
48 EmissionDeferredModule(std::shared_ptr M,
49 std::shared_ptr Resolver)
50 : M(std::move(M)), Resolver(std::move(Resolver)) {}
49 EmissionDeferredModule(VModuleKey K, std::shared_ptr M)
50 : K(std::move(K)), M(std::move(M)) {}
5151
5252 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
5353 switch (EmitState) {
138138 // We don't need the mangled names set any more: Once we've emitted this
139139 // to the base layer we'll just look for symbols there.
140140 MangledSymbols.reset();
141 return BaseLayer.addModule(std::move(M), std::move(Resolver));
141 return BaseLayer.addModule(std::move(K), std::move(M));
142142 }
143143
144144 // If the mangled name of the given GlobalValue matches the given search
192192
193193 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
194194 BaseLayerHandleT Handle;
195 VModuleKey K;
195196 std::shared_ptr M;
196 std::shared_ptr Resolver;
197197 mutable std::unique_ptr> MangledSymbols;
198198 };
199199
211211 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
212212
213213 /// @brief Add the given module to the lazy emitting layer.
214 Expected
215 addModule(std::shared_ptr M,
216 std::shared_ptr Resolver) {
214 Expected addModule(VModuleKey K, std::shared_ptr M) {
217215 return ModuleList.insert(
218216 ModuleList.end(),
219 llvm::make_unique(std::move(M),
220 std::move(Resolver)));
217 llvm::make_unique(std::move(K), std::move(M)));
221218 }
222219
223220 /// @brief Remove the module represented by the given handle.
9191 return SymbolsNotFound;
9292 }
9393
94 /// @brief An ORC SymbolResolver implementation that uses a legacy
95 /// findSymbol-like function to perform lookup;
96 template
97 class LegacyLookupFnResolver final : public SymbolResolver {
98 public:
99 using ErrorReporter = std::function;
100
101 LegacyLookupFnResolver(LegacyLookupFn LegacyLookup, ErrorReporter ReportError)
102 : LegacyLookup(std::move(LegacyLookup)),
103 ReportError(std::move(ReportError)) {}
104
105 SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
106 const SymbolNameSet &Symbols) final {
107 if (auto RemainingSymbols =
108 lookupFlagsWithLegacyFn(Flags, Symbols, LegacyLookup))
109 return std::move(*RemainingSymbols);
110 else {
111 ReportError(RemainingSymbols.takeError());
112 return Symbols;
113 }
114 }
115
116 SymbolNameSet lookup(AsynchronousSymbolQuery &Query,
117 SymbolNameSet Symbols) final {
118 return lookupWithLegacyFn(Query, Symbols, LegacyLookup);
119 }
120
121 private:
122 LegacyLookupFn LegacyLookup;
123 ErrorReporter ReportError;
124 };
125
126 template
127 std::shared_ptr>
128 createLegacyLookupResolver(LegacyLookupFn LegacyLookup,
129 std::function ErrorReporter) {
130 return std::make_shared>(
131 std::move(LegacyLookup), std::move(ErrorReporter));
132 };
133
94134 } // End namespace orc
95135 } // End namespace llvm
96136
1414 #ifndef LLVM_EXECUTIONENGINE_ORC_NULLRESOLVER_H
1515 #define LLVM_EXECUTIONENGINE_ORC_NULLRESOLVER_H
1616
17 #include "llvm/ExecutionEngine/Orc/Core.h"
1718 #include "llvm/ExecutionEngine/RuntimeDyld.h"
1819
1920 namespace llvm {
2021 namespace orc {
22
23 class NullResolver : public SymbolResolver {
24 public:
25 SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
26 const SymbolNameSet &Symbols) override;
27
28 SymbolNameSet lookup(AsynchronousSymbolQuery &Query,
29 SymbolNameSet Symbols) override;
30 };
2131
2232 /// SymbolResolver impliementation that rejects all resolution requests.
2333 /// Useful for clients that have no cross-object fixups.
1414 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
1515
1616 #include "llvm/ExecutionEngine/JITSymbol.h"
17 #include "llvm/ExecutionEngine/Orc/Core.h"
1718 #include
1819 #include
1920 #include
4344 ///
4445 /// @return A handle for the added objects.
4546 template
46 Expected addObject(ObjectPtr Obj,
47 std::shared_ptr Resolver) {
48 return BaseLayer.addObject(Transform(std::move(Obj)), std::move(Resolver));
47 Expected addObject(VModuleKey K, ObjectPtr Obj) {
48 return BaseLayer.addObject(std::move(K), Transform(std::move(Obj)));
4949 }
5050
5151 /// @brief Remove the object set associated with the handle H.
1717 #include "llvm/ADT/StringMap.h"
1818 #include "llvm/ADT/StringRef.h"
1919 #include "llvm/ExecutionEngine/JITSymbol.h"
20 #include "llvm/ExecutionEngine/Orc/Core.h"
21 #include "llvm/ExecutionEngine/Orc/Legacy.h"
2022 #include "llvm/ExecutionEngine/RuntimeDyld.h"
2123 #include "llvm/Object/ObjectFile.h"
2224 #include "llvm/Support/Error.h"
5456 void operator=(const LinkedObject&) = delete;
5557 virtual ~LinkedObject() = default;
5658
57 virtual void finalize() = 0;
59 virtual Error finalize() = 0;
5860
5961 virtual JITSymbol::GetAddressFtor
6062 getSymbolMaterializer(std::string Name) = 0;
106108 using NotifyFinalizedFtor = std::function;
107109
108110 private:
109
110
111 template
112 typename FinalizerFtor>
111 template typename FinalizerFtor>
113112 class ConcreteLinkedObject : public LinkedObject {
114113 public:
115 ConcreteLinkedObject(ObjectPtr Obj, MemoryManagerPtrT MemMgr,
116 SymbolResolverPtrT Resolver,
117 FinalizerFtor Finalizer,
118 bool ProcessAllSections)
119 : MemMgr(std::move(MemMgr)),
120 PFC(llvm::make_unique(std::move(Obj),
121 std::move(Resolver),
122 std::move(Finalizer),
123 ProcessAllSections)) {
114 ConcreteLinkedObject(ExecutionSession &ES, ObjectPtr Obj,
115 MemoryManagerPtrT MemMgr,
116 std::shared_ptr Resolver,
117 FinalizerFtor Finalizer, bool ProcessAllSections)
118 : MemMgr(std::move(MemMgr)),
119 PFC(llvm::make_unique(
120 ES, std::move(Obj), std::move(Resolver), std::move(Finalizer),
121 ProcessAllSections)) {
124122 buildInitialSymbolTable(PFC->Obj);
125123 }
126124
132130 PFC->Handle = H;
133131 }
134132
135 void finalize() override {
133 Error finalize() override {
136134 assert(PFC && "mapSectionAddress called on finalized LinkedObject");
137135
138 RuntimeDyld RTDyld(*MemMgr, *PFC->Resolver);
136 JITSymbolResolverAdapter ResolverAdapter(PFC->ES, *PFC->Resolver);
137 RuntimeDyld RTDyld(*MemMgr, ResolverAdapter);
139138 RTDyld.setProcessAllSections(PFC->ProcessAllSections);
140139 PFC->RTDyld = &RTDyld;
141140
142141 this->Finalized = true;
143 PFC->Finalizer(PFC->Handle, RTDyld, std::move(PFC->Obj),
144 [&]() {
145 this->updateSymbolTable(RTDyld);
146 });
142 auto Err = PFC->Finalizer(PFC->Handle, RTDyld, std::move(PFC->Obj),
143 [&]() { this->updateSymbolTable(RTDyld); });
147144
148145 // Release resources.
149146 PFC = nullptr;
147 return Err;
150148 }
151149
152150 JITSymbol::GetAddressFtor getSymbolMaterializer(std::string Name) override {
153 return
154 [this, Name]() {
155 // The symbol may be materialized between the creation of this lambda
156 // and its execution, so we need to double check.
157 if (!this->Finalized)
158 this->finalize();
159 return this->getSymbol(Name, false).getAddress();
160 };
151 return [this, Name]() -> Expected {
152 // The symbol may be materialized between the creation of this lambda
153 // and its execution, so we need to double check.
154 if (!this->Finalized)
155 if (auto Err = this->finalize())
156 return std::move(Err);
157 return this->getSymbol(Name, false).getAddress();
158 };
161159 }
162160
163161 void mapSectionAddress(const void *LocalAddress,
193191 // Contains the information needed prior to finalization: the object files,
194192 // memory manager, resolver, and flags needed for RuntimeDyld.
195193 struct PreFinalizeContents {
196 PreFinalizeContents(ObjectPtr Obj, SymbolResolverPtrT Resolver,
194 PreFinalizeContents(ExecutionSession &ES, ObjectPtr Obj,
195 std::shared_ptr Resolver,
197196 FinalizerFtor Finalizer, bool ProcessAllSections)
198 : Obj(std::move(Obj)), Resolver(std::move(Resolver)),
199 Finalizer(std::move(Finalizer)),
200 ProcessAllSections(ProcessAllSections) {}
201
197 : ES(ES), Obj(std::move(Obj)), Resolver(std::move(Resolver)),
198 Finalizer(std::move(Finalizer)),
199 ProcessAllSections(ProcessAllSections) {}
200
201 ExecutionSession &ES;
202202 ObjectPtr Obj;
203 SymbolResolverPtrT Resolver;
203 std::shared_ptr Resolver;
204204 FinalizerFtor Finalizer;
205205 bool ProcessAllSections;
206206 ObjHandleT Handle;
211211 std::unique_ptr PFC;
212212 };
213213
214 template
215 typename FinalizerFtor>
216 std::unique_ptr<
217 ConcreteLinkedObject>
218 createLinkedObject(ObjectPtr Obj, MemoryManagerPtrT MemMgr,
219 SymbolResolverPtrT Resolver,
220 FinalizerFtor Finalizer,
221 bool ProcessAllSections) {
222 using LOS = ConcreteLinkedObject
223 FinalizerFtor>;
224 return llvm::make_unique(std::move(Obj), std::move(MemMgr),
214 template
215 std::unique_ptr>
216 createLinkedObject(ExecutionSession &ES, ObjectPtr Obj,
217 MemoryManagerPtrT MemMgr,
218 std::shared_ptr Resolver,
219 FinalizerFtor Finalizer, bool ProcessAllSections) {
220 using LOS = ConcreteLinkedObject;
221 return llvm::make_unique(ES, std::move(Obj), std::move(MemMgr),
225222 std::move(Resolver), std::move(Finalizer),
226223 ProcessAllSections);
227224 }
230227
231228 /// @brief Functor for creating memory managers.
232229 using MemoryManagerGetter =
233 std::function()>;
230 std::function(VModuleKey)>;
231
232 using ResolverGetter =
233 std::function(VModuleKey)>;
234234
235235 /// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded,
236236 /// and NotifyFinalized functors.
237237 RTDyldObjectLinkingLayer(
238 MemoryManagerGetter GetMemMgr,
238 ExecutionSession &ES, MemoryManagerGetter GetMemMgr,
239 ResolverGetter GetResolver,
239240 NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(),
240241 NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor())
241 : GetMemMgr(GetMemMgr),
242 : ES(ES), GetMemMgr(std::move(GetMemMgr)),
243 GetResolver(std::move(GetResolver)),
242244 NotifyLoaded(std::move(NotifyLoaded)),
243 NotifyFinalized(std::move(NotifyFinalized)),
244 ProcessAllSections(false) {}
245 NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {
246 }
245247
246248 /// @brief Set the 'ProcessAllSections' flag.
247249 ///
257259 ///
258260 /// @return A handle that can be used to refer to the loaded object (for
259261 /// symbol searching, finalization, freeing memory, etc.).
260 Expected addObject(ObjectPtr Obj,
261 std::shared_ptr Resolver) {
262 Expected addObject(VModuleKey K, ObjectPtr Obj) {
262263 auto Finalizer = [&](ObjHandleT H, RuntimeDyld &RTDyld,
263264 const ObjectPtr &ObjToLoad,
264 std::function LOSHandleLoad) {
265 std::function LOSHandleLoad) -> Error {
265266 std::unique_ptr Info =
266267 RTDyld.loadObject(*ObjToLoad->getBinary());
267268
272273
273274 RTDyld.finalizeWithMemoryManagerLocking();
274275
276 if (RTDyld.hasError())
277 return make_error(RTDyld.getErrorString(),
278 inconvertibleErrorCode());
279
275280 if (this->NotifyFinalized)
276281 this->NotifyFinalized(H);
282
283 return Error::success();
277284 };
278285
279286 auto LO =
280 createLinkedObject(std::move(Obj), GetMemMgr(),
281 std::move(Resolver), std::move(Finalizer),
282 ProcessAllSections);
287 createLinkedObject(ES, std::move(Obj), GetMemMgr(K), GetResolver(K),
288 std::move(Finalizer), ProcessAllSections);
283289 // LOS is an owning-ptr. Keep a non-owning one so that we can set the handle
284290 // below.
285291 auto *LOPtr = LO.get();
338344 /// @brief Immediately emit and finalize the object represented by the given
339345 /// handle.
340346 /// @param H Handle for object to emit/finalize.
341 Error emitAndFinalize(ObjHandleT H) {
342 (*H)->finalize();
343 return Error::success();
344 }
347 Error emitAndFinalize(ObjHandleT H) { return (*H)->finalize(); }
345348
346349 private:
347
350 ExecutionSession &ES;
348351 LinkedObjectListT LinkedObjList;
349352 MemoryManagerGetter GetMemMgr;
353 ResolverGetter GetResolver;
350354 NotifyLoadedFtor NotifyLoaded;
351355 NotifyFinalizedFtor NotifyFinalized;
352356 bool ProcessAllSections = false;
4444
4545 auto UnresolvedSymbols = R.lookup(Query, InternedSymbols);
4646
47 if (!UnresolvedSymbols.empty())
48 Err = joinErrors(std::move(Err),
49 make_error("Unresolved symbols",
50 inconvertibleErrorCode()));
47 if (!UnresolvedSymbols.empty()) {
48 std::string ErrorMsg = "Unresolved symbols: ";
49
50 ErrorMsg += **UnresolvedSymbols.begin();
51 for (auto I = std::next(UnresolvedSymbols.begin()),
52 E = UnresolvedSymbols.end();
53 I != E; ++I) {
54 ErrorMsg += ", ";
55 ErrorMsg += **I;
56 }
57
58 Err =
59 joinErrors(std::move(Err),
60 make_error(ErrorMsg, inconvertibleErrorCode()));
61 }
5162
5263 if (Err)
5364 return std::move(Err);
1313 namespace llvm {
1414 namespace orc {
1515
16 SymbolNameSet NullResolver::lookupFlags(SymbolFlagsMap &Flags,
17 const SymbolNameSet &Symbols) {
18 return Symbols;
19 }
20
21 SymbolNameSet NullResolver::lookup(AsynchronousSymbolQuery &Query,
22 SymbolNameSet Symbols) {
23 assert(Symbols.empty() && "Null resolver: Symbols must be empty");
24 return Symbols;
25 }
26
1627 JITSymbol NullLegacyResolver::findSymbol(const std::string &Name) {
1728 llvm_unreachable("Unexpected cross-object symbol reference");
1829 }
5151
5252 class GenericHandle {
5353 public:
54 GenericHandle(orc::VModuleKey K) : K(K) {}
55
5456 virtual ~GenericHandle() = default;
5557
5658 virtual JITSymbol findSymbolIn(const std::string &Name,
5759 bool ExportedSymbolsOnly) = 0;
58 virtual Error removeModule() = 0;
60 virtual Error removeModule(orc::ExecutionSession &ES) = 0;
61
62 orc::VModuleKey K;
5963 };
6064
6165 template class GenericHandleImpl : public GenericHandle {
6266 public:
63 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle)
64 : Layer(Layer), Handle(std::move(Handle)) {}
67 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle,
68 orc::VModuleKey K)
69 : GenericHandle(std::move(K)), Layer(Layer), Handle(std::move(Handle)) {
70 }
6571
6672 JITSymbol findSymbolIn(const std::string &Name,
6773 bool ExportedSymbolsOnly) override {
6874 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
6975 }
7076
71 Error removeModule() override { return Layer.removeModule(Handle); }
77 Error removeModule(orc::ExecutionSession &ES) override {
78 auto Err = Layer.removeModule(Handle);
79 ES.releaseVModule(K);
80 return Err;
81 }
7282
7383 private:
7484 LayerT &Layer;
8191 private:
8292 using LayerT = orc::RTDyldObjectLinkingLayer;
8393 public:
84
85 GenericHandleImpl(LayerT &Layer, typename LayerT::ObjHandleT Handle)
86 : Layer(Layer), Handle(std::move(Handle)) {}
94 GenericHandleImpl(LayerT &Layer, typename LayerT::ObjHandleT Handle,
95 orc::VModuleKey K)
96 : GenericHandle(std::move(K)), Layer(Layer), Handle(std::move(Handle)) {
97 }
8798
8899 JITSymbol findSymbolIn(const std::string &Name,
89100 bool ExportedSymbolsOnly) override {
90101 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
91102 }
92103
93 Error removeModule() override { return Layer.removeObject(Handle); }
104 Error removeModule(orc::ExecutionSession &ES) override {
105 auto Err = Layer.removeObject(Handle);
106 ES.releaseVModule(K);
107 return Err;
108 }
94109
95110 private:
96111 LayerT &Layer;
97112 typename LayerT::ObjHandleT Handle;
98113 };
99114
100
101115 template
102116 std::unique_ptr>
103 createGenericHandle(LayerT &Layer, HandleT Handle) {
104 return llvm::make_unique>(Layer,
105 std::move(Handle));
117 createGenericHandle(LayerT &Layer, HandleT Handle, orc::VModuleKey K) {
118 return llvm::make_unique>(
119 Layer, std::move(Handle), std::move(K));
106120 }
107121
108122 } // end namespace detail
125139
126140 using OwningObject = object::OwningBinary;
127141
142 class CBindingsResolver : public orc::SymbolResolver {
143 public:
144 CBindingsResolver(OrcCBindingsStack &Stack,
145 LLVMOrcSymbolResolverFn ExternalResolver,
146 void *ExternalResolverCtx)
147 : Stack(Stack), ExternalResolver(std::move(ExternalResolver)),
148 ExternalResolverCtx(std::move(ExternalResolverCtx)) {}
149
150 orc::SymbolNameSet lookupFlags(orc::SymbolFlagsMap &SymbolFlags,
151 const orc::SymbolNameSet &Symbols) override {
152 orc::SymbolNameSet SymbolsNotFound;
153
154 for (auto &S : Symbols) {
155 if (auto Sym = findSymbol(*S))
156 SymbolFlags[S] = Sym.getFlags();
157 else if (auto Err = Sym.takeError()) {
158 Stack.reportError(std::move(Err));
159 return {};
160 } else
161 SymbolsNotFound.insert(S);
162 }
163
164 return SymbolsNotFound;
165 }
166
167 orc::SymbolNameSet lookup(orc::AsynchronousSymbolQuery &Query,
168 orc::SymbolNameSet Symbols) override {
169 orc::SymbolNameSet UnresolvedSymbols;
170
171 for (auto &S : Symbols) {
172 if (auto Sym = findSymbol(*S)) {
173 if (auto Addr = Sym.getAddress())
174 Query.setDefinition(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
175 else {
176 Query.setFailed(Addr.takeError());
177 return {};
178 }
179 } else if (auto Err = Sym.takeError()) {
180 Query.setFailed(std::move(Err));
181 return {};
182 } else
183 UnresolvedSymbols.insert(S);
184 }
185
186 return UnresolvedSymbols;
187 }
188
189 private:
190 JITSymbol findSymbol(const std::string &Name) {
191 // Search order:
192 // 1. JIT'd symbols.
193 // 2. Runtime overrides.
194 // 3. External resolver (if present).
195
196 if (auto Sym = Stack.CODLayer.findSymbol(Name, true))
197 return Sym;
198 else if (auto Err = Sym.takeError())
199 return Sym.takeError();
200
201 if (auto Sym = Stack.CXXRuntimeOverrides.searchOverrides(Name))
202 return Sym;
203
204 if (ExternalResolver)
205 return JITSymbol(ExternalResolver(Name.c_str(), ExternalResolverCtx),
206 JITSymbolFlags::Exported);
207
208 return JITSymbol(nullptr);
209 }
210
211 OrcCBindingsStack &Stack;
212 LLVMOrcSymbolResolverFn ExternalResolver;
213 void *ExternalResolverCtx = nullptr;
214 };
215
128216 public:
129217 using ModuleHandleT = unsigned;
130218
131219 OrcCBindingsStack(TargetMachine &TM,
132220 std::unique_ptr CCMgr,
133221 IndirectStubsManagerBuilder IndirectStubsMgrBuilder)
134 : DL(TM.createDataLayout()), IndirectStubsMgr(IndirectStubsMgrBuilder()),
135 CCMgr(std::move(CCMgr)),
136 ObjectLayer(
137 []() {
138 return std::make_shared();
139 }),
222 : ES(SSP), DL(TM.createDataLayout()),
223 IndirectStubsMgr(IndirectStubsMgrBuilder()), CCMgr(std::move(CCMgr)),
224 ObjectLayer(ES,
225 [](orc::VModuleKey K) {
226 return std::make_shared();
227 },
228 [this](orc::VModuleKey K) {
229 auto ResolverI = Resolvers.find(K);
230 assert(ResolverI != Resolvers.end() &&
231 "No resolver for module K");
232 auto Resolver = std::move(ResolverI->second);
233 Resolvers.erase(ResolverI);
234 return Resolver;
235 }),
140236 CompileLayer(ObjectLayer, orc::SimpleCompiler(TM)),
141 CODLayer(CompileLayer,
237 CODLayer(ES, CompileLayer,
238 [this](orc::VModuleKey K) {
239 auto ResolverI = Resolvers.find(K);
240 assert(ResolverI != Resolvers.end() &&
241 "No resolver for module K");
242 return ResolverI->second;
243 },
244 [this](orc::VModuleKey K,
245 std::shared_ptr Resolver) {
246 assert(!Resolvers.count(K) && "Resolver already present");
247 Resolvers[K] = std::move(Resolver);
248 },
142249 [](Function &F) { return std::set({&F}); },
143250 *this->CCMgr, std::move(IndirectStubsMgrBuilder), false),
144251 CXXRuntimeOverrides(
194301 JITTargetAddress Addr) {
195302 return mapError(IndirectStubsMgr->updatePointer(Name, Addr));
196303 }
197
198 std::shared_ptr
199 createResolver(LLVMOrcSymbolResolverFn ExternalResolver,
200 void *ExternalResolverCtx) {
201 return orc::createLambdaResolver(
202 [this, ExternalResolver, ExternalResolverCtx](const std::string &Name)
203 -> JITSymbol {
204 // Search order:
205 // 1. JIT'd symbols.
206 // 2. Runtime overrides.
207 // 3. External resolver (if present).
208
209 if (auto Sym = CODLayer.findSymbol(Name, true))
210 return Sym;
211 else if (auto Err = Sym.takeError())
212 return Sym.takeError();
213
214 if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name))
215 return Sym;
216
217 if (ExternalResolver)
218 return JITSymbol(
219 ExternalResolver(Name.c_str(), ExternalResolverCtx),
220 JITSymbolFlags::Exported);
221
222 return JITSymbol(nullptr);
223 },
224 [](const std::string &Name) -> JITSymbol {
225 return JITSymbol(nullptr);
226 });
227 }
228
229304 template
230305 LLVMOrcErrorCode
231306 addIRModule(ModuleHandleT &RetHandle, LayerT &Layer,
246321 for (auto Dtor : orc::getDestructors(*M))
247322 DtorNames.push_back(mangle(Dtor.Func->getName()));
248323
249 // Create the resolver.
250 auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx);
251
252324 // Add the module to the JIT.
253325 ModuleHandleT H;
254 if (auto LHOrErr = Layer.addModule(std::move(M), std::move(Resolver)))
255 H = createHandle(Layer, *LHOrErr);
326 orc::VModuleKey K = ES.allocateVModule();
327 Resolvers[K] = std::make_shared(*this, ExternalResolver,
328 ExternalResolverCtx);
329 if (auto LHOrErr = Layer.addModule(K, std::move(M)))
330 H = createHandle(Layer, *LHOrErr, K);
256331 else
257332 return mapError(LHOrErr.takeError());
258333
287362 }
288363
289364 LLVMOrcErrorCode removeModule(ModuleHandleT H) {
290 if (auto Err = GenericHandles[H]->removeModule())
365 if (auto Err = GenericHandles[H]->removeModule(ES))
291366 return mapError(std::move(Err));
292367 GenericHandles[H] = nullptr;
293368 FreeHandleIndexes.push_back(H);
304379 auto OwningObj =
305380 std::make_shared(std::move(Obj), std::move(ObjBuffer));
306381
307 // Create the resolver.
308 auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx);
382 orc::VModuleKey K = ES.allocateVModule();
383 Resolvers[K] = std::make_shared(
384 *this, ExternalResolver, ExternalResolverCtx);
309385
310386 ModuleHandleT H;
311 if (auto HOrErr = ObjectLayer.addObject(std::move(OwningObj),
312 std::move(Resolver)))
313 H = createHandle(ObjectLayer, *HOrErr);
387 if (auto HOrErr = ObjectLayer.addObject(K, std::move(OwningObj)))
388 H = createHandle(ObjectLayer, *HOrErr, K);
314389 else
315390 return mapError(HOrErr.takeError());
316391
357432
358433 private:
359434 template
360 unsigned createHandle(LayerT &Layer, HandleT Handle) {
435 unsigned createHandle(LayerT &Layer, HandleT Handle, orc::VModuleKey K) {
361436 unsigned NewHandle;
362437 if (!FreeHandleIndexes.empty()) {
363438 NewHandle = FreeHandleIndexes.back();
364439 FreeHandleIndexes.pop_back();
365440 GenericHandles[NewHandle] =
366 detail::createGenericHandle(Layer, std::move(Handle));
441 detail::createGenericHandle(Layer, std::move(Handle), std::move(K));
367442 return NewHandle;
368443 } else {
369444 NewHandle = GenericHandles.size();
370445 GenericHandles.push_back(
371 detail::createGenericHandle(Layer, std::move(Handle)));
446 detail::createGenericHandle(Layer, std::move(Handle), std::move(K)));
372447 }
373448 return NewHandle;
374449 }
385460 return Result;
386461 }
387462
463 void reportError(Error Err) {
464 // FIXME: Report errors on the execution session.
465 logAllUnhandledErrors(std::move(Err), errs(), "ORC error: ");
466 };
467
468 orc::SymbolStringPool SSP;
469 orc::ExecutionSession ES;
470
388471 DataLayout DL;
389472 SectionMemoryManager CCMgrMemMgr;
390473
401484 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
402485 std::vector> IRStaticDestructorRunners;
403486 std::string ErrMsg;
487
488 std::map> Resolvers;
404489 };
405490
406491 } // end namespace llvm
137137 std::shared_ptr ClientMM;
138138 };
139139
140 class LinkingResolver : public LegacyJITSymbolResolver {
140 class LinkingORCResolver : public orc::SymbolResolver {
141141 public:
142 LinkingResolver(OrcMCJITReplacement &M) : M(M) {}
143
144 JITSymbol findSymbol(const std::string &Name) override {
145 return M.ClientResolver->findSymbol(Name);
146 }
147
148 JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
149 if (auto Sym = M.findMangledSymbol(Name))
150 return Sym;
151 return M.ClientResolver->findSymbolInLogicalDylib(Name);
142 LinkingORCResolver(OrcMCJITReplacement &M) : M(M) {}
143
144 SymbolNameSet lookupFlags(SymbolFlagsMap &SymbolFlags,
145 const SymbolNameSet &Symbols) override {
146 SymbolNameSet UnresolvedSymbols;
147
148 for (auto &S : Symbols) {
149 if (auto Sym = M.findMangledSymbol(*S)) {
150 SymbolFlags[S] = Sym.getFlags();
151 } else if (auto Err = Sym.takeError()) {
152 M.reportError(std::move(Err));
153 return SymbolNameSet();
154 } else {
155 if (auto Sym2 = M.ClientResolver->findSymbolInLogicalDylib(*S)) {
156 SymbolFlags[S] = Sym2.getFlags();
157 } else if (auto Err = Sym2.takeError()) {
158 M.reportError(std::move(Err));
159 return SymbolNameSet();
160 } else
161 UnresolvedSymbols.insert(S);
162 }
163 }
164
165 return UnresolvedSymbols;
166 }
167
168 SymbolNameSet lookup(AsynchronousSymbolQuery &Query,
169 SymbolNameSet Symbols) override {
170 SymbolNameSet UnresolvedSymbols;
171
172 for (auto &S : Symbols) {
173 if (auto Sym = M.findMangledSymbol(*S)) {
174 if (auto Addr = Sym.getAddress())
175 Query.setDefinition(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
176 else {
177 Query.setFailed(Addr.takeError());
178 return SymbolNameSet();
179 }
180 } else if (auto Err = Sym.takeError()) {
181 Query.setFailed(std::move(Err));
182 return SymbolNameSet();
183 } else {
184 if (auto Sym2 = M.ClientResolver->findSymbol(*S)) {
185 if (auto Addr = Sym2.getAddress())
186 Query.setDefinition(S,
187 JITEvaluatedSymbol(*Addr, Sym2.getFlags()));
188 else {
189 Query.setFailed(Addr.takeError());
190 return SymbolNameSet();
191 }
192 } else if (auto Err = Sym2.takeError()) {
193 Query.setFailed(std::move(Err));
194 return SymbolNameSet();
195 } else
196 UnresolvedSymbols.insert(S);
197 }
198 }
199
200 return UnresolvedSymbols;
152201 }
153202
154203 private:
165214 std::move(TM));
166215 }
167216
217 void reportError(Error Err) {
218 logAllUnhandledErrors(std::move(Err), errs(), "MCJIT error: ");
219 }
220
168221 public:
169222 OrcMCJITReplacement(std::shared_ptr MemMgr,
170223 std::shared_ptr ClientResolver,
171224 std::unique_ptr TM)
172 : ExecutionEngine(TM->createDataLayout()), TM(std::move(TM)),
225 : ExecutionEngine(TM->createDataLayout()), ES(SSP), TM(std::move(TM)),
173226 MemMgr(
174227 std::make_shared(*this, std::move(MemMgr))),
175 Resolver(std::make_sharedResolver>(*this)),
228 Resolver(std::make_sharedORCResolver>(*this)),
176229 ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
177230 NotifyFinalized(*this),
178 ObjectLayer([this]() { return this->MemMgr; }, NotifyObjectLoaded,
179 NotifyFinalized),
231 ObjectLayer(ES, [this](VModuleKey K) { return this->MemMgr; },
232 [this](VModuleKey K) { return this->Resolver; },
233 NotifyObjectLoaded, NotifyFinalized),
180234 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
181235 LazyEmitLayer(CompileLayer) {}
182236
200254 delete Mod;
201255 };
202256 LocalModules.push_back(std::shared_ptr(MPtr, std::move(Deleter)));
203 cantFail(LazyEmitLayer.addModule(LocalModules.back(), Resolver));
257 cantFail(
258 LazyEmitLayer.addModule(ES.allocateVModule(), LocalModules.back()));
204259 }
205260
206261 void addObjectFile(std::unique_ptr O) override {
207262 auto Obj =
208263 std::make_shared>(std::move(O),
209264 nullptr);
210 cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
265 cantFail(ObjectLayer.addObject(ES.allocateVModule(), std::move(Obj)));
211266 }
212267
213268 void addObjectFile(object::OwningBinary O) override {
214269 auto Obj =
215270 std::make_shared>(std::move(O));
216 cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
271 cantFail(ObjectLayer.addObject(ES.allocateVModule(), std::move(Obj)));
217272 }
218273
219274 void addArchive(object::OwningBinary A) override {
321376 auto Obj =
322377 std::make_shared>(
323378 std::move(ChildObj), nullptr);
324 cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
379 cantFail(ObjectLayer.addObject(ES.allocateVModule(), std::move(Obj)));
325380 if (auto Sym = ObjectLayer.findSymbol(Name, true))
326381 return Sym;
327382 }
373428 using CompileLayerT = IRCompileLayer;
374429 using LazyEmitLayerT = LazyEmittingLayer;
375430
431 SymbolStringPool SSP;
432 ExecutionSession ES;
433
376434 std::unique_ptr TM;
377435 std::shared_ptr MemMgr;
378 std::shared_ptrResolver> Resolver;
436 std::shared_ptrORCResolver> Resolver;
379437 std::shared_ptr ClientResolver;
380438 Mangler Mang;
381439
6060 std::unique_ptr CCMgr,
6161 IndirectStubsManagerBuilder IndirectStubsMgrBuilder,
6262 bool InlineStubs)
63 : TM(std::move(TM)), DL(this->TM->createDataLayout()),
63 : ES(SSP), TM(std::move(TM)), DL(this->TM->createDataLayout()),
6464 CCMgr(std::move(CCMgr)),
65 ObjectLayer([]() { return std::make_shared(); }),
65 ObjectLayer(ES,
66 [](orc::VModuleKey) {
67 return std::make_shared();
68 },
69 [&](orc::VModuleKey K) {
70 auto ResolverI = Resolvers.find(K);
71 assert(ResolverI != Resolvers.end() &&
72 "Missing resolver for module K");
73 auto Resolver = std::move(ResolverI->second);
74 Resolvers.erase(ResolverI);
75 return Resolver;
76 }),
6677 CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
6778 IRDumpLayer(CompileLayer, createDebugDumper()),
68 CODLayer(IRDumpLayer, extractSingleFunction, *this->CCMgr,
69 std::move(IndirectStubsMgrBuilder), InlineStubs),
79 CODLayer(
80 ES, IRDumpLayer,
81 [&](orc::VModuleKey K) {
82 auto ResolverI = Resolvers.find(K);
83 assert(ResolverI != Resolvers.end() &&
84 "Missing resolver for module K");
85 auto Resolver = std::move(ResolverI->second);
86 Resolvers.erase(ResolverI);
87 return Resolver;
88 },
89 [&](orc::VModuleKey K, std::shared_ptr R) {
90 assert(!Resolvers.count(K) && "Resolver already present");
91 Resolvers[K] = std::move(R);
92 },
93 extractSingleFunction, *this->CCMgr,
94 std::move(IndirectStubsMgrBuilder), InlineStubs),
7095 CXXRuntimeOverrides(
7196 [this](const std::string &S) { return mangle(S); }) {}
7297
113138 // 2) Check for C++ runtime overrides.
114139 // 3) Search the host process (LLI)'s symbol table.
115140 if (!ModulesHandle) {
116 auto Resolver =
117 orc::createLambdaResolver(
118 [this](const std::string &Name) -> JITSymbol {
119 if (auto Sym = CODLayer.findSymbol(Name, true))
120 return Sym;
121 return CXXRuntimeOverrides.searchOverrides(Name);
141 auto LegacyLookupInDylib = [this](const std::string &Name) -> JITSymbol {
142 if (auto Sym = CODLayer.findSymbol(Name, true))
143 return Sym;
144 else if (auto Err = Sym.takeError())
145 return std::move(Err);
146 return CXXRuntimeOverrides.searchOverrides(Name);
147 };
148
149 auto LegacyLookup =
150 [this, LegacyLookupInDylib](const std::string &Name) -> JITSymbol {
151 if (auto Sym = LegacyLookupInDylib(Name))
152 return Sym;
153 else if (auto Err = Sym.takeError())
154 return std::move(Err);
155
156 if (auto Addr = RTDyldMemoryManager::getSymbolAddressInProcess(Name))
157 return JITSymbol(Addr, JITSymbolFlags::Exported);
158
159 return nullptr;
160 };
161
162 auto K = ES.allocateVModule();
163 assert(!Resolvers.count(K) && "Resolver already present");
164 Resolvers[K] = orc::createSymbolResolver(
165 [this, LegacyLookupInDylib](orc::SymbolFlagsMap &SymbolFlags,
166 const orc::SymbolNameSet &Symbols) {
167 auto NotFoundViaLegacyLookup = lookupFlagsWithLegacyFn(
168 SymbolFlags, Symbols, LegacyLookupInDylib);
169 if (!NotFoundViaLegacyLookup) {
170 logAllUnhandledErrors(NotFoundViaLegacyLookup.takeError(), errs(),
171 "OrcLazyJIT lookupFlags error: ");
172 SymbolFlags.clear();
173 return orc::SymbolNameSet();
174 }
175 return std::move(*NotFoundViaLegacyLookup);
122176 },
123 [](const std::string &Name) {
124 if (auto Addr =
125 RTDyldMemoryManager::getSymbolAddressInProcess(Name))
126 return JITSymbol(Addr, JITSymbolFlags::Exported);
127 return JITSymbol(nullptr);
128 }
129 );
177 [LegacyLookup](orc::AsynchronousSymbolQuery &Query,
178 orc::SymbolNameSet Symbols) {
179 return lookupWithLegacyFn(Query, Symbols, LegacyLookup);
180 });
130181
131182 // Add the module to the JIT.
132183 if (auto ModulesHandleOrErr =
133 CODLayer.addModule(std::move(M), std::move(Resolver)))
184 CODLayer.addModule(std::move(K), std::move(M)))
134185 ModulesHandle = std::move(*ModulesHandleOrErr);
135186 else
136187 return ModulesHandleOrErr.takeError();
177228
178229 static TransformFtor createDebugDumper();
179230
231 orc::SymbolStringPool SSP;
232 orc::ExecutionSession ES;
233
234 std::map> Resolvers;
235
180236 std::unique_ptr TM;
181237 DataLayout DL;
182238 SectionMemoryManager CCMgrMemMgr;
5858
5959 DummyCallbackManager CallbackMgr;
6060
61 SymbolStringPool SSP;
62 ExecutionSession ES(SSP);
63
64 auto GetResolver =
65 [](orc::VModuleKey) -> std::shared_ptr {
66 llvm_unreachable("Should never be called");
67 };
68
69 auto SetResolver = [](orc::VModuleKey, std::shared_ptr) {
70 llvm_unreachable("Should never be called");
71 };
72
6173 llvm::orc::CompileOnDemandLayer COD(
62 TestBaseLayer, [](Function &F) { return std::set{&F}; },
63 CallbackMgr, [] { return llvm::make_unique(); }, true);
74 ES, TestBaseLayer, GetResolver, SetResolver,
75 [](Function &F) { return std::set{&F}; }, CallbackMgr,
76 [] { return llvm::make_unique(); }, true);
6477
6578 auto Sym = COD.findSymbol("foo", true);
6679
1414
1515 struct MockBaseLayer {
1616 typedef int ModuleHandleT;
17 ModuleHandleT addModule(
18 std::shared_ptr,
19 std::unique_ptr MemMgr,
20 std::unique_ptr Resolver) {
21 EXPECT_FALSE(MemMgr);
17 ModuleHandleT addModule(llvm::orc::VModuleKey,
18 std::shared_ptr) {
2219 return 42;
2320 }
2421 };
2623 TEST(LazyEmittingLayerTest, Empty) {
2724 MockBaseLayer M;
2825 llvm::orc::LazyEmittingLayer L(M);
29 cantFail(L.addModule(std::unique_ptr(), nullptr));
26 cantFail(
27 L.addModule(llvm::orc::VModuleKey(), std::unique_ptr()));
3028 }
3129
3230 }
1010 #include "llvm/ADT/STLExtras.h"
1111 #include "llvm/ADT/SmallVector.h"
1212 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
13 #include "llvm/ExecutionEngine/Orc/NullResolver.h"
1413 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
1514 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
1615 #include "llvm/Object/ObjectFile.h"
4847 MockBaseLayer() : MockSymbol(nullptr) { resetExpectations(); }
4948
5049 template
51 llvm::Expected
52 addObject(ObjPtrT Obj,
53 std::shared_ptr Resolver) {
54 EXPECT_EQ(MockResolver, Resolver) << "Resolver should pass through";
50 llvm::Expected addObject(VModuleKey K, ObjPtrT Obj) {
51 EXPECT_EQ(MockKey, K) << "Key should pass through";
5552 EXPECT_EQ(MockObject + 1, *Obj) << "Transform should be applied";
5653 LastCalled = "addObject";
5754 MockObjHandle = 111;
5855 return MockObjHandle;
5956 }
6057
61 template
62 void expectAddObject(ObjPtrT Obj,
63 std::shared_ptr Resolver) {
64 MockResolver = Resolver;
58 template void expectAddObject(VModuleKey K, ObjPtrT Obj) {
59 MockKey = K;
6560 MockObject = *Obj;
6661 }
6762
161156 private:
162157 // Backing fields for remembering parameter/return values
163158 std::string LastCalled;
164 std::shared_ptr MockResolver;
159 VModuleKey MockKey;
165160 MockObjectFile MockObject;
166161 ObjHandleT MockObjHandle;
167162 std::string MockName;
174169 // Clear remembered parameters between calls
175170 void resetExpectations() {
176171 LastCalled = "nothing";
177 MockResolver = nullptr;
172 MockKey = 0;
178173 MockObject = 0;
179174 MockObjHandle = 0;
180175 MockName = "bogus";
188183 // Test each operation on ObjectTransformLayer.
189184 TEST(ObjectTransformLayerTest, Main) {
190185 MockBaseLayer M;
186
187 SymbolStringPool SSP;
188 ExecutionSession ES(SSP);
191189
192190 // Create one object transform layer using a transform (as a functor)
193191 // that allocates new objects, and deals in unique pointers.
204202 });
205203
206204 // Test addObject with T1 (allocating)
205 auto K1 = ES.allocateVModule();
207206 auto Obj1 = std::make_shared(211);
208 auto SR = std::make_shared();
209 M.expectAddObject(Obj1, SR);
210 auto H = cantFail(T1.addObject(std::move(Obj1), SR));
207 M.expectAddObject(K1, Obj1);
208 auto H = cantFail(T1.addObject(K1, std::move(Obj1)));
211209 M.verifyAddObject(H);
212210
213211 // Test addObjectSet with T2 (mutating)
212 auto K2 = ES.allocateVModule();
214213 auto Obj2 = std::make_shared(222);
215 M.expectAddObject(Obj2, SR);
216 H = cantFail(T2.addObject(Obj2, SR));
214 M.expectAddObject(K2, Obj2);
215 H = cantFail(T2.addObject(K2, Obj2));
217216 M.verifyAddObject(H);
218217 EXPECT_EQ(223, *Obj2) << "Expected mutation";
219218
290289
291290 // Construct the jit layers.
292291 RTDyldObjectLinkingLayer BaseLayer(
293 []() {
294 return std::make_shared();
295 });
292 ES,
293 [](VModuleKey) { return std::make_shared(); },
294 [](VModuleKey) -> std::shared_ptr {
295 llvm_unreachable("Should never be called");
296 });
296297
297298 auto IdentityTransform =
298299 [](std::shared_ptr>
310311
311312 // Make sure that the calls from IRCompileLayer to ObjectTransformLayer
312313 // compile.
313 auto Resolver = std::make_shared();
314 cantFail(CompileLayer.addModule(std::shared_ptr(), Resolver));
314 cantFail(CompileLayer.addModule(ES.allocateVModule(),
315 std::shared_ptr()));
315316
316317 // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
317318 // compile.
1111 #include "llvm/ExecutionEngine/ExecutionEngine.h"
1212 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
1313 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
14 #include "llvm/ExecutionEngine/Orc/Legacy.h"
1415 #include "llvm/ExecutionEngine/Orc/NullResolver.h"
1516 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
1617 #include "llvm/IR/Constants.h"
6566 bool DebugSectionSeen = false;
6667 auto MM = std::make_shared(DebugSectionSeen);
6768
68 RTDyldObjectLinkingLayer ObjLayer([&MM]() { return MM; });
69 SymbolStringPool SSP;
70 ExecutionSession ES(SSP);
71
72 RTDyldObjectLinkingLayer ObjLayer(
73 ES, [&MM](VModuleKey) { return MM; },
74 [](orc::VModuleKey) { return std::make_shared(); });
6975
7076 LLVMContext Context;
7177 auto M = llvm::make_unique("", Context);
9197 std::make_shared>(
9298 SimpleCompiler(*TM)(*M));
9399
94 auto Resolver =
95 createLambdaResolver(
96 [](const std::string &Name) {
97 return JITSymbol(nullptr);
98 },
99 [](const std::string &Name) {
100 return JITSymbol(nullptr);
101 });
102
103100 {
104101 // Test with ProcessAllSections = false (the default).
105 auto H = cantFail(ObjLayer.addObject(Obj, Resolver));
102 auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), Obj));
106103 cantFail(ObjLayer.emitAndFinalize(H));
107104 EXPECT_EQ(DebugSectionSeen, false)
108105 << "Unexpected debug info section";
112109 {
113110 // Test with ProcessAllSections = true.
114111 ObjLayer.setProcessAllSections(true);
115 auto H = cantFail(ObjLayer.addObject(Obj, Resolver));
112 auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), Obj));
116113 cantFail(ObjLayer.emitAndFinalize(H));
117114 EXPECT_EQ(DebugSectionSeen, true)
118115 << "Expected debug info section not seen";
124121 if (!TM)
125122 return;
126123
124 SymbolStringPool SSP;
125 ExecutionSession ES(SSP);
126
127127 auto MM = std::make_shared();
128128
129 RTDyldObjectLinkingLayer ObjLayer([&MM]() { return MM; });
129 std::map> Resolvers;
130
131 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) { return MM; },
132 [&](VModuleKey K) {
133 auto I = Resolvers.find(K);
134 assert(I != Resolvers.end() &&
135 "Missing resolver");
136 auto R = std::move(I->second);
137 Resolvers.erase(I);
138 return R;
139 });
130140 SimpleCompiler Compile(*TM);
131141
132142 // Create a pair of modules that will trigger recursive finalization:
169179 std::make_shared>(
170180 Compile(*MB2.getModule()));
171181
172 auto Resolver =
173 createLambdaResolver(
174 [&](const std::string &Name) {
175 if (auto Sym = ObjLayer.findSymbol(Name, true))
176 return Sym;
177 return JITSymbol(nullptr);
182 auto K1 = ES.allocateVModule();
183 Resolvers[K1] = std::make_shared();
184 cantFail(ObjLayer.addObject(K1, std::move(Obj1)));
185
186 auto K2 = ES.allocateVModule();
187 auto LegacyLookup = [&](const std::string &Name) {
188 return ObjLayer.findSymbol(Name, true);
189 };
190
191 Resolvers[K2] = createSymbolResolver(
192 [&](SymbolFlagsMap &SymbolFlags, const SymbolNameSet &Symbols) {
193 return cantFail(
194 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup));
178195 },
179 [](const std::string &Name) {
180 return JITSymbol(nullptr);
196 [&](AsynchronousSymbolQuery &Query, const SymbolNameSet &Symbols) {
197 return lookupWithLegacyFn(Query, Symbols, LegacyLookup);
181198 });
182199
183 cantFail(ObjLayer.addObject(std::move(Obj1), Resolver));
184 auto H = cantFail(ObjLayer.addObject(std::move(Obj2), Resolver));
200 auto H = cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
185201 cantFail(ObjLayer.emitAndFinalize(H));
186202 cantFail(ObjLayer.removeObject(H));
187203
195211 if (!TM)
196212 return;
197213
214 SymbolStringPool SSP;
215 ExecutionSession ES(SSP);
216
198217 auto MM = std::make_shared();
199218
200 RTDyldObjectLinkingLayer ObjLayer([&MM]() { return MM; });
219 RTDyldObjectLinkingLayer ObjLayer(
220 ES, [&MM](VModuleKey) { return MM; },
221 [](VModuleKey) { return std::make_shared(); });
201222 SimpleCompiler Compile(*TM);
202223
203224 // Create a pair of unrelated modules:
242263 std::make_shared>(
243264 Compile(*MB2.getModule()));
244265
245 auto NR = std::make_shared();
246 auto H = cantFail(ObjLayer.addObject(std::move(Obj1), NR));
247 cantFail(ObjLayer.addObject(std::move(Obj2), NR));
266 auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj1)));
267 cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj2)));
248268 cantFail(ObjLayer.emitAndFinalize(H));
249269 cantFail(ObjLayer.removeObject(H));
250270
255275 }
256276
257277 TEST_F(RTDyldObjectLinkingLayerExecutionTest, TestNotifyLoadedSignature) {
278 SymbolStringPool SSP;
279 ExecutionSession ES(SSP);
258280 RTDyldObjectLinkingLayer ObjLayer(
259 []() { return nullptr; },
281 ES, [](VModuleKey) { return nullptr; },
282 [](VModuleKey) { return std::make_shared(); },
260283 [](RTDyldObjectLinkingLayer::ObjHandleT,
261284 const RTDyldObjectLinkingLayer::ObjectPtr &obj,
262285 const RuntimeDyld::LoadedObjectInfo &info) {});