Home | History | Annotate | Download | only in ParallelJIT
      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 //===----------------------------------------------------------------------===//
     21 
     22 #include "llvm/ADT/APInt.h"
     23 #include "llvm/ADT/STLExtras.h"
     24 #include "llvm/ExecutionEngine/ExecutionEngine.h"
     25 #include "llvm/ExecutionEngine/GenericValue.h"
     26 #include "llvm/IR/Argument.h"
     27 #include "llvm/IR/BasicBlock.h"
     28 #include "llvm/IR/Constants.h"
     29 #include "llvm/IR/DerivedTypes.h"
     30 #include "llvm/IR/Function.h"
     31 #include "llvm/IR/InstrTypes.h"
     32 #include "llvm/IR/Instruction.h"
     33 #include "llvm/IR/Instructions.h"
     34 #include "llvm/IR/LLVMContext.h"
     35 #include "llvm/IR/Module.h"
     36 #include "llvm/IR/Type.h"
     37 #include "llvm/Support/Casting.h"
     38 #include "llvm/Support/TargetSelect.h"
     39 #include <algorithm>
     40 #include <cassert>
     41 #include <cstddef>
     42 #include <cstdint>
     43 #include <iostream>
     44 #include <memory>
     45 #include <vector>
     46 #include <pthread.h>
     47 
     48 using namespace llvm;
     49 
     50 static Function* createAdd1(Module *M) {
     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".
     53   // The '0' terminates the list of argument types.
     54   Function *Add1F =
     55     cast<Function>(M->getOrInsertFunction("add1",
     56                                           Type::getInt32Ty(M->getContext()),
     57                                           Type::getInt32Ty(M->getContext())));
     58 
     59   // Add a basic block to the function. As before, it automatically inserts
     60   // because of the last argument.
     61   BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
     62 
     63   // Get pointers to the constant `1'.
     64   Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
     65 
     66   // Get pointers to the integer argument of the add1 function...
     67   assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
     68   Argument *ArgX = &*Add1F->arg_begin();          // Get the arg
     69   ArgX->setName("AnArg");            // Give it a nice symbolic name for fun.
     70 
     71   // Create the add instruction, inserting it into the end of BB.
     72   Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
     73 
     74   // Create the return instruction and add it to the basic block
     75   ReturnInst::Create(M->getContext(), Add, BB);
     76 
     77   // Now, function add1 is ready.
     78   return Add1F;
     79 }
     80 
     81 static Function *CreateFibFunction(Module *M) {
     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.
     84   Function *FibF =
     85     cast<Function>(M->getOrInsertFunction("fib",
     86                                           Type::getInt32Ty(M->getContext()),
     87                                           Type::getInt32Ty(M->getContext())));
     88 
     89   // Add a basic block to the function.
     90   BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
     91 
     92   // Get pointers to the constants.
     93   Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
     94   Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
     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.
    101   BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
    102   // Create an exit block.
    103   BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
    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
    110   ReturnInst::Create(M->getContext(), One, RetBB);
    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
    125   ReturnInst::Create(M->getContext(), Sum, RecurseBB);
    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 }
    326