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