blob: 3e483275edbb1e986e20df05e9fbd529f36772f5 [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"
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"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000029#include "llvm/Support/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 =
Owen Anderson1d0be152009-08-13 21:58:54 +000038 cast<Function>(M->getOrInsertFunction("add1",
39 Type::getInt32Ty(M->getContext()),
40 Type::getInt32Ty(M->getContext()),
Chris Lattner6a987542007-01-07 07:40:09 +000041 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000042
Reid Spencere8cdc8b2005-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 Anderson1d0be152009-08-13 21:58:54 +000045 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen00b168892005-07-27 06:12:32 +000046
Reid Spencere8cdc8b2005-07-12 21:51:33 +000047 // Get pointers to the constant `1'.
Owen Anderson1d0be152009-08-13 21:58:54 +000048 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000049
Reid Spencere8cdc8b2005-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 Cohen00b168892005-07-27 06:12:32 +000054
Reid Spencere8cdc8b2005-07-12 21:51:33 +000055 // Create the add instruction, inserting it into the end of BB.
Gabor Greif7cbd8a32008-05-16 19:29:10 +000056 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000057
Reid Spencere8cdc8b2005-07-12 21:51:33 +000058 // Create the return instruction and add it to the basic block
Owen Anderson1d0be152009-08-13 21:58:54 +000059 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000060
61 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000062 return Add1F;
63}
64
Chris Lattner6a987542007-01-07 07:40:09 +000065static Function *CreateFibFunction(Module *M) {
Reid Spencere8cdc8b2005-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 Lattner6a987542007-01-07 07:40:09 +000068 Function *FibF =
Owen Anderson1d0be152009-08-13 21:58:54 +000069 cast<Function>(M->getOrInsertFunction("fib",
70 Type::getInt32Ty(M->getContext()),
71 Type::getInt32Ty(M->getContext()),
Chris Lattner6a987542007-01-07 07:40:09 +000072 (Type *)0));
Jeff Cohen00b168892005-07-27 06:12:32 +000073
Reid Spencere8cdc8b2005-07-12 21:51:33 +000074 // Add a basic block to the function.
Owen Anderson1d0be152009-08-13 21:58:54 +000075 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000076
Reid Spencere8cdc8b2005-07-12 21:51:33 +000077 // Get pointers to the constants.
Owen Anderson1d0be152009-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 Cohen00b168892005-07-27 06:12:32 +000080
Reid Spencere8cdc8b2005-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 Cohen00b168892005-07-27 06:12:32 +000084
Reid Spencere8cdc8b2005-07-12 21:51:33 +000085 // Create the true_block.
Owen Anderson1d0be152009-08-13 21:58:54 +000086 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencere8cdc8b2005-07-12 21:51:33 +000087 // Create an exit block.
Owen Anderson1d0be152009-08-13 21:58:54 +000088 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000089
Reid Spencere8cdc8b2005-07-12 21:51:33 +000090 // Create the "if (arg < 2) goto exitbb"
Owen Anderson333c4002009-07-09 23:48:35 +000091 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greif051a9502008-04-06 20:25:17 +000092 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000093
Reid Spencere8cdc8b2005-07-12 21:51:33 +000094 // Create: ret int 1
Owen Anderson1d0be152009-08-13 21:58:54 +000095 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +000096
Reid Spencere8cdc8b2005-07-12 21:51:33 +000097 // create fib(x-1)
Gabor Greif7cbd8a32008-05-16 19:29:10 +000098 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +000099 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000100
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000101 // create fib(x-2)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000102 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000103 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000104
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000105 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +0000106 Value *Sum =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000107 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000108
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000109 // Create the return instruction and add it to the basic block
Owen Anderson1d0be152009-08-13 21:58:54 +0000110 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000111
Reid Spencere8cdc8b2005-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 Cohen00b168892005-07-27 06:12:32 +0000131
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000132 int result = pthread_cond_init( &condition, NULL );
133 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000134
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000135 result = pthread_mutex_init( &mutex, NULL );
136 assert( result == 0 );
137 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000138
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000139 ~WaitForThreads()
140 {
141 int result = pthread_cond_destroy( &condition );
142 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000143
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000144 result = pthread_mutex_destroy( &mutex );
145 assert( result == 0 );
146 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000147
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000148 // All threads will stop here until another thread calls releaseThreads
149 void block()
150 {
151 int result = pthread_mutex_lock( &mutex );
152 assert( result == 0 );
153 n ++;
154 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000155
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000156 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000157 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000158 {
159 // There are enough threads blocked that we can release all of them
160 std::cout << "Unblocking threads from block()" << std::endl;
161 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000162 }
163 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000164 {
165 // We just need to wait until someone unblocks us
166 result = pthread_cond_wait( &condition, &mutex );
167 assert( result == 0 );
168 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000170 // unlock the mutex before returning
171 result = pthread_mutex_unlock( &mutex );
172 assert( result == 0 );
173 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000174
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000175 // If there are num or more threads blocked, it will signal them all
176 // Otherwise, this thread blocks until there are enough OTHER threads
177 // blocked
178 void releaseThreads( size_t num )
179 {
180 int result = pthread_mutex_lock( &mutex );
181 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000182
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000183 if ( n >= num ) {
184 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
185 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000186 }
187 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000188 {
189 waitFor = num;
190 pthread_cond_wait( &condition, &mutex );
191 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000192
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000193 // unlock the mutex before returning
194 result = pthread_mutex_unlock( &mutex );
195 assert( result == 0 );
196 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000197
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000198private:
199 void unblockThreads()
200 {
201 // Reset the counters to zero: this way, if any new threads
202 // enter while threads are exiting, they will block instead
203 // of triggering a new release of threads
204 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000205
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000206 // Reset waitFor to zero: this way, if waitFor threads enter
207 // while threads are exiting, they will block instead of
208 // triggering a new release of threads
209 waitFor = 0;
210
211 int result = pthread_cond_broadcast( &condition );
Chris Lattner70987722008-04-08 05:49:09 +0000212 assert(result == 0); result=result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000213 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000214
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000215 size_t n;
216 size_t waitFor;
217 pthread_cond_t condition;
218 pthread_mutex_t mutex;
219};
220
221static WaitForThreads synchronize;
222
223void* callFunc( void* param )
224{
225 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000226
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000227 // Call the `foo' function with no arguments:
228 std::vector<GenericValue> Args(1);
Reid Spencer34bd70d2007-03-06 17:24:31 +0000229 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen00b168892005-07-27 06:12:32 +0000230
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000231 synchronize.block(); // wait until other threads are at this point
232 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000233
Reid Spencer34bd70d2007-03-06 17:24:31 +0000234 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000235}
236
Chris Lattnerda062882009-06-17 16:48:44 +0000237int main() {
238 InitializeNativeTarget();
Owen Anderson8b477ed2009-07-01 16:58:40 +0000239 LLVMContext Context;
Chris Lattnerda062882009-06-17 16:48:44 +0000240
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000241 // Create some module to put our function into it.
Owen Anderson31895e72009-07-01 21:22:36 +0000242 Module *M = new Module("test", Context);
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000244 Function* add1F = createAdd1( M );
245 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000247 // Now we create the JIT.
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000248 ExecutionEngine* EE = EngineBuilder(M).create();
Jeff Cohen00b168892005-07-27 06:12:32 +0000249
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000250 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
251 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000252
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000253 // Create one thread for add1 and two threads for fib
254 struct threadParams add1 = { EE, add1F, 1000 };
255 struct threadParams fib1 = { EE, fibF, 39 };
256 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000257
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000258 pthread_t add1Thread;
259 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
260 if ( result != 0 ) {
261 std::cerr << "Could not create thread" << std::endl;
262 return 1;
263 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000264
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000265 pthread_t fibThread1;
266 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
267 if ( result != 0 ) {
268 std::cerr << "Could not create thread" << std::endl;
269 return 1;
270 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000271
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000272 pthread_t fibThread2;
273 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
274 if ( result != 0 ) {
275 std::cerr << "Could not create thread" << std::endl;
276 return 1;
277 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000278
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000279 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000280
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000281 void* returnValue;
282 result = pthread_join( add1Thread, &returnValue );
283 if ( result != 0 ) {
284 std::cerr << "Could not join thread" << std::endl;
285 return 1;
286 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000287 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000288
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000289 result = pthread_join( fibThread1, &returnValue );
290 if ( result != 0 ) {
291 std::cerr << "Could not join thread" << std::endl;
292 return 1;
293 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000294 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000295
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000296 result = pthread_join( fibThread2, &returnValue );
297 if ( result != 0 ) {
298 std::cerr << "Could not join thread" << std::endl;
299 return 1;
300 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000301 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000302
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000303 return 0;
304}