blob: be40a282150d135aa1b605f0d03a2efffc1b2cec [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 =
Owen Anderson1d0be152009-08-13 21:58:54 +000039 cast<Function>(M->getOrInsertFunction("add1",
40 Type::getInt32Ty(M->getContext()),
41 Type::getInt32Ty(M->getContext()),
Chris Lattner6a987542007-01-07 07:40:09 +000042 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000043
Reid Spencere8cdc8b2005-07-12 21:51:33 +000044 // Add a basic block to the function. As before, it automatically inserts
45 // because of the last argument.
Owen Anderson1d0be152009-08-13 21:58:54 +000046 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen00b168892005-07-27 06:12:32 +000047
Reid Spencere8cdc8b2005-07-12 21:51:33 +000048 // Get pointers to the constant `1'.
Owen Anderson1d0be152009-08-13 21:58:54 +000049 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000050
Reid Spencere8cdc8b2005-07-12 21:51:33 +000051 // Get pointers to the integer argument of the add1 function...
52 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
53 Argument *ArgX = Add1F->arg_begin(); // Get the arg
54 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000055
Reid Spencere8cdc8b2005-07-12 21:51:33 +000056 // Create the add instruction, inserting it into the end of BB.
Gabor Greif7cbd8a32008-05-16 19:29:10 +000057 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000058
Reid Spencere8cdc8b2005-07-12 21:51:33 +000059 // Create the return instruction and add it to the basic block
Owen Anderson1d0be152009-08-13 21:58:54 +000060 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000061
62 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000063 return Add1F;
64}
65
Chris Lattner6a987542007-01-07 07:40:09 +000066static Function *CreateFibFunction(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000067 // Create the fib function and insert it into module M. This function is said
68 // to return an int and take an int parameter.
Chris Lattner6a987542007-01-07 07:40:09 +000069 Function *FibF =
Owen Anderson1d0be152009-08-13 21:58:54 +000070 cast<Function>(M->getOrInsertFunction("fib",
71 Type::getInt32Ty(M->getContext()),
72 Type::getInt32Ty(M->getContext()),
Chris Lattner6a987542007-01-07 07:40:09 +000073 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000074
Reid Spencere8cdc8b2005-07-12 21:51:33 +000075 // Add a basic block to the function.
Owen Anderson1d0be152009-08-13 21:58:54 +000076 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000077
Reid Spencere8cdc8b2005-07-12 21:51:33 +000078 // Get pointers to the constants.
Owen Anderson1d0be152009-08-13 21:58:54 +000079 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
80 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
Jeff Cohen00b168892005-07-27 06:12:32 +000081
Reid Spencere8cdc8b2005-07-12 21:51:33 +000082 // Get pointer to the integer argument of the add1 function...
83 Argument *ArgX = FibF->arg_begin(); // Get the arg.
84 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000085
Reid Spencere8cdc8b2005-07-12 21:51:33 +000086 // Create the true_block.
Owen Anderson1d0be152009-08-13 21:58:54 +000087 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencere8cdc8b2005-07-12 21:51:33 +000088 // Create an exit block.
Owen Anderson1d0be152009-08-13 21:58:54 +000089 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000090
Reid Spencere8cdc8b2005-07-12 21:51:33 +000091 // Create the "if (arg < 2) goto exitbb"
Owen Anderson333c4002009-07-09 23:48:35 +000092 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greif051a9502008-04-06 20:25:17 +000093 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000094
Reid Spencere8cdc8b2005-07-12 21:51:33 +000095 // Create: ret int 1
Owen Anderson1d0be152009-08-13 21:58:54 +000096 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000097
Reid Spencere8cdc8b2005-07-12 21:51:33 +000098 // create fib(x-1)
Gabor Greif7cbd8a32008-05-16 19:29:10 +000099 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000100 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000101
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000102 // create fib(x-2)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000103 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000104 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000105
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000106 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +0000107 Value *Sum =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000108 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000109
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000110 // Create the return instruction and add it to the basic block
Owen Anderson1d0be152009-08-13 21:58:54 +0000111 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000112
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000113 return FibF;
114}
115
116struct threadParams {
117 ExecutionEngine* EE;
118 Function* F;
119 int value;
120};
121
122// We block the subthreads just before they begin to execute:
123// we want all of them to call into the JIT at the same time,
124// to verify that the locking is working correctly.
125class WaitForThreads
126{
127public:
128 WaitForThreads()
129 {
130 n = 0;
131 waitFor = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000132
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000133 int result = pthread_cond_init( &condition, NULL );
134 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000135
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000136 result = pthread_mutex_init( &mutex, NULL );
137 assert( result == 0 );
138 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000139
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000140 ~WaitForThreads()
141 {
142 int result = pthread_cond_destroy( &condition );
143 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000144
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000145 result = pthread_mutex_destroy( &mutex );
146 assert( result == 0 );
147 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000148
Reid Spencere8cdc8b2005-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 );
153 assert( result == 0 );
154 n ++;
155 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000156
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000157 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000158 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000159 {
160 // There are enough threads blocked that we can release all of them
161 std::cout << "Unblocking threads from block()" << std::endl;
162 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000163 }
164 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000165 {
166 // We just need to wait until someone unblocks us
167 result = pthread_cond_wait( &condition, &mutex );
168 assert( result == 0 );
169 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000170
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000171 // unlock the mutex before returning
172 result = pthread_mutex_unlock( &mutex );
173 assert( result == 0 );
174 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000175
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000176 // If there are num or more threads blocked, it will signal them all
177 // Otherwise, this thread blocks until there are enough OTHER threads
178 // blocked
179 void releaseThreads( size_t num )
180 {
181 int result = pthread_mutex_lock( &mutex );
182 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000183
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000184 if ( n >= num ) {
185 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
186 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000187 }
188 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000189 {
190 waitFor = num;
191 pthread_cond_wait( &condition, &mutex );
192 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000193
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000194 // unlock the mutex before returning
195 result = pthread_mutex_unlock( &mutex );
196 assert( result == 0 );
197 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000198
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000199private:
200 void unblockThreads()
201 {
202 // Reset the counters to zero: this way, if any new threads
203 // enter while threads are exiting, they will block instead
204 // of triggering a new release of threads
205 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000206
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000207 // Reset waitFor to zero: this way, if waitFor threads enter
208 // while threads are exiting, they will block instead of
209 // triggering a new release of threads
210 waitFor = 0;
211
212 int result = pthread_cond_broadcast( &condition );
Chris Lattner70987722008-04-08 05:49:09 +0000213 assert(result == 0); result=result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000214 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000215
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000216 size_t n;
217 size_t waitFor;
218 pthread_cond_t condition;
219 pthread_mutex_t mutex;
220};
221
222static WaitForThreads synchronize;
223
224void* callFunc( void* param )
225{
226 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000227
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000228 // Call the `foo' function with no arguments:
229 std::vector<GenericValue> Args(1);
Reid Spencer34bd70d2007-03-06 17:24:31 +0000230 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen00b168892005-07-27 06:12:32 +0000231
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000232 synchronize.block(); // wait until other threads are at this point
233 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000234
Reid Spencer34bd70d2007-03-06 17:24:31 +0000235 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000236}
237
Chris Lattnerda062882009-06-17 16:48:44 +0000238int main() {
239 InitializeNativeTarget();
Owen Anderson8b477ed2009-07-01 16:58:40 +0000240 LLVMContext Context;
Chris Lattnerda062882009-06-17 16:48:44 +0000241
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000242 // Create some module to put our function into it.
Owen Anderson31895e72009-07-01 21:22:36 +0000243 Module *M = new Module("test", Context);
Jeff Cohen00b168892005-07-27 06:12:32 +0000244
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000245 Function* add1F = createAdd1( M );
246 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000248 // Now we create the JIT.
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000249 ExecutionEngine* EE = EngineBuilder(M).create();
Jeff Cohen00b168892005-07-27 06:12:32 +0000250
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000251 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
252 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000253
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000254 // Create one thread for add1 and two threads for fib
255 struct threadParams add1 = { EE, add1F, 1000 };
256 struct threadParams fib1 = { EE, fibF, 39 };
257 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000258
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000259 pthread_t add1Thread;
260 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
261 if ( result != 0 ) {
262 std::cerr << "Could not create thread" << std::endl;
263 return 1;
264 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000265
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000266 pthread_t fibThread1;
267 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
268 if ( result != 0 ) {
269 std::cerr << "Could not create thread" << std::endl;
270 return 1;
271 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000272
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000273 pthread_t fibThread2;
274 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
275 if ( result != 0 ) {
276 std::cerr << "Could not create thread" << std::endl;
277 return 1;
278 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000279
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000280 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000281
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000282 void* returnValue;
283 result = pthread_join( add1Thread, &returnValue );
284 if ( result != 0 ) {
285 std::cerr << "Could not join thread" << std::endl;
286 return 1;
287 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000288 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000289
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000290 result = pthread_join( fibThread1, &returnValue );
291 if ( result != 0 ) {
292 std::cerr << "Could not join thread" << std::endl;
293 return 1;
294 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000295 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000296
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000297 result = pthread_join( fibThread2, &returnValue );
298 if ( result != 0 ) {
299 std::cerr << "Could not join thread" << std::endl;
300 return 1;
301 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000302 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000303
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000304 return 0;
305}