Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceGlobalContext.h - Global context defs -----*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares aspects of the compilation that persist across |
| 11 | // multiple functions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef SUBZERO_SRC_ICEGLOBALCONTEXT_H |
| 16 | #define SUBZERO_SRC_ICEGLOBALCONTEXT_H |
| 17 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 18 | #include "IceDefs.h" |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 19 | #include "IceClFlags.h" |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 20 | #include "IceIntrinsics.h" |
Matt Wala | 1bd2fce | 2014-08-08 14:02:09 -0700 | [diff] [blame] | 21 | #include "IceRNG.h" |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 22 | #include "IceThreading.h" |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 23 | #include "IceTimerTree.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 24 | #include "IceTypes.h" |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 25 | #include "IceUtils.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 26 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 27 | #include <array> |
| 28 | #include <functional> |
| 29 | #include <mutex> |
| 30 | #include <thread> |
| 31 | #include <type_traits> |
| 32 | #include <vector> |
| 33 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 34 | namespace Ice { |
| 35 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 36 | class ClFlags; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 37 | class ConstantPool; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 38 | class EmitterWorkItem; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 39 | class FuncSigType; |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 40 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 41 | // LockedPtr is a way to provide automatically locked access to some object. |
| 42 | template <typename T> class LockedPtr { |
| 43 | LockedPtr() = delete; |
| 44 | LockedPtr(const LockedPtr &) = delete; |
| 45 | LockedPtr &operator=(const LockedPtr &) = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 46 | |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 47 | public: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 48 | LockedPtr(T *Value, GlobalLockType *Lock) : Value(Value), Lock(Lock) { |
| 49 | Lock->lock(); |
| 50 | } |
| 51 | LockedPtr(LockedPtr &&Other) : Value(Other.Value), Lock(Other.Lock) { |
| 52 | Other.Value = nullptr; |
| 53 | Other.Lock = nullptr; |
| 54 | } |
| 55 | ~LockedPtr() { Lock->unlock(); } |
| 56 | T *operator->() const { return Value; } |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 59 | T *Value; |
| 60 | GlobalLockType *Lock; |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 63 | class GlobalContext { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 64 | GlobalContext() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 65 | GlobalContext(const GlobalContext &) = delete; |
| 66 | GlobalContext &operator=(const GlobalContext &) = delete; |
| 67 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 68 | // CodeStats collects rudimentary statistics during translation. |
| 69 | class CodeStats { |
| 70 | CodeStats(const CodeStats &) = delete; |
| 71 | CodeStats &operator=(const CodeStats &) = default; |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 72 | #define CODESTATS_TABLE \ |
| 73 | /* dump string, enum value */ \ |
| 74 | X("Inst Count ", InstCount) \ |
| 75 | X("Regs Saved ", RegsSaved) \ |
| 76 | X("Frame Bytes ", FrameByte) \ |
| 77 | X("Spills ", NumSpills) \ |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 78 | X("Fills ", NumFills) \ |
| 79 | X("R/P Imms ", NumRPImms) |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 80 | //#define X(str, tag) |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 81 | |
| 82 | public: |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 83 | enum CSTag { |
| 84 | #define X(str, tag) CS_##tag, |
| 85 | CODESTATS_TABLE |
| 86 | #undef X |
| 87 | CS_NUM |
| 88 | }; |
| 89 | CodeStats() { reset(); } |
| 90 | void reset() { Stats.fill(0); } |
| 91 | void update(CSTag Tag, uint32_t Count = 1) { |
| 92 | assert(Tag < Stats.size()); |
| 93 | Stats[Tag] += Count; |
| 94 | } |
| 95 | void add(const CodeStats &Other) { |
| 96 | for (uint32_t i = 0; i < Stats.size(); ++i) |
| 97 | Stats[i] += Other.Stats[i]; |
| 98 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 99 | void dump(const IceString &Name, Ostream &Str); |
| 100 | |
| 101 | private: |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 102 | std::array<uint32_t, CS_NUM> Stats; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 105 | // TimerList is a vector of TimerStack objects, with extra methods |
| 106 | // to initialize and merge these vectors. |
| 107 | class TimerList : public std::vector<TimerStack> { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 108 | TimerList(const TimerList &) = delete; |
| 109 | TimerList &operator=(const TimerList &) = delete; |
| 110 | |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 111 | public: |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 112 | TimerList() = default; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 113 | // initInto() initializes a target list of timers based on the |
| 114 | // current list. In particular, it creates the same number of |
| 115 | // timers, in the same order, with the same names, but initially |
| 116 | // empty of timing data. |
| 117 | void initInto(TimerList &Dest) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 118 | if (!BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 119 | return; |
| 120 | Dest.clear(); |
| 121 | for (const TimerStack &Stack : *this) { |
| 122 | Dest.push_back(TimerStack(Stack.getName())); |
| 123 | } |
| 124 | } |
| 125 | void mergeFrom(TimerList &Src) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 126 | if (!BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 127 | return; |
| 128 | assert(size() == Src.size()); |
| 129 | size_type i = 0; |
| 130 | for (TimerStack &Stack : *this) { |
| 131 | assert(Stack.getName() == Src[i].getName()); |
| 132 | Stack.mergeFrom(Src[i]); |
| 133 | ++i; |
| 134 | } |
| 135 | } |
| 136 | }; |
| 137 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 138 | // ThreadContext contains thread-local data. This data can be |
| 139 | // combined/reduced as needed after all threads complete. |
| 140 | class ThreadContext { |
| 141 | ThreadContext(const ThreadContext &) = delete; |
| 142 | ThreadContext &operator=(const ThreadContext &) = delete; |
| 143 | |
| 144 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 145 | ThreadContext() = default; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 146 | CodeStats StatsFunction; |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 147 | CodeStats StatsCumulative; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 148 | TimerList Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 151 | public: |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 152 | // The dump stream is a log stream while emit is the stream code |
| 153 | // is emitted to. The error stream is strictly for logging errors. |
| 154 | GlobalContext(Ostream *OsDump, Ostream *OsEmit, Ostream *OsError, |
| 155 | ELFStreamer *ELFStreamer, const ClFlags &Flags); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 156 | ~GlobalContext(); |
| 157 | |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 158 | // |
| 159 | // The dump, error, and emit streams need to be used by only one |
| 160 | // thread at a time. This is done by exclusively reserving the |
| 161 | // streams via lockStr() and unlockStr(). The OstreamLocker class |
| 162 | // can be used to conveniently manage this. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 163 | // |
| 164 | // The model is that a thread grabs the stream lock, then does an |
| 165 | // arbitrary amount of work during which far-away callees may grab |
| 166 | // the stream and do something with it, and finally the thread |
| 167 | // releases the stream lock. This allows large chunks of output to |
| 168 | // be dumped or emitted without risking interleaving from multiple |
| 169 | // threads. |
| 170 | void lockStr() { StrLock.lock(); } |
| 171 | void unlockStr() { StrLock.unlock(); } |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 172 | Ostream &getStrDump() { return *StrDump; } |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 173 | Ostream &getStrError() { return *StrError; } |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 174 | Ostream &getStrEmit() { return *StrEmit; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 175 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 176 | LockedPtr<ErrorCode> getErrorStatus() { |
| 177 | return LockedPtr<ErrorCode>(&ErrorStatus, &ErrorStatusLock); |
| 178 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 179 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 180 | // When emitting assembly, we allow a string to be prepended to |
| 181 | // names of translated functions. This makes it easier to create an |
| 182 | // execution test against a reference translator like llc, with both |
| 183 | // translators using the same bitcode as input. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 184 | IceString mangleName(const IceString &Name) const; |
| 185 | |
| 186 | // Manage Constants. |
| 187 | // getConstant*() functions are not const because they might add |
| 188 | // something to the constant pool. |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 189 | Constant *getConstantInt(Type Ty, int64_t Value); |
| 190 | Constant *getConstantInt1(int8_t ConstantInt1); |
| 191 | Constant *getConstantInt8(int8_t ConstantInt8); |
| 192 | Constant *getConstantInt16(int16_t ConstantInt16); |
| 193 | Constant *getConstantInt32(int32_t ConstantInt32); |
| 194 | Constant *getConstantInt64(int64_t ConstantInt64); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 195 | Constant *getConstantFloat(float Value); |
| 196 | Constant *getConstantDouble(double Value); |
| 197 | // Returns a symbolic constant. |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 198 | Constant *getConstantSym(RelocOffsetT Offset, const IceString &Name, |
| 199 | bool SuppressMangling); |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 200 | Constant *getConstantExternSym(const IceString &Name); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 201 | // Returns an undef. |
| 202 | Constant *getConstantUndef(Type Ty); |
| 203 | // Returns a zero value. |
| 204 | Constant *getConstantZero(Type Ty); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 205 | // getConstantPool() returns a copy of the constant pool for |
| 206 | // constants of a given type. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 207 | ConstantList getConstantPool(Type Ty); |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 208 | // Returns a copy of the list of external symbols. |
| 209 | ConstantList getConstantExternSyms(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 210 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 211 | const ClFlags &getFlags() const { return Flags; } |
| 212 | |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 213 | bool isIRGenerationDisabled() const { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 214 | return getFlags().getDisableIRGeneration(); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 215 | } |
| 216 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 217 | // Allocate data of type T using the global allocator. We allow entities |
| 218 | // allocated from this global allocator to be either trivially or |
| 219 | // non-trivially destructible. We optimize the case when T is trivially |
| 220 | // destructible by not registering a destructor. Destructors will be invoked |
| 221 | // during GlobalContext destruction in the reverse object creation order. |
| 222 | template <typename T> |
| 223 | typename std::enable_if<std::is_trivially_destructible<T>::value, T>::type * |
| 224 | allocate() { |
| 225 | return getAllocator()->Allocate<T>(); |
| 226 | } |
| 227 | |
| 228 | template <typename T> |
| 229 | typename std::enable_if<!std::is_trivially_destructible<T>::value, T>::type * |
| 230 | allocate() { |
| 231 | T *Ret = getAllocator()->Allocate<T>(); |
| 232 | getDestructors()->emplace_back([Ret]() { Ret->~T(); }); |
| 233 | return Ret; |
| 234 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 235 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 236 | const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; } |
| 237 | |
Matt Wala | 1bd2fce | 2014-08-08 14:02:09 -0700 | [diff] [blame] | 238 | // TODO(wala,stichnot): Make the RNG play nicely with multithreaded |
| 239 | // translation. |
| 240 | RandomNumberGenerator &getRNG() { return RNG; } |
| 241 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 242 | ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); } |
| 243 | |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 244 | // Reset stats at the beginning of a function. |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 245 | void resetStats() { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 246 | if (BuildDefs::dump()) |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 247 | ICE_TLS_GET_FIELD(TLS)->StatsFunction.reset(); |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 248 | } |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 249 | void dumpStats(const IceString &Name, bool Final = false); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 250 | void statsUpdateEmitted(uint32_t InstCount) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 251 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 252 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 253 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 254 | Tls->StatsFunction.update(CodeStats::CS_InstCount, InstCount); |
| 255 | Tls->StatsCumulative.update(CodeStats::CS_InstCount, InstCount); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 256 | } |
| 257 | void statsUpdateRegistersSaved(uint32_t Num) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 258 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 259 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 260 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 261 | Tls->StatsFunction.update(CodeStats::CS_RegsSaved, Num); |
| 262 | Tls->StatsCumulative.update(CodeStats::CS_RegsSaved, Num); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 263 | } |
| 264 | void statsUpdateFrameBytes(uint32_t Bytes) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 265 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 266 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 267 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 268 | Tls->StatsFunction.update(CodeStats::CS_FrameByte, Bytes); |
| 269 | Tls->StatsCumulative.update(CodeStats::CS_FrameByte, Bytes); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 270 | } |
| 271 | void statsUpdateSpills() { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 272 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 273 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 274 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 275 | Tls->StatsFunction.update(CodeStats::CS_NumSpills); |
| 276 | Tls->StatsCumulative.update(CodeStats::CS_NumSpills); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 277 | } |
| 278 | void statsUpdateFills() { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 279 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 280 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 281 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 282 | Tls->StatsFunction.update(CodeStats::CS_NumFills); |
| 283 | Tls->StatsCumulative.update(CodeStats::CS_NumFills); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 286 | // Number of Randomized or Pooled Immediates |
| 287 | void statsUpdateRPImms() { |
| 288 | if (!getFlags().getDumpStats()) |
| 289 | return; |
| 290 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 291 | Tls->StatsFunction.update(CodeStats::CS_NumRPImms); |
| 292 | Tls->StatsCumulative.update(CodeStats::CS_NumRPImms); |
| 293 | } |
| 294 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 295 | // These are predefined TimerStackIdT values. |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 296 | enum TimerStackKind { TSK_Default = 0, TSK_Funcs, TSK_Num }; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 297 | |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 298 | // newTimerStackID() creates a new TimerStack in the global space. |
| 299 | // It does not affect any TimerStack objects in TLS. |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 300 | TimerStackIdT newTimerStackID(const IceString &Name); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 301 | // dumpTimers() dumps the global timer data. As such, one probably |
| 302 | // wants to call mergeTimerStacks() as a prerequisite. |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 303 | void dumpTimers(TimerStackIdT StackID = TSK_Default, |
| 304 | bool DumpCumulative = true); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 305 | // The following methods affect only the calling thread's TLS timer |
| 306 | // data. |
| 307 | TimerIdT getTimerID(TimerStackIdT StackID, const IceString &Name); |
| 308 | void pushTimer(TimerIdT ID, TimerStackIdT StackID); |
| 309 | void popTimer(TimerIdT ID, TimerStackIdT StackID); |
| 310 | void resetTimer(TimerStackIdT StackID); |
| 311 | void setTimerName(TimerStackIdT StackID, const IceString &NewName); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 312 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 313 | // This is the first work item sequence number that the parser |
| 314 | // produces, and correspondingly the first sequence number that the |
| 315 | // emitter thread will wait for. Start numbering at 1 to leave room |
| 316 | // for a sentinel, in case e.g. we wish to inject items with a |
| 317 | // special sequence number that may be executed out of order. |
| 318 | static uint32_t getFirstSequenceNumber() { return 1; } |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 319 | // Adds a newly parsed and constructed function to the Cfg work |
| 320 | // queue. Notifies any idle workers that a new function is |
| 321 | // available for translating. May block if the work queue is too |
| 322 | // large, in order to control memory footprint. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 323 | void optQueueBlockingPush(std::unique_ptr<Cfg> Func); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 324 | // Takes a Cfg from the work queue for translating. May block if |
| 325 | // the work queue is currently empty. Returns nullptr if there is |
| 326 | // no more work - the queue is empty and either end() has been |
| 327 | // called or the Sequential flag was set. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 328 | std::unique_ptr<Cfg> optQueueBlockingPop(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 329 | // Notifies that no more work will be added to the work queue. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 330 | void optQueueNotifyEnd() { OptQ.notifyEnd(); } |
| 331 | |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 332 | // Emit file header for output file. |
| 333 | void emitFileHeader(); |
| 334 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 335 | void lowerConstants(); |
| 336 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 337 | void emitQueueBlockingPush(EmitterWorkItem *Item); |
| 338 | EmitterWorkItem *emitQueueBlockingPop(); |
| 339 | void emitQueueNotifyEnd() { EmitQ.notifyEnd(); } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 340 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 341 | void initParserThread() { |
| 342 | ThreadContext *Tls = new ThreadContext(); |
| 343 | auto Timers = getTimers(); |
| 344 | Timers->initInto(Tls->Timers); |
| 345 | AllThreadContexts.push_back(Tls); |
| 346 | ICE_TLS_SET_FIELD(TLS, Tls); |
| 347 | } |
| 348 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 349 | void startWorkerThreads() { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 350 | size_t NumWorkers = getFlags().getNumTranslationThreads(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 351 | auto Timers = getTimers(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 352 | for (size_t i = 0; i < NumWorkers; ++i) { |
| 353 | ThreadContext *WorkerTLS = new ThreadContext(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 354 | Timers->initInto(WorkerTLS->Timers); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 355 | AllThreadContexts.push_back(WorkerTLS); |
| 356 | TranslationThreads.push_back(std::thread( |
| 357 | &GlobalContext::translateFunctionsWrapper, this, WorkerTLS)); |
| 358 | } |
| 359 | if (NumWorkers) { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 360 | ThreadContext *WorkerTLS = new ThreadContext(); |
| 361 | Timers->initInto(WorkerTLS->Timers); |
| 362 | AllThreadContexts.push_back(WorkerTLS); |
| 363 | EmitterThreads.push_back( |
| 364 | std::thread(&GlobalContext::emitterWrapper, this, WorkerTLS)); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | void waitForWorkerThreads() { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 369 | optQueueNotifyEnd(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 370 | for (std::thread &Worker : TranslationThreads) { |
| 371 | Worker.join(); |
| 372 | } |
| 373 | TranslationThreads.clear(); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 374 | |
| 375 | // Only notify the emit queue to end after all the translation |
| 376 | // threads have ended. |
| 377 | emitQueueNotifyEnd(); |
| 378 | for (std::thread &Worker : EmitterThreads) { |
| 379 | Worker.join(); |
| 380 | } |
| 381 | EmitterThreads.clear(); |
| 382 | |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 383 | if (BuildDefs::dump()) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 384 | auto Timers = getTimers(); |
| 385 | for (ThreadContext *TLS : AllThreadContexts) |
| 386 | Timers->mergeFrom(TLS->Timers); |
| 387 | } |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 388 | if (BuildDefs::dump()) { |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 389 | // Do a separate loop over AllThreadContexts to avoid holding |
| 390 | // two locks at once. |
| 391 | auto Stats = getStatsCumulative(); |
| 392 | for (ThreadContext *TLS : AllThreadContexts) |
| 393 | Stats->add(TLS->StatsCumulative); |
| 394 | } |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // Translation thread startup routine. |
| 398 | void translateFunctionsWrapper(ThreadContext *MyTLS) { |
| 399 | ICE_TLS_SET_FIELD(TLS, MyTLS); |
| 400 | translateFunctions(); |
| 401 | } |
| 402 | // Translate functions from the Cfg queue until the queue is empty. |
| 403 | void translateFunctions(); |
| 404 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 405 | // Emitter thread startup routine. |
| 406 | void emitterWrapper(ThreadContext *MyTLS) { |
| 407 | ICE_TLS_SET_FIELD(TLS, MyTLS); |
| 408 | emitItems(); |
| 409 | } |
| 410 | // Emit functions and global initializers from the emitter queue |
| 411 | // until the queue is empty. |
| 412 | void emitItems(); |
| 413 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 414 | // Uses DataLowering to lower Globals. Side effects: |
| 415 | // - discards the initializer list for the global variable in Globals. |
| 416 | // - clears the Globals array. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 417 | void lowerGlobals(const IceString &SectionSuffix); |
| 418 | |
| 419 | // Lowers the profile information. |
| 420 | void lowerProfileData(); |
| 421 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 422 | // Utility function to match a symbol name against a match string. |
| 423 | // This is used in a few cases where we want to take some action on |
| 424 | // a particular function or symbol based on a command-line argument, |
| 425 | // such as changing the verbose level for a particular function. An |
| 426 | // empty Match argument means match everything. Returns true if |
| 427 | // there is a match. |
| 428 | static bool matchSymbolName(const IceString &SymbolName, |
| 429 | const IceString &Match) { |
| 430 | return Match.empty() || Match == SymbolName; |
| 431 | } |
| 432 | |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 433 | // Return the randomization cookie for diversification. |
| 434 | // Initialize the cookie if necessary |
| 435 | uint32_t getRandomizationCookie() const { return RandomizationCookie; } |
| 436 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 437 | private: |
| 438 | // Try to ensure mutexes are allocated on separate cache lines. |
| 439 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 440 | // Destructors collaborate with Allocator |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 441 | ICE_CACHELINE_BOUNDARY; |
| 442 | // Managed by getAllocator() |
| 443 | GlobalLockType AllocLock; |
| 444 | ArenaAllocator<> Allocator; |
| 445 | |
| 446 | ICE_CACHELINE_BOUNDARY; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 447 | // Managed by getDestructors() |
| 448 | typedef std::vector<std::function<void()>> DestructorArray; |
| 449 | GlobalLockType DestructorsLock; |
| 450 | DestructorArray Destructors; |
| 451 | |
| 452 | ICE_CACHELINE_BOUNDARY; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 453 | // Managed by getConstantPool() |
| 454 | GlobalLockType ConstPoolLock; |
| 455 | std::unique_ptr<ConstantPool> ConstPool; |
| 456 | |
| 457 | ICE_CACHELINE_BOUNDARY; |
| 458 | // Managed by getErrorStatus() |
| 459 | GlobalLockType ErrorStatusLock; |
| 460 | ErrorCode ErrorStatus; |
| 461 | |
| 462 | ICE_CACHELINE_BOUNDARY; |
| 463 | // Managed by getStatsCumulative() |
| 464 | GlobalLockType StatsLock; |
| 465 | CodeStats StatsCumulative; |
| 466 | |
| 467 | ICE_CACHELINE_BOUNDARY; |
| 468 | // Managed by getTimers() |
| 469 | GlobalLockType TimerLock; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 470 | TimerList Timers; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 471 | |
| 472 | ICE_CACHELINE_BOUNDARY; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 473 | // StrLock is a global lock on the dump and emit output streams. |
| 474 | typedef std::mutex StrLockType; |
| 475 | StrLockType StrLock; |
Jim Stichnoth | 620ad73 | 2015-04-28 14:12:20 -0700 | [diff] [blame] | 476 | Ostream *StrDump; // Stream for dumping / diagnostics |
| 477 | Ostream *StrEmit; // Stream for code emission |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 478 | Ostream *StrError; // Stream for logging errors. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 479 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 480 | ICE_CACHELINE_BOUNDARY; |
| 481 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 482 | Intrinsics IntrinsicsInfo; |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 483 | const ClFlags &Flags; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 484 | RandomNumberGenerator RNG; // TODO(stichnot): Move into Cfg. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 485 | // TODO(jpp): move to EmitterContext. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 486 | std::unique_ptr<ELFObjectWriter> ObjectWriter; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 487 | BoundedProducerConsumerQueue<Cfg> OptQ; |
| 488 | BoundedProducerConsumerQueue<EmitterWorkItem> EmitQ; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 489 | // DataLowering is only ever used by a single thread at a time (either in |
| 490 | // emitItems(), or in IceCompiler::run before the compilation is over.) |
| 491 | // TODO(jpp): move to EmitterContext. |
| 492 | std::unique_ptr<TargetDataLowering> DataLowering; |
| 493 | // If !HasEmittedCode, SubZero will accumulate all Globals (which are "true" |
| 494 | // program global variables) until the first code WorkItem is seen. |
| 495 | // TODO(jpp): move to EmitterContext. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 496 | bool HasSeenCode = false; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 497 | // TODO(jpp): move to EmitterContext. |
| 498 | VariableDeclarationList Globals; |
| 499 | // TODO(jpp): move to EmitterContext. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 500 | VariableDeclaration *ProfileBlockInfoVarDecl; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 501 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 502 | LockedPtr<ArenaAllocator<>> getAllocator() { |
| 503 | return LockedPtr<ArenaAllocator<>>(&Allocator, &AllocLock); |
| 504 | } |
| 505 | LockedPtr<ConstantPool> getConstPool() { |
| 506 | return LockedPtr<ConstantPool>(ConstPool.get(), &ConstPoolLock); |
| 507 | } |
| 508 | LockedPtr<CodeStats> getStatsCumulative() { |
| 509 | return LockedPtr<CodeStats>(&StatsCumulative, &StatsLock); |
| 510 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 511 | LockedPtr<TimerList> getTimers() { |
| 512 | return LockedPtr<TimerList>(&Timers, &TimerLock); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 513 | } |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 514 | LockedPtr<DestructorArray> getDestructors() { |
| 515 | return LockedPtr<DestructorArray>(&Destructors, &DestructorsLock); |
| 516 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 517 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 518 | void accumulateGlobals(std::unique_ptr<VariableDeclarationList> Globls) { |
| 519 | if (Globls != nullptr) |
| 520 | Globals.insert(Globals.end(), Globls->begin(), Globls->end()); |
| 521 | } |
| 522 | |
| 523 | void lowerGlobalsIfNoCodeHasBeenSeen() { |
| 524 | if (HasSeenCode) |
| 525 | return; |
| 526 | constexpr char NoSuffix[] = ""; |
| 527 | lowerGlobals(NoSuffix); |
| 528 | HasSeenCode = true; |
| 529 | } |
| 530 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 531 | llvm::SmallVector<ThreadContext *, 128> AllThreadContexts; |
| 532 | llvm::SmallVector<std::thread, 128> TranslationThreads; |
| 533 | llvm::SmallVector<std::thread, 128> EmitterThreads; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 534 | // Each thread has its own TLS pointer which is also held in |
| 535 | // AllThreadContexts. |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 536 | ICE_TLS_DECLARE_FIELD(ThreadContext *, TLS); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 537 | |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 538 | // Private helpers for mangleName() |
| 539 | typedef llvm::SmallVector<char, 32> ManglerVector; |
| 540 | void incrementSubstitutions(ManglerVector &OldName) const; |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 541 | |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 542 | // Randomization Cookie |
| 543 | // Managed by getRandomizationCookie() |
| 544 | GlobalLockType RandomizationCookieLock; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 545 | uint32_t RandomizationCookie = 0; |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 546 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 547 | public: |
| 548 | static void TlsInit() { ICE_TLS_INIT_FIELD(TLS); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 549 | }; |
| 550 | |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 551 | // Helper class to push and pop a timer marker. The constructor |
| 552 | // pushes a marker, and the destructor pops it. This is for |
| 553 | // convenient timing of regions of code. |
| 554 | class TimerMarker { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 555 | TimerMarker() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 556 | TimerMarker(const TimerMarker &) = delete; |
| 557 | TimerMarker &operator=(const TimerMarker &) = delete; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 558 | |
| 559 | public: |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 560 | TimerMarker(TimerIdT ID, GlobalContext *Ctx, |
| 561 | TimerStackIdT StackID = GlobalContext::TSK_Default) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 562 | : ID(ID), Ctx(Ctx), StackID(StackID) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 563 | if (BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 564 | push(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 565 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 566 | TimerMarker(TimerIdT ID, const Cfg *Func, |
| 567 | TimerStackIdT StackID = GlobalContext::TSK_Default) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 568 | : ID(ID), Ctx(nullptr), StackID(StackID) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 569 | // Ctx gets set at the beginning of pushCfg(). |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 570 | if (BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 571 | pushCfg(Func); |
| 572 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 573 | |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 574 | ~TimerMarker() { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 575 | if (BuildDefs::dump() && Active) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 576 | Ctx->popTimer(ID, StackID); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | private: |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 580 | void push(); |
| 581 | void pushCfg(const Cfg *Func); |
| 582 | const TimerIdT ID; |
| 583 | GlobalContext *Ctx; |
| 584 | const TimerStackIdT StackID; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 585 | bool Active = false; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 586 | }; |
| 587 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 588 | // Helper class for locking the streams and then automatically |
| 589 | // unlocking them. |
| 590 | class OstreamLocker { |
| 591 | private: |
| 592 | OstreamLocker() = delete; |
| 593 | OstreamLocker(const OstreamLocker &) = delete; |
| 594 | OstreamLocker &operator=(const OstreamLocker &) = delete; |
| 595 | |
| 596 | public: |
| 597 | explicit OstreamLocker(GlobalContext *Ctx) : Ctx(Ctx) { Ctx->lockStr(); } |
| 598 | ~OstreamLocker() { Ctx->unlockStr(); } |
| 599 | |
| 600 | private: |
| 601 | GlobalContext *const Ctx; |
| 602 | }; |
| 603 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 604 | } // end of namespace Ice |
| 605 | |
| 606 | #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H |