Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit d0838f8

Browse files
committed
[ExecutionEngine] Re-apply r241962 with fixes for ARM.
Patch by Pierre-Andre Saulais. Thanks Pierre-Andre! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242213 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ba77f37 commit d0838f8

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

include/llvm/ExecutionEngine/ExecutionEngine.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <map>
3232
#include <string>
3333
#include <vector>
34+
#include <functional>
3435

3536
namespace llvm {
3637

@@ -89,6 +90,8 @@ class ExecutionEngineState {
8990
uint64_t RemoveMapping(StringRef Name);
9091
};
9192

93+
using FunctionCreator = std::function<void *(const std::string &)>;
94+
9295
/// \brief Abstract interface for implementation execution of LLVM modules,
9396
/// designed to support both interpreter and just-in-time (JIT) compiler
9497
/// implementations.
@@ -147,7 +150,7 @@ class ExecutionEngine {
147150
/// LazyFunctionCreator - If an unknown function is needed, this function
148151
/// pointer is invoked to create it. If this returns null, the JIT will
149152
/// abort.
150-
void *(*LazyFunctionCreator)(const std::string &);
153+
FunctionCreator LazyFunctionCreator;
151154

152155
/// getMangledName - Get mangled name.
153156
std::string getMangledName(const GlobalValue *GV);
@@ -470,8 +473,8 @@ class ExecutionEngine {
470473
/// InstallLazyFunctionCreator - If an unknown function is needed, the
471474
/// specified function pointer is invoked to create it. If it returns null,
472475
/// the JIT will abort.
473-
void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
474-
LazyFunctionCreator = P;
476+
void InstallLazyFunctionCreator(FunctionCreator C) {
477+
LazyFunctionCreator = C;
475478
}
476479

477480
protected:

unittests/ExecutionEngine/MCJIT/MCJITTest.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "llvm/ExecutionEngine/MCJIT.h"
16+
#include "llvm/Support/DynamicLibrary.h"
1617
#include "MCJITTestBase.h"
1718
#include "gtest/gtest.h"
1819

@@ -199,4 +200,85 @@ TEST_F(MCJITTest, multiple_decl_lookups) {
199200
EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail.";
200201
}
201202

203+
typedef void * (*FunctionHandlerPtr)(const std::string &str);
204+
205+
TEST_F(MCJITTest, lazy_function_creator_pointer) {
206+
SKIP_UNSUPPORTED_PLATFORM;
207+
208+
Function *Foo = insertExternalReferenceToFunction<int32_t(void)>(M.get(),
209+
"\1Foo");
210+
startFunction<int32_t(void)>(M.get(), "Parent");
211+
CallInst *Call = Builder.CreateCall(Foo, {});
212+
Builder.CreateRet(Call);
213+
214+
createJIT(std::move(M));
215+
216+
// Set up the lazy function creator that records the name of the last
217+
// unresolved external function found in the module. Using a function pointer
218+
// prevents us from capturing local variables, which is why this is static.
219+
static std::string UnresolvedExternal;
220+
FunctionHandlerPtr UnresolvedHandler = [] (const std::string &str) {
221+
// Try to resolve the function in the current process before marking it as
222+
// unresolved. This solves an issue on ARM where '__aeabi_*' function names
223+
// are passed to this handler.
224+
void *symbol =
225+
llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(str.c_str());
226+
if (symbol) {
227+
return symbol;
228+
}
229+
230+
UnresolvedExternal = str;
231+
return (void *)(uintptr_t)-1;
232+
};
233+
TheJIT->InstallLazyFunctionCreator(UnresolvedHandler);
234+
235+
// JIT the module.
236+
TheJIT->finalizeObject();
237+
238+
// Verify that our handler was called.
239+
EXPECT_EQ(UnresolvedExternal, "Foo");
240+
}
241+
242+
TEST_F(MCJITTest, lazy_function_creator_lambda) {
243+
SKIP_UNSUPPORTED_PLATFORM;
244+
245+
Function *Foo1 = insertExternalReferenceToFunction<int32_t(void)>(M.get(),
246+
"\1Foo1");
247+
Function *Foo2 = insertExternalReferenceToFunction<int32_t(void)>(M.get(),
248+
"\1Foo2");
249+
startFunction<int32_t(void)>(M.get(), "Parent");
250+
CallInst *Call1 = Builder.CreateCall(Foo1, {});
251+
CallInst *Call2 = Builder.CreateCall(Foo2, {});
252+
Value *Result = Builder.CreateAdd(Call1, Call2);
253+
Builder.CreateRet(Result);
254+
255+
createJIT(std::move(M));
256+
257+
// Set up the lazy function creator that records the name of unresolved
258+
// external functions in the module.
259+
std::vector<std::string> UnresolvedExternals;
260+
auto UnresolvedHandler = [&UnresolvedExternals] (const std::string &str) {
261+
// Try to resolve the function in the current process before marking it as
262+
// unresolved. This solves an issue on ARM where '__aeabi_*' function names
263+
// are passed to this handler.
264+
void *symbol =
265+
llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(str.c_str());
266+
if (symbol) {
267+
return symbol;
268+
}
269+
UnresolvedExternals.push_back(str);
270+
return (void *)(uintptr_t)-1;
271+
};
272+
TheJIT->InstallLazyFunctionCreator(UnresolvedHandler);
273+
274+
// JIT the module.
275+
TheJIT->finalizeObject();
276+
277+
// Verify that our handler was called for each unresolved function.
278+
auto I = UnresolvedExternals.begin(), E = UnresolvedExternals.end();
279+
EXPECT_EQ(UnresolvedExternals.size(), 2u);
280+
EXPECT_FALSE(std::find(I, E, "Foo1") == E);
281+
EXPECT_FALSE(std::find(I, E, "Foo2") == E);
282+
}
283+
202284
}

0 commit comments

Comments
 (0)