Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// | 1 //===- subzero/src/IceThreading.h - Threading functions ---------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file declares some utility functions. | 10 // This file defines threading-related functions. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #ifndef SUBZERO_SRC_ICEUTILS_H | 14 #ifndef SUBZERO_SRC_ICETHREADING_H |
| 15 #define SUBZERO_SRC_ICEUTILS_H | 15 #define SUBZERO_SRC_ICETHREADING_H |
| 16 | 16 |
| 17 #include <climits> | |
| 18 #include <condition_variable> | 17 #include <condition_variable> |
| 18 #include <mutex> | |
| 19 | |
| 20 #include "IceDefs.h" | |
| 19 | 21 |
| 20 namespace Ice { | 22 namespace Ice { |
| 21 | 23 |
| 22 // Similar to bit_cast, but allows copying from types of unrelated | |
| 23 // sizes. This method was introduced to enable the strict aliasing | |
| 24 // optimizations of GCC 4.4. Basically, GCC mindlessly relies on | |
| 25 // obscure details in the C++ standard that make reinterpret_cast | |
| 26 // virtually useless. | |
| 27 template <class D, class S> inline D bit_copy(const S &source) { | |
| 28 D destination; | |
| 29 // This use of memcpy is safe: source and destination cannot overlap. | |
| 30 memcpy(&destination, reinterpret_cast<const void *>(&source), | |
| 31 sizeof(destination)); | |
| 32 return destination; | |
| 33 } | |
| 34 | |
| 35 class Utils { | |
| 36 public: | |
| 37 // Check whether an N-bit two's-complement representation can hold value. | |
| 38 template <typename T> static inline bool IsInt(int N, T value) { | |
| 39 assert((0 < N) && | |
| 40 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); | |
| 41 T limit = static_cast<T>(1) << (N - 1); | |
| 42 return (-limit <= value) && (value < limit); | |
| 43 } | |
| 44 | |
| 45 template <typename T> static inline bool IsUint(int N, T value) { | |
| 46 assert((0 < N) && | |
| 47 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); | |
| 48 T limit = static_cast<T>(1) << N; | |
| 49 return (0 <= value) && (value < limit); | |
| 50 } | |
| 51 | |
| 52 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { | |
| 53 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || | |
| 54 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); | |
| 55 } | |
| 56 | |
| 57 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) { | |
| 58 assert(llvm::isPowerOf2_64(Align)); | |
| 59 uint64_t Mod = Pos & (Align - 1); | |
| 60 if (Mod == 0) | |
| 61 return 0; | |
| 62 return Align - Mod; | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 // BoundedProducerConsumerQueue is a work queue that allows multiple | 24 // BoundedProducerConsumerQueue is a work queue that allows multiple |
| 67 // producers and multiple consumers. A producer adds entries using | 25 // producers and multiple consumers. A producer adds entries using |
| 68 // blockingPush(), and may block if the queue is "full". A producer | 26 // blockingPush(), and may block if the queue is "full". A producer |
| 69 // uses notifyEnd() to indicate that no more entries will be added. A | 27 // uses notifyEnd() to indicate that no more entries will be added. A |
| 70 // consumer removes an item using blockingPop(), which will return | 28 // consumer removes an item using blockingPop(), which will return |
| 71 // nullptr if notifyEnd() has been called and the queue is empty (it | 29 // nullptr if notifyEnd() has been called and the queue is empty (it |
| 72 // never returns nullptr if the queue contained any items). | 30 // never returns nullptr if the queue contained any items). |
| 73 // | 31 // |
| 74 // The MaxSize ctor arg controls the maximum size the queue can grow | 32 // The MaxSize ctor arg controls the maximum size the queue can grow |
| 75 // to (subject to a hard limit of MaxStaticSize-1). The Sequential | 33 // to (subject to a hard limit of MaxStaticSize-1). The Sequential |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 89 // MaxStaticSize, where the queue boundaries are denoted by the Front | 47 // MaxStaticSize, where the queue boundaries are denoted by the Front |
| 90 // and Back fields. Front==Back indicates an empty queue. | 48 // and Back fields. Front==Back indicates an empty queue. |
| 91 template <typename T, size_t MaxStaticSize = 128> | 49 template <typename T, size_t MaxStaticSize = 128> |
| 92 class BoundedProducerConsumerQueue { | 50 class BoundedProducerConsumerQueue { |
| 93 BoundedProducerConsumerQueue() = delete; | 51 BoundedProducerConsumerQueue() = delete; |
| 94 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; | 52 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; |
| 95 BoundedProducerConsumerQueue & | 53 BoundedProducerConsumerQueue & |
| 96 operator=(const BoundedProducerConsumerQueue &) = delete; | 54 operator=(const BoundedProducerConsumerQueue &) = delete; |
| 97 | 55 |
| 98 public: | 56 public: |
| 99 BoundedProducerConsumerQueue(size_t MaxSize, bool Sequential) | 57 const static size_t UnlimitedSize = 0; |
| 100 : Back(0), Front(0), MaxSize(std::min(MaxSize, MaxStaticSize)), | 58 BoundedProducerConsumerQueue(bool Sequential, size_t MaxSize = UnlimitedSize) |
| 59 : Back(0), Front(0), | |
| 60 MaxSize(MaxSize == UnlimitedSize ? MaxStaticSize | |
| 61 : std::min(MaxSize, MaxStaticSize)), | |
|
JF
2015/02/08 21:15:04
Why have UnlimitedSize at all when MaxStaticSize w
Jim Stichnoth
2015/02/10 07:51:47
Oh, right! Done.
| |
| 101 Sequential(Sequential), IsEnded(false) {} | 62 Sequential(Sequential), IsEnded(false) {} |
| 102 void blockingPush(T *Item) { | 63 void blockingPush(T *Item) { |
| 103 { | 64 { |
| 104 std::unique_lock<GlobalLockType> L(Lock); | 65 std::unique_lock<GlobalLockType> L(Lock); |
| 105 // If the work queue is already "full", wait for a consumer to | 66 // If the work queue is already "full", wait for a consumer to |
| 106 // grab an element and shrink the queue. | 67 // grab an element and shrink the queue. |
| 107 Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); | 68 Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); |
| 108 push(Item); | 69 push(Item); |
| 109 } | 70 } |
| 110 GrewOrEnded.notify_one(); | 71 GrewOrEnded.notify_one(); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 void push(T *Item) { | 144 void push(T *Item) { |
| 184 WorkItems[Back++ & MaxStaticSizeMask] = Item; | 145 WorkItems[Back++ & MaxStaticSizeMask] = Item; |
| 185 assert(size() <= MaxStaticSize); | 146 assert(size() <= MaxStaticSize); |
| 186 } | 147 } |
| 187 T *pop() { | 148 T *pop() { |
| 188 assert(!empty()); | 149 assert(!empty()); |
| 189 return WorkItems[Front++ & MaxStaticSizeMask]; | 150 return WorkItems[Front++ & MaxStaticSizeMask]; |
| 190 } | 151 } |
| 191 }; | 152 }; |
| 192 | 153 |
| 154 class EmitterWorkItem { | |
|
JF
2015/02/08 21:15:05
Overall this class looks like it wants to be a vir
Jim Stichnoth
2015/02/10 07:51:47
In some world, I would have Cfg, Assembler, and Va
| |
| 155 EmitterWorkItem() = delete; | |
| 156 EmitterWorkItem(const EmitterWorkItem &) = delete; | |
| 157 EmitterWorkItem &operator=(const EmitterWorkItem &) = delete; | |
| 158 | |
| 159 public: | |
| 160 // ItemKind can be one of the following: | |
| 161 // | |
| 162 // WI_Nop: No actual work. This is a placeholder to maintain | |
| 163 // sequence numbers in case there is a translation error. | |
| 164 // | |
| 165 // WI_GlobalInits: A list of global declarations and initializers. | |
| 166 // | |
| 167 // WI_Asm: A function that has already had emitIAS() called on it. | |
| 168 // The work is transferred via the Assembler buffer, and the | |
| 169 // originating Cfg has been deleted (to recover lots of memory). | |
| 170 // | |
| 171 // WI_Cfg: A Cfg that has not yet had emit() or emitIAS() called on | |
| 172 // it. This is only used as a debugging configuration when we want | |
| 173 // to emit "readable" assembly code, possibly annotated with | |
| 174 // liveness and other information only available in the Cfg and not | |
| 175 // in the Assembler buffer. | |
| 176 enum ItemKind { WI_Nop, WI_GlobalInits, WI_Asm, WI_Cfg }; | |
| 177 // Constructor for a WI_Nop work item. | |
| 178 explicit EmitterWorkItem(uint32_t Seq) | |
| 179 : Sequence(Seq), Kind(WI_Nop), GlobalInits(nullptr), Function(nullptr), | |
| 180 RawFunc(nullptr) {} | |
| 181 // Constructor for a WI_GlobalInits work item. | |
| 182 EmitterWorkItem(uint32_t Seq, VariableDeclarationList *D) | |
| 183 : Sequence(Seq), Kind(WI_GlobalInits), GlobalInits(D), Function(nullptr), | |
| 184 RawFunc(nullptr) {} | |
| 185 // Constructor for a WI_Asm work item. | |
| 186 EmitterWorkItem(uint32_t Seq, Assembler *A) | |
| 187 : Sequence(Seq), Kind(WI_Asm), GlobalInits(nullptr), Function(A), | |
| 188 RawFunc(nullptr) {} | |
| 189 // Constructor for a WI_Cfg work item. | |
| 190 EmitterWorkItem(uint32_t Seq, Cfg *F) | |
| 191 : Sequence(Seq), Kind(WI_Cfg), GlobalInits(nullptr), Function(nullptr), | |
| 192 RawFunc(F) {} | |
| 193 uint32_t getSequenceNumber() const { return Sequence; } | |
| 194 ItemKind getKind() const { return Kind; } | |
| 195 VariableDeclarationList *getGlobalInits() const { | |
| 196 assert(getKind() == WI_GlobalInits); | |
| 197 return GlobalInits; | |
| 198 } | |
| 199 Assembler *getAsm() const { | |
| 200 assert(getKind() == WI_Asm); | |
| 201 return Function; | |
| 202 } | |
| 203 Cfg *getCfg() const { | |
| 204 assert(getKind() == WI_Cfg); | |
| 205 return RawFunc; | |
| 206 } | |
| 207 ~EmitterWorkItem(); | |
| 208 | |
| 209 private: | |
| 210 const uint32_t Sequence; | |
| 211 const ItemKind Kind; | |
| 212 VariableDeclarationList *const GlobalInits; | |
| 213 Assembler *const Function; | |
| 214 Cfg *const RawFunc; | |
| 215 }; | |
| 216 | |
| 193 } // end of namespace Ice | 217 } // end of namespace Ice |
| 194 | 218 |
| 195 #endif // SUBZERO_SRC_ICEUTILS_H | 219 #endif // SUBZERO_SRC_ICETHREADING_H |
| OLD | NEW |