1 //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Parallel JIT 11 // 12 // This test program creates two LLVM functions then calls them from three 13 // 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 "llvm/ExecutionEngine/GenericValue.h" 21 #include "llvm/ExecutionEngine/Interpreter.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/DerivedTypes.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/LLVMContext.h" 26 #include "llvm/IR/Module.h" 27 #include "llvm/Support/TargetSelect.h" 28 #include <iostream> 29 #include <pthread.h> 30 using namespace llvm; 31 32 static Function* createAdd1(Module *M) { 33 // Create the add1 function entry and insert this entry into module M. The 34 // function will have a return type of "int" and take an argument of "int". 35 // The '0' terminates the list of argument types. 36 Function *Add1F = 37 cast<Function>(M->getOrInsertFunction("add1", 38 Type::getInt32Ty(M->getContext()), 39 Type::getInt32Ty(M->getContext()), 40 (Type *)0)); 41 42 // Add a basic block to the function. As before, it automatically inserts 43 // because of the last argument. 44 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F); 45 46 // Get pointers to the constant `1'. 47 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1); 48 49 // Get pointers to the integer argument of the add1 function... 50 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg 51 Argument *ArgX = Add1F->arg_begin(); // Get the arg 52 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 53 54 // Create the add instruction, inserting it into the end of BB. 55 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB); 56 57 // Create the return instruction and add it to the basic block 58 ReturnInst::Create(M->getContext(), Add, BB); 59 60 // Now, function add1 is ready. 61 return Add1F; 62 } 63 64 static Function *CreateFibFunction(Module *M) { 65 // Create the fib function and insert it into module M. This function is said 66 // to return an int and take an int parameter. 67 Function *FibF = 68 cast<Function>(M->getOrInsertFunction("fib", 69 Type::getInt32Ty(M->getContext()), 70 Type::getInt32Ty(M->getContext()), 71 (Type *)0)); 72 73 // Add a basic block to the function. 74 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF); 75 76 // Get pointers to the constants. 77 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1); 78 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2); 79 80 // Get pointer to the integer argument of the add1 function... 81 Argument *ArgX = FibF->arg_begin(); // Get the arg. 82 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 83 84 // Create the true_block. 85 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF); 86 // Create an exit block. 87 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF); 88 89 // Create the "if (arg < 2) goto exitbb" 90 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond"); 91 BranchInst::Create(RetBB, RecurseBB, CondInst, BB); 92 93 // Create: ret int 1 94 ReturnInst::Create(M->getContext(), One, RetBB); 95 96 // create fib(x-1) 97 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB); 98 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB); 99 100 // create fib(x-2) 101 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB); 102 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB); 103 104 // fib(x-1)+fib(x-2) 105 Value *Sum = 106 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB); 107 108 // Create the return instruction and add it to the basic block 109 ReturnInst::Create(M->getContext(), Sum, RecurseBB); 110 111 return FibF; 112 } 113 114 struct threadParams { 115 ExecutionEngine* EE; 116 Function* F; 117 int value; 118 }; 119 120 // We block the subthreads just before they begin to execute: 121 // we want all of them to call into the JIT at the same time, 122 // to verify that the locking is working correctly. 123 class WaitForThreads 124 { 125 public: 126 WaitForThreads() 127 { 128 n = 0; 129 waitFor = 0; 130 131 int result = pthread_cond_init( &condition, NULL ); 132 assert( result == 0 ); 133 134 result = pthread_mutex_init( &mutex, NULL ); 135 assert( result == 0 ); 136 } 137 138 ~WaitForThreads() 139 { 140 int result = pthread_cond_destroy( &condition ); 141 (void)result; 142 assert( result == 0 ); 143 144 result = pthread_mutex_destroy( &mutex ); 145 assert( result == 0 ); 146 } 147 148 // All threads will stop here until another thread calls releaseThreads 149 void block() 150 { 151 int result = pthread_mutex_lock( &mutex ); 152 (void)result; 153 assert( result == 0 ); 154 n ++; 155 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl; 156 157 assert( waitFor == 0 || n <= waitFor ); 158 if ( waitFor > 0 && n == waitFor ) 159 { 160 // There are enough threads blocked that we can release all of them 161 std::cout << "Unblocking threads from block()" << std::endl; 162 unblockThreads(); 163 } 164 else 165 { 166 // We just need to wait until someone unblocks us 167 result = pthread_cond_wait( &condition, &mutex ); 168 assert( result == 0 ); 169 } 170 171 // unlock the mutex before returning 172 result = pthread_mutex_unlock( &mutex ); 173 assert( result == 0 ); 174 } 175 176 // If there are num or more threads blocked, it will signal them all 177 // Otherwise, this thread blocks until there are enough OTHER threads 178 // blocked 179 void releaseThreads( size_t num ) 180 { 181 int result = pthread_mutex_lock( &mutex ); 182 (void)result; 183 assert( result == 0 ); 184 185 if ( n >= num ) { 186 std::cout << "Unblocking threads from releaseThreads()" << std::endl; 187 unblockThreads(); 188 } 189 else 190 { 191 waitFor = num; 192 pthread_cond_wait( &condition, &mutex ); 193 } 194 195 // unlock the mutex before returning 196 result = pthread_mutex_unlock( &mutex ); 197 assert( result == 0 ); 198 } 199 200 private: 201 void unblockThreads() 202 { 203 // Reset the counters to zero: this way, if any new threads 204 // enter while threads are exiting, they will block instead 205 // of triggering a new release of threads 206 n = 0; 207 208 // Reset waitFor to zero: this way, if waitFor threads enter 209 // while threads are exiting, they will block instead of 210 // triggering a new release of threads 211 waitFor = 0; 212 213 int result = pthread_cond_broadcast( &condition ); 214 (void)result; 215 assert(result == 0); 216 } 217 218 size_t n; 219 size_t waitFor; 220 pthread_cond_t condition; 221 pthread_mutex_t mutex; 222 }; 223 224 static WaitForThreads synchronize; 225 226 void* callFunc( void* param ) 227 { 228 struct threadParams* p = (struct threadParams*) param; 229 230 // Call the `foo' function with no arguments: 231 std::vector<GenericValue> Args(1); 232 Args[0].IntVal = APInt(32, p->value); 233 234 synchronize.block(); // wait until other threads are at this point 235 GenericValue gv = p->EE->runFunction(p->F, Args); 236 237 return (void*)(intptr_t)gv.IntVal.getZExtValue(); 238 } 239 240 int main() { 241 InitializeNativeTarget(); 242 LLVMContext Context; 243 244 // Create some module to put our function into it. 245 std::unique_ptr<Module> Owner = make_unique<Module>("test", Context); 246 Module *M = Owner.get(); 247 248 Function* add1F = createAdd1( M ); 249 Function* fibF = CreateFibFunction( M ); 250 251 // Now we create the JIT. 252 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create(); 253 254 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M; 255 //~ std::cout << "\n\nRunning foo: " << std::flush; 256 257 // Create one thread for add1 and two threads for fib 258 struct threadParams add1 = { EE, add1F, 1000 }; 259 struct threadParams fib1 = { EE, fibF, 39 }; 260 struct threadParams fib2 = { EE, fibF, 42 }; 261 262 pthread_t add1Thread; 263 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 ); 264 if ( result != 0 ) { 265 std::cerr << "Could not create thread" << std::endl; 266 return 1; 267 } 268 269 pthread_t fibThread1; 270 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 ); 271 if ( result != 0 ) { 272 std::cerr << "Could not create thread" << std::endl; 273 return 1; 274 } 275 276 pthread_t fibThread2; 277 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 ); 278 if ( result != 0 ) { 279 std::cerr << "Could not create thread" << std::endl; 280 return 1; 281 } 282 283 synchronize.releaseThreads(3); // wait until other threads are at this point 284 285 void* returnValue; 286 result = pthread_join( add1Thread, &returnValue ); 287 if ( result != 0 ) { 288 std::cerr << "Could not join thread" << std::endl; 289 return 1; 290 } 291 std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl; 292 293 result = pthread_join( fibThread1, &returnValue ); 294 if ( result != 0 ) { 295 std::cerr << "Could not join thread" << std::endl; 296 return 1; 297 } 298 std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl; 299 300 result = pthread_join( fibThread2, &returnValue ); 301 if ( result != 0 ) { 302 std::cerr << "Could not join thread" << std::endl; 303 return 1; 304 } 305 std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl; 306 307 return 0; 308 } 309