Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- ThreadedFunctionQueue.h - Function work units for threads -*- C++ -*-==// | |
| 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 #ifndef THREADEDFUNCTIONQUEUE_H | |
| 11 #define THREADEDFUNCTIONQUEUE_H | |
| 12 | |
| 13 #include "llvm/IR/Module.h" | |
| 14 | |
| 15 // A "queue" that keeps track of which functions have been assigned to | |
| 16 // threads and which functions have not yet been assigned. It does not | |
| 17 // actually use a queue data structure and instead uses a number which | |
| 18 // tracks the minimum unassigned function ID, expecting each thread | |
| 19 // to have the same view of function IDs. | |
| 20 class ThreadedFunctionQueue { | |
| 21 public: | |
| 22 ThreadedFunctionQueue(Module *mod, unsigned SplitModuleCount) | |
| 23 : SplitModuleCount(SplitModuleCount), | |
| 24 NumFunctions(0), | |
| 25 CurrentFunction(0) { | |
| 26 // This is only the approximate number of functions. | |
| 27 // Some additional declarations will be added by other passes: | |
| 28 // AddPNaClExternalDeclsPass (for setjmp/longjmp and nacl variants), | |
| 29 // and the NaCl Atomics pass adds declarations of NaCl atomic intrinsics. | |
| 30 NumFunctions = mod->getFunctionList().size(); | |
| 31 } | |
| 32 ~ThreadedFunctionQueue() {} | |
| 33 | |
| 34 // Assign functions in a static manner between threads. | |
| 35 bool GrabFunctionStatic(unsigned FuncIndex, unsigned ThreadIndex) const { | |
| 36 return FuncIndex % SplitModuleCount == ThreadIndex; | |
| 37 } | |
| 38 | |
| 39 // Assign functions between threads dynamically. | |
| 40 // Returns true if FuncIndex is unassigned and the calling thread | |
| 41 // is assigned functions [FuncIndex, FuncIndex + ChunkSize). | |
| 42 // Returns false if the calling thread is not assigned functions | |
| 43 // [FuncIndex, FuncIndex + ChunkSize). | |
| 44 // | |
| 45 // NextIndex will be set to the next unassigned function ID, so that | |
| 46 // the calling thread will know which function ID to attempt to grab | |
| 47 // next. Each thread may have a different value for the ideal ChunkSize | |
| 48 // so it is hard to predict the next available function solely based | |
| 49 // on incrementing by ChunkSize. | |
| 50 bool GrabFunctionDynamic(unsigned FuncIndex, unsigned ChunkSize, | |
| 51 unsigned &NextIndex) { | |
| 52 unsigned Cur = CurrentFunction; | |
| 53 if (FuncIndex < Cur) { | |
| 54 NextIndex = Cur; | |
| 55 return false; | |
| 56 } | |
| 57 NextIndex = Cur + ChunkSize; | |
| 58 return __sync_bool_compare_and_swap(&CurrentFunction, Cur, NextIndex); | |
| 59 } | |
| 60 | |
| 61 // Returns a recommended ChunkSize for use in calling GrabFunctionDynamic(). | |
| 62 // ChunkSize starts out "large" to reduce synchronization cost. However, | |
| 63 // it cannot be too large, otherwise it will encompass too many bytes | |
| 64 // and defeats streaming translation. Assigning too many functions to | |
| 65 // a single thread also throws off load balancing, so the ChunkSize is | |
| 66 // reduced when the remaining number of functions is low so that | |
| 67 // load balancing can be achieved near the end. | |
| 68 unsigned RecommendedChunkSize() { | |
|
Jim Stichnoth
2014/03/18 20:49:49
declare as const
jvoung (off chromium)
2014/03/18 22:01:17
Done.
| |
| 69 // Since NumFunctions may be less than the actual number of functions | |
| 70 // (see note in ctor), clamp remaining functions to 1. | |
| 71 unsigned ApproxRemainingFuncs = | |
| 72 std::max((int)(NumFunctions - CurrentFunction), 1); | |
| 73 unsigned DynamicChunkSize = ApproxRemainingFuncs / (SplitModuleCount * 4); | |
| 74 if (DynamicChunkSize > 8U) | |
| 75 return 8U; | |
| 76 else | |
| 77 return std::max(1U, DynamicChunkSize); | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 unsigned SplitModuleCount; | |
|
Jim Stichnoth
2014/03/18 20:49:49
Make SplitModuleCount and NumFunctions const?
jvoung (off chromium)
2014/03/18 22:01:17
Done.
| |
| 82 unsigned NumFunctions; | |
| 83 volatile unsigned CurrentFunction; | |
| 84 }; | |
| 85 | |
| 86 #endif | |
| OLD | NEW |