blob: 300c4328639212c44067a97140dcc77b6b85d148 [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"
29#include <iostream>
30using namespace llvm;
31
Chris Lattner6a987542007-01-07 07:40:09 +000032static Function* createAdd1(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000033 // 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.
Chris Lattner6a987542007-01-07 07:40:09 +000036 Function *Add1F =
37 cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
38 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000039
Reid Spencere8cdc8b2005-07-12 21:51:33 +000040 // Add a basic block to the function. As before, it automatically inserts
41 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000042 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
Jeff Cohen00b168892005-07-27 06:12:32 +000043
Reid Spencere8cdc8b2005-07-12 21:51:33 +000044 // Get pointers to the constant `1'.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000045 Value *One = ConstantInt::get(Type::Int32Ty, 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000046
Reid Spencere8cdc8b2005-07-12 21:51:33 +000047 // Get pointers to the integer argument of the add1 function...
48 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
49 Argument *ArgX = Add1F->arg_begin(); // Get the arg
50 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000051
Reid Spencere8cdc8b2005-07-12 21:51:33 +000052 // Create the add instruction, inserting it into the end of BB.
53 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000054
Reid Spencere8cdc8b2005-07-12 21:51:33 +000055 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000056 ReturnInst::Create(Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000057
58 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000059 return Add1F;
60}
61
Chris Lattner6a987542007-01-07 07:40:09 +000062static Function *CreateFibFunction(Module *M) {
Reid Spencere8cdc8b2005-07-12 21:51:33 +000063 // Create the fib function and insert it into module M. This function is said
64 // to return an int and take an int parameter.
Chris Lattner6a987542007-01-07 07:40:09 +000065 Function *FibF =
66 cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
67 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000068
Reid Spencere8cdc8b2005-07-12 21:51:33 +000069 // Add a basic block to the function.
Gabor Greif051a9502008-04-06 20:25:17 +000070 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000071
Reid Spencere8cdc8b2005-07-12 21:51:33 +000072 // Get pointers to the constants.
Reid Spencerdb8d2be2006-12-31 05:50:28 +000073 Value *One = ConstantInt::get(Type::Int32Ty, 1);
74 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
Jeff Cohen00b168892005-07-27 06:12:32 +000075
Reid Spencere8cdc8b2005-07-12 21:51:33 +000076 // Get pointer to the integer argument of the add1 function...
77 Argument *ArgX = FibF->arg_begin(); // Get the arg.
78 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000079
Reid Spencere8cdc8b2005-07-12 21:51:33 +000080 // Create the true_block.
Gabor Greif051a9502008-04-06 20:25:17 +000081 BasicBlock *RetBB = BasicBlock::Create("return", FibF);
Reid Spencere8cdc8b2005-07-12 21:51:33 +000082 // Create an exit block.
Gabor Greif051a9502008-04-06 20:25:17 +000083 BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000084
Reid Spencere8cdc8b2005-07-12 21:51:33 +000085 // Create the "if (arg < 2) goto exitbb"
Reid Spencere4d87aa2006-12-23 06:05:41 +000086 Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
Gabor Greif051a9502008-04-06 20:25:17 +000087 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000088
Reid Spencere8cdc8b2005-07-12 21:51:33 +000089 // Create: ret int 1
Gabor Greif051a9502008-04-06 20:25:17 +000090 ReturnInst::Create(One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000091
Reid Spencere8cdc8b2005-07-12 21:51:33 +000092 // create fib(x-1)
93 Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +000094 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000095
Reid Spencere8cdc8b2005-07-12 21:51:33 +000096 // create fib(x-2)
97 Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +000098 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000099
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000100 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +0000101 Value *Sum =
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000102 BinaryOperator::createAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000103
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000104 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +0000105 ReturnInst::Create(Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000106
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000107 return FibF;
108}
109
110struct threadParams {
111 ExecutionEngine* EE;
112 Function* F;
113 int value;
114};
115
116// We block the subthreads just before they begin to execute:
117// we want all of them to call into the JIT at the same time,
118// to verify that the locking is working correctly.
119class WaitForThreads
120{
121public:
122 WaitForThreads()
123 {
124 n = 0;
125 waitFor = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000126
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000127 int result = pthread_cond_init( &condition, NULL );
128 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000129
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000130 result = pthread_mutex_init( &mutex, NULL );
131 assert( result == 0 );
132 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000133
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000134 ~WaitForThreads()
135 {
136 int result = pthread_cond_destroy( &condition );
137 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000138
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000139 result = pthread_mutex_destroy( &mutex );
140 assert( result == 0 );
141 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000142
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000143 // All threads will stop here until another thread calls releaseThreads
144 void block()
145 {
146 int result = pthread_mutex_lock( &mutex );
147 assert( result == 0 );
148 n ++;
149 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000150
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000151 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000152 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000153 {
154 // There are enough threads blocked that we can release all of them
155 std::cout << "Unblocking threads from block()" << std::endl;
156 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000157 }
158 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000159 {
160 // We just need to wait until someone unblocks us
161 result = pthread_cond_wait( &condition, &mutex );
162 assert( result == 0 );
163 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000164
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000165 // unlock the mutex before returning
166 result = pthread_mutex_unlock( &mutex );
167 assert( result == 0 );
168 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000170 // If there are num or more threads blocked, it will signal them all
171 // Otherwise, this thread blocks until there are enough OTHER threads
172 // blocked
173 void releaseThreads( size_t num )
174 {
175 int result = pthread_mutex_lock( &mutex );
176 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000177
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000178 if ( n >= num ) {
179 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
180 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000181 }
182 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000183 {
184 waitFor = num;
185 pthread_cond_wait( &condition, &mutex );
186 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000187
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000188 // unlock the mutex before returning
189 result = pthread_mutex_unlock( &mutex );
190 assert( result == 0 );
191 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000192
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000193private:
194 void unblockThreads()
195 {
196 // Reset the counters to zero: this way, if any new threads
197 // enter while threads are exiting, they will block instead
198 // of triggering a new release of threads
199 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000200
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000201 // Reset waitFor to zero: this way, if waitFor threads enter
202 // while threads are exiting, they will block instead of
203 // triggering a new release of threads
204 waitFor = 0;
205
206 int result = pthread_cond_broadcast( &condition );
207 assert( result == 0 );
208 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000209
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000210 size_t n;
211 size_t waitFor;
212 pthread_cond_t condition;
213 pthread_mutex_t mutex;
214};
215
216static WaitForThreads synchronize;
217
218void* callFunc( void* param )
219{
220 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000221
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000222 // Call the `foo' function with no arguments:
223 std::vector<GenericValue> Args(1);
Reid Spencer34bd70d2007-03-06 17:24:31 +0000224 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen00b168892005-07-27 06:12:32 +0000225
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000226 synchronize.block(); // wait until other threads are at this point
227 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Reid Spencer34bd70d2007-03-06 17:24:31 +0000229 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000230}
231
Jeff Cohen00b168892005-07-27 06:12:32 +0000232int main()
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000233{
234 // Create some module to put our function into it.
235 Module *M = new Module("test");
Jeff Cohen00b168892005-07-27 06:12:32 +0000236
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000237 Function* add1F = createAdd1( M );
238 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000239
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000240 // Now we create the JIT.
241 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
242 ExecutionEngine* EE = ExecutionEngine::create(MP, false);
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000244 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
245 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000247 // Create one thread for add1 and two threads for fib
248 struct threadParams add1 = { EE, add1F, 1000 };
249 struct threadParams fib1 = { EE, fibF, 39 };
250 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000251
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000252 pthread_t add1Thread;
253 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
254 if ( result != 0 ) {
255 std::cerr << "Could not create thread" << std::endl;
256 return 1;
257 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000258
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000259 pthread_t fibThread1;
260 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
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 fibThread2;
267 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
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 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000274
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000275 void* returnValue;
276 result = pthread_join( add1Thread, &returnValue );
277 if ( result != 0 ) {
278 std::cerr << "Could not join thread" << std::endl;
279 return 1;
280 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000281 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000282
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000283 result = pthread_join( fibThread1, &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 << "Fib1 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( fibThread2, &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 << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000296
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000297 return 0;
298}