blob: 5c605c002ea84efdd6458414832fe019b8dccc1a [file] [log] [blame]
Reid Spencere8cdc8b2005-07-12 21:51:33 +00001//===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// Parallel JIT
11//
Jeff Cohen00b168892005-07-27 06:12:32 +000012// This test program creates two LLVM functions then calls them from three
Reid Spencere8cdc8b2005-07-12 21:51:33 +000013// 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.
19
20#include <pthread.h>
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"
28#include <iostream>
29using namespace llvm;
30
Jeff Cohen00b168892005-07-27 06:12:32 +000031static Function* createAdd1(Module* M)
Reid Spencere8cdc8b2005-07-12 21:51:33 +000032{
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);
Jeff Cohen00b168892005-07-27 06:12:32 +000037
Reid Spencere8cdc8b2005-07-12 21:51:33 +000038 // 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);
Jeff Cohen00b168892005-07-27 06:12:32 +000041
Reid Spencere8cdc8b2005-07-12 21:51:33 +000042 // Get pointers to the constant `1'.
43 Value *One = ConstantSInt::get(Type::IntTy, 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000044
Reid Spencere8cdc8b2005-07-12 21:51:33 +000045 // 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.
Jeff Cohen00b168892005-07-27 06:12:32 +000049
Reid Spencere8cdc8b2005-07-12 21:51:33 +000050 // Create the add instruction, inserting it into the end of BB.
51 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000052
Reid Spencere8cdc8b2005-07-12 21:51:33 +000053 // Create the return instruction and add it to the basic block
54 new ReturnInst(Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000055
56 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000057 return Add1F;
58}
59
60static Function *CreateFibFunction(Module *M)
61{
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);
Jeff Cohen00b168892005-07-27 06:12:32 +000065
Reid Spencere8cdc8b2005-07-12 21:51:33 +000066 // Add a basic block to the function.
67 BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000068
Reid Spencere8cdc8b2005-07-12 21:51:33 +000069 // Get pointers to the constants.
70 Value *One = ConstantSInt::get(Type::IntTy, 1);
71 Value *Two = ConstantSInt::get(Type::IntTy, 2);
Jeff Cohen00b168892005-07-27 06:12:32 +000072
Reid Spencere8cdc8b2005-07-12 21:51:33 +000073 // 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.
Jeff Cohen00b168892005-07-27 06:12:32 +000076
Reid Spencere8cdc8b2005-07-12 21:51:33 +000077 // Create the true_block.
78 BasicBlock *RetBB = new BasicBlock("return", FibF);
79 // Create an exit block.
80 BasicBlock* RecurseBB = new BasicBlock("recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000081
Reid Spencere8cdc8b2005-07-12 21:51:33 +000082 // Create the "if (arg < 2) goto exitbb"
83 Value *CondInst = BinaryOperator::createSetLE(ArgX, Two, "cond", BB);
84 new BranchInst(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000085
Reid Spencere8cdc8b2005-07-12 21:51:33 +000086 // Create: ret int 1
87 new ReturnInst(One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000088
Reid Spencere8cdc8b2005-07-12 21:51:33 +000089 // create fib(x-1)
90 Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
91 Value *CallFibX1 = new CallInst(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000092
Reid Spencere8cdc8b2005-07-12 21:51:33 +000093 // create fib(x-2)
94 Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
95 Value *CallFibX2 = new CallInst(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000096
Reid Spencere8cdc8b2005-07-12 21:51:33 +000097 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +000098 Value *Sum =
Reid Spencere8cdc8b2005-07-12 21:51:33 +000099 BinaryOperator::createAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000100
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000101 // Create the return instruction and add it to the basic block
102 new ReturnInst(Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000103
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000104 return FibF;
105}
106
107struct threadParams {
108 ExecutionEngine* EE;
109 Function* F;
110 int value;
111};
112
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.
116class WaitForThreads
117{
118public:
119 WaitForThreads()
120 {
121 n = 0;
122 waitFor = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000123
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000124 int result = pthread_cond_init( &condition, NULL );
125 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000126
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000127 result = pthread_mutex_init( &mutex, NULL );
128 assert( result == 0 );
129 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000130
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000131 ~WaitForThreads()
132 {
133 int result = pthread_cond_destroy( &condition );
134 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000135
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000136 result = pthread_mutex_destroy( &mutex );
137 assert( result == 0 );
138 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000139
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000140 // All threads will stop here until another thread calls releaseThreads
141 void block()
142 {
143 int result = pthread_mutex_lock( &mutex );
144 assert( result == 0 );
145 n ++;
146 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000147
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000148 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000149 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000150 {
151 // There are enough threads blocked that we can release all of them
152 std::cout << "Unblocking threads from block()" << std::endl;
153 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000154 }
155 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000156 {
157 // We just need to wait until someone unblocks us
158 result = pthread_cond_wait( &condition, &mutex );
159 assert( result == 0 );
160 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000161
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000162 // unlock the mutex before returning
163 result = pthread_mutex_unlock( &mutex );
164 assert( result == 0 );
165 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000166
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000167 // If there are num or more threads blocked, it will signal them all
168 // Otherwise, this thread blocks until there are enough OTHER threads
169 // blocked
170 void releaseThreads( size_t num )
171 {
172 int result = pthread_mutex_lock( &mutex );
173 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000174
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000175 if ( n >= num ) {
176 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
177 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000178 }
179 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000180 {
181 waitFor = num;
182 pthread_cond_wait( &condition, &mutex );
183 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000184
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000185 // unlock the mutex before returning
186 result = pthread_mutex_unlock( &mutex );
187 assert( result == 0 );
188 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000189
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000190private:
191 void unblockThreads()
192 {
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
196 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000197
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000198 // 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
201 waitFor = 0;
202
203 int result = pthread_cond_broadcast( &condition );
204 assert( result == 0 );
205 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000206
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000207 size_t n;
208 size_t waitFor;
209 pthread_cond_t condition;
210 pthread_mutex_t mutex;
211};
212
213static WaitForThreads synchronize;
214
215void* callFunc( void* param )
216{
217 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000218
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000219 // Call the `foo' function with no arguments:
220 std::vector<GenericValue> Args(1);
221 Args[0].IntVal = p->value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000222
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000223 synchronize.block(); // wait until other threads are at this point
224 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000225
Reid Spencer6fb0d732005-07-13 23:20:24 +0000226 return (void*) intptr_t(gv.IntVal);
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000227}
228
Jeff Cohen00b168892005-07-27 06:12:32 +0000229int main()
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000230{
231 // Create some module to put our function into it.
232 Module *M = new Module("test");
Jeff Cohen00b168892005-07-27 06:12:32 +0000233
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000234 Function* add1F = createAdd1( M );
235 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000236
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000237 // Now we create the JIT.
238 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
239 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000241 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
242 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000244 // 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 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000248
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000249 pthread_t add1Thread;
250 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
251 if ( result != 0 ) {
252 std::cerr << "Could not create thread" << std::endl;
253 return 1;
254 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000255
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000256 pthread_t fibThread1;
257 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
258 if ( result != 0 ) {
259 std::cerr << "Could not create thread" << std::endl;
260 return 1;
261 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000262
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000263 pthread_t fibThread2;
264 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
265 if ( result != 0 ) {
266 std::cerr << "Could not create thread" << std::endl;
267 return 1;
268 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000269
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000270 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000271
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000272 void* returnValue;
273 result = pthread_join( add1Thread, &returnValue );
274 if ( result != 0 ) {
275 std::cerr << "Could not join thread" << std::endl;
276 return 1;
277 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000278 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000279
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000280 result = pthread_join( fibThread1, &returnValue );
281 if ( result != 0 ) {
282 std::cerr << "Could not join thread" << std::endl;
283 return 1;
284 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000285 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000286
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000287 result = pthread_join( fibThread2, &returnValue );
288 if ( result != 0 ) {
289 std::cerr << "Could not join thread" << std::endl;
290 return 1;
291 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000292 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000293
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000294 return 0;
295}