ParallelJIT.cpp 9.0 KB

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