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