blob: ff44375e3921e27a21bc39bde34d20a73c4a1247 [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.
Eugene Zelenko578a1d22016-05-25 01:18:36 +000019//
20//===----------------------------------------------------------------------===//
Reid Spencere8cdc8b2005-07-12 21:51:33 +000021
Eugene Zelenko578a1d22016-05-25 01:18:36 +000022#include "llvm/ADT/APInt.h"
NAKAMURA Takumia757af92015-03-02 01:04:34 +000023#include "llvm/ADT/STLExtras.h"
Eugene Zelenko578a1d22016-05-25 01:18:36 +000024#include "llvm/ExecutionEngine/ExecutionEngine.h"
Reid Spencere8cdc8b2005-07-12 21:51:33 +000025#include "llvm/ExecutionEngine/GenericValue.h"
Eugene Zelenko578a1d22016-05-25 01:18:36 +000026#include "llvm/IR/Argument.h"
27#include "llvm/IR/BasicBlock.h"
Chandler Carruth0a084602013-01-02 11:56:33 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
Eugene Zelenko578a1d22016-05-25 01:18:36 +000030#include "llvm/IR/Function.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
Chandler Carruth0a084602013-01-02 11:56:33 +000033#include "llvm/IR/Instructions.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Module.h"
Eugene Zelenko578a1d22016-05-25 01:18:36 +000036#include "llvm/IR/Type.h"
37#include "llvm/Support/Casting.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000038#include "llvm/Support/TargetSelect.h"
Eugene Zelenko578a1d22016-05-25 01:18:36 +000039#include <algorithm>
40#include <cassert>
41#include <cstddef>
42#include <cstdint>
Reid Spencere8cdc8b2005-07-12 21:51:33 +000043#include <iostream>
Eugene Zelenko578a1d22016-05-25 01:18:36 +000044#include <memory>
45#include <vector>
Chandler Carruth4ca7e092012-12-04 10:16:57 +000046#include <pthread.h>
Hans Wennborgd16725c2015-09-29 18:02:48 +000047
Reid Spencere8cdc8b2005-07-12 21:51:33 +000048using namespace llvm;
49
Chris Lattner6a987542007-01-07 07:40:09 +000050static Function* createAdd1(Module *M) {
Reid Spencere8cdc8b2005-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 Lattner6a987542007-01-07 07:40:09 +000054 Function *Add1F =
Owen Anderson1d0be152009-08-13 21:58:54 +000055 cast<Function>(M->getOrInsertFunction("add1",
56 Type::getInt32Ty(M->getContext()),
Serge Guelton9d544002017-04-11 15:01:18 +000057 Type::getInt32Ty(M->getContext())));
Jeff Cohen00b168892005-07-27 06:12:32 +000058
Reid Spencere8cdc8b2005-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 Anderson1d0be152009-08-13 21:58:54 +000061 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
Jeff Cohen00b168892005-07-27 06:12:32 +000062
Reid Spencere8cdc8b2005-07-12 21:51:33 +000063 // Get pointers to the constant `1'.
Owen Anderson1d0be152009-08-13 21:58:54 +000064 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
Jeff Cohen00b168892005-07-27 06:12:32 +000065
Reid Spencere8cdc8b2005-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 Smith7d7668e2015-11-07 00:55:46 +000068 Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
Reid Spencere8cdc8b2005-07-12 21:51:33 +000069 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000070
Reid Spencere8cdc8b2005-07-12 21:51:33 +000071 // Create the add instruction, inserting it into the end of BB.
Gabor Greif7cbd8a32008-05-16 19:29:10 +000072 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000073
Reid Spencere8cdc8b2005-07-12 21:51:33 +000074 // Create the return instruction and add it to the basic block
Owen Anderson1d0be152009-08-13 21:58:54 +000075 ReturnInst::Create(M->getContext(), Add, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +000076
77 // Now, function add1 is ready.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000078 return Add1F;
79}
80
Chris Lattner6a987542007-01-07 07:40:09 +000081static Function *CreateFibFunction(Module *M) {
Reid Spencere8cdc8b2005-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 Lattner6a987542007-01-07 07:40:09 +000084 Function *FibF =
Owen Anderson1d0be152009-08-13 21:58:54 +000085 cast<Function>(M->getOrInsertFunction("fib",
86 Type::getInt32Ty(M->getContext()),
Serge Guelton9d544002017-04-11 15:01:18 +000087 Type::getInt32Ty(M->getContext())));
Jeff Cohen00b168892005-07-27 06:12:32 +000088
Reid Spencere8cdc8b2005-07-12 21:51:33 +000089 // Add a basic block to the function.
Owen Anderson1d0be152009-08-13 21:58:54 +000090 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +000091
Reid Spencere8cdc8b2005-07-12 21:51:33 +000092 // Get pointers to the constants.
Owen Anderson1d0be152009-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 Cohen00b168892005-07-27 06:12:32 +000095
Reid Spencere8cdc8b2005-07-12 21:51:33 +000096 // Get pointer to the integer argument of the add1 function...
Duncan P. N. Exon Smith7d7668e2015-11-07 00:55:46 +000097 Argument *ArgX = &*FibF->arg_begin(); // Get the arg.
Reid Spencere8cdc8b2005-07-12 21:51:33 +000098 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
Jeff Cohen00b168892005-07-27 06:12:32 +000099
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000100 // Create the true_block.
Owen Anderson1d0be152009-08-13 21:58:54 +0000101 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000102 // Create an exit block.
Owen Anderson1d0be152009-08-13 21:58:54 +0000103 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000104
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000105 // Create the "if (arg < 2) goto exitbb"
Owen Anderson333c4002009-07-09 23:48:35 +0000106 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
Gabor Greif051a9502008-04-06 20:25:17 +0000107 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000108
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000109 // Create: ret int 1
Owen Anderson1d0be152009-08-13 21:58:54 +0000110 ReturnInst::Create(M->getContext(), One, RetBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000111
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000112 // create fib(x-1)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000113 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000114 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000115
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000116 // create fib(x-2)
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000117 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000118 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000119
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000120 // fib(x-1)+fib(x-2)
Jeff Cohen00b168892005-07-27 06:12:32 +0000121 Value *Sum =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000122 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000123
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000124 // Create the return instruction and add it to the basic block
Owen Anderson1d0be152009-08-13 21:58:54 +0000125 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000126
Reid Spencere8cdc8b2005-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 Cohen00b168892005-07-27 06:12:32 +0000146
Hans Wennborgd16725c2015-09-29 18:02:48 +0000147 int result = pthread_cond_init( &condition, nullptr );
Hans Wennborg56819a82017-07-19 15:06:31 +0000148 (void)result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000149 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000150
Hans Wennborgd16725c2015-09-29 18:02:48 +0000151 result = pthread_mutex_init( &mutex, nullptr );
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000152 assert( result == 0 );
153 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000154
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000155 ~WaitForThreads()
156 {
157 int result = pthread_cond_destroy( &condition );
Ahmed Charles9959dff2014-03-06 06:35:46 +0000158 (void)result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000159 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000160
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000161 result = pthread_mutex_destroy( &mutex );
162 assert( result == 0 );
163 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000164
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000165 // All threads will stop here until another thread calls releaseThreads
166 void block()
167 {
168 int result = pthread_mutex_lock( &mutex );
Ahmed Charles9959dff2014-03-06 06:35:46 +0000169 (void)result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000170 assert( result == 0 );
171 n ++;
172 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000173
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000174 assert( waitFor == 0 || n <= waitFor );
Jeff Cohen00b168892005-07-27 06:12:32 +0000175 if ( waitFor > 0 && n == waitFor )
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000176 {
177 // There are enough threads blocked that we can release all of them
178 std::cout << "Unblocking threads from block()" << std::endl;
179 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000180 }
181 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000182 {
183 // We just need to wait until someone unblocks us
184 result = pthread_cond_wait( &condition, &mutex );
185 assert( result == 0 );
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 +0000193 // If there are num or more threads blocked, it will signal them all
194 // Otherwise, this thread blocks until there are enough OTHER threads
195 // blocked
196 void releaseThreads( size_t num )
197 {
198 int result = pthread_mutex_lock( &mutex );
Ahmed Charles9959dff2014-03-06 06:35:46 +0000199 (void)result;
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000200 assert( result == 0 );
Jeff Cohen00b168892005-07-27 06:12:32 +0000201
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000202 if ( n >= num ) {
203 std::cout << "Unblocking threads from releaseThreads()" << std::endl;
204 unblockThreads();
Jeff Cohen00b168892005-07-27 06:12:32 +0000205 }
206 else
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000207 {
208 waitFor = num;
209 pthread_cond_wait( &condition, &mutex );
210 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000211
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000212 // unlock the mutex before returning
213 result = pthread_mutex_unlock( &mutex );
214 assert( result == 0 );
215 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000216
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000217private:
218 void unblockThreads()
219 {
220 // Reset the counters to zero: this way, if any new threads
221 // enter while threads are exiting, they will block instead
222 // of triggering a new release of threads
223 n = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000224
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000225 // Reset waitFor to zero: this way, if waitFor threads enter
226 // while threads are exiting, they will block instead of
227 // triggering a new release of threads
228 waitFor = 0;
229
230 int result = pthread_cond_broadcast( &condition );
Chandler Carruth637f9492012-02-20 00:02:49 +0000231 (void)result;
232 assert(result == 0);
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000233 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000234
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000235 size_t n;
236 size_t waitFor;
237 pthread_cond_t condition;
238 pthread_mutex_t mutex;
239};
240
241static WaitForThreads synchronize;
242
243void* callFunc( void* param )
244{
245 struct threadParams* p = (struct threadParams*) param;
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000247 // Call the `foo' function with no arguments:
248 std::vector<GenericValue> Args(1);
Reid Spencer34bd70d2007-03-06 17:24:31 +0000249 Args[0].IntVal = APInt(32, p->value);
Jeff Cohen00b168892005-07-27 06:12:32 +0000250
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000251 synchronize.block(); // wait until other threads are at this point
252 GenericValue gv = p->EE->runFunction(p->F, Args);
Jeff Cohen00b168892005-07-27 06:12:32 +0000253
Reid Spencer34bd70d2007-03-06 17:24:31 +0000254 return (void*)(intptr_t)gv.IntVal.getZExtValue();
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000255}
256
Chris Lattnerda062882009-06-17 16:48:44 +0000257int main() {
258 InitializeNativeTarget();
Owen Anderson8b477ed2009-07-01 16:58:40 +0000259 LLVMContext Context;
Chris Lattnerda062882009-06-17 16:48:44 +0000260
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000261 // Create some module to put our function into it.
Rafael Espindola3f4ed322014-08-19 04:04:25 +0000262 std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
263 Module *M = Owner.get();
Jeff Cohen00b168892005-07-27 06:12:32 +0000264
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000265 Function* add1F = createAdd1( M );
266 Function* fibF = CreateFibFunction( M );
Jeff Cohen00b168892005-07-27 06:12:32 +0000267
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000268 // Now we create the JIT.
Rafael Espindola3f4ed322014-08-19 04:04:25 +0000269 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
Jeff Cohen00b168892005-07-27 06:12:32 +0000270
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000271 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
272 //~ std::cout << "\n\nRunning foo: " << std::flush;
Jeff Cohen00b168892005-07-27 06:12:32 +0000273
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000274 // Create one thread for add1 and two threads for fib
275 struct threadParams add1 = { EE, add1F, 1000 };
276 struct threadParams fib1 = { EE, fibF, 39 };
277 struct threadParams fib2 = { EE, fibF, 42 };
Jeff Cohen00b168892005-07-27 06:12:32 +0000278
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000279 pthread_t add1Thread;
Hans Wennborgd16725c2015-09-29 18:02:48 +0000280 int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 );
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000281 if ( result != 0 ) {
282 std::cerr << "Could not create thread" << std::endl;
283 return 1;
284 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000285
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000286 pthread_t fibThread1;
Hans Wennborgd16725c2015-09-29 18:02:48 +0000287 result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 );
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000288 if ( result != 0 ) {
289 std::cerr << "Could not create thread" << std::endl;
290 return 1;
291 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000292
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000293 pthread_t fibThread2;
Hans Wennborgd16725c2015-09-29 18:02:48 +0000294 result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 );
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000295 if ( result != 0 ) {
296 std::cerr << "Could not create thread" << std::endl;
297 return 1;
298 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000299
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000300 synchronize.releaseThreads(3); // wait until other threads are at this point
Jeff Cohen00b168892005-07-27 06:12:32 +0000301
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000302 void* returnValue;
303 result = pthread_join( add1Thread, &returnValue );
304 if ( result != 0 ) {
305 std::cerr << "Could not join thread" << std::endl;
306 return 1;
307 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000308 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000309
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000310 result = pthread_join( fibThread1, &returnValue );
311 if ( result != 0 ) {
312 std::cerr << "Could not join thread" << std::endl;
313 return 1;
314 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000315 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000316
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000317 result = pthread_join( fibThread2, &returnValue );
318 if ( result != 0 ) {
319 std::cerr << "Could not join thread" << std::endl;
320 return 1;
321 }
Reid Spencer6fb0d732005-07-13 23:20:24 +0000322 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
Jeff Cohen00b168892005-07-27 06:12:32 +0000323
Reid Spencere8cdc8b2005-07-12 21:51:33 +0000324 return 0;
325}