It has finally happened. Spiller is now using live interval info.
[llvm/msp430.git] / examples / ParallelJIT / ParallelJIT.cpp
blobe812d84eafc85a8ef254414f0caaf02e74746b9e
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
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.
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>
30 using namespace llvm;
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.
36 Function *Add1F =
37 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
38 (Type *)0));
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.
59 return Add1F;
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.
65 Function *FibF =
66 cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
67 (Type *)0));
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);
89 // Create: ret int 1
90 ReturnInst::Create(One, RetBB);
92 // create fib(x-1)
93 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
94 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
96 // create fib(x-2)
97 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
98 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
100 // fib(x-1)+fib(x-2)
101 Value *Sum =
102 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
104 // Create the return instruction and add it to the basic block
105 ReturnInst::Create(Sum, RecurseBB);
107 return FibF;
110 struct threadParams {
111 ExecutionEngine* EE;
112 Function* F;
113 int value;
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.
119 class WaitForThreads
121 public:
122 WaitForThreads()
124 n = 0;
125 waitFor = 0;
127 int result = pthread_cond_init( &condition, NULL );
128 assert( result == 0 );
130 result = pthread_mutex_init( &mutex, NULL );
131 assert( result == 0 );
134 ~WaitForThreads()
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
144 void block()
146 int result = pthread_mutex_lock( &mutex );
147 assert( result == 0 );
148 n ++;
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;
156 unblockThreads();
158 else
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
172 // blocked
173 void releaseThreads( size_t num )
175 int result = pthread_mutex_lock( &mutex );
176 assert( result == 0 );
178 if ( n >= num ) {
179 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
180 unblockThreads();
182 else
184 waitFor = num;
185 pthread_cond_wait( &condition, &mutex );
188 // unlock the mutex before returning
189 result = pthread_mutex_unlock( &mutex );
190 assert( result == 0 );
193 private:
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
199 n = 0;
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;
206 int result = pthread_cond_broadcast( &condition );
207 assert(result == 0); result=result;
210 size_t n;
211 size_t waitFor;
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();
232 int main()
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 );
254 if ( result != 0 ) {
255 std::cerr << "Could not create thread" << std::endl;
256 return 1;
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;
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;
273 synchronize.releaseThreads(3); // wait until other threads are at this point
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;
281 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
283 result = pthread_join( fibThread1, &returnValue );
284 if ( result != 0 ) {
285 std::cerr << "Could not join thread" << std::endl;
286 return 1;
288 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
290 result = pthread_join( fibThread2, &returnValue );
291 if ( result != 0 ) {
292 std::cerr << "Could not join thread" << std::endl;
293 return 1;
295 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
297 return 0;