blob: 3c485d4c964d1532972d4b57adaeab2686de65aa [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
NAKAMURA Takumi85c9bac2015-03-02 01:04:34 +000020#include "llvm/ADT/STLExtras.h"
Reid Spencer68550972005-07-12 21:51:33 +000021#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000022#include "llvm/ExecutionEngine/Interpreter.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>
Hans Wennborgcc9deb42015-09-29 18:02:48 +000031
Reid Spencer68550972005-07-12 21:51:33 +000032using namespace llvm;
33
Chris Lattnerb800b392007-01-07 07:40:09 +000034static Function* createAdd1(Module *M) {
Reid Spencer68550972005-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 Lattnerb800b392007-01-07 07:40:09 +000038 Function *Add1F =
Owen Anderson55f1c092009-08-13 21:58:54 +000039 cast<Function>(M->getOrInsertFunction("add1",
40 Type::getInt32Ty(M->getContext()),
41 Type::getInt32Ty(M->getContext()),
Hans Wennborgcc9deb42015-09-29 18:02:48 +000042 nullptr));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000043
Reid Spencer68550972005-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 Anderson55f1c092009-08-13 21:58:54 +000046 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000047
Reid Spencer68550972005-07-12 21:51:33 +000048 // Get pointers to the constant `1'.
Owen Anderson55f1c092009-08-13 21:58:54 +000049 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000050
Reid Spencer68550972005-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
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000053 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
Reid Spencer68550972005-07-12 21:51:33 +000054 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000055
Reid Spencer68550972005-07-12 21:51:33 +000056 // Create the add instruction, inserting it into the end of BB.
Gabor Greife1f6e4b2008-05-16 19:29:10 +000057 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000058
Reid Spencer68550972005-07-12 21:51:33 +000059 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +000060 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000061
62 // Now, function add1 is ready.
Reid Spencer68550972005-07-12 21:51:33 +000063 return Add1F;
64}
65
Chris Lattnerb800b392007-01-07 07:40:09 +000066static Function *CreateFibFunction(Module *M) {
Reid Spencer68550972005-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 Lattnerb800b392007-01-07 07:40:09 +000069 Function *FibF =
Owen Anderson55f1c092009-08-13 21:58:54 +000070 cast<Function>(M->getOrInsertFunction("fib",
71 Type::getInt32Ty(M->getContext()),
72 Type::getInt32Ty(M->getContext()),
Hans Wennborgcc9deb42015-09-29 18:02:48 +000073 nullptr));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000074
Reid Spencer68550972005-07-12 21:51:33 +000075 // Add a basic block to the function.
Owen Anderson55f1c092009-08-13 21:58:54 +000076 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000077
Reid Spencer68550972005-07-12 21:51:33 +000078 // Get pointers to the constants.
Owen Anderson55f1c092009-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 Cohen5f4ef3c2005-07-27 06:12:32 +000081
Reid Spencer68550972005-07-12 21:51:33 +000082 // Get pointer to the integer argument of the add1 function...
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000083 Argument *ArgX = &*FibF->arg_begin(); // Get the arg.
Reid Spencer68550972005-07-12 21:51:33 +000084 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000085
Reid Spencer68550972005-07-12 21:51:33 +000086 // Create the true_block.
Owen Anderson55f1c092009-08-13 21:58:54 +000087 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencer68550972005-07-12 21:51:33 +000088 // Create an exit block.
Owen Anderson55f1c092009-08-13 21:58:54 +000089 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000090
Reid Spencer68550972005-07-12 21:51:33 +000091 // Create the "if (arg < 2) goto exitbb"
Owen Anderson1e5f00e2009-07-09 23:48:35 +000092 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greife9ecc682008-04-06 20:25:17 +000093 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000094
Reid Spencer68550972005-07-12 21:51:33 +000095 // Create: ret int 1
Owen Anderson55f1c092009-08-13 21:58:54 +000096 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000097
Reid Spencer68550972005-07-12 21:51:33 +000098 // create fib(x-1)
Gabor Greife1f6e4b2008-05-16 19:29:10 +000099 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000100 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000101
Reid Spencer68550972005-07-12 21:51:33 +0000102 // create fib(x-2)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000103 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000104 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000105
Reid Spencer68550972005-07-12 21:51:33 +0000106 // fib(x-1)+fib(x-2)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000107 Value *Sum =
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000108 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000109
Reid Spencer68550972005-07-12 21:51:33 +0000110 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +0000111 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000112
Reid Spencer68550972005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000132
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000133 int result = pthread_cond_init( &condition, nullptr );
Reid Spencer68550972005-07-12 21:51:33 +0000134 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000135
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000136 result = pthread_mutex_init( &mutex, nullptr );
Reid Spencer68550972005-07-12 21:51:33 +0000137 assert( result == 0 );
138 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000139
Reid Spencer68550972005-07-12 21:51:33 +0000140 ~WaitForThreads()
141 {
142 int result = pthread_cond_destroy( &condition );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000143 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000144 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000145
Reid Spencer68550972005-07-12 21:51:33 +0000146 result = pthread_mutex_destroy( &mutex );
147 assert( result == 0 );
148 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000149
Reid Spencer68550972005-07-12 21:51:33 +0000150 // All threads will stop here until another thread calls releaseThreads
151 void block()
152 {
153 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000154 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000155 assert( result == 0 );
156 n ++;
157 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000158
Reid Spencer68550972005-07-12 21:51:33 +0000159 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000160 if ( waitFor > 0 && n == waitFor )
Reid Spencer68550972005-07-12 21:51:33 +0000161 {
162 // There are enough threads blocked that we can release all of them
163 std::cout << "Unblocking threads from block()" << std::endl;
164 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000165 }
166 else
Reid Spencer68550972005-07-12 21:51:33 +0000167 {
168 // We just need to wait until someone unblocks us
169 result = pthread_cond_wait( &condition, &mutex );
170 assert( result == 0 );
171 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000172
Reid Spencer68550972005-07-12 21:51:33 +0000173 // unlock the mutex before returning
174 result = pthread_mutex_unlock( &mutex );
175 assert( result == 0 );
176 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000177
Reid Spencer68550972005-07-12 21:51:33 +0000178 // If there are num or more threads blocked, it will signal them all
179 // Otherwise, this thread blocks until there are enough OTHER threads
180 // blocked
181 void releaseThreads( size_t num )
182 {
183 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000184 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000185 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000186
Reid Spencer68550972005-07-12 21:51:33 +0000187 if ( n >= num ) {
188 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
189 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000190 }
191 else
Reid Spencer68550972005-07-12 21:51:33 +0000192 {
193 waitFor = num;
194 pthread_cond_wait( &condition, &mutex );
195 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000196
Reid Spencer68550972005-07-12 21:51:33 +0000197 // unlock the mutex before returning
198 result = pthread_mutex_unlock( &mutex );
199 assert( result == 0 );
200 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000201
Reid Spencer68550972005-07-12 21:51:33 +0000202private:
203 void unblockThreads()
204 {
205 // Reset the counters to zero: this way, if any new threads
206 // enter while threads are exiting, they will block instead
207 // of triggering a new release of threads
208 n = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000209
Reid Spencer68550972005-07-12 21:51:33 +0000210 // Reset waitFor to zero: this way, if waitFor threads enter
211 // while threads are exiting, they will block instead of
212 // triggering a new release of threads
213 waitFor = 0;
214
215 int result = pthread_cond_broadcast( &condition );
Chandler Carruth3bb7d412012-02-20 00:02:49 +0000216 (void)result;
217 assert(result == 0);
Reid Spencer68550972005-07-12 21:51:33 +0000218 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000219
Reid Spencer68550972005-07-12 21:51:33 +0000220 size_t n;
221 size_t waitFor;
222 pthread_cond_t condition;
223 pthread_mutex_t mutex;
224};
225
226static WaitForThreads synchronize;
227
228void* callFunc( void* param )
229{
230 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000231
Reid Spencer68550972005-07-12 21:51:33 +0000232 // Call the `foo' function with no arguments:
233 std::vector<GenericValue> Args(1);
Reid Spencer8a9ae482007-03-06 17:24:31 +0000234 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000235
Reid Spencer68550972005-07-12 21:51:33 +0000236 synchronize.block(); // wait until other threads are at this point
237 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000238
Reid Spencer8a9ae482007-03-06 17:24:31 +0000239 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencer68550972005-07-12 21:51:33 +0000240}
241
Chris Lattnerd24df242009-06-17 16:48:44 +0000242int main() {
243 InitializeNativeTarget();
Owen Anderson6773d382009-07-01 16:58:40 +0000244 LLVMContext Context;
Chris Lattnerd24df242009-06-17 16:48:44 +0000245
Reid Spencer68550972005-07-12 21:51:33 +0000246 // Create some module to put our function into it.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000247 std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
248 Module *M = Owner.get();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000249
Reid Spencer68550972005-07-12 21:51:33 +0000250 Function* add1F = createAdd1( M );
251 Function* fibF = CreateFibFunction( M );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000252
Reid Spencer68550972005-07-12 21:51:33 +0000253 // Now we create the JIT.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000254 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000255
Reid Spencer68550972005-07-12 21:51:33 +0000256 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
257 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000258
Reid Spencer68550972005-07-12 21:51:33 +0000259 // Create one thread for add1 and two threads for fib
260 struct threadParams add1 = { EE, add1F, 1000 };
261 struct threadParams fib1 = { EE, fibF, 39 };
262 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000263
Reid Spencer68550972005-07-12 21:51:33 +0000264 pthread_t add1Thread;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000265 int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 );
Reid Spencer68550972005-07-12 21:51:33 +0000266 if ( result != 0 ) {
267 std::cerr << "Could not create thread" << std::endl;
268 return 1;
269 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Reid Spencer68550972005-07-12 21:51:33 +0000271 pthread_t fibThread1;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000272 result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 );
Reid Spencer68550972005-07-12 21:51:33 +0000273 if ( result != 0 ) {
274 std::cerr << "Could not create thread" << std::endl;
275 return 1;
276 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000277
Reid Spencer68550972005-07-12 21:51:33 +0000278 pthread_t fibThread2;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000279 result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 );
Reid Spencer68550972005-07-12 21:51:33 +0000280 if ( result != 0 ) {
281 std::cerr << "Could not create thread" << std::endl;
282 return 1;
283 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000284
Reid Spencer68550972005-07-12 21:51:33 +0000285 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000286
Reid Spencer68550972005-07-12 21:51:33 +0000287 void* returnValue;
288 result = pthread_join( add1Thread, &returnValue );
289 if ( result != 0 ) {
290 std::cerr << "Could not join thread" << std::endl;
291 return 1;
292 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000293 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000294
Reid Spencer68550972005-07-12 21:51:33 +0000295 result = pthread_join( fibThread1, &returnValue );
296 if ( result != 0 ) {
297 std::cerr << "Could not join thread" << std::endl;
298 return 1;
299 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000300 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000301
Reid Spencer68550972005-07-12 21:51:33 +0000302 result = pthread_join( fibThread2, &returnValue );
303 if ( result != 0 ) {
304 std::cerr << "Could not join thread" << std::endl;
305 return 1;
306 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000307 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000308
Reid Spencer68550972005-07-12 21:51:33 +0000309 return 0;
310}