blob: 2aa63d91ffb364692c4c50ef51af2660584efab6 [file] [log] [blame]
Reid Spencer68550972005-07-12 21:51:33 +00001//===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerbcf65db2007-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 Spencer68550972005-07-12 21:51:33 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Parallel JIT
11//
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000012// This test program creates two LLVM functions then calls them from three
Reid Spencer68550972005-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
Reid Spencer68550972005-07-12 21:51:33 +000020#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000021#include "llvm/ExecutionEngine/Interpreter.h"
22#include "llvm/ExecutionEngine/JIT.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000028#include "llvm/Support/TargetSelect.h"
Reid Spencer68550972005-07-12 21:51:33 +000029#include <iostream>
Chandler Carruth605e30e2012-12-04 10:16:57 +000030#include <pthread.h>
Reid Spencer68550972005-07-12 21:51:33 +000031using namespace llvm;
32
Chris Lattnerb800b392007-01-07 07:40:09 +000033static Function* createAdd1(Module *M) {
Reid Spencer68550972005-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 Lattnerb800b392007-01-07 07:40:09 +000037 Function *Add1F =
Owen Anderson55f1c092009-08-13 21:58:54 +000038 cast<Function>(M->getOrInsertFunction("add1",
39 Type::getInt32Ty(M->getContext()),
40 Type::getInt32Ty(M->getContext()),
Chris Lattnerb800b392007-01-07 07:40:09 +000041 (Type *)0));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000042
Reid Spencer68550972005-07-12 21:51:33 +000043 // Add a basic block to the function. As before, it automatically inserts
44 // because of the last argument.
Owen Anderson55f1c092009-08-13 21:58:54 +000045 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000046
Reid Spencer68550972005-07-12 21:51:33 +000047 // Get pointers to the constant `1'.
Owen Anderson55f1c092009-08-13 21:58:54 +000048 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000049
Reid Spencer68550972005-07-12 21:51:33 +000050 // Get pointers to the integer argument of the add1 function...
51 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
52 Argument *ArgX = Add1F->arg_begin(); // Get the arg
53 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000054
Reid Spencer68550972005-07-12 21:51:33 +000055 // Create the add instruction, inserting it into the end of BB.
Gabor Greife1f6e4b2008-05-16 19:29:10 +000056 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000057
Reid Spencer68550972005-07-12 21:51:33 +000058 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +000059 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000060
61 // Now, function add1 is ready.
Reid Spencer68550972005-07-12 21:51:33 +000062 return Add1F;
63}
64
Chris Lattnerb800b392007-01-07 07:40:09 +000065static Function *CreateFibFunction(Module *M) {
Reid Spencer68550972005-07-12 21:51:33 +000066 // Create the fib function and insert it into module M. This function is said
67 // to return an int and take an int parameter.
Chris Lattnerb800b392007-01-07 07:40:09 +000068 Function *FibF =
Owen Anderson55f1c092009-08-13 21:58:54 +000069 cast<Function>(M->getOrInsertFunction("fib",
70 Type::getInt32Ty(M->getContext()),
71 Type::getInt32Ty(M->getContext()),
Chris Lattnerb800b392007-01-07 07:40:09 +000072 (Type *)0));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000073
Reid Spencer68550972005-07-12 21:51:33 +000074 // Add a basic block to the function.
Owen Anderson55f1c092009-08-13 21:58:54 +000075 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000076
Reid Spencer68550972005-07-12 21:51:33 +000077 // Get pointers to the constants.
Owen Anderson55f1c092009-08-13 21:58:54 +000078 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
79 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000080
Reid Spencer68550972005-07-12 21:51:33 +000081 // Get pointer to the integer argument of the add1 function...
82 Argument *ArgX = FibF->arg_begin(); // Get the arg.
83 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000084
Reid Spencer68550972005-07-12 21:51:33 +000085 // Create the true_block.
Owen Anderson55f1c092009-08-13 21:58:54 +000086 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencer68550972005-07-12 21:51:33 +000087 // Create an exit block.
Owen Anderson55f1c092009-08-13 21:58:54 +000088 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000089
Reid Spencer68550972005-07-12 21:51:33 +000090 // Create the "if (arg < 2) goto exitbb"
Owen Anderson1e5f00e2009-07-09 23:48:35 +000091 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greife9ecc682008-04-06 20:25:17 +000092 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000093
Reid Spencer68550972005-07-12 21:51:33 +000094 // Create: ret int 1
Owen Anderson55f1c092009-08-13 21:58:54 +000095 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000096
Reid Spencer68550972005-07-12 21:51:33 +000097 // create fib(x-1)
Gabor Greife1f6e4b2008-05-16 19:29:10 +000098 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +000099 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000100
Reid Spencer68550972005-07-12 21:51:33 +0000101 // create fib(x-2)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000102 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000103 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000104
Reid Spencer68550972005-07-12 21:51:33 +0000105 // fib(x-1)+fib(x-2)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000106 Value *Sum =
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000107 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000108
Reid Spencer68550972005-07-12 21:51:33 +0000109 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +0000110 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000111
Reid Spencer68550972005-07-12 21:51:33 +0000112 return FibF;
113}
114
115struct threadParams {
116 ExecutionEngine* EE;
117 Function* F;
118 int value;
119};
120
121// We block the subthreads just before they begin to execute:
122// we want all of them to call into the JIT at the same time,
123// to verify that the locking is working correctly.
124class WaitForThreads
125{
126public:
127 WaitForThreads()
128 {
129 n = 0;
130 waitFor = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000131
Reid Spencer68550972005-07-12 21:51:33 +0000132 int result = pthread_cond_init( &condition, NULL );
133 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000134
Reid Spencer68550972005-07-12 21:51:33 +0000135 result = pthread_mutex_init( &mutex, NULL );
136 assert( result == 0 );
137 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000138
Reid Spencer68550972005-07-12 21:51:33 +0000139 ~WaitForThreads()
140 {
141 int result = pthread_cond_destroy( &condition );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000142 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000143 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000144
Reid Spencer68550972005-07-12 21:51:33 +0000145 result = pthread_mutex_destroy( &mutex );
146 assert( result == 0 );
147 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000148
Reid Spencer68550972005-07-12 21:51:33 +0000149 // All threads will stop here until another thread calls releaseThreads
150 void block()
151 {
152 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000153 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000154 assert( result == 0 );
155 n ++;
156 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000157
Reid Spencer68550972005-07-12 21:51:33 +0000158 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000159 if ( waitFor > 0 && n == waitFor )
Reid Spencer68550972005-07-12 21:51:33 +0000160 {
161 // There are enough threads blocked that we can release all of them
162 std::cout << "Unblocking threads from block()" << std::endl;
163 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000164 }
165 else
Reid Spencer68550972005-07-12 21:51:33 +0000166 {
167 // We just need to wait until someone unblocks us
168 result = pthread_cond_wait( &condition, &mutex );
169 assert( result == 0 );
170 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000171
Reid Spencer68550972005-07-12 21:51:33 +0000172 // unlock the mutex before returning
173 result = pthread_mutex_unlock( &mutex );
174 assert( result == 0 );
175 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000176
Reid Spencer68550972005-07-12 21:51:33 +0000177 // If there are num or more threads blocked, it will signal them all
178 // Otherwise, this thread blocks until there are enough OTHER threads
179 // blocked
180 void releaseThreads( size_t num )
181 {
182 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000183 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000184 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000185
Reid Spencer68550972005-07-12 21:51:33 +0000186 if ( n >= num ) {
187 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
188 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000189 }
190 else
Reid Spencer68550972005-07-12 21:51:33 +0000191 {
192 waitFor = num;
193 pthread_cond_wait( &condition, &mutex );
194 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000195
Reid Spencer68550972005-07-12 21:51:33 +0000196 // unlock the mutex before returning
197 result = pthread_mutex_unlock( &mutex );
198 assert( result == 0 );
199 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000200
Reid Spencer68550972005-07-12 21:51:33 +0000201private:
202 void unblockThreads()
203 {
204 // Reset the counters to zero: this way, if any new threads
205 // enter while threads are exiting, they will block instead
206 // of triggering a new release of threads
207 n = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000208
Reid Spencer68550972005-07-12 21:51:33 +0000209 // Reset waitFor to zero: this way, if waitFor threads enter
210 // while threads are exiting, they will block instead of
211 // triggering a new release of threads
212 waitFor = 0;
213
214 int result = pthread_cond_broadcast( &condition );
Chandler Carruth3bb7d412012-02-20 00:02:49 +0000215 (void)result;
216 assert(result == 0);
Reid Spencer68550972005-07-12 21:51:33 +0000217 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000218
Reid Spencer68550972005-07-12 21:51:33 +0000219 size_t n;
220 size_t waitFor;
221 pthread_cond_t condition;
222 pthread_mutex_t mutex;
223};
224
225static WaitForThreads synchronize;
226
227void* callFunc( void* param )
228{
229 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000230
Reid Spencer68550972005-07-12 21:51:33 +0000231 // Call the `foo' function with no arguments:
232 std::vector<GenericValue> Args(1);
Reid Spencer8a9ae482007-03-06 17:24:31 +0000233 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000234
Reid Spencer68550972005-07-12 21:51:33 +0000235 synchronize.block(); // wait until other threads are at this point
236 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000237
Reid Spencer8a9ae482007-03-06 17:24:31 +0000238 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencer68550972005-07-12 21:51:33 +0000239}
240
Chris Lattnerd24df242009-06-17 16:48:44 +0000241int main() {
242 InitializeNativeTarget();
Owen Anderson6773d382009-07-01 16:58:40 +0000243 LLVMContext Context;
Chris Lattnerd24df242009-06-17 16:48:44 +0000244
Reid Spencer68550972005-07-12 21:51:33 +0000245 // Create some module to put our function into it.
Owen Anderson1cf085d2009-07-01 21:22:36 +0000246 Module *M = new Module("test", Context);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000247
Reid Spencer68550972005-07-12 21:51:33 +0000248 Function* add1F = createAdd1( M );
249 Function* fibF = CreateFibFunction( M );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000250
Reid Spencer68550972005-07-12 21:51:33 +0000251 // Now we create the JIT.
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000252 ExecutionEngine* EE = EngineBuilder(M).create();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000253
Reid Spencer68550972005-07-12 21:51:33 +0000254 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
255 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000256
Reid Spencer68550972005-07-12 21:51:33 +0000257 // Create one thread for add1 and two threads for fib
258 struct threadParams add1 = { EE, add1F, 1000 };
259 struct threadParams fib1 = { EE, fibF, 39 };
260 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000261
Reid Spencer68550972005-07-12 21:51:33 +0000262 pthread_t add1Thread;
263 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
264 if ( result != 0 ) {
265 std::cerr << "Could not create thread" << std::endl;
266 return 1;
267 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000268
Reid Spencer68550972005-07-12 21:51:33 +0000269 pthread_t fibThread1;
270 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
271 if ( result != 0 ) {
272 std::cerr << "Could not create thread" << std::endl;
273 return 1;
274 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000275
Reid Spencer68550972005-07-12 21:51:33 +0000276 pthread_t fibThread2;
277 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
278 if ( result != 0 ) {
279 std::cerr << "Could not create thread" << std::endl;
280 return 1;
281 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000282
Reid Spencer68550972005-07-12 21:51:33 +0000283 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000284
Reid Spencer68550972005-07-12 21:51:33 +0000285 void* returnValue;
286 result = pthread_join( add1Thread, &returnValue );
287 if ( result != 0 ) {
288 std::cerr << "Could not join thread" << std::endl;
289 return 1;
290 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000291 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Reid Spencer68550972005-07-12 21:51:33 +0000293 result = pthread_join( fibThread1, &returnValue );
294 if ( result != 0 ) {
295 std::cerr << "Could not join thread" << std::endl;
296 return 1;
297 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000298 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000299
Reid Spencer68550972005-07-12 21:51:33 +0000300 result = pthread_join( fibThread2, &returnValue );
301 if ( result != 0 ) {
302 std::cerr << "Could not join thread" << std::endl;
303 return 1;
304 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000305 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Reid Spencer68550972005-07-12 21:51:33 +0000307 return 0;
308}