MultiJITTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===- MultiJITTest.cpp - Unit tests for instantiating multiple JITs ------===//
  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. #include "llvm/ExecutionEngine/JIT.h"
  10. #include "llvm/AsmParser/Parser.h"
  11. #include "llvm/ExecutionEngine/GenericValue.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/Support/SourceMgr.h"
  15. #include "gtest/gtest.h"
  16. #include <vector>
  17. using namespace llvm;
  18. namespace {
  19. // ARM, PowerPC and SystemZ tests disabled pending fix for PR10783.
  20. #if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
  21. && !defined(__aarch64__)
  22. std::unique_ptr<Module> loadAssembly(LLVMContext &Context,
  23. const char *Assembly) {
  24. SMDiagnostic Error;
  25. std::unique_ptr<Module> Ret(
  26. ParseAssemblyString(Assembly, nullptr, Error, Context));
  27. std::string errMsg;
  28. raw_string_ostream os(errMsg);
  29. Error.print("", os);
  30. EXPECT_TRUE((bool)Ret) << os.str();
  31. return std::move(Ret);
  32. }
  33. std::unique_ptr<Module> createModule1(LLVMContext &Context1, Function *&FooF1) {
  34. std::unique_ptr<Module> Ret = loadAssembly(Context1,
  35. "define i32 @add1(i32 %ArgX1) { "
  36. "entry: "
  37. " %addresult = add i32 1, %ArgX1 "
  38. " ret i32 %addresult "
  39. "} "
  40. " "
  41. "define i32 @foo1() { "
  42. "entry: "
  43. " %add1 = call i32 @add1(i32 10) "
  44. " ret i32 %add1 "
  45. "} ");
  46. FooF1 = Ret->getFunction("foo1");
  47. return std::move(Ret);
  48. }
  49. std::unique_ptr<Module> createModule2(LLVMContext &Context2, Function *&FooF2) {
  50. std::unique_ptr<Module> Ret = loadAssembly(Context2,
  51. "define i32 @add2(i32 %ArgX2) { "
  52. "entry: "
  53. " %addresult = add i32 2, %ArgX2 "
  54. " ret i32 %addresult "
  55. "} "
  56. " "
  57. "define i32 @foo2() { "
  58. "entry: "
  59. " %add2 = call i32 @add2(i32 10) "
  60. " ret i32 %add2 "
  61. "} ");
  62. FooF2 = Ret->getFunction("foo2");
  63. return std::move(Ret);
  64. }
  65. TEST(MultiJitTest, EagerMode) {
  66. LLVMContext Context1;
  67. Function *FooF1 = nullptr;
  68. std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
  69. LLVMContext Context2;
  70. Function *FooF2 = nullptr;
  71. std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
  72. // Now we create the JIT in eager mode
  73. std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
  74. EE1->DisableLazyCompilation(true);
  75. std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
  76. EE2->DisableLazyCompilation(true);
  77. // Call the `foo' function with no arguments:
  78. std::vector<GenericValue> noargs;
  79. GenericValue gv1 = EE1->runFunction(FooF1, noargs);
  80. GenericValue gv2 = EE2->runFunction(FooF2, noargs);
  81. // Import result of execution:
  82. EXPECT_EQ(gv1.IntVal, 11);
  83. EXPECT_EQ(gv2.IntVal, 12);
  84. EE1->freeMachineCodeForFunction(FooF1);
  85. EE2->freeMachineCodeForFunction(FooF2);
  86. }
  87. TEST(MultiJitTest, LazyMode) {
  88. LLVMContext Context1;
  89. Function *FooF1 = nullptr;
  90. std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
  91. LLVMContext Context2;
  92. Function *FooF2 = nullptr;
  93. std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
  94. // Now we create the JIT in lazy mode
  95. std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
  96. EE1->DisableLazyCompilation(false);
  97. std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
  98. EE2->DisableLazyCompilation(false);
  99. // Call the `foo' function with no arguments:
  100. std::vector<GenericValue> noargs;
  101. GenericValue gv1 = EE1->runFunction(FooF1, noargs);
  102. GenericValue gv2 = EE2->runFunction(FooF2, noargs);
  103. // Import result of execution:
  104. EXPECT_EQ(gv1.IntVal, 11);
  105. EXPECT_EQ(gv2.IntVal, 12);
  106. EE1->freeMachineCodeForFunction(FooF1);
  107. EE2->freeMachineCodeForFunction(FooF2);
  108. }
  109. extern "C" {
  110. extern void *getPointerToNamedFunction(const char *Name);
  111. }
  112. TEST(MultiJitTest, JitPool) {
  113. LLVMContext Context1;
  114. Function *FooF1 = nullptr;
  115. std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
  116. LLVMContext Context2;
  117. Function *FooF2 = nullptr;
  118. std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
  119. // Now we create two JITs
  120. std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
  121. std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
  122. Function *F1 = EE1->FindFunctionNamed("foo1");
  123. void *foo1 = EE1->getPointerToFunction(F1);
  124. Function *F2 = EE2->FindFunctionNamed("foo2");
  125. void *foo2 = EE2->getPointerToFunction(F2);
  126. // Function in M1
  127. EXPECT_EQ(getPointerToNamedFunction("foo1"), foo1);
  128. // Function in M2
  129. EXPECT_EQ(getPointerToNamedFunction("foo2"), foo2);
  130. // Symbol search
  131. intptr_t
  132. sa = (intptr_t)getPointerToNamedFunction("getPointerToNamedFunction");
  133. EXPECT_TRUE(sa != 0);
  134. intptr_t fa = (intptr_t)&getPointerToNamedFunction;
  135. EXPECT_TRUE(fa != 0);
  136. #ifdef __i386__
  137. // getPointerToNamedFunction might be indirect jump on Win32 --enable-shared.
  138. // FF 25 <disp32>: jmp *(pointer to IAT)
  139. if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
  140. fa = *(intptr_t *)(fa + 2); // Address to IAT
  141. EXPECT_TRUE(fa != 0);
  142. fa = *(intptr_t *)fa; // Bound value of IAT
  143. }
  144. #elif defined(__x86_64__)
  145. // getPointerToNamedFunction might be indirect jump
  146. // on Win32 x64 --enable-shared.
  147. // FF 25 <pcrel32>: jmp *(RIP + pointer to IAT)
  148. if (sa != fa && memcmp((char *)fa, "\xFF\x25", 2) == 0) {
  149. fa += *(int32_t *)(fa + 2) + 6; // Address to IAT(RIP)
  150. fa = *(intptr_t *)fa; // Bound value of IAT
  151. }
  152. #endif
  153. EXPECT_TRUE(sa == fa);
  154. }
  155. #endif // !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__)
  156. } // anonymous namespace