16855097cSReid Spencer //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===// 26855097cSReid Spencer // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 66855097cSReid Spencer // 76855097cSReid Spencer //===----------------------------------------------------------------------===// 86855097cSReid Spencer // 96855097cSReid Spencer // Parallel JIT 106855097cSReid Spencer // 116855097cSReid Spencer // This test program creates two LLVM functions then calls them from three 126855097cSReid Spencer // separate threads. It requires the pthreads library. 136855097cSReid Spencer // The three threads are created and then block waiting on a condition variable. 146855097cSReid Spencer // Once all threads are blocked on the conditional variable, the main thread 156855097cSReid Spencer // wakes them up. This complicated work is performed so that all three threads 166855097cSReid Spencer // call into the JIT at the same time (or the best possible approximation of the 176855097cSReid Spencer // same time). This test had assertion errors until I got the locking right. 182b8e4170SEugene Zelenko // 192b8e4170SEugene Zelenko //===----------------------------------------------------------------------===// 206855097cSReid Spencer 212b8e4170SEugene Zelenko #include "llvm/ADT/APInt.h" 2285c9bacaSNAKAMURA Takumi #include "llvm/ADT/STLExtras.h" 232b8e4170SEugene Zelenko #include "llvm/ExecutionEngine/ExecutionEngine.h" 246855097cSReid Spencer #include "llvm/ExecutionEngine/GenericValue.h" 252b8e4170SEugene Zelenko #include "llvm/IR/Argument.h" 262b8e4170SEugene Zelenko #include "llvm/IR/BasicBlock.h" 27005f27a0SChandler Carruth #include "llvm/IR/Constants.h" 28005f27a0SChandler Carruth #include "llvm/IR/DerivedTypes.h" 292b8e4170SEugene Zelenko #include "llvm/IR/Function.h" 302b8e4170SEugene Zelenko #include "llvm/IR/InstrTypes.h" 312b8e4170SEugene Zelenko #include "llvm/IR/Instruction.h" 32005f27a0SChandler Carruth #include "llvm/IR/Instructions.h" 33005f27a0SChandler Carruth #include "llvm/IR/LLVMContext.h" 34005f27a0SChandler Carruth #include "llvm/IR/Module.h" 352b8e4170SEugene Zelenko #include "llvm/IR/Type.h" 362b8e4170SEugene Zelenko #include "llvm/Support/Casting.h" 372bb40357SEvan Cheng #include "llvm/Support/TargetSelect.h" 382b8e4170SEugene Zelenko #include <algorithm> 392b8e4170SEugene Zelenko #include <cassert> 402b8e4170SEugene Zelenko #include <cstddef> 412b8e4170SEugene Zelenko #include <cstdint> 426855097cSReid Spencer #include <iostream> 432b8e4170SEugene Zelenko #include <memory> 442b8e4170SEugene Zelenko #include <vector> 45605e30e9SChandler Carruth #include <pthread.h> 46cc9deb48SHans Wennborg 476855097cSReid Spencer using namespace llvm; 486855097cSReid Spencer 49b800b392SChris Lattner static Function* createAdd1(Module *M) { 50473e3420SJames Y Knight LLVMContext &Context = M->getContext(); 516855097cSReid Spencer // Create the add1 function entry and insert this entry into module M. The 526855097cSReid Spencer // function will have a return type of "int" and take an argument of "int". 53b800b392SChris Lattner Function *Add1F = 5413680223SJames Y Knight Function::Create(FunctionType::get(Type::getInt32Ty(Context), 5513680223SJames Y Knight {Type::getInt32Ty(Context)}, false), 5613680223SJames Y Knight Function::ExternalLinkage, "add1", M); 576855097cSReid Spencer 586855097cSReid Spencer // Add a basic block to the function. As before, it automatically inserts 596855097cSReid Spencer // because of the last argument. 60473e3420SJames Y Knight BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F); 616855097cSReid Spencer 626855097cSReid Spencer // Get pointers to the constant `1'. 63473e3420SJames Y Knight Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1); 646855097cSReid Spencer 656855097cSReid Spencer // Get pointers to the integer argument of the add1 function... 666855097cSReid Spencer assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg 675717ecbaSDuncan P. N. Exon Smith Argument *ArgX = &*Add1F->arg_begin(); // Get the arg 686855097cSReid Spencer ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 696855097cSReid Spencer 706855097cSReid Spencer // Create the add instruction, inserting it into the end of BB. 71e1f6e4b2SGabor Greif Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB); 726855097cSReid Spencer 736855097cSReid Spencer // Create the return instruction and add it to the basic block 74473e3420SJames Y Knight ReturnInst::Create(Context, Add, BB); 756855097cSReid Spencer 766855097cSReid Spencer // Now, function add1 is ready. 776855097cSReid Spencer return Add1F; 786855097cSReid Spencer } 796855097cSReid Spencer 80b800b392SChris Lattner static Function *CreateFibFunction(Module *M) { 81473e3420SJames Y Knight LLVMContext &Context = M->getContext(); 826855097cSReid Spencer // Create the fib function and insert it into module M. This function is said 836855097cSReid Spencer // to return an int and take an int parameter. 8413680223SJames Y Knight FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context), 8513680223SJames Y Knight {Type::getInt32Ty(Context)}, false); 86b800b392SChris Lattner Function *FibF = 8713680223SJames Y Knight Function::Create(FibFTy, Function::ExternalLinkage, "fib", M); 886855097cSReid Spencer 896855097cSReid Spencer // Add a basic block to the function. 90473e3420SJames Y Knight BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF); 916855097cSReid Spencer 926855097cSReid Spencer // Get pointers to the constants. 93473e3420SJames Y Knight Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1); 94473e3420SJames Y Knight Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2); 956855097cSReid Spencer 966855097cSReid Spencer // Get pointer to the integer argument of the add1 function... 975717ecbaSDuncan P. N. Exon Smith Argument *ArgX = &*FibF->arg_begin(); // Get the arg. 986855097cSReid Spencer ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. 996855097cSReid Spencer 1006855097cSReid Spencer // Create the true_block. 101473e3420SJames Y Knight BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF); 1026855097cSReid Spencer // Create an exit block. 103473e3420SJames Y Knight BasicBlock *RecurseBB = BasicBlock::Create(Context, "recurse", FibF); 1046855097cSReid Spencer 1056855097cSReid Spencer // Create the "if (arg < 2) goto exitbb" 1061e5f00e7SOwen Anderson Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond"); 107e9ecc68dSGabor Greif BranchInst::Create(RetBB, RecurseBB, CondInst, BB); 1086855097cSReid Spencer 1096855097cSReid Spencer // Create: ret int 1 110473e3420SJames Y Knight ReturnInst::Create(Context, One, RetBB); 1116855097cSReid Spencer 1126855097cSReid Spencer // create fib(x-1) 113e1f6e4b2SGabor Greif Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB); 114e9ecc68dSGabor Greif Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB); 1156855097cSReid Spencer 1166855097cSReid Spencer // create fib(x-2) 117e1f6e4b2SGabor Greif Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB); 118e9ecc68dSGabor Greif Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB); 1196855097cSReid Spencer 1206855097cSReid Spencer // fib(x-1)+fib(x-2) 1216855097cSReid Spencer Value *Sum = 122e1f6e4b2SGabor Greif BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB); 1236855097cSReid Spencer 1246855097cSReid Spencer // Create the return instruction and add it to the basic block 125473e3420SJames Y Knight ReturnInst::Create(Context, Sum, RecurseBB); 1266855097cSReid Spencer 1276855097cSReid Spencer return FibF; 1286855097cSReid Spencer } 1296855097cSReid Spencer 1306855097cSReid Spencer struct threadParams { 1316855097cSReid Spencer ExecutionEngine* EE; 1326855097cSReid Spencer Function* F; 1336855097cSReid Spencer int value; 1346855097cSReid Spencer }; 1356855097cSReid Spencer 1366855097cSReid Spencer // We block the subthreads just before they begin to execute: 1376855097cSReid Spencer // we want all of them to call into the JIT at the same time, 1386855097cSReid Spencer // to verify that the locking is working correctly. 1396855097cSReid Spencer class WaitForThreads 1406855097cSReid Spencer { 1416855097cSReid Spencer public: 1426855097cSReid Spencer WaitForThreads() 1436855097cSReid Spencer { 1446855097cSReid Spencer n = 0; 1456855097cSReid Spencer waitFor = 0; 1466855097cSReid Spencer 147cc9deb48SHans Wennborg int result = pthread_cond_init( &condition, nullptr ); 148e7a411dfSHans Wennborg (void)result; 1496855097cSReid Spencer assert( result == 0 ); 1506855097cSReid Spencer 151cc9deb48SHans Wennborg result = pthread_mutex_init( &mutex, nullptr ); 1526855097cSReid Spencer assert( result == 0 ); 1536855097cSReid Spencer } 1546855097cSReid Spencer 1556855097cSReid Spencer ~WaitForThreads() 1566855097cSReid Spencer { 1576855097cSReid Spencer int result = pthread_cond_destroy( &condition ); 158de14dcc2SAhmed Charles (void)result; 1596855097cSReid Spencer assert( result == 0 ); 1606855097cSReid Spencer 1616855097cSReid Spencer result = pthread_mutex_destroy( &mutex ); 1626855097cSReid Spencer assert( result == 0 ); 1636855097cSReid Spencer } 1646855097cSReid Spencer 1656855097cSReid Spencer // All threads will stop here until another thread calls releaseThreads 1666855097cSReid Spencer void block() 1676855097cSReid Spencer { 1686855097cSReid Spencer int result = pthread_mutex_lock( &mutex ); 169de14dcc2SAhmed Charles (void)result; 1706855097cSReid Spencer assert( result == 0 ); 1716855097cSReid Spencer n ++; 1726855097cSReid Spencer //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl; 1736855097cSReid Spencer 1746855097cSReid Spencer assert( waitFor == 0 || n <= waitFor ); 1756855097cSReid Spencer if ( waitFor > 0 && n == waitFor ) 1766855097cSReid Spencer { 1776855097cSReid Spencer // There are enough threads blocked that we can release all of them 1786855097cSReid Spencer std::cout << "Unblocking threads from block()" << std::endl; 1796855097cSReid Spencer unblockThreads(); 1806855097cSReid Spencer } 1816855097cSReid Spencer else 1826855097cSReid Spencer { 1836855097cSReid Spencer // We just need to wait until someone unblocks us 1846855097cSReid Spencer result = pthread_cond_wait( &condition, &mutex ); 1856855097cSReid Spencer assert( result == 0 ); 1866855097cSReid Spencer } 1876855097cSReid Spencer 1886855097cSReid Spencer // unlock the mutex before returning 1896855097cSReid Spencer result = pthread_mutex_unlock( &mutex ); 1906855097cSReid Spencer assert( result == 0 ); 1916855097cSReid Spencer } 1926855097cSReid Spencer 1936855097cSReid Spencer // If there are num or more threads blocked, it will signal them all 1946855097cSReid Spencer // Otherwise, this thread blocks until there are enough OTHER threads 1956855097cSReid Spencer // blocked 1966855097cSReid Spencer void releaseThreads( size_t num ) 1976855097cSReid Spencer { 1986855097cSReid Spencer int result = pthread_mutex_lock( &mutex ); 199de14dcc2SAhmed Charles (void)result; 2006855097cSReid Spencer assert( result == 0 ); 2016855097cSReid Spencer 2026855097cSReid Spencer if ( n >= num ) { 2036855097cSReid Spencer std::cout << "Unblocking threads from releaseThreads()" << std::endl; 2046855097cSReid Spencer unblockThreads(); 2056855097cSReid Spencer } 2066855097cSReid Spencer else 2076855097cSReid Spencer { 2086855097cSReid Spencer waitFor = num; 2096855097cSReid Spencer pthread_cond_wait( &condition, &mutex ); 2106855097cSReid Spencer } 2116855097cSReid Spencer 2126855097cSReid Spencer // unlock the mutex before returning 2136855097cSReid Spencer result = pthread_mutex_unlock( &mutex ); 2146855097cSReid Spencer assert( result == 0 ); 2156855097cSReid Spencer } 2166855097cSReid Spencer 2176855097cSReid Spencer private: 2186855097cSReid Spencer void unblockThreads() 2196855097cSReid Spencer { 2206855097cSReid Spencer // Reset the counters to zero: this way, if any new threads 2216855097cSReid Spencer // enter while threads are exiting, they will block instead 2226855097cSReid Spencer // of triggering a new release of threads 2236855097cSReid Spencer n = 0; 2246855097cSReid Spencer 2256855097cSReid Spencer // Reset waitFor to zero: this way, if waitFor threads enter 2266855097cSReid Spencer // while threads are exiting, they will block instead of 2276855097cSReid Spencer // triggering a new release of threads 2286855097cSReid Spencer waitFor = 0; 2296855097cSReid Spencer 2306855097cSReid Spencer int result = pthread_cond_broadcast( &condition ); 2313bb7d41aSChandler Carruth (void)result; 2323bb7d41aSChandler Carruth assert(result == 0); 2336855097cSReid Spencer } 2346855097cSReid Spencer 2356855097cSReid Spencer size_t n; 2366855097cSReid Spencer size_t waitFor; 2376855097cSReid Spencer pthread_cond_t condition; 2386855097cSReid Spencer pthread_mutex_t mutex; 2396855097cSReid Spencer }; 2406855097cSReid Spencer 2416855097cSReid Spencer static WaitForThreads synchronize; 2426855097cSReid Spencer 2436855097cSReid Spencer void* callFunc( void* param ) 2446855097cSReid Spencer { 2456855097cSReid Spencer struct threadParams* p = (struct threadParams*) param; 2466855097cSReid Spencer 2476855097cSReid Spencer // Call the `foo' function with no arguments: 2486855097cSReid Spencer std::vector<GenericValue> Args(1); 2498a9ae48dSReid Spencer Args[0].IntVal = APInt(32, p->value); 2506855097cSReid Spencer 2516855097cSReid Spencer synchronize.block(); // wait until other threads are at this point 2526855097cSReid Spencer GenericValue gv = p->EE->runFunction(p->F, Args); 2536855097cSReid Spencer 2548a9ae48dSReid Spencer return (void*)(intptr_t)gv.IntVal.getZExtValue(); 2556855097cSReid Spencer } 2566855097cSReid Spencer 257d24df245SChris Lattner int main() { 258d24df245SChris Lattner InitializeNativeTarget(); 2596773d388SOwen Anderson LLVMContext Context; 260d24df245SChris Lattner 2616855097cSReid Spencer // Create some module to put our function into it. 262*0eaee545SJonas Devlieghere std::unique_ptr<Module> Owner = std::make_unique<Module>("test", Context); 2632a8a2795SRafael Espindola Module *M = Owner.get(); 2646855097cSReid Spencer 2656855097cSReid Spencer Function* add1F = createAdd1( M ); 2666855097cSReid Spencer Function* fibF = CreateFibFunction( M ); 2676855097cSReid Spencer 2686855097cSReid Spencer // Now we create the JIT. 2692a8a2795SRafael Espindola ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create(); 2706855097cSReid Spencer 2716855097cSReid Spencer //~ std::cout << "We just constructed this LLVM module:\n\n" << *M; 2726855097cSReid Spencer //~ std::cout << "\n\nRunning foo: " << std::flush; 2736855097cSReid Spencer 2746855097cSReid Spencer // Create one thread for add1 and two threads for fib 2756855097cSReid Spencer struct threadParams add1 = { EE, add1F, 1000 }; 2766855097cSReid Spencer struct threadParams fib1 = { EE, fibF, 39 }; 2776855097cSReid Spencer struct threadParams fib2 = { EE, fibF, 42 }; 2786855097cSReid Spencer 2796855097cSReid Spencer pthread_t add1Thread; 280cc9deb48SHans Wennborg int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 ); 2816855097cSReid Spencer if ( result != 0 ) { 2826855097cSReid Spencer std::cerr << "Could not create thread" << std::endl; 2836855097cSReid Spencer return 1; 2846855097cSReid Spencer } 2856855097cSReid Spencer 2866855097cSReid Spencer pthread_t fibThread1; 287cc9deb48SHans Wennborg result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 ); 2886855097cSReid Spencer if ( result != 0 ) { 2896855097cSReid Spencer std::cerr << "Could not create thread" << std::endl; 2906855097cSReid Spencer return 1; 2916855097cSReid Spencer } 2926855097cSReid Spencer 2936855097cSReid Spencer pthread_t fibThread2; 294cc9deb48SHans Wennborg result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 ); 2956855097cSReid Spencer if ( result != 0 ) { 2966855097cSReid Spencer std::cerr << "Could not create thread" << std::endl; 2976855097cSReid Spencer return 1; 2986855097cSReid Spencer } 2996855097cSReid Spencer 3006855097cSReid Spencer synchronize.releaseThreads(3); // wait until other threads are at this point 3016855097cSReid Spencer 3026855097cSReid Spencer void* returnValue; 3036855097cSReid Spencer result = pthread_join( add1Thread, &returnValue ); 3046855097cSReid Spencer if ( result != 0 ) { 3056855097cSReid Spencer std::cerr << "Could not join thread" << std::endl; 3066855097cSReid Spencer return 1; 3076855097cSReid Spencer } 3088640f2bdSReid Spencer std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl; 3096855097cSReid Spencer 3106855097cSReid Spencer result = pthread_join( fibThread1, &returnValue ); 3116855097cSReid Spencer if ( result != 0 ) { 3126855097cSReid Spencer std::cerr << "Could not join thread" << std::endl; 3136855097cSReid Spencer return 1; 3146855097cSReid Spencer } 3158640f2bdSReid Spencer std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl; 3166855097cSReid Spencer 3176855097cSReid Spencer result = pthread_join( fibThread2, &returnValue ); 3186855097cSReid Spencer if ( result != 0 ) { 3196855097cSReid Spencer std::cerr << "Could not join thread" << std::endl; 3206855097cSReid Spencer return 1; 3216855097cSReid Spencer } 3228640f2bdSReid Spencer std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl; 3236855097cSReid Spencer 3246855097cSReid Spencer return 0; 3256855097cSReid Spencer } 326