1 //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
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 // Parallel JIT
11 //
12 // This test program creates two LLVM functions then calls them from three
13 // separate threads. It requires the pthreads library.
14 // The three threads are created and then block waiting on a condition variable.
15 // Once all threads are blocked on the conditional variable, the main thread
16 // wakes them up. This complicated work is performed so that all three threads
17 // call into the JIT at the same time (or the best possible approximation of the
18 // same time). This test had assertion errors until I got the locking right.
19
20 #include "llvm/ExecutionEngine/GenericValue.h"
21 #include "llvm/ExecutionEngine/Interpreter.h"
22 #include "llvm/ExecutionEngine/JIT.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/TargetSelect.h"
29 #include <iostream>
30 #include <pthread.h>
31 using namespace llvm;
32
createAdd1(Module * M)33 static Function* createAdd1(Module *M) {
34 // Create the add1 function entry and insert this entry into module M. The
35 // function will have a return type of "int" and take an argument of "int".
36 // The '0' terminates the list of argument types.
37 Function *Add1F =
38 cast<Function>(M->getOrInsertFunction("add1",
39 Type::getInt32Ty(M->getContext()),
40 Type::getInt32Ty(M->getContext()),
41 (Type *)0));
42
43 // Add a basic block to the function. As before, it automatically inserts
44 // because of the last argument.
45 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
46
47 // Get pointers to the constant `1'.
48 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
49
50 // Get pointers to the integer argument of the add1 function...
51 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
52 Argument *ArgX = Add1F->arg_begin(); // Get the arg
53 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
54
55 // Create the add instruction, inserting it into the end of BB.
56 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
57
58 // Create the return instruction and add it to the basic block
59 ReturnInst::Create(M->getContext(), Add, BB);
60
61 // Now, function add1 is ready.
62 return Add1F;
63 }
64
CreateFibFunction(Module * M)65 static Function *CreateFibFunction(Module *M) {
66 // Create the fib function and insert it into module M. This function is said
67 // to return an int and take an int parameter.
68 Function *FibF =
69 cast<Function>(M->getOrInsertFunction("fib",
70 Type::getInt32Ty(M->getContext()),
71 Type::getInt32Ty(M->getContext()),
72 (Type *)0));
73
74 // Add a basic block to the function.
75 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
76
77 // Get pointers to the constants.
78 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
79 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
80
81 // Get pointer to the integer argument of the add1 function...
82 Argument *ArgX = FibF->arg_begin(); // Get the arg.
83 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
84
85 // Create the true_block.
86 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
87 // Create an exit block.
88 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
89
90 // Create the "if (arg < 2) goto exitbb"
91 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
92 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
93
94 // Create: ret int 1
95 ReturnInst::Create(M->getContext(), One, RetBB);
96
97 // create fib(x-1)
98 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
99 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
100
101 // create fib(x-2)
102 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
103 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
104
105 // fib(x-1)+fib(x-2)
106 Value *Sum =
107 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
108
109 // Create the return instruction and add it to the basic block
110 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
111
112 return FibF;
113 }
114
115 struct threadParams {
116 ExecutionEngine* EE;
117 Function* F;
118 int value;
119 };
120
121 // We block the subthreads just before they begin to execute:
122 // we want all of them to call into the JIT at the same time,
123 // to verify that the locking is working correctly.
124 class WaitForThreads
125 {
126 public:
WaitForThreads()127 WaitForThreads()
128 {
129 n = 0;
130 waitFor = 0;
131
132 int result = pthread_cond_init( &condition, NULL );
133 assert( result == 0 );
134
135 result = pthread_mutex_init( &mutex, NULL );
136 assert( result == 0 );
137 }
138
~WaitForThreads()139 ~WaitForThreads()
140 {
141 int result = pthread_cond_destroy( &condition );
142 (void)result;
143 assert( result == 0 );
144
145 result = pthread_mutex_destroy( &mutex );
146 assert( result == 0 );
147 }
148
149 // All threads will stop here until another thread calls releaseThreads
block()150 void block()
151 {
152 int result = pthread_mutex_lock( &mutex );
153 (void)result;
154 assert( result == 0 );
155 n ++;
156 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
157
158 assert( waitFor == 0 || n <= waitFor );
159 if ( waitFor > 0 && n == waitFor )
160 {
161 // There are enough threads blocked that we can release all of them
162 std::cout << "Unblocking threads from block()" << std::endl;
163 unblockThreads();
164 }
165 else
166 {
167 // We just need to wait until someone unblocks us
168 result = pthread_cond_wait( &condition, &mutex );
169 assert( result == 0 );
170 }
171
172 // unlock the mutex before returning
173 result = pthread_mutex_unlock( &mutex );
174 assert( result == 0 );
175 }
176
177 // If there are num or more threads blocked, it will signal them all
178 // Otherwise, this thread blocks until there are enough OTHER threads
179 // blocked
releaseThreads(size_t num)180 void releaseThreads( size_t num )
181 {
182 int result = pthread_mutex_lock( &mutex );
183 (void)result;
184 assert( result == 0 );
185
186 if ( n >= num ) {
187 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
188 unblockThreads();
189 }
190 else
191 {
192 waitFor = num;
193 pthread_cond_wait( &condition, &mutex );
194 }
195
196 // unlock the mutex before returning
197 result = pthread_mutex_unlock( &mutex );
198 assert( result == 0 );
199 }
200
201 private:
unblockThreads()202 void unblockThreads()
203 {
204 // Reset the counters to zero: this way, if any new threads
205 // enter while threads are exiting, they will block instead
206 // of triggering a new release of threads
207 n = 0;
208
209 // Reset waitFor to zero: this way, if waitFor threads enter
210 // while threads are exiting, they will block instead of
211 // triggering a new release of threads
212 waitFor = 0;
213
214 int result = pthread_cond_broadcast( &condition );
215 (void)result;
216 assert(result == 0);
217 }
218
219 size_t n;
220 size_t waitFor;
221 pthread_cond_t condition;
222 pthread_mutex_t mutex;
223 };
224
225 static WaitForThreads synchronize;
226
callFunc(void * param)227 void* callFunc( void* param )
228 {
229 struct threadParams* p = (struct threadParams*) param;
230
231 // Call the `foo' function with no arguments:
232 std::vector<GenericValue> Args(1);
233 Args[0].IntVal = APInt(32, p->value);
234
235 synchronize.block(); // wait until other threads are at this point
236 GenericValue gv = p->EE->runFunction(p->F, Args);
237
238 return (void*)(intptr_t)gv.IntVal.getZExtValue();
239 }
240
main()241 int main() {
242 InitializeNativeTarget();
243 LLVMContext Context;
244
245 // Create some module to put our function into it.
246 Module *M = new Module("test", Context);
247
248 Function* add1F = createAdd1( M );
249 Function* fibF = CreateFibFunction( M );
250
251 // Now we create the JIT.
252 ExecutionEngine* EE = EngineBuilder(M).create();
253
254 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
255 //~ std::cout << "\n\nRunning foo: " << std::flush;
256
257 // Create one thread for add1 and two threads for fib
258 struct threadParams add1 = { EE, add1F, 1000 };
259 struct threadParams fib1 = { EE, fibF, 39 };
260 struct threadParams fib2 = { EE, fibF, 42 };
261
262 pthread_t add1Thread;
263 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
264 if ( result != 0 ) {
265 std::cerr << "Could not create thread" << std::endl;
266 return 1;
267 }
268
269 pthread_t fibThread1;
270 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
271 if ( result != 0 ) {
272 std::cerr << "Could not create thread" << std::endl;
273 return 1;
274 }
275
276 pthread_t fibThread2;
277 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
278 if ( result != 0 ) {
279 std::cerr << "Could not create thread" << std::endl;
280 return 1;
281 }
282
283 synchronize.releaseThreads(3); // wait until other threads are at this point
284
285 void* returnValue;
286 result = pthread_join( add1Thread, &returnValue );
287 if ( result != 0 ) {
288 std::cerr << "Could not join thread" << std::endl;
289 return 1;
290 }
291 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
292
293 result = pthread_join( fibThread1, &returnValue );
294 if ( result != 0 ) {
295 std::cerr << "Could not join thread" << std::endl;
296 return 1;
297 }
298 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
299
300 result = pthread_join( fibThread2, &returnValue );
301 if ( result != 0 ) {
302 std::cerr << "Could not join thread" << std::endl;
303 return 1;
304 }
305 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
306
307 return 0;
308 }
309