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/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"
32 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.
37 cast
<Function
>(M
->getOrInsertFunction("add1", Type::Int32Ty
, Type::Int32Ty
,
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
);
44 // Get pointers to the constant `1'.
45 Value
*One
= ConstantInt::get(Type::Int32Ty
, 1);
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.
52 // Create the add instruction, inserting it into the end of BB.
53 Instruction
*Add
= BinaryOperator::CreateAdd(One
, ArgX
, "addresult", BB
);
55 // Create the return instruction and add it to the basic block
56 ReturnInst::Create(Add
, BB
);
58 // Now, function add1 is ready.
62 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.
66 cast
<Function
>(M
->getOrInsertFunction("fib", Type::Int32Ty
, Type::Int32Ty
,
69 // Add a basic block to the function.
70 BasicBlock
*BB
= BasicBlock::Create("EntryBlock", FibF
);
72 // Get pointers to the constants.
73 Value
*One
= ConstantInt::get(Type::Int32Ty
, 1);
74 Value
*Two
= ConstantInt::get(Type::Int32Ty
, 2);
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.
80 // Create the true_block.
81 BasicBlock
*RetBB
= BasicBlock::Create("return", FibF
);
82 // Create an exit block.
83 BasicBlock
* RecurseBB
= BasicBlock::Create("recurse", FibF
);
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
);
90 ReturnInst::Create(One
, RetBB
);
93 Value
*Sub
= BinaryOperator::CreateSub(ArgX
, One
, "arg", RecurseBB
);
94 Value
*CallFibX1
= CallInst::Create(FibF
, Sub
, "fibx1", RecurseBB
);
97 Sub
= BinaryOperator::CreateSub(ArgX
, Two
, "arg", RecurseBB
);
98 Value
*CallFibX2
= CallInst::Create(FibF
, Sub
, "fibx2", RecurseBB
);
102 BinaryOperator::CreateAdd(CallFibX1
, CallFibX2
, "addresult", RecurseBB
);
104 // Create the return instruction and add it to the basic block
105 ReturnInst::Create(Sum
, RecurseBB
);
110 struct threadParams
{
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.
127 int result
= pthread_cond_init( &condition
, NULL
);
128 assert( result
== 0 );
130 result
= pthread_mutex_init( &mutex
, NULL
);
131 assert( result
== 0 );
136 int result
= pthread_cond_destroy( &condition
);
137 assert( result
== 0 );
139 result
= pthread_mutex_destroy( &mutex
);
140 assert( result
== 0 );
143 // All threads will stop here until another thread calls releaseThreads
146 int result
= pthread_mutex_lock( &mutex
);
147 assert( result
== 0 );
149 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
151 assert( waitFor
== 0 || n
<= waitFor
);
152 if ( waitFor
> 0 && n
== waitFor
)
154 // There are enough threads blocked that we can release all of them
155 std::cout
<< "Unblocking threads from block()" << std::endl
;
160 // We just need to wait until someone unblocks us
161 result
= pthread_cond_wait( &condition
, &mutex
);
162 assert( result
== 0 );
165 // unlock the mutex before returning
166 result
= pthread_mutex_unlock( &mutex
);
167 assert( result
== 0 );
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
173 void releaseThreads( size_t num
)
175 int result
= pthread_mutex_lock( &mutex
);
176 assert( result
== 0 );
179 std::cout
<< "Unblocking threads from releaseThreads()" << std::endl
;
185 pthread_cond_wait( &condition
, &mutex
);
188 // unlock the mutex before returning
189 result
= pthread_mutex_unlock( &mutex
);
190 assert( result
== 0 );
194 void unblockThreads()
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
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
206 int result
= pthread_cond_broadcast( &condition
);
207 assert(result
== 0); result
=result
;
212 pthread_cond_t condition
;
213 pthread_mutex_t mutex
;
216 static WaitForThreads synchronize
;
218 void* callFunc( void* param
)
220 struct threadParams
* p
= (struct threadParams
*) param
;
222 // Call the `foo' function with no arguments:
223 std::vector
<GenericValue
> Args(1);
224 Args
[0].IntVal
= APInt(32, p
->value
);
226 synchronize
.block(); // wait until other threads are at this point
227 GenericValue gv
= p
->EE
->runFunction(p
->F
, Args
);
229 return (void*)(intptr_t)gv
.IntVal
.getZExtValue();
234 // Create some module to put our function into it.
235 Module
*M
= new Module("test");
237 Function
* add1F
= createAdd1( M
);
238 Function
* fibF
= CreateFibFunction( M
);
240 // Now we create the JIT.
241 ExistingModuleProvider
* MP
= new ExistingModuleProvider(M
);
242 ExecutionEngine
* EE
= ExecutionEngine::create(MP
, false);
244 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
245 //~ std::cout << "\n\nRunning foo: " << std::flush;
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 };
252 pthread_t add1Thread
;
253 int result
= pthread_create( &add1Thread
, NULL
, callFunc
, &add1
);
255 std::cerr
<< "Could not create thread" << std::endl
;
259 pthread_t fibThread1
;
260 result
= pthread_create( &fibThread1
, NULL
, callFunc
, &fib1
);
262 std::cerr
<< "Could not create thread" << std::endl
;
266 pthread_t fibThread2
;
267 result
= pthread_create( &fibThread2
, NULL
, callFunc
, &fib2
);
269 std::cerr
<< "Could not create thread" << std::endl
;
273 synchronize
.releaseThreads(3); // wait until other threads are at this point
276 result
= pthread_join( add1Thread
, &returnValue
);
278 std::cerr
<< "Could not join thread" << std::endl
;
281 std::cout
<< "Add1 returned " << intptr_t(returnValue
) << std::endl
;
283 result
= pthread_join( fibThread1
, &returnValue
);
285 std::cerr
<< "Could not join thread" << std::endl
;
288 std::cout
<< "Fib1 returned " << intptr_t(returnValue
) << std::endl
;
290 result
= pthread_join( fibThread2
, &returnValue
);
292 std::cerr
<< "Could not join thread" << std::endl
;
295 std::cout
<< "Fib2 returned " << intptr_t(returnValue
) << std::endl
;