1 //===- MCJITTest.cpp - Unit tests for the MCJIT -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This test suite verifies basic MCJIT functionality such as making function
11 // calls, using global variables, and compiling multpile modules.
13 //===----------------------------------------------------------------------===//
15 #include "MCJITTestBase.h"
16 #include "llvm/Support/DynamicLibrary.h"
17 #include "gtest/gtest.h"
23 class MCJITTest
: public testing::Test
, public MCJITTestBase
{
25 void SetUp() override
{ M
.reset(createEmptyModule("<main>")); }
28 // FIXME: Ensure creating an execution engine does not crash when constructed
29 // with a null module.
31 TEST_F(MCJITTest, null_module) {
36 // FIXME: In order to JIT an empty module, there needs to be
37 // an interface to ExecutionEngine that forces compilation but
38 // does not require retrieval of a pointer to a function/global.
40 TEST_F(MCJITTest, empty_module) {
42 //EXPECT_NE(0, TheJIT->getObjectImage())
43 // << "Unable to generate executable loaded object image";
47 TEST_F(MCJITTest
, global_variable
) {
48 SKIP_UNSUPPORTED_PLATFORM
;
51 GlobalValue
*Global
= insertGlobalInt32(M
.get(), "test_global", initialValue
);
52 createJIT(std::move(M
));
53 void *globalPtr
= TheJIT
->getPointerToGlobal(Global
);
54 EXPECT_TRUE(nullptr != globalPtr
)
55 << "Unable to get pointer to global value from JIT";
57 EXPECT_EQ(initialValue
, *(int32_t*)globalPtr
)
58 << "Unexpected initial value of global";
61 TEST_F(MCJITTest
, add_function
) {
62 SKIP_UNSUPPORTED_PLATFORM
;
64 Function
*F
= insertAddFunction(M
.get());
65 createJIT(std::move(M
));
66 uint64_t addPtr
= TheJIT
->getFunctionAddress(F
->getName().str());
67 EXPECT_TRUE(0 != addPtr
)
68 << "Unable to get pointer to function from JIT";
70 ASSERT_TRUE(addPtr
!= 0) << "Unable to get pointer to function .";
71 int (*AddPtr
)(int, int) = (int(*)(int, int))addPtr
;
72 EXPECT_EQ(0, AddPtr(0, 0));
73 EXPECT_EQ(1, AddPtr(1, 0));
74 EXPECT_EQ(3, AddPtr(1, 2));
75 EXPECT_EQ(-5, AddPtr(-2, -3));
76 EXPECT_EQ(30, AddPtr(10, 20));
77 EXPECT_EQ(-30, AddPtr(-10, -20));
78 EXPECT_EQ(-40, AddPtr(-10, -30));
81 TEST_F(MCJITTest
, run_main
) {
82 SKIP_UNSUPPORTED_PLATFORM
;
85 Function
*Main
= insertMainFunction(M
.get(), 6);
86 createJIT(std::move(M
));
87 uint64_t ptr
= TheJIT
->getFunctionAddress(Main
->getName().str());
89 << "Unable to get pointer to main() from JIT";
91 int (*FuncPtr
)() = (int(*)())ptr
;
92 int returnCode
= FuncPtr();
93 EXPECT_EQ(returnCode
, rc
);
96 TEST_F(MCJITTest
, return_global
) {
97 SKIP_UNSUPPORTED_PLATFORM
;
99 int32_t initialNum
= 7;
100 GlobalVariable
*GV
= insertGlobalInt32(M
.get(), "myglob", initialNum
);
102 Function
*ReturnGlobal
= startFunction
<int32_t(void)>(M
.get(),
104 Value
*ReadGlobal
= Builder
.CreateLoad(GV
);
105 endFunctionWithRet(ReturnGlobal
, ReadGlobal
);
107 createJIT(std::move(M
));
108 uint64_t rgvPtr
= TheJIT
->getFunctionAddress(ReturnGlobal
->getName().str());
109 EXPECT_TRUE(0 != rgvPtr
);
111 int32_t(*FuncPtr
)() = (int32_t(*)())rgvPtr
;
112 EXPECT_EQ(initialNum
, FuncPtr())
113 << "Invalid value for global returned from JITted function";
116 // FIXME: This case fails due to a bug with getPointerToGlobal().
117 // The bug is due to MCJIT not having an implementation of getPointerToGlobal()
118 // which results in falling back on the ExecutionEngine implementation that
119 // allocates a new memory block for the global instead of using the same
120 // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
121 // has the correct initial value, but updates to the real global (accessed by
122 // JITted code) are not propagated. Instead, getPointerToGlobal() should return
123 // a pointer into the loaded ObjectImage to reference the emitted global.
125 TEST_F(MCJITTest, increment_global) {
126 SKIP_UNSUPPORTED_PLATFORM;
128 int32_t initialNum = 5;
129 Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
130 GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
131 Value *DerefGV = Builder.CreateLoad(GV);
132 Value *AddResult = Builder.CreateAdd(DerefGV,
133 ConstantInt::get(Context, APInt(32, 1)));
134 Builder.CreateStore(AddResult, GV);
135 endFunctionWithRet(IncrementGlobal, AddResult);
138 void *gvPtr = TheJIT->getPointerToGlobal(GV);
139 EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
141 void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
142 EXPECT_TRUE(0 != vPtr)
143 << "Unable to get pointer to main() from JIT";
145 int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
147 for(int i = 1; i < 3; ++i) {
148 int32_t result = FuncPtr();
149 EXPECT_EQ(initialNum + i, result); // OK
150 EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
155 // PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
156 #if !defined(__arm__)
158 TEST_F(MCJITTest
, multiple_functions
) {
159 SKIP_UNSUPPORTED_PLATFORM
;
161 unsigned int numLevels
= 23;
162 int32_t innerRetVal
= 5;
164 Function
*Inner
= startFunction
<int32_t(void)>(M
.get(), "Inner");
165 endFunctionWithRet(Inner
, ConstantInt::get(Context
, APInt(32, innerRetVal
)));
168 for (unsigned int i
= 0; i
< numLevels
; ++i
) {
169 std::stringstream funcName
;
170 funcName
<< "level_" << i
;
171 Outer
= startFunction
<int32_t(void)>(M
.get(), funcName
.str());
172 Value
*innerResult
= Builder
.CreateCall(Inner
, {});
173 endFunctionWithRet(Outer
, innerResult
);
178 createJIT(std::move(M
));
179 uint64_t ptr
= TheJIT
->getFunctionAddress(Outer
->getName().str());
180 EXPECT_TRUE(0 != ptr
)
181 << "Unable to get pointer to outer function from JIT";
183 int32_t(*FuncPtr
)() = (int32_t(*)())ptr
;
184 EXPECT_EQ(innerRetVal
, FuncPtr())
185 << "Incorrect result returned from function";
188 #endif /*!defined(__arm__)*/
190 TEST_F(MCJITTest
, multiple_decl_lookups
) {
191 SKIP_UNSUPPORTED_PLATFORM
;
193 Function
*Foo
= insertExternalReferenceToFunction
<void(void)>(M
.get(), "_exit");
194 createJIT(std::move(M
));
195 void *A
= TheJIT
->getPointerToFunction(Foo
);
196 void *B
= TheJIT
->getPointerToFunction(Foo
);
198 EXPECT_TRUE(A
!= nullptr) << "Failed lookup - test not correctly configured.";
199 EXPECT_EQ(A
, B
) << "Repeat calls to getPointerToFunction fail.";
202 typedef void * (*FunctionHandlerPtr
)(const std::string
&str
);
204 TEST_F(MCJITTest
, lazy_function_creator_pointer
) {
205 SKIP_UNSUPPORTED_PLATFORM
;
207 Function
*Foo
= insertExternalReferenceToFunction
<int32_t(void)>(M
.get(),
209 startFunction
<int32_t(void)>(M
.get(), "Parent");
210 CallInst
*Call
= Builder
.CreateCall(Foo
, {});
211 Builder
.CreateRet(Call
);
213 createJIT(std::move(M
));
215 // Set up the lazy function creator that records the name of the last
216 // unresolved external function found in the module. Using a function pointer
217 // prevents us from capturing local variables, which is why this is static.
218 static std::string UnresolvedExternal
;
219 FunctionHandlerPtr UnresolvedHandler
= [] (const std::string
&str
) {
220 // Try to resolve the function in the current process before marking it as
221 // unresolved. This solves an issue on ARM where '__aeabi_*' function names
222 // are passed to this handler.
224 llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(str
.c_str());
229 UnresolvedExternal
= str
;
230 return (void *)(uintptr_t)-1;
232 TheJIT
->InstallLazyFunctionCreator(UnresolvedHandler
);
235 TheJIT
->finalizeObject();
237 // Verify that our handler was called.
238 EXPECT_EQ(UnresolvedExternal
, "Foo");
241 TEST_F(MCJITTest
, lazy_function_creator_lambda
) {
242 SKIP_UNSUPPORTED_PLATFORM
;
244 Function
*Foo1
= insertExternalReferenceToFunction
<int32_t(void)>(M
.get(),
246 Function
*Foo2
= insertExternalReferenceToFunction
<int32_t(void)>(M
.get(),
248 startFunction
<int32_t(void)>(M
.get(), "Parent");
249 CallInst
*Call1
= Builder
.CreateCall(Foo1
, {});
250 CallInst
*Call2
= Builder
.CreateCall(Foo2
, {});
251 Value
*Result
= Builder
.CreateAdd(Call1
, Call2
);
252 Builder
.CreateRet(Result
);
254 createJIT(std::move(M
));
256 // Set up the lazy function creator that records the name of unresolved
257 // external functions in the module.
258 std::vector
<std::string
> UnresolvedExternals
;
259 auto UnresolvedHandler
= [&UnresolvedExternals
] (const std::string
&str
) {
260 // Try to resolve the function in the current process before marking it as
261 // unresolved. This solves an issue on ARM where '__aeabi_*' function names
262 // are passed to this handler.
264 llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(str
.c_str());
268 UnresolvedExternals
.push_back(str
);
269 return (void *)(uintptr_t)-1;
271 TheJIT
->InstallLazyFunctionCreator(UnresolvedHandler
);
274 TheJIT
->finalizeObject();
276 // Verify that our handler was called for each unresolved function.
277 auto I
= UnresolvedExternals
.begin(), E
= UnresolvedExternals
.end();
278 EXPECT_EQ(UnresolvedExternals
.size(), 2u);
279 EXPECT_FALSE(std::find(I
, E
, "Foo1") == E
);
280 EXPECT_FALSE(std::find(I
, E
, "Foo2") == E
);
283 } // end anonymous namespace