blob: a6d7dcf7b556c0d8fde244f03e0c1e3aec857c4e [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>
21#include "llvm/Module.h"
22#include "llvm/Constants.h"
Reid Spencer56427032007-01-19 22:45:50 +000023#include "llvm/DerivedTypes.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000024#include "llvm/Instructions.h"
25#include "llvm/ModuleProvider.h"
Jeff Cohenafebb442006-03-24 03:11:31 +000026#include "llvm/ExecutionEngine/JIT.h"
27#include "llvm/ExecutionEngine/Interpreter.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000028#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerda062882009-06-17 16:48:44 +000029#include "llvm/Target/TargetSelect.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000030#include <iostream>
31using namespace llvm;
32
Chris Lattner6a987542007-01-07 07:40:09 +000033static Function* createAdd1(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000034 // Create the add1 function entry and insert this entry into module M. The
35 // function will have a return type of "int" and take an argument of "int".
36 // The '0' terminates the list of argument types.
Chris Lattner6a987542007-01-07 07:40:09 +000037 Function *Add1F =
38 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
39 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000040
Reid Spencere8cdc8b2005-07-12 21:51:33 +000041 // Add a basic block to the function. As before, it automatically inserts
42 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000043 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
Jeff Cohen00b168892005-07-27 06:12:32 +000044
Reid Spencere8cdc8b2005-07-12 21:51:33 +000045 // Get pointers to the constant `1'.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000046 Value *One = ConstantInt::get(Type::Int32Ty, 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000047
Reid Spencere8cdc8b2005-07-12 21:51:33 +000048 // Get pointers to the integer argument of the add1 function...
49 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
50 Argument *ArgX = Add1F->arg_begin(); // Get the arg
51 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000052
Reid Spencere8cdc8b2005-07-12 21:51:33 +000053 // Create the add instruction, inserting it into the end of BB.
Gabor Greif7cbd8a32008-05-16 19:29:10 +000054 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000055
Reid Spencere8cdc8b2005-07-12 21:51:33 +000056 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000057 ReturnInst::Create(Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000058
59 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000060 return Add1F;
61}
62
Chris Lattner6a987542007-01-07 07:40:09 +000063static Function *CreateFibFunction(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000064 // Create the fib function and insert it into module M. This function is said
65 // to return an int and take an int parameter.
Chris Lattner6a987542007-01-07 07:40:09 +000066 Function *FibF =
67 cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
68 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000069
Reid Spencere8cdc8b2005-07-12 21:51:33 +000070 // Add a basic block to the function.
Gabor Greif051a9502008-04-06 20:25:17 +000071 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000072
Reid Spencere8cdc8b2005-07-12 21:51:33 +000073 // Get pointers to the constants.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000074 Value *One = ConstantInt::get(Type::Int32Ty, 1);
75 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
Jeff Cohen00b168892005-07-27 06:12:32 +000076
Reid Spencere8cdc8b2005-07-12 21:51:33 +000077 // Get pointer to the integer argument of the add1 function...
78 Argument *ArgX = FibF->arg_begin(); // Get the arg.
79 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000080
Reid Spencere8cdc8b2005-07-12 21:51:33 +000081 // Create the true_block.
Gabor Greif051a9502008-04-06 20:25:17 +000082 BasicBlock *RetBB = BasicBlock::Create("return", FibF);
Reid Spencere8cdc8b2005-07-12 21:51:33 +000083 // Create an exit block.
Gabor Greif051a9502008-04-06 20:25:17 +000084 BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000085
Reid Spencere8cdc8b2005-07-12 21:51:33 +000086 // Create the "if (arg < 2) goto exitbb"
Reid Spencere4d87aa2006-12-23 06:05:41 +000087 Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
Gabor Greif051a9502008-04-06 20:25:17 +000088 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000089
Reid Spencere8cdc8b2005-07-12 21:51:33 +000090 // Create: ret int 1
Gabor Greif051a9502008-04-06 20:25:17 +000091 ReturnInst::Create(One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000092
Reid Spencere8cdc8b2005-07-12 21:51:33 +000093 // create fib(x-1)
Gabor Greif7cbd8a32008-05-16 19:29:10 +000094 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +000095 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000096
Reid Spencere8cdc8b2005-07-12 21:51:33 +000097 // create fib(x-2)
Gabor Greif7cbd8a32008-05-16 19:29:10 +000098 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +000099 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000100
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000101 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +0000102 Value *Sum =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000103 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000104
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000105 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +0000106 ReturnInst::Create(Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000107
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000108 return FibF;
109}
110
111struct threadParams {
112 ExecutionEngine* EE;
113 Function* F;
114 int value;
115};
116
117// We block the subthreads just before they begin to execute:
118// we want all of them to call into the JIT at the same time,
119// to verify that the locking is working correctly.
120class WaitForThreads
121{
122public:
123 WaitForThreads()
124 {
125 n = 0;
126 waitFor = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000127
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000128 int result = pthread_cond_init( &condition, NULL );
129 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000130
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000131 result = pthread_mutex_init( &mutex, NULL );
132 assert( result == 0 );
133 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000134
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000135 ~WaitForThreads()
136 {
137 int result = pthread_cond_destroy( &condition );
138 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000139
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000140 result = pthread_mutex_destroy( &mutex );
141 assert( result == 0 );
142 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000143
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000144 // All threads will stop here until another thread calls releaseThreads
145 void block()
146 {
147 int result = pthread_mutex_lock( &mutex );
148 assert( result == 0 );
149 n ++;
150 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000151
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000152 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000153 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000154 {
155 // There are enough threads blocked that we can release all of them
156 std::cout << "Unblocking threads from block()" << std::endl;
157 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000158 }
159 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000160 {
161 // We just need to wait until someone unblocks us
162 result = pthread_cond_wait( &condition, &mutex );
163 assert( result == 0 );
164 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000165
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000166 // unlock the mutex before returning
167 result = pthread_mutex_unlock( &mutex );
168 assert( result == 0 );
169 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000170
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000171 // If there are num or more threads blocked, it will signal them all
172 // Otherwise, this thread blocks until there are enough OTHER threads
173 // blocked
174 void releaseThreads( size_t num )
175 {
176 int result = pthread_mutex_lock( &mutex );
177 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000178
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000179 if ( n >= num ) {
180 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
181 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000182 }
183 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000184 {
185 waitFor = num;
186 pthread_cond_wait( &condition, &mutex );
187 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000188
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000189 // unlock the mutex before returning
190 result = pthread_mutex_unlock( &mutex );
191 assert( result == 0 );
192 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000193
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000194private:
195 void unblockThreads()
196 {
197 // Reset the counters to zero: this way, if any new threads
198 // enter while threads are exiting, they will block instead
199 // of triggering a new release of threads
200 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000201
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000202 // Reset waitFor to zero: this way, if waitFor threads enter
203 // while threads are exiting, they will block instead of
204 // triggering a new release of threads
205 waitFor = 0;
206
207 int result = pthread_cond_broadcast( &condition );
Chris Lattner70987722008-04-08 05:49:09 +0000208 assert(result == 0); result=result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000209 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000210
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000211 size_t n;
212 size_t waitFor;
213 pthread_cond_t condition;
214 pthread_mutex_t mutex;
215};
216
217static WaitForThreads synchronize;
218
219void* callFunc( void* param )
220{
221 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000222
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000223 // Call the `foo' function with no arguments:
224 std::vector<GenericValue> Args(1);
Reid Spencer34bd70d2007-03-06 17:24:31 +0000225 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen00b168892005-07-27 06:12:32 +0000226
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000227 synchronize.block(); // wait until other threads are at this point
228 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000229
Reid Spencer34bd70d2007-03-06 17:24:31 +0000230 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000231}
232
Chris Lattnerda062882009-06-17 16:48:44 +0000233int main() {
234 InitializeNativeTarget();
235
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000236 // Create some module to put our function into it.
237 Module *M = new Module("test");
Jeff Cohen00b168892005-07-27 06:12:32 +0000238
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000239 Function* add1F = createAdd1( M );
240 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000241
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000242 // Now we create the JIT.
243 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
244 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Jeff Cohen00b168892005-07-27 06:12:32 +0000245
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000246 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
247 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000248
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000249 // Create one thread for add1 and two threads for fib
250 struct threadParams add1 = { EE, add1F, 1000 };
251 struct threadParams fib1 = { EE, fibF, 39 };
252 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000253
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000254 pthread_t add1Thread;
255 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
256 if ( result != 0 ) {
257 std::cerr << "Could not create thread" << std::endl;
258 return 1;
259 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000260
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000261 pthread_t fibThread1;
262 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
263 if ( result != 0 ) {
264 std::cerr << "Could not create thread" << std::endl;
265 return 1;
266 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000267
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000268 pthread_t fibThread2;
269 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
270 if ( result != 0 ) {
271 std::cerr << "Could not create thread" << std::endl;
272 return 1;
273 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000274
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000275 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000276
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000277 void* returnValue;
278 result = pthread_join( add1Thread, &returnValue );
279 if ( result != 0 ) {
280 std::cerr << "Could not join thread" << std::endl;
281 return 1;
282 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000283 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000284
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000285 result = pthread_join( fibThread1, &returnValue );
286 if ( result != 0 ) {
287 std::cerr << "Could not join thread" << std::endl;
288 return 1;
289 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000290 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000291
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000292 result = pthread_join( fibThread2, &returnValue );
293 if ( result != 0 ) {
294 std::cerr << "Could not join thread" << std::endl;
295 return 1;
296 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000297 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000298
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000299 return 0;
300}