MultiJITTest.cpp 5.3 KB

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