Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===// |
| 2 | // |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Parallel JIT |
| 10 | // |
| 11 | // This test program creates two LLVM functions then calls them from three |
| 12 | // separate threads. It requires the pthreads library. |
| 13 | // The three threads are created and then block waiting on a condition variable. |
| 14 | // Once all threads are blocked on the conditional variable, the main thread |
| 15 | // wakes them up. This complicated work is performed so that all three threads |
| 16 | // call into the JIT at the same time (or the best possible approximation of the |
| 17 | // same time). This test had assertion errors until I got the locking right. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/ADT/APInt.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 24 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 25 | #include "llvm/IR/Argument.h" |
| 26 | #include "llvm/IR/BasicBlock.h" |
| 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
| 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/IR/InstrTypes.h" |
| 31 | #include "llvm/IR/Instruction.h" |
| 32 | #include "llvm/IR/Instructions.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/Module.h" |
| 35 | #include "llvm/IR/Type.h" |
| 36 | #include "llvm/Support/Casting.h" |
| 37 | #include "llvm/Support/TargetSelect.h" |
| 38 | #include <algorithm> |
| 39 | #include <cassert> |
| 40 | #include <cstddef> |
| 41 | #include <cstdint> |
| 42 | #include <iostream> |
| 43 | #include <memory> |
| 44 | #include <vector> |
| 45 | #include <pthread.h> |
| 46 | |
| 47 | using namespace llvm; |
| 48 | |
| 49 | static Function* createAdd1(Module *M) { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 50 | LLVMContext &Context = M->getContext(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 51 | // 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". |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 53 | Function *Add1F = |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 54 | Function::Create(FunctionType::get(Type::getInt32Ty(Context), |
| 55 | {Type::getInt32Ty(Context)}, false), |
| 56 | Function::ExternalLinkage, "add1", M); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 57 | |
| 58 | // Add a basic block to the function. As before, it automatically inserts |
| 59 | // because of the last argument. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 60 | BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 61 | |
| 62 | // Get pointers to the constant `1'. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 63 | Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 64 | |
| 65 | // Get pointers to the integer argument of the add1 function... |
| 66 | assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg |
| 67 | Argument *ArgX = &*Add1F->arg_begin(); // Get the arg |
| 68 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
| 69 | |
| 70 | // Create the add instruction, inserting it into the end of BB. |
| 71 | Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB); |
| 72 | |
| 73 | // Create the return instruction and add it to the basic block |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 74 | ReturnInst::Create(Context, Add, BB); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 75 | |
| 76 | // Now, function add1 is ready. |
| 77 | return Add1F; |
| 78 | } |
| 79 | |
| 80 | static Function *CreateFibFunction(Module *M) { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 81 | LLVMContext &Context = M->getContext(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 82 | // Create the fib function and insert it into module M. This function is said |
| 83 | // to return an int and take an int parameter. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 84 | FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context), |
| 85 | {Type::getInt32Ty(Context)}, false); |
| 86 | Function *FibF = |
| 87 | Function::Create(FibFTy, Function::ExternalLinkage, "fib", M); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 88 | |
| 89 | // Add a basic block to the function. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 90 | BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 91 | |
| 92 | // Get pointers to the constants. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 93 | Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1); |
| 94 | Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 95 | |
| 96 | // Get pointer to the integer argument of the add1 function... |
| 97 | Argument *ArgX = &*FibF->arg_begin(); // Get the arg. |
| 98 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
| 99 | |
| 100 | // Create the true_block. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 101 | BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 102 | // Create an exit block. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 103 | BasicBlock *RecurseBB = BasicBlock::Create(Context, "recurse", FibF); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 104 | |
| 105 | // Create the "if (arg < 2) goto exitbb" |
| 106 | Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond"); |
| 107 | BranchInst::Create(RetBB, RecurseBB, CondInst, BB); |
| 108 | |
| 109 | // Create: ret int 1 |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 110 | ReturnInst::Create(Context, One, RetBB); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 111 | |
| 112 | // create fib(x-1) |
| 113 | Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB); |
| 114 | Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB); |
| 115 | |
| 116 | // create fib(x-2) |
| 117 | Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB); |
| 118 | Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB); |
| 119 | |
| 120 | // fib(x-1)+fib(x-2) |
| 121 | Value *Sum = |
| 122 | BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB); |
| 123 | |
| 124 | // Create the return instruction and add it to the basic block |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 125 | ReturnInst::Create(Context, Sum, RecurseBB); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 126 | |
| 127 | return FibF; |
| 128 | } |
| 129 | |
| 130 | struct 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. |
| 139 | class WaitForThreads |
| 140 | { |
| 141 | public: |
| 142 | WaitForThreads() |
| 143 | { |
| 144 | n = 0; |
| 145 | waitFor = 0; |
| 146 | |
| 147 | int result = pthread_cond_init( &condition, nullptr ); |
| 148 | (void)result; |
| 149 | assert( result == 0 ); |
| 150 | |
| 151 | result = pthread_mutex_init( &mutex, nullptr ); |
| 152 | assert( result == 0 ); |
| 153 | } |
| 154 | |
| 155 | ~WaitForThreads() |
| 156 | { |
| 157 | int result = pthread_cond_destroy( &condition ); |
| 158 | (void)result; |
| 159 | assert( result == 0 ); |
| 160 | |
| 161 | result = pthread_mutex_destroy( &mutex ); |
| 162 | assert( result == 0 ); |
| 163 | } |
| 164 | |
| 165 | // All threads will stop here until another thread calls releaseThreads |
| 166 | void block() |
| 167 | { |
| 168 | int result = pthread_mutex_lock( &mutex ); |
| 169 | (void)result; |
| 170 | assert( result == 0 ); |
| 171 | n ++; |
| 172 | //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl; |
| 173 | |
| 174 | assert( waitFor == 0 || n <= waitFor ); |
| 175 | if ( waitFor > 0 && n == waitFor ) |
| 176 | { |
| 177 | // There are enough threads blocked that we can release all of them |
| 178 | std::cout << "Unblocking threads from block()" << std::endl; |
| 179 | unblockThreads(); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | // We just need to wait until someone unblocks us |
| 184 | result = pthread_cond_wait( &condition, &mutex ); |
| 185 | assert( result == 0 ); |
| 186 | } |
| 187 | |
| 188 | // unlock the mutex before returning |
| 189 | result = pthread_mutex_unlock( &mutex ); |
| 190 | assert( result == 0 ); |
| 191 | } |
| 192 | |
| 193 | // 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 ); |
| 199 | (void)result; |
| 200 | assert( result == 0 ); |
| 201 | |
| 202 | if ( n >= num ) { |
| 203 | std::cout << "Unblocking threads from releaseThreads()" << std::endl; |
| 204 | unblockThreads(); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | waitFor = num; |
| 209 | pthread_cond_wait( &condition, &mutex ); |
| 210 | } |
| 211 | |
| 212 | // unlock the mutex before returning |
| 213 | result = pthread_mutex_unlock( &mutex ); |
| 214 | assert( result == 0 ); |
| 215 | } |
| 216 | |
| 217 | private: |
| 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; |
| 224 | |
| 225 | // 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 ); |
| 231 | (void)result; |
| 232 | assert(result == 0); |
| 233 | } |
| 234 | |
| 235 | size_t n; |
| 236 | size_t waitFor; |
| 237 | pthread_cond_t condition; |
| 238 | pthread_mutex_t mutex; |
| 239 | }; |
| 240 | |
| 241 | static WaitForThreads synchronize; |
| 242 | |
| 243 | void* callFunc( void* param ) |
| 244 | { |
| 245 | struct threadParams* p = (struct threadParams*) param; |
| 246 | |
| 247 | // Call the `foo' function with no arguments: |
| 248 | std::vector<GenericValue> Args(1); |
| 249 | Args[0].IntVal = APInt(32, p->value); |
| 250 | |
| 251 | synchronize.block(); // wait until other threads are at this point |
| 252 | GenericValue gv = p->EE->runFunction(p->F, Args); |
| 253 | |
| 254 | return (void*)(intptr_t)gv.IntVal.getZExtValue(); |
| 255 | } |
| 256 | |
| 257 | int main() { |
| 258 | InitializeNativeTarget(); |
| 259 | LLVMContext Context; |
| 260 | |
| 261 | // Create some module to put our function into it. |
| 262 | std::unique_ptr<Module> Owner = make_unique<Module>("test", Context); |
| 263 | Module *M = Owner.get(); |
| 264 | |
| 265 | Function* add1F = createAdd1( M ); |
| 266 | Function* fibF = CreateFibFunction( M ); |
| 267 | |
| 268 | // Now we create the JIT. |
| 269 | ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create(); |
| 270 | |
| 271 | //~ std::cout << "We just constructed this LLVM module:\n\n" << *M; |
| 272 | //~ std::cout << "\n\nRunning foo: " << std::flush; |
| 273 | |
| 274 | // 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 }; |
| 278 | |
| 279 | pthread_t add1Thread; |
| 280 | int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 ); |
| 281 | if ( result != 0 ) { |
| 282 | std::cerr << "Could not create thread" << std::endl; |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | pthread_t fibThread1; |
| 287 | result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 ); |
| 288 | if ( result != 0 ) { |
| 289 | std::cerr << "Could not create thread" << std::endl; |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | pthread_t fibThread2; |
| 294 | result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 ); |
| 295 | if ( result != 0 ) { |
| 296 | std::cerr << "Could not create thread" << std::endl; |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | synchronize.releaseThreads(3); // wait until other threads are at this point |
| 301 | |
| 302 | 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 | } |
| 308 | std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl; |
| 309 | |
| 310 | result = pthread_join( fibThread1, &returnValue ); |
| 311 | if ( result != 0 ) { |
| 312 | std::cerr << "Could not join thread" << std::endl; |
| 313 | return 1; |
| 314 | } |
| 315 | std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl; |
| 316 | |
| 317 | result = pthread_join( fibThread2, &returnValue ); |
| 318 | if ( result != 0 ) { |
| 319 | std::cerr << "Could not join thread" << std::endl; |
| 320 | return 1; |
| 321 | } |
| 322 | std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl; |
| 323 | |
| 324 | return 0; |
| 325 | } |