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