blob: eadd0f58e5d50d6f3029f846077cf7e658ab25eb [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//
Chris Lattnerfc001bb2007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencere8cdc8b2005-07-12 21:51:33 +00007//
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>
Owen Anderson8b477ed2009-07-01 16:58:40 +000021#include "llvm/LLVMContext.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000022#include "llvm/Module.h"
23#include "llvm/Constants.h"
Reid Spencer56427032007-01-19 22:45:50 +000024#include "llvm/DerivedTypes.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000025#include "llvm/Instructions.h"
26#include "llvm/ModuleProvider.h"
Jeff Cohenafebb442006-03-24 03:11:31 +000027#include "llvm/ExecutionEngine/JIT.h"
28#include "llvm/ExecutionEngine/Interpreter.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000029#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerda062882009-06-17 16:48:44 +000030#include "llvm/Target/TargetSelect.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000031#include <iostream>
32using namespace llvm;
33
Chris Lattner6a987542007-01-07 07:40:09 +000034static Function* createAdd1(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000035 // 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.
Chris Lattner6a987542007-01-07 07:40:09 +000038 Function *Add1F =
39 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
40 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000041
Reid Spencere8cdc8b2005-07-12 21:51:33 +000042 // Add a basic block to the function. As before, it automatically inserts
43 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000044 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
Jeff Cohen00b168892005-07-27 06:12:32 +000045
Reid Spencere8cdc8b2005-07-12 21:51:33 +000046 // Get pointers to the constant `1'.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000047 Value *One = ConstantInt::get(Type::Int32Ty, 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000048
Reid Spencere8cdc8b2005-07-12 21:51:33 +000049 // Get pointers to the integer argument of the add1 function...
50 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
51 Argument *ArgX = Add1F->arg_begin(); // Get the arg
52 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000053
Reid Spencere8cdc8b2005-07-12 21:51:33 +000054 // Create the add instruction, inserting it into the end of BB.
Gabor Greif7cbd8a32008-05-16 19:29:10 +000055 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000056
Reid Spencere8cdc8b2005-07-12 21:51:33 +000057 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000058 ReturnInst::Create(Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000059
60 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000061 return Add1F;
62}
63
Chris Lattner6a987542007-01-07 07:40:09 +000064static Function *CreateFibFunction(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000065 // Create the fib function and insert it into module M. This function is said
66 // to return an int and take an int parameter.
Chris Lattner6a987542007-01-07 07:40:09 +000067 Function *FibF =
68 cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
69 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000070
Reid Spencere8cdc8b2005-07-12 21:51:33 +000071 // Add a basic block to the function.
Gabor Greif051a9502008-04-06 20:25:17 +000072 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000073
Reid Spencere8cdc8b2005-07-12 21:51:33 +000074 // Get pointers to the constants.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000075 Value *One = ConstantInt::get(Type::Int32Ty, 1);
76 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
Jeff Cohen00b168892005-07-27 06:12:32 +000077
Reid Spencere8cdc8b2005-07-12 21:51:33 +000078 // Get pointer to the integer argument of the add1 function...
79 Argument *ArgX = FibF->arg_begin(); // Get the arg.
80 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000081
Reid Spencere8cdc8b2005-07-12 21:51:33 +000082 // Create the true_block.
Gabor Greif051a9502008-04-06 20:25:17 +000083 BasicBlock *RetBB = BasicBlock::Create("return", FibF);
Reid Spencere8cdc8b2005-07-12 21:51:33 +000084 // Create an exit block.
Gabor Greif051a9502008-04-06 20:25:17 +000085 BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000086
Reid Spencere8cdc8b2005-07-12 21:51:33 +000087 // Create the "if (arg < 2) goto exitbb"
Reid Spencere4d87aa2006-12-23 06:05:41 +000088 Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
Gabor Greif051a9502008-04-06 20:25:17 +000089 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000090
Reid Spencere8cdc8b2005-07-12 21:51:33 +000091 // Create: ret int 1
Gabor Greif051a9502008-04-06 20:25:17 +000092 ReturnInst::Create(One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000093
Reid Spencere8cdc8b2005-07-12 21:51:33 +000094 // create fib(x-1)
Gabor Greif7cbd8a32008-05-16 19:29:10 +000095 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +000096 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000097
Reid Spencere8cdc8b2005-07-12 21:51:33 +000098 // create fib(x-2)
Gabor Greif7cbd8a32008-05-16 19:29:10 +000099 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000100 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000101
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000102 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +0000103 Value *Sum =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000104 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000105
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000106 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +0000107 ReturnInst::Create(Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000108
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000109 return FibF;
110}
111
112struct threadParams {
113 ExecutionEngine* EE;
114 Function* F;
115 int value;
116};
117
118// We block the subthreads just before they begin to execute:
119// we want all of them to call into the JIT at the same time,
120// to verify that the locking is working correctly.
121class WaitForThreads
122{
123public:
124 WaitForThreads()
125 {
126 n = 0;
127 waitFor = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000128
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000129 int result = pthread_cond_init( &condition, NULL );
130 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000131
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000132 result = pthread_mutex_init( &mutex, NULL );
133 assert( result == 0 );
134 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000135
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000136 ~WaitForThreads()
137 {
138 int result = pthread_cond_destroy( &condition );
139 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000140
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000141 result = pthread_mutex_destroy( &mutex );
142 assert( result == 0 );
143 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000144
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000145 // All threads will stop here until another thread calls releaseThreads
146 void block()
147 {
148 int result = pthread_mutex_lock( &mutex );
149 assert( result == 0 );
150 n ++;
151 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000152
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000153 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000154 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000155 {
156 // There are enough threads blocked that we can release all of them
157 std::cout << "Unblocking threads from block()" << std::endl;
158 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000159 }
160 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000161 {
162 // We just need to wait until someone unblocks us
163 result = pthread_cond_wait( &condition, &mutex );
164 assert( result == 0 );
165 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000166
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000167 // unlock the mutex before returning
168 result = pthread_mutex_unlock( &mutex );
169 assert( result == 0 );
170 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000171
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000172 // If there are num or more threads blocked, it will signal them all
173 // Otherwise, this thread blocks until there are enough OTHER threads
174 // blocked
175 void releaseThreads( size_t num )
176 {
177 int result = pthread_mutex_lock( &mutex );
178 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000179
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000180 if ( n >= num ) {
181 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
182 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000183 }
184 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000185 {
186 waitFor = num;
187 pthread_cond_wait( &condition, &mutex );
188 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000189
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000190 // unlock the mutex before returning
191 result = pthread_mutex_unlock( &mutex );
192 assert( result == 0 );
193 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000194
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000195private:
196 void unblockThreads()
197 {
198 // Reset the counters to zero: this way, if any new threads
199 // enter while threads are exiting, they will block instead
200 // of triggering a new release of threads
201 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000202
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000203 // Reset waitFor to zero: this way, if waitFor threads enter
204 // while threads are exiting, they will block instead of
205 // triggering a new release of threads
206 waitFor = 0;
207
208 int result = pthread_cond_broadcast( &condition );
Chris Lattner70987722008-04-08 05:49:09 +0000209 assert(result == 0); result=result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000210 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000211
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000212 size_t n;
213 size_t waitFor;
214 pthread_cond_t condition;
215 pthread_mutex_t mutex;
216};
217
218static WaitForThreads synchronize;
219
220void* callFunc( void* param )
221{
222 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000223
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000224 // Call the `foo' function with no arguments:
225 std::vector<GenericValue> Args(1);
Reid Spencer34bd70d2007-03-06 17:24:31 +0000226 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen00b168892005-07-27 06:12:32 +0000227
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000228 synchronize.block(); // wait until other threads are at this point
229 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000230
Reid Spencer34bd70d2007-03-06 17:24:31 +0000231 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000232}
233
Chris Lattnerda062882009-06-17 16:48:44 +0000234int main() {
235 InitializeNativeTarget();
Owen Anderson8b477ed2009-07-01 16:58:40 +0000236 LLVMContext Context;
Chris Lattnerda062882009-06-17 16:48:44 +0000237
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000238 // Create some module to put our function into it.
Owen Anderson31895e72009-07-01 21:22:36 +0000239 Module *M = new Module("test", Context);
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000241 Function* add1F = createAdd1( M );
242 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000244 // Now we create the JIT.
245 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
246 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000248 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
249 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000250
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000251 // Create one thread for add1 and two threads for fib
252 struct threadParams add1 = { EE, add1F, 1000 };
253 struct threadParams fib1 = { EE, fibF, 39 };
254 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000255
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000256 pthread_t add1Thread;
257 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
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 fibThread1;
264 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
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 pthread_t fibThread2;
271 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
272 if ( result != 0 ) {
273 std::cerr << "Could not create thread" << std::endl;
274 return 1;
275 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000276
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000277 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000278
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000279 void* returnValue;
280 result = pthread_join( add1Thread, &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 << "Add1 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( fibThread1, &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 << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000293
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000294 result = pthread_join( fibThread2, &returnValue );
295 if ( result != 0 ) {
296 std::cerr << "Could not join thread" << std::endl;
297 return 1;
298 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000299 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000300
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000301 return 0;
302}