ParallelJIT.cpp 8.9 KB

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