blob: 6fb8bd61982b5a141d305c2827d6badc88e9fb5a [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()),
57 Type::getInt32Ty(M->getContext()),
Hans Wennborgcc9deb42015-09-29 18:02:48 +000058 nullptr));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000059
Reid Spencer68550972005-07-12 21:51:33 +000060 // Add a basic block to the function. As before, it automatically inserts
61 // because of the last argument.
Owen Anderson55f1c092009-08-13 21:58:54 +000062 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000063
Reid Spencer68550972005-07-12 21:51:33 +000064 // Get pointers to the constant `1'.
Owen Anderson55f1c092009-08-13 21:58:54 +000065 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000066
Reid Spencer68550972005-07-12 21:51:33 +000067 // Get pointers to the integer argument of the add1 function...
68 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000069 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
Reid Spencer68550972005-07-12 21:51:33 +000070 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000071
Reid Spencer68550972005-07-12 21:51:33 +000072 // Create the add instruction, inserting it into the end of BB.
Gabor Greife1f6e4b2008-05-16 19:29:10 +000073 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000074
Reid Spencer68550972005-07-12 21:51:33 +000075 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +000076 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000077
78 // Now, function add1 is ready.
Reid Spencer68550972005-07-12 21:51:33 +000079 return Add1F;
80}
81
Chris Lattnerb800b392007-01-07 07:40:09 +000082static Function *CreateFibFunction(Module *M) {
Reid Spencer68550972005-07-12 21:51:33 +000083 // Create the fib function and insert it into module M. This function is said
84 // to return an int and take an int parameter.
Chris Lattnerb800b392007-01-07 07:40:09 +000085 Function *FibF =
Owen Anderson55f1c092009-08-13 21:58:54 +000086 cast<Function>(M->getOrInsertFunction("fib",
87 Type::getInt32Ty(M->getContext()),
88 Type::getInt32Ty(M->getContext()),
Hans Wennborgcc9deb42015-09-29 18:02:48 +000089 nullptr));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000090
Reid Spencer68550972005-07-12 21:51:33 +000091 // Add a basic block to the function.
Owen Anderson55f1c092009-08-13 21:58:54 +000092 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000093
Reid Spencer68550972005-07-12 21:51:33 +000094 // Get pointers to the constants.
Owen Anderson55f1c092009-08-13 21:58:54 +000095 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
96 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000097
Reid Spencer68550972005-07-12 21:51:33 +000098 // Get pointer to the integer argument of the add1 function...
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000099 Argument *ArgX = &*FibF->arg_begin(); // Get the arg.
Reid Spencer68550972005-07-12 21:51:33 +0000100 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000101
Reid Spencer68550972005-07-12 21:51:33 +0000102 // Create the true_block.
Owen Anderson55f1c092009-08-13 21:58:54 +0000103 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencer68550972005-07-12 21:51:33 +0000104 // Create an exit block.
Owen Anderson55f1c092009-08-13 21:58:54 +0000105 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000106
Reid Spencer68550972005-07-12 21:51:33 +0000107 // Create the "if (arg < 2) goto exitbb"
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000108 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greife9ecc682008-04-06 20:25:17 +0000109 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000110
Reid Spencer68550972005-07-12 21:51:33 +0000111 // Create: ret int 1
Owen Anderson55f1c092009-08-13 21:58:54 +0000112 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000113
Reid Spencer68550972005-07-12 21:51:33 +0000114 // create fib(x-1)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000115 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000116 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000117
Reid Spencer68550972005-07-12 21:51:33 +0000118 // create fib(x-2)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000119 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greife9ecc682008-04-06 20:25:17 +0000120 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000121
Reid Spencer68550972005-07-12 21:51:33 +0000122 // fib(x-1)+fib(x-2)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000123 Value *Sum =
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000124 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000125
Reid Spencer68550972005-07-12 21:51:33 +0000126 // Create the return instruction and add it to the basic block
Owen Anderson55f1c092009-08-13 21:58:54 +0000127 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000128
Reid Spencer68550972005-07-12 21:51:33 +0000129 return FibF;
130}
131
132struct threadParams {
133 ExecutionEngine* EE;
134 Function* F;
135 int value;
136};
137
138// We block the subthreads just before they begin to execute:
139// we want all of them to call into the JIT at the same time,
140// to verify that the locking is working correctly.
141class WaitForThreads
142{
143public:
144 WaitForThreads()
145 {
146 n = 0;
147 waitFor = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000148
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000149 int result = pthread_cond_init( &condition, nullptr );
Reid Spencer68550972005-07-12 21:51:33 +0000150 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000151
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000152 result = pthread_mutex_init( &mutex, nullptr );
Reid Spencer68550972005-07-12 21:51:33 +0000153 assert( result == 0 );
154 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000155
Reid Spencer68550972005-07-12 21:51:33 +0000156 ~WaitForThreads()
157 {
158 int result = pthread_cond_destroy( &condition );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000159 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000160 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000161
Reid Spencer68550972005-07-12 21:51:33 +0000162 result = pthread_mutex_destroy( &mutex );
163 assert( result == 0 );
164 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000165
Reid Spencer68550972005-07-12 21:51:33 +0000166 // All threads will stop here until another thread calls releaseThreads
167 void block()
168 {
169 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000170 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000171 assert( result == 0 );
172 n ++;
173 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000174
Reid Spencer68550972005-07-12 21:51:33 +0000175 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000176 if ( waitFor > 0 && n == waitFor )
Reid Spencer68550972005-07-12 21:51:33 +0000177 {
178 // There are enough threads blocked that we can release all of them
179 std::cout << "Unblocking threads from block()" << std::endl;
180 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000181 }
182 else
Reid Spencer68550972005-07-12 21:51:33 +0000183 {
184 // We just need to wait until someone unblocks us
185 result = pthread_cond_wait( &condition, &mutex );
186 assert( result == 0 );
187 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000188
Reid Spencer68550972005-07-12 21:51:33 +0000189 // unlock the mutex before returning
190 result = pthread_mutex_unlock( &mutex );
191 assert( result == 0 );
192 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000193
Reid Spencer68550972005-07-12 21:51:33 +0000194 // If there are num or more threads blocked, it will signal them all
195 // Otherwise, this thread blocks until there are enough OTHER threads
196 // blocked
197 void releaseThreads( size_t num )
198 {
199 int result = pthread_mutex_lock( &mutex );
Ahmed Charlesde14dcc2014-03-06 06:35:46 +0000200 (void)result;
Reid Spencer68550972005-07-12 21:51:33 +0000201 assert( result == 0 );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000202
Reid Spencer68550972005-07-12 21:51:33 +0000203 if ( n >= num ) {
204 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
205 unblockThreads();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000206 }
207 else
Reid Spencer68550972005-07-12 21:51:33 +0000208 {
209 waitFor = num;
210 pthread_cond_wait( &condition, &mutex );
211 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000212
Reid Spencer68550972005-07-12 21:51:33 +0000213 // unlock the mutex before returning
214 result = pthread_mutex_unlock( &mutex );
215 assert( result == 0 );
216 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000217
Reid Spencer68550972005-07-12 21:51:33 +0000218private:
219 void unblockThreads()
220 {
221 // Reset the counters to zero: this way, if any new threads
222 // enter while threads are exiting, they will block instead
223 // of triggering a new release of threads
224 n = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000225
Reid Spencer68550972005-07-12 21:51:33 +0000226 // Reset waitFor to zero: this way, if waitFor threads enter
227 // while threads are exiting, they will block instead of
228 // triggering a new release of threads
229 waitFor = 0;
230
231 int result = pthread_cond_broadcast( &condition );
Chandler Carruth3bb7d412012-02-20 00:02:49 +0000232 (void)result;
233 assert(result == 0);
Reid Spencer68550972005-07-12 21:51:33 +0000234 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000235
Reid Spencer68550972005-07-12 21:51:33 +0000236 size_t n;
237 size_t waitFor;
238 pthread_cond_t condition;
239 pthread_mutex_t mutex;
240};
241
242static WaitForThreads synchronize;
243
244void* callFunc( void* param )
245{
246 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000247
Reid Spencer68550972005-07-12 21:51:33 +0000248 // Call the `foo' function with no arguments:
249 std::vector<GenericValue> Args(1);
Reid Spencer8a9ae482007-03-06 17:24:31 +0000250 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000251
Reid Spencer68550972005-07-12 21:51:33 +0000252 synchronize.block(); // wait until other threads are at this point
253 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000254
Reid Spencer8a9ae482007-03-06 17:24:31 +0000255 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencer68550972005-07-12 21:51:33 +0000256}
257
Chris Lattnerd24df242009-06-17 16:48:44 +0000258int main() {
259 InitializeNativeTarget();
Owen Anderson6773d382009-07-01 16:58:40 +0000260 LLVMContext Context;
Chris Lattnerd24df242009-06-17 16:48:44 +0000261
Reid Spencer68550972005-07-12 21:51:33 +0000262 // Create some module to put our function into it.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000263 std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
264 Module *M = Owner.get();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000265
Reid Spencer68550972005-07-12 21:51:33 +0000266 Function* add1F = createAdd1( M );
267 Function* fibF = CreateFibFunction( M );
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000268
Reid Spencer68550972005-07-12 21:51:33 +0000269 // Now we create the JIT.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000270 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000271
Reid Spencer68550972005-07-12 21:51:33 +0000272 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
273 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000274
Reid Spencer68550972005-07-12 21:51:33 +0000275 // Create one thread for add1 and two threads for fib
276 struct threadParams add1 = { EE, add1F, 1000 };
277 struct threadParams fib1 = { EE, fibF, 39 };
278 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000279
Reid Spencer68550972005-07-12 21:51:33 +0000280 pthread_t add1Thread;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000281 int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 );
Reid Spencer68550972005-07-12 21:51:33 +0000282 if ( result != 0 ) {
283 std::cerr << "Could not create thread" << std::endl;
284 return 1;
285 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000286
Reid Spencer68550972005-07-12 21:51:33 +0000287 pthread_t fibThread1;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000288 result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 );
Reid Spencer68550972005-07-12 21:51:33 +0000289 if ( result != 0 ) {
290 std::cerr << "Could not create thread" << std::endl;
291 return 1;
292 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000293
Reid Spencer68550972005-07-12 21:51:33 +0000294 pthread_t fibThread2;
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000295 result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 );
Reid Spencer68550972005-07-12 21:51:33 +0000296 if ( result != 0 ) {
297 std::cerr << "Could not create thread" << std::endl;
298 return 1;
299 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000300
Reid Spencer68550972005-07-12 21:51:33 +0000301 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000302
Reid Spencer68550972005-07-12 21:51:33 +0000303 void* returnValue;
304 result = pthread_join( add1Thread, &returnValue );
305 if ( result != 0 ) {
306 std::cerr << "Could not join thread" << std::endl;
307 return 1;
308 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000309 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000310
Reid Spencer68550972005-07-12 21:51:33 +0000311 result = pthread_join( fibThread1, &returnValue );
312 if ( result != 0 ) {
313 std::cerr << "Could not join thread" << std::endl;
314 return 1;
315 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000316 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000317
Reid Spencer68550972005-07-12 21:51:33 +0000318 result = pthread_join( fibThread2, &returnValue );
319 if ( result != 0 ) {
320 std::cerr << "Could not join thread" << std::endl;
321 return 1;
322 }
Reid Spencer8640f2b2005-07-13 23:20:24 +0000323 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000324
Reid Spencer68550972005-07-12 21:51:33 +0000325 return 0;
326}