llvm.org GIT mirror llvm / b9b04ee
[ExecutionEngine] Use std::function rather than a function pointer for the LazyFunctionCreator. Patch by Pierre-Andre Saulais. Thanks Pierre! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241962 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 5 years ago
2 changed file(s) with 72 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
3030 #include
3131 #include
3232 #include
33 #include
3334
3435 namespace llvm {
3536
8889 uint64_t RemoveMapping(StringRef Name);
8990 };
9091
92 using FunctionCreator = std::function;
93
9194 /// \brief Abstract interface for implementation execution of LLVM modules,
9295 /// designed to support both interpreter and just-in-time (JIT) compiler
9396 /// implementations.
146149 /// LazyFunctionCreator - If an unknown function is needed, this function
147150 /// pointer is invoked to create it. If this returns null, the JIT will
148151 /// abort.
149 void *(*LazyFunctionCreator)(const std::string &);
152 FunctionCreator LazyFunctionCreator;
150153
151154 /// getMangledName - Get mangled name.
152155 std::string getMangledName(const GlobalValue *GV);
469472 /// InstallLazyFunctionCreator - If an unknown function is needed, the
470473 /// specified function pointer is invoked to create it. If it returns null,
471474 /// the JIT will abort.
472 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
473 LazyFunctionCreator = P;
475 void InstallLazyFunctionCreator(FunctionCreator C) {
476 LazyFunctionCreator = C;
474477 }
475478
476479 protected:
198198 EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail.";
199199 }
200200
201 }
201 typedef void * (*FunctionHandlerPtr)(const std::string &str);
202
203 TEST_F(MCJITTest, lazy_function_creator_pointer) {
204 SKIP_UNSUPPORTED_PLATFORM;
205
206 Function *Foo = insertExternalReferenceToFunction(M.get(),
207 "\1Foo");
208 Function *Parent = startFunction(M.get(), "Parent");
209 CallInst *Call = Builder.CreateCall(Foo, {});
210 Builder.CreateRet(Call);
211
212 createJIT(std::move(M));
213
214 // Set up the lazy function creator that records the name of the last
215 // unresolved external function found in the module. Using a function pointer
216 // prevents us from capturing local variables, which is why this is static.
217 static std::string UnresolvedExternal;
218 FunctionHandlerPtr UnresolvedHandler = [] (const std::string &str) {
219 UnresolvedExternal = str;
220 return (void *)(uintptr_t)-1;
221 };
222 TheJIT->InstallLazyFunctionCreator(UnresolvedHandler);
223
224 // JIT the module.
225 TheJIT->finalizeObject();
226
227 // Verify that our handler was called.
228 EXPECT_EQ(UnresolvedExternal, "Foo");
229 }
230
231 TEST_F(MCJITTest, lazy_function_creator_lambda) {
232 SKIP_UNSUPPORTED_PLATFORM;
233
234 Function *Foo1 = insertExternalReferenceToFunction(M.get(),
235 "\1Foo1");
236 Function *Foo2 = insertExternalReferenceToFunction(M.get(),
237 "\1Foo2");
238 Function *Parent = startFunction(M.get(), "Parent");
239 CallInst *Call1 = Builder.CreateCall(Foo1, {});
240 CallInst *Call2 = Builder.CreateCall(Foo2, {});
241 Value *Result = Builder.CreateAdd(Call1, Call2);
242 Builder.CreateRet(Result);
243
244 createJIT(std::move(M));
245
246 // Set up the lazy function creator that records the name of unresolved
247 // external functions in the module.
248 std::vector UnresolvedExternals;
249 auto UnresolvedHandler = [&UnresolvedExternals] (const std::string &str) {
250 llvm:dbgs() << "str is '" << str << "'\n";
251 UnresolvedExternals.push_back(str);
252 return (void *)(uintptr_t)-1;
253 };
254 TheJIT->InstallLazyFunctionCreator(UnresolvedHandler);
255
256 // JIT the module.
257 TheJIT->finalizeObject();
258
259 // Verify that our handler was called for each unresolved function.
260 auto I = UnresolvedExternals.begin(), E = UnresolvedExternals.end();
261 EXPECT_EQ(UnresolvedExternals.size(), 2);
262 EXPECT_FALSE(std::find(I, E, "Foo1") == E);
263 EXPECT_FALSE(std::find(I, E, "Foo2") == E);
264 }
265
266 }