blob: f1932d2471cb88dcf190c290d0df9a94732d4ba0 [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.
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000019//
20//===----------------------------------------------------------------------===//
Reid Spencer68550972005-07-12 21:51:33 +000021
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000022#include "llvm/ADT/APInt.h"
NAKAMURA Takumi85c9bac2015-03-02 01:04:34 +000023#include "llvm/ADT/STLExtras.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000024#include "llvm/ExecutionEngine/ExecutionEngine.h"
Reid Spencer68550972005-07-12 21:51:33 +000025#include "llvm/ExecutionEngine/GenericValue.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000026#include "llvm/IR/Argument.h"
27#include "llvm/IR/BasicBlock.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000030#include "llvm/IR/Function.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000033#include "llvm/IR/Instructions.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Module.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000036#include "llvm/IR/Type.h"
37#include "llvm/Support/Casting.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetSelect.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000039#include <algorithm>
40#include <cassert>
41#include <cstddef>
42#include <cstdint>
Reid Spencer68550972005-07-12 21:51:33 +000043#include <iostream>
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000044#include <memory>
45#include <vector>
Chandler Carruth605e30e2012-12-04 10:16:57 +000046#include <pthread.h>
Hans Wennborgcc9deb42015-09-29 18:02:48 +000047
Reid Spencer68550972005-07-12 21:51:33 +000048using namespace llvm;
49
Chris Lattnerb800b392007-01-07 07:40:09 +000050static Function* createAdd1(Module *M) {
Reid Spencer68550972005-07-12 21:51:33 +000051 // Create the add1 function entry and insert this entry into module M. The
52 // function will have a return type of "int" and take an argument of "int".
53 // The '0' terminates the list of argument types.
Chris Lattnerb800b392007-01-07 07:40:09 +000054 Function *Add1F =
Owen Anderson55f1c092009-08-13 21:58:54 +000055 cast<Function>(M->getOrInsertFunction("add1",
56 Type::getInt32Ty(M->getContext()),
Serge Guelton59a2d7b2017-04-11 15:01:18 +000057 Type::getInt32Ty(M->getContext())));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000058
Reid Spencer68550972005-07-12 21:51:33 +000059 // Add a basic block to the function. As before, it automatically inserts
60 // because of the last argument.
Owen Anderson55f1c092009-08-13 21:58:54 +000061 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000062
Reid Spencer68550972005-07-12 21:51:33 +000063 // Get pointers to the constant `1'.
Owen Anderson55f1c092009-08-13 21:58:54 +000064 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000065
Reid Spencer68550972005-07-12 21:51:33 +000066 // Get pointers to the integer argument of the add1 function...
67 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000068 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
Reid Spencer68550972005-07-12 21:51:33 +000069 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000070
Reid Spencer68550972005-07-12 21:51:33 +000071 // Create the add instruction, inserting it into the end of BB.
Gabor Greife1f6e4b2008-05-16 19:29:10 +000072 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000073
Reid Spencer68550972005-07-12 21:51:33 +000074 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +000075 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000076
77 // Now, function add1 is ready.
Reid Spencer68550972005-07-12 21:51:33 +000078 return Add1F;
79}
80
Chris Lattnerb800b392007-01-07 07:40:09 +000081static Function *CreateFibFunction(Module *M) {
Reid Spencer68550972005-07-12 21:51:33 +000082 // Create the fib function and insert it into module M. This function is said
83 // to return an int and take an int parameter.
Chris Lattnerb800b392007-01-07 07:40:09 +000084 Function *FibF =
Owen Anderson55f1c092009-08-13 21:58:54 +000085 cast<Function>(M->getOrInsertFunction("fib",
86 Type::getInt32Ty(M->getContext()),
Serge Guelton59a2d7b2017-04-11 15:01:18 +000087 Type::getInt32Ty(M->getContext())));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000088
Reid Spencer68550972005-07-12 21:51:33 +000089 // Add a basic block to the function.
Owen Anderson55f1c092009-08-13 21:58:54 +000090 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000091
Reid Spencer68550972005-07-12 21:51:33 +000092 // Get pointers to the constants.
Owen Anderson55f1c092009-08-13 21:58:54 +000093 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
94 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000095
Reid Spencer68550972005-07-12 21:51:33 +000096 // Get pointer to the integer argument of the add1 function...
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000097 Argument *ArgX = &*FibF->arg_begin(); // Get the arg.
Reid Spencer68550972005-07-12 21:51:33 +000098 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000099
Reid Spencer68550972005-07-12 21:51:33 +0000100 // Create the true_block.
Owen Anderson55f1c092009-08-13 21:58:54 +0000101 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencer68550972005-07-12 21:51:33 +0000102 // Create an exit block.
Owen Anderson55f1c092009-08-13 21:58:54 +0000103 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000104
Reid Spencer68550972005-07-12 21:51:33 +0000105 // Create the "if (arg < 2) goto exitbb"
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000106 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greife9ecc682008-04-06 20:25:17 +0000107 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000108
Reid Spencer68550972005-07-12 21:51:33 +0000109 // Create: ret int 1
Owen Anderson55f1c092009-08-13 21:58:54 +0000110 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000111
Reid Spencer68550972005-07-12 21:51:33 +0000112 // create fib(x-1)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000113 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000114 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000115
Reid Spencer68550972005-07-12 21:51:33 +0000116 // create fib(x-2)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000117 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000118 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000119
Reid Spencer68550972005-07-12 21:51:33 +0000120 // fib(x-1)+fib(x-2)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000121 Value *Sum =
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000122 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000123
Reid Spencer68550972005-07-12 21:51:33 +0000124 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +0000125 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000126
Reid Spencer68550972005-07-12 21:51:33 +0000127 return FibF;
128}
129
130struct threadParams {
131 ExecutionEngine* EE;
132 Function* F;
133 int value;
134};
135
136// We block the subthreads just before they begin to execute:
137// we want all of them to call into the JIT at the same time,
138// to verify that the locking is working correctly.
139class WaitForThreads
140{
141public:
142 WaitForThreads()
143 {
144 n = 0;
145 waitFor = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000146
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000147 int result = pthread_cond_init( &condition, nullptr );
Reid Spencer68550972005-07-12 21:51:33 +0000148 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000149
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000150 result = pthread_mutex_init( &mutex, nullptr );
Reid Spencer68550972005-07-12 21:51:33 +0000151 assert( result == 0 );
152 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000153
Reid Spencer68550972005-07-12 21:51:33 +0000154 ~WaitForThreads()
155 {
156 int result = pthread_cond_destroy( &condition );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000157 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000158 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000159
Reid Spencer68550972005-07-12 21:51:33 +0000160 result = pthread_mutex_destroy( &mutex );
161 assert( result == 0 );
162 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000163
Reid Spencer68550972005-07-12 21:51:33 +0000164 // All threads will stop here until another thread calls releaseThreads
165 void block()
166 {
167 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000168 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000169 assert( result == 0 );
170 n ++;
171 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000172
Reid Spencer68550972005-07-12 21:51:33 +0000173 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000174 if ( waitFor > 0 && n == waitFor )
Reid Spencer68550972005-07-12 21:51:33 +0000175 {
176 // There are enough threads blocked that we can release all of them
177 std::cout << "Unblocking threads from block()" << std::endl;
178 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000179 }
180 else
Reid Spencer68550972005-07-12 21:51:33 +0000181 {
182 // We just need to wait until someone unblocks us
183 result = pthread_cond_wait( &condition, &mutex );
184 assert( result == 0 );
185 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000186
Reid Spencer68550972005-07-12 21:51:33 +0000187 // unlock the mutex before returning
188 result = pthread_mutex_unlock( &mutex );
189 assert( result == 0 );
190 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000191
Reid Spencer68550972005-07-12 21:51:33 +0000192 // If there are num or more threads blocked, it will signal them all
193 // Otherwise, this thread blocks until there are enough OTHER threads
194 // blocked
195 void releaseThreads( size_t num )
196 {
197 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000198 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000199 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000200
Reid Spencer68550972005-07-12 21:51:33 +0000201 if ( n >= num ) {
202 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
203 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000204 }
205 else
Reid Spencer68550972005-07-12 21:51:33 +0000206 {
207 waitFor = num;
208 pthread_cond_wait( &condition, &mutex );
209 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000210
Reid Spencer68550972005-07-12 21:51:33 +0000211 // unlock the mutex before returning
212 result = pthread_mutex_unlock( &mutex );
213 assert( result == 0 );
214 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000215
Reid Spencer68550972005-07-12 21:51:33 +0000216private:
217 void unblockThreads()
218 {
219 // Reset the counters to zero: this way, if any new threads
220 // enter while threads are exiting, they will block instead
221 // of triggering a new release of threads
222 n = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000223
Reid Spencer68550972005-07-12 21:51:33 +0000224 // Reset waitFor to zero: this way, if waitFor threads enter
225 // while threads are exiting, they will block instead of
226 // triggering a new release of threads
227 waitFor = 0;
228
229 int result = pthread_cond_broadcast( &condition );
Chandler Carruth3bb7d412012-02-20 00:02:49 +0000230 (void)result;
231 assert(result == 0);
Reid Spencer68550972005-07-12 21:51:33 +0000232 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000233
Reid Spencer68550972005-07-12 21:51:33 +0000234 size_t n;
235 size_t waitFor;
236 pthread_cond_t condition;
237 pthread_mutex_t mutex;
238};
239
240static WaitForThreads synchronize;
241
242void* callFunc( void* param )
243{
244 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000245
Reid Spencer68550972005-07-12 21:51:33 +0000246 // Call the `foo' function with no arguments:
247 std::vector<GenericValue> Args(1);
Reid Spencer8a9ae482007-03-06 17:24:31 +0000248 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000249
Reid Spencer68550972005-07-12 21:51:33 +0000250 synchronize.block(); // wait until other threads are at this point
251 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000252
Reid Spencer8a9ae482007-03-06 17:24:31 +0000253 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencer68550972005-07-12 21:51:33 +0000254}
255
Chris Lattnerd24df242009-06-17 16:48:44 +0000256int main() {
257 InitializeNativeTarget();
Owen Anderson6773d382009-07-01 16:58:40 +0000258 LLVMContext Context;
Chris Lattnerd24df242009-06-17 16:48:44 +0000259
Reid Spencer68550972005-07-12 21:51:33 +0000260 // Create some module to put our function into it.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000261 std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
262 Module *M = Owner.get();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000263
Reid Spencer68550972005-07-12 21:51:33 +0000264 Function* add1F = createAdd1( M );
265 Function* fibF = CreateFibFunction( M );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000266
Reid Spencer68550972005-07-12 21:51:33 +0000267 // Now we create the JIT.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000268 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000269
Reid Spencer68550972005-07-12 21:51:33 +0000270 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
271 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000272
Reid Spencer68550972005-07-12 21:51:33 +0000273 // Create one thread for add1 and two threads for fib
274 struct threadParams add1 = { EE, add1F, 1000 };
275 struct threadParams fib1 = { EE, fibF, 39 };
276 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000277
Reid Spencer68550972005-07-12 21:51:33 +0000278 pthread_t add1Thread;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000279 int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 );
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 pthread_t fibThread1;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000286 result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 );
Reid Spencer68550972005-07-12 21:51:33 +0000287 if ( result != 0 ) {
288 std::cerr << "Could not create thread" << std::endl;
289 return 1;
290 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000291
Reid Spencer68550972005-07-12 21:51:33 +0000292 pthread_t fibThread2;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000293 result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 );
Reid Spencer68550972005-07-12 21:51:33 +0000294 if ( result != 0 ) {
295 std::cerr << "Could not create thread" << std::endl;
296 return 1;
297 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000298
Reid Spencer68550972005-07-12 21:51:33 +0000299 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000300
Reid Spencer68550972005-07-12 21:51:33 +0000301 void* returnValue;
302 result = pthread_join( add1Thread, &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 << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000308
Reid Spencer68550972005-07-12 21:51:33 +0000309 result = pthread_join( fibThread1, &returnValue );
310 if ( result != 0 ) {
311 std::cerr << "Could not join thread" << std::endl;
312 return 1;
313 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000314 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000315
Reid Spencer68550972005-07-12 21:51:33 +0000316 result = pthread_join( fibThread2, &returnValue );
317 if ( result != 0 ) {
318 std::cerr << "Could not join thread" << std::endl;
319 return 1;
320 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000321 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000322
Reid Spencer68550972005-07-12 21:51:33 +0000323 return 0;
324}