1 //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/ModuleProvider.h"
27 #include "llvm/ExecutionEngine/JIT.h"
28 #include "llvm/ExecutionEngine/Interpreter.h"
29 #include "llvm/ExecutionEngine/GenericValue.h"
30 #include "llvm/Target/TargetSelect.h"
34 static Function* createAdd1(Module *M) {
35 // Create the add1 function entry and insert this entry into module M. The
36 // function will have a return type of "int" and take an argument of "int".
37 // The '0' terminates the list of argument types.
39 cast<Function>(M->getOrInsertFunction("add1",
40 Type::getInt32Ty(M->getContext()),
41 Type::getInt32Ty(M->getContext()),
44 // Add a basic block to the function. As before, it automatically inserts
45 // because of the last argument.
46 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
48 // Get pointers to the constant `1'.
49 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
51 // Get pointers to the integer argument of the add1 function...
52 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
53 Argument *ArgX = Add1F->arg_begin(); // Get the arg
54 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
56 // Create the add instruction, inserting it into the end of BB.
57 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
59 // Create the return instruction and add it to the basic block
60 ReturnInst::Create(M->getContext(), Add, BB);
62 // Now, function add1 is ready.
66 static Function *CreateFibFunction(Module *M) {
67 // Create the fib function and insert it into module M. This function is said
68 // to return an int and take an int parameter.
70 cast<Function>(M->getOrInsertFunction("fib",
71 Type::getInt32Ty(M->getContext()),
72 Type::getInt32Ty(M->getContext()),
75 // Add a basic block to the function.
76 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
78 // Get pointers to the constants.
79 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
80 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
82 // Get pointer to the integer argument of the add1 function...
83 Argument *ArgX = FibF->arg_begin(); // Get the arg.
84 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
86 // Create the true_block.
87 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
88 // Create an exit block.
89 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
91 // Create the "if (arg < 2) goto exitbb"
92 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
93 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
96 ReturnInst::Create(M->getContext(), One, RetBB);
99 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
100 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
103 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
104 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
108 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
110 // Create the return instruction and add it to the basic block
111 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
116 struct threadParams {
122 // We block the subthreads just before they begin to execute:
123 // we want all of them to call into the JIT at the same time,
124 // to verify that the locking is working correctly.
133 int result = pthread_cond_init( &condition, NULL );
134 assert( result == 0 );
136 result = pthread_mutex_init( &mutex, NULL );
137 assert( result == 0 );
142 int result = pthread_cond_destroy( &condition );
143 assert( result == 0 );
145 result = pthread_mutex_destroy( &mutex );
146 assert( result == 0 );
149 // All threads will stop here until another thread calls releaseThreads
152 int result = pthread_mutex_lock( &mutex );
153 assert( result == 0 );
155 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
157 assert( waitFor == 0 || n <= waitFor );
158 if ( waitFor > 0 && n == waitFor )
160 // There are enough threads blocked that we can release all of them
161 std::cout << "Unblocking threads from block()" << std::endl;
166 // We just need to wait until someone unblocks us
167 result = pthread_cond_wait( &condition, &mutex );
168 assert( result == 0 );
171 // unlock the mutex before returning
172 result = pthread_mutex_unlock( &mutex );
173 assert( result == 0 );
176 // If there are num or more threads blocked, it will signal them all
177 // Otherwise, this thread blocks until there are enough OTHER threads
179 void releaseThreads( size_t num )
181 int result = pthread_mutex_lock( &mutex );
182 assert( result == 0 );
185 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
191 pthread_cond_wait( &condition, &mutex );
194 // unlock the mutex before returning
195 result = pthread_mutex_unlock( &mutex );
196 assert( result == 0 );
200 void unblockThreads()
202 // Reset the counters to zero: this way, if any new threads
203 // enter while threads are exiting, they will block instead
204 // of triggering a new release of threads
207 // Reset waitFor to zero: this way, if waitFor threads enter
208 // while threads are exiting, they will block instead of
209 // triggering a new release of threads
212 int result = pthread_cond_broadcast( &condition );
213 assert(result == 0); result=result;
218 pthread_cond_t condition;
219 pthread_mutex_t mutex;
222 static WaitForThreads synchronize;
224 void* callFunc( void* param )
226 struct threadParams* p = (struct threadParams*) param;
228 // Call the `foo' function with no arguments:
229 std::vector<GenericValue> Args(1);
230 Args[0].IntVal = APInt(32, p->value);
232 synchronize.block(); // wait until other threads are at this point
233 GenericValue gv = p->EE->runFunction(p->F, Args);
235 return (void*)(intptr_t)gv.IntVal.getZExtValue();
239 InitializeNativeTarget();
242 // Create some module to put our function into it.
243 Module *M = new Module("test", Context);
245 Function* add1F = createAdd1( M );
246 Function* fibF = CreateFibFunction( M );
248 // Now we create the JIT.
249 ExecutionEngine* EE = EngineBuilder(M).create();
251 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
252 //~ std::cout << "\n\nRunning foo: " << std::flush;
254 // Create one thread for add1 and two threads for fib
255 struct threadParams add1 = { EE, add1F, 1000 };
256 struct threadParams fib1 = { EE, fibF, 39 };
257 struct threadParams fib2 = { EE, fibF, 42 };
259 pthread_t add1Thread;
260 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
262 std::cerr << "Could not create thread" << std::endl;
266 pthread_t fibThread1;
267 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
269 std::cerr << "Could not create thread" << std::endl;
273 pthread_t fibThread2;
274 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
276 std::cerr << "Could not create thread" << std::endl;
280 synchronize.releaseThreads(3); // wait until other threads are at this point
283 result = pthread_join( add1Thread, &returnValue );
285 std::cerr << "Could not join thread" << std::endl;
288 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
290 result = pthread_join( fibThread1, &returnValue );
292 std::cerr << "Could not join thread" << std::endl;
295 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
297 result = pthread_join( fibThread2, &returnValue );
299 std::cerr << "Could not join thread" << std::endl;
302 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;