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,
39 // Add a basic block to the function. As before, it automatically inserts
40 // because of the last argument.
41 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
43 // Get pointers to the constant `1'.
44 Value *One = ConstantSInt::get(Type::IntTy, 1);
46 // Get pointers to the integer argument of the add1 function...
47 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
48 Argument *ArgX = Add1F->arg_begin(); // Get the arg
49 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
51 // Create the add instruction, inserting it into the end of BB.
52 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
54 // Create the return instruction and add it to the basic block
55 new ReturnInst(Add, BB);
57 // Now, function add1 is ready.
61 static 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 = M->getOrInsertFunction("fib", Type::IntTy, Type::IntTy,
68 // Add a basic block to the function.
69 BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
71 // Get pointers to the constants.
72 Value *One = ConstantSInt::get(Type::IntTy, 1);
73 Value *Two = ConstantSInt::get(Type::IntTy, 2);
75 // Get pointer to the integer argument of the add1 function...
76 Argument *ArgX = FibF->arg_begin(); // Get the arg.
77 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
79 // Create the true_block.
80 BasicBlock *RetBB = new BasicBlock("return", FibF);
81 // Create an exit block.
82 BasicBlock* RecurseBB = new BasicBlock("recurse", FibF);
84 // Create the "if (arg < 2) goto exitbb"
85 Value *CondInst = BinaryOperator::createSetLE(ArgX, Two, "cond", BB);
86 new BranchInst(RetBB, RecurseBB, CondInst, BB);
89 new ReturnInst(One, RetBB);
92 Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
93 Value *CallFibX1 = new CallInst(FibF, Sub, "fibx1", RecurseBB);
96 Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
97 Value *CallFibX2 = new CallInst(FibF, Sub, "fibx2", RecurseBB);
101 BinaryOperator::createAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
103 // Create the return instruction and add it to the basic block
104 new ReturnInst(Sum, RecurseBB);
109 struct threadParams {
115 // We block the subthreads just before they begin to execute:
116 // we want all of them to call into the JIT at the same time,
117 // to verify that the locking is working correctly.
126 int result = pthread_cond_init( &condition, NULL );
127 assert( result == 0 );
129 result = pthread_mutex_init( &mutex, NULL );
130 assert( result == 0 );
135 int result = pthread_cond_destroy( &condition );
136 assert( result == 0 );
138 result = pthread_mutex_destroy( &mutex );
139 assert( result == 0 );
142 // All threads will stop here until another thread calls releaseThreads
145 int result = pthread_mutex_lock( &mutex );
146 assert( result == 0 );
148 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
150 assert( waitFor == 0 || n <= waitFor );
151 if ( waitFor > 0 && n == waitFor )
153 // There are enough threads blocked that we can release all of them
154 std::cout << "Unblocking threads from block()" << std::endl;
159 // We just need to wait until someone unblocks us
160 result = pthread_cond_wait( &condition, &mutex );
161 assert( result == 0 );
164 // unlock the mutex before returning
165 result = pthread_mutex_unlock( &mutex );
166 assert( result == 0 );
169 // If there are num or more threads blocked, it will signal them all
170 // Otherwise, this thread blocks until there are enough OTHER threads
172 void releaseThreads( size_t num )
174 int result = pthread_mutex_lock( &mutex );
175 assert( result == 0 );
178 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
184 pthread_cond_wait( &condition, &mutex );
187 // unlock the mutex before returning
188 result = pthread_mutex_unlock( &mutex );
189 assert( result == 0 );
193 void unblockThreads()
195 // Reset the counters to zero: this way, if any new threads
196 // enter while threads are exiting, they will block instead
197 // of triggering a new release of threads
200 // Reset waitFor to zero: this way, if waitFor threads enter
201 // while threads are exiting, they will block instead of
202 // triggering a new release of threads
205 int result = pthread_cond_broadcast( &condition );
206 assert( result == 0 );
211 pthread_cond_t condition;
212 pthread_mutex_t mutex;
215 static WaitForThreads synchronize;
217 void* callFunc( void* param )
219 struct threadParams* p = (struct threadParams*) param;
221 // Call the `foo' function with no arguments:
222 std::vector<GenericValue> Args(1);
223 Args[0].IntVal = p->value;
225 synchronize.block(); // wait until other threads are at this point
226 GenericValue gv = p->EE->runFunction(p->F, Args);
228 return (void*) intptr_t(gv.IntVal);
233 // Create some module to put our function into it.
234 Module *M = new Module("test");
236 Function* add1F = createAdd1( M );
237 Function* fibF = CreateFibFunction( M );
239 // Now we create the JIT.
240 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
241 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
243 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
244 //~ std::cout << "\n\nRunning foo: " << std::flush;
246 // Create one thread for add1 and two threads for fib
247 struct threadParams add1 = { EE, add1F, 1000 };
248 struct threadParams fib1 = { EE, fibF, 39 };
249 struct threadParams fib2 = { EE, fibF, 42 };
251 pthread_t add1Thread;
252 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
254 std::cerr << "Could not create thread" << std::endl;
258 pthread_t fibThread1;
259 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
261 std::cerr << "Could not create thread" << std::endl;
265 pthread_t fibThread2;
266 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
268 std::cerr << "Could not create thread" << std::endl;
272 synchronize.releaseThreads(3); // wait until other threads are at this point
275 result = pthread_join( add1Thread, &returnValue );
277 std::cerr << "Could not join thread" << std::endl;
280 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
282 result = pthread_join( fibThread1, &returnValue );
284 std::cerr << "Could not join thread" << std::endl;
287 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
289 result = pthread_join( fibThread2, &returnValue );
291 std::cerr << "Could not join thread" << std::endl;
294 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;