MCJITTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This test suite verifies basic MCJIT functionality such as making function
  11. // calls, using global variables, and compiling multpile modules.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ExecutionEngine/MCJIT.h"
  15. #include "MCJITTestBase.h"
  16. #include "gtest/gtest.h"
  17. using namespace llvm;
  18. namespace {
  19. class MCJITTest : public testing::Test, public MCJITTestBase {
  20. protected:
  21. virtual void SetUp() { M.reset(createEmptyModule("<main>")); }
  22. };
  23. // FIXME: Ensure creating an execution engine does not crash when constructed
  24. // with a null module.
  25. /*
  26. TEST_F(MCJITTest, null_module) {
  27. createJIT(0);
  28. }
  29. */
  30. // FIXME: In order to JIT an empty module, there needs to be
  31. // an interface to ExecutionEngine that forces compilation but
  32. // does not require retrieval of a pointer to a function/global.
  33. /*
  34. TEST_F(MCJITTest, empty_module) {
  35. createJIT(M.take());
  36. //EXPECT_NE(0, TheJIT->getObjectImage())
  37. // << "Unable to generate executable loaded object image";
  38. }
  39. */
  40. TEST_F(MCJITTest, global_variable) {
  41. SKIP_UNSUPPORTED_PLATFORM;
  42. int initialValue = 5;
  43. GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
  44. createJIT(std::move(M));
  45. void *globalPtr = TheJIT->getPointerToGlobal(Global);
  46. EXPECT_TRUE(nullptr != globalPtr)
  47. << "Unable to get pointer to global value from JIT";
  48. EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
  49. << "Unexpected initial value of global";
  50. }
  51. TEST_F(MCJITTest, add_function) {
  52. SKIP_UNSUPPORTED_PLATFORM;
  53. Function *F = insertAddFunction(M.get());
  54. createJIT(std::move(M));
  55. uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
  56. EXPECT_TRUE(0 != addPtr)
  57. << "Unable to get pointer to function from JIT";
  58. ASSERT_TRUE(addPtr != 0) << "Unable to get pointer to function .";
  59. int (*AddPtr)(int, int) = (int(*)(int, int))addPtr ;
  60. EXPECT_EQ(0, AddPtr(0, 0));
  61. EXPECT_EQ(1, AddPtr(1, 0));
  62. EXPECT_EQ(3, AddPtr(1, 2));
  63. EXPECT_EQ(-5, AddPtr(-2, -3));
  64. EXPECT_EQ(30, AddPtr(10, 20));
  65. EXPECT_EQ(-30, AddPtr(-10, -20));
  66. EXPECT_EQ(-40, AddPtr(-10, -30));
  67. }
  68. TEST_F(MCJITTest, run_main) {
  69. SKIP_UNSUPPORTED_PLATFORM;
  70. int rc = 6;
  71. Function *Main = insertMainFunction(M.get(), 6);
  72. createJIT(std::move(M));
  73. uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
  74. EXPECT_TRUE(0 != ptr)
  75. << "Unable to get pointer to main() from JIT";
  76. int (*FuncPtr)(void) = (int(*)(void))ptr;
  77. int returnCode = FuncPtr();
  78. EXPECT_EQ(returnCode, rc);
  79. }
  80. TEST_F(MCJITTest, return_global) {
  81. SKIP_UNSUPPORTED_PLATFORM;
  82. int32_t initialNum = 7;
  83. GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
  84. Function *ReturnGlobal = startFunction<int32_t(void)>(M.get(),
  85. "ReturnGlobal");
  86. Value *ReadGlobal = Builder.CreateLoad(GV);
  87. endFunctionWithRet(ReturnGlobal, ReadGlobal);
  88. createJIT(std::move(M));
  89. uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
  90. EXPECT_TRUE(0 != rgvPtr);
  91. int32_t(*FuncPtr)(void) = (int32_t(*)(void))rgvPtr;
  92. EXPECT_EQ(initialNum, FuncPtr())
  93. << "Invalid value for global returned from JITted function";
  94. }
  95. // FIXME: This case fails due to a bug with getPointerToGlobal().
  96. // The bug is due to MCJIT not having an implementation of getPointerToGlobal()
  97. // which results in falling back on the ExecutionEngine implementation that
  98. // allocates a new memory block for the global instead of using the same
  99. // global variable that is emitted by MCJIT. Hence, the pointer (gvPtr below)
  100. // has the correct initial value, but updates to the real global (accessed by
  101. // JITted code) are not propagated. Instead, getPointerToGlobal() should return
  102. // a pointer into the loaded ObjectImage to reference the emitted global.
  103. /*
  104. TEST_F(MCJITTest, increment_global) {
  105. SKIP_UNSUPPORTED_PLATFORM;
  106. int32_t initialNum = 5;
  107. Function *IncrementGlobal = startFunction<int32_t(void)>(M.get(), "IncrementGlobal");
  108. GlobalVariable *GV = insertGlobalInt32(M.get(), "my_global", initialNum);
  109. Value *DerefGV = Builder.CreateLoad(GV);
  110. Value *AddResult = Builder.CreateAdd(DerefGV,
  111. ConstantInt::get(Context, APInt(32, 1)));
  112. Builder.CreateStore(AddResult, GV);
  113. endFunctionWithRet(IncrementGlobal, AddResult);
  114. createJIT(M.take());
  115. void *gvPtr = TheJIT->getPointerToGlobal(GV);
  116. EXPECT_EQ(initialNum, *(int32_t*)gvPtr);
  117. void *vPtr = TheJIT->getFunctionAddress(IncrementGlobal->getName().str());
  118. EXPECT_TRUE(0 != vPtr)
  119. << "Unable to get pointer to main() from JIT";
  120. int32_t(*FuncPtr)(void) = (int32_t(*)(void))(intptr_t)vPtr;
  121. for(int i = 1; i < 3; ++i) {
  122. int32_t result = FuncPtr();
  123. EXPECT_EQ(initialNum + i, result); // OK
  124. EXPECT_EQ(initialNum + i, *(int32_t*)gvPtr); // FAILS
  125. }
  126. }
  127. */
  128. // PR16013: XFAIL this test on ARM, which currently can't handle multiple relocations.
  129. #if !defined(__arm__)
  130. TEST_F(MCJITTest, multiple_functions) {
  131. SKIP_UNSUPPORTED_PLATFORM;
  132. unsigned int numLevels = 23;
  133. int32_t innerRetVal= 5;
  134. Function *Inner = startFunction<int32_t(void)>(M.get(), "Inner");
  135. endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
  136. Function *Outer;
  137. for (unsigned int i = 0; i < numLevels; ++i) {
  138. std::stringstream funcName;
  139. funcName << "level_" << i;
  140. Outer = startFunction<int32_t(void)>(M.get(), funcName.str());
  141. Value *innerResult = Builder.CreateCall(Inner);
  142. endFunctionWithRet(Outer, innerResult);
  143. Inner = Outer;
  144. }
  145. createJIT(std::move(M));
  146. uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
  147. EXPECT_TRUE(0 != ptr)
  148. << "Unable to get pointer to outer function from JIT";
  149. int32_t(*FuncPtr)(void) = (int32_t(*)(void))ptr;
  150. EXPECT_EQ(innerRetVal, FuncPtr())
  151. << "Incorrect result returned from function";
  152. }
  153. #endif /*!defined(__arm__)*/
  154. }