1 //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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"
31 static 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 = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy, 0);
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);
42 // Get pointers to the constant `1'.
43 Value *One = ConstantSInt::get(Type::IntTy, 1);
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.
50 // Create the add instruction, inserting it into the end of BB.
51 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
53 // Create the return instruction and add it to the basic block
54 new ReturnInst(Add, BB);
56 // Now, function add1 is ready.
60 static Function *CreateFibFunction(Module *M)
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);
66 // Add a basic block to the function.
67 BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
69 // Get pointers to the constants.
70 Value *One = ConstantSInt::get(Type::IntTy, 1);
71 Value *Two = ConstantSInt::get(Type::IntTy, 2);
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.
77 // Create the true_block.
78 BasicBlock *RetBB = new BasicBlock("return", FibF);
79 // Create an exit block.
80 BasicBlock* RecurseBB = new BasicBlock("recurse", FibF);
82 // Create the "if (arg < 2) goto exitbb"
83 Value *CondInst = BinaryOperator::createSetLE(ArgX, Two, "cond", BB);
84 new BranchInst(RetBB, RecurseBB, CondInst, BB);
87 new ReturnInst(One, RetBB);
90 Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
91 Value *CallFibX1 = new CallInst(FibF, Sub, "fibx1", RecurseBB);
94 Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
95 Value *CallFibX2 = new CallInst(FibF, Sub, "fibx2", RecurseBB);
99 BinaryOperator::createAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
101 // Create the return instruction and add it to the basic block
102 new ReturnInst(Sum, RecurseBB);
107 struct threadParams {
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.
124 int result = pthread_cond_init( &condition, NULL );
125 assert( result == 0 );
127 result = pthread_mutex_init( &mutex, NULL );
128 assert( result == 0 );
133 int result = pthread_cond_destroy( &condition );
134 assert( result == 0 );
136 result = pthread_mutex_destroy( &mutex );
137 assert( result == 0 );
140 // All threads will stop here until another thread calls releaseThreads
143 int result = pthread_mutex_lock( &mutex );
144 assert( result == 0 );
146 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
148 assert( waitFor == 0 || n <= waitFor );
149 if ( waitFor > 0 && n == waitFor )
151 // There are enough threads blocked that we can release all of them
152 std::cout << "Unblocking threads from block()" << std::endl;
157 // We just need to wait until someone unblocks us
158 result = pthread_cond_wait( &condition, &mutex );
159 assert( result == 0 );
162 // unlock the mutex before returning
163 result = pthread_mutex_unlock( &mutex );
164 assert( result == 0 );
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
170 void releaseThreads( size_t num )
172 int result = pthread_mutex_lock( &mutex );
173 assert( result == 0 );
176 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
182 pthread_cond_wait( &condition, &mutex );
185 // unlock the mutex before returning
186 result = pthread_mutex_unlock( &mutex );
187 assert( result == 0 );
191 void unblockThreads()
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
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
203 int result = pthread_cond_broadcast( &condition );
204 assert( result == 0 );
209 pthread_cond_t condition;
210 pthread_mutex_t mutex;
213 static WaitForThreads synchronize;
215 void* callFunc( void* param )
217 struct threadParams* p = (struct threadParams*) param;
219 // Call the `foo' function with no arguments:
220 std::vector<GenericValue> Args(1);
221 Args[0].IntVal = p->value;
223 synchronize.block(); // wait until other threads are at this point
224 GenericValue gv = p->EE->runFunction(p->F, Args);
226 return (void*) intptr_t(gv.IntVal);
231 // Create some module to put our function into it.
232 Module *M = new Module("test");
234 Function* add1F = createAdd1( M );
235 Function* fibF = CreateFibFunction( M );
237 // Now we create the JIT.
238 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
239 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
241 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
242 //~ std::cout << "\n\nRunning foo: " << std::flush;
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 };
249 pthread_t add1Thread;
250 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
252 std::cerr << "Could not create thread" << std::endl;
256 pthread_t fibThread1;
257 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
259 std::cerr << "Could not create thread" << std::endl;
263 pthread_t fibThread2;
264 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
266 std::cerr << "Could not create thread" << std::endl;
270 synchronize.releaseThreads(3); // wait until other threads are at this point
273 result = pthread_join( add1Thread, &returnValue );
275 std::cerr << "Could not join thread" << std::endl;
278 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
280 result = pthread_join( fibThread1, &returnValue );
282 std::cerr << "Could not join thread" << std::endl;
285 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
287 result = pthread_join( fibThread2, &returnValue );
289 std::cerr << "Could not join thread" << std::endl;
292 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;