Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(732)

Side by Side Diff: src/IceGlobalContext.h

Issue 848193003: Subzero: Add locking to prepare for multithreaded translation. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Remove GlobalContext::IsStrLocked. Make LockedPtr<>::Lock a pointer. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceGlobalContext.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceGlobalContext.h - Global context defs -----*- C++ -*-===// 1 //===- subzero/src/IceGlobalContext.h - Global context defs -----*- 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 aspects of the compilation that persist across 10 // This file declares aspects of the compilation that persist across
11 // multiple functions. 11 // multiple functions.
12 // 12 //
13 //===----------------------------------------------------------------------===// 13 //===----------------------------------------------------------------------===//
14 14
15 #ifndef SUBZERO_SRC_ICEGLOBALCONTEXT_H 15 #ifndef SUBZERO_SRC_ICEGLOBALCONTEXT_H
16 #define SUBZERO_SRC_ICEGLOBALCONTEXT_H 16 #define SUBZERO_SRC_ICEGLOBALCONTEXT_H
17 17
18 #include <memory> 18 #include <memory>
19 #include <mutex>
19 20
20 #include "IceDefs.h" 21 #include "IceDefs.h"
21 #include "IceClFlags.h" 22 #include "IceClFlags.h"
22 #include "IceIntrinsics.h" 23 #include "IceIntrinsics.h"
23 #include "IceRNG.h" 24 #include "IceRNG.h"
24 #include "IceTimerTree.h" 25 #include "IceTimerTree.h"
25 #include "IceTypes.h" 26 #include "IceTypes.h"
26 27
27 namespace Ice { 28 namespace Ice {
28 29
29 class ClFlags; 30 class ClFlags;
31 class ConstantPool;
30 class FuncSigType; 32 class FuncSigType;
31 33
32 // This class collects rudimentary statistics during translation. 34 typedef std::mutex GlobalLockType;
33 class CodeStats { 35
34 CodeStats(const CodeStats &) = delete; 36 // LockedPtr is a way to provide automatically locked access to some object.
35 CodeStats &operator=(const CodeStats &) = default; 37 template <typename T> class LockedPtr {
38 LockedPtr() = delete;
39 LockedPtr(const LockedPtr &) = delete;
40 LockedPtr &operator=(const LockedPtr &) = delete;
36 41
37 public: 42 public:
38 CodeStats() 43 LockedPtr(T *Value, GlobalLockType *Lock) : Value(Value), Lock(Lock) {
39 : InstructionsEmitted(0), RegistersSaved(0), FrameBytes(0), Spills(0), 44 Lock->lock();
40 Fills(0) {} 45 }
41 void reset() { *this = CodeStats(); } 46 LockedPtr(LockedPtr &&Other) : Value(Other.Value), Lock(Other.Lock) {
42 void updateEmitted(uint32_t InstCount) { InstructionsEmitted += InstCount; } 47 Other.Value = nullptr;
43 void updateRegistersSaved(uint32_t Num) { RegistersSaved += Num; } 48 Other.Lock = nullptr;
44 void updateFrameBytes(uint32_t Bytes) { FrameBytes += Bytes; } 49 }
45 void updateSpills() { ++Spills; } 50 ~LockedPtr() { Lock->unlock(); }
46 void updateFills() { ++Fills; } 51 T *operator->() const { return Value; }
47 void dump(const IceString &Name, Ostream &Str);
48 52
49 private: 53 private:
50 uint32_t InstructionsEmitted; 54 T *Value;
51 uint32_t RegistersSaved; 55 GlobalLockType *Lock;
52 uint32_t FrameBytes;
53 uint32_t Spills;
54 uint32_t Fills;
55 }; 56 };
56 57
57 // TODO: Accesses to all non-const fields of GlobalContext need to
58 // be synchronized, especially the constant pool, the allocator, and
59 // the output streams.
60 class GlobalContext { 58 class GlobalContext {
61 GlobalContext(const GlobalContext &) = delete; 59 GlobalContext(const GlobalContext &) = delete;
62 GlobalContext &operator=(const GlobalContext &) = delete; 60 GlobalContext &operator=(const GlobalContext &) = delete;
63 61
62 // CodeStats collects rudimentary statistics during translation.
63 class CodeStats {
64 CodeStats(const CodeStats &) = delete;
65 CodeStats &operator=(const CodeStats &) = default;
66
67 public:
68 CodeStats()
69 : InstructionsEmitted(0), RegistersSaved(0), FrameBytes(0), Spills(0),
70 Fills(0) {}
71 void reset() { *this = CodeStats(); }
72 void updateEmitted(uint32_t InstCount) { InstructionsEmitted += InstCount; }
73 void updateRegistersSaved(uint32_t Num) { RegistersSaved += Num; }
74 void updateFrameBytes(uint32_t Bytes) { FrameBytes += Bytes; }
75 void updateSpills() { ++Spills; }
76 void updateFills() { ++Fills; }
77 void dump(const IceString &Name, Ostream &Str);
78
79 private:
80 uint32_t InstructionsEmitted;
81 uint32_t RegistersSaved;
82 uint32_t FrameBytes;
83 uint32_t Spills;
84 uint32_t Fills;
85 };
86
87 // ThreadContext contains thread-local data. This data can be
88 // combined/reduced as needed after all threads complete.
89 class ThreadContext {
90 ThreadContext(const ThreadContext &) = delete;
91 ThreadContext &operator=(const ThreadContext &) = delete;
92
93 public:
94 ThreadContext() {}
95 CodeStats StatsFunction;
96 std::vector<TimerStack> Timers;
97 };
98
64 public: 99 public:
65 GlobalContext(Ostream *OsDump, Ostream *OsEmit, ELFStreamer *ELFStreamer, 100 GlobalContext(Ostream *OsDump, Ostream *OsEmit, ELFStreamer *ELFStreamer,
66 VerboseMask Mask, TargetArch Arch, OptLevel Opt, 101 VerboseMask Mask, TargetArch Arch, OptLevel Opt,
67 IceString TestPrefix, const ClFlags &Flags); 102 IceString TestPrefix, const ClFlags &Flags);
68 ~GlobalContext(); 103 ~GlobalContext();
69 104
70 // Returns true if any of the specified options in the verbose mask 105 // Returns true if any of the specified options in the verbose mask
71 // are set. If the argument is omitted, it checks if any verbose 106 // are set. If the argument is omitted, it checks if any verbose
72 // options at all are set. 107 // options at all are set.
73 VerboseMask getVerbose() const { return VMask; } 108 VerboseMask getVerbose() const { return VMask; }
74 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; } 109 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; }
75 void setVerbose(VerboseMask Mask) { VMask = Mask; } 110 void setVerbose(VerboseMask Mask) { VMask = Mask; }
76 void addVerbose(VerboseMask Mask) { VMask |= Mask; } 111 void addVerbose(VerboseMask Mask) { VMask |= Mask; }
77 void subVerbose(VerboseMask Mask) { VMask &= ~Mask; } 112 void subVerbose(VerboseMask Mask) { VMask &= ~Mask; }
78 113
114 // The dump and emit streams need to be used by only one thread at a
115 // time. This is done by exclusively reserving the streams via
116 // lockStr() and unlockStr(). The OstreamLocker class can be used
117 // to conveniently manage this.
118 //
119 // The model is that a thread grabs the stream lock, then does an
120 // arbitrary amount of work during which far-away callees may grab
121 // the stream and do something with it, and finally the thread
122 // releases the stream lock. This allows large chunks of output to
123 // be dumped or emitted without risking interleaving from multiple
124 // threads.
125 void lockStr() { StrLock.lock(); }
126 void unlockStr() { StrLock.unlock(); }
79 Ostream &getStrDump() { return *StrDump; } 127 Ostream &getStrDump() { return *StrDump; }
80 Ostream &getStrEmit() { return *StrEmit; } 128 Ostream &getStrEmit() { return *StrEmit; }
81 129
82 TargetArch getTargetArch() const { return Arch; } 130 TargetArch getTargetArch() const { return Arch; }
83 OptLevel getOptLevel() const { return Opt; } 131 OptLevel getOptLevel() const { return Opt; }
84 132
85 // When emitting assembly, we allow a string to be prepended to 133 // When emitting assembly, we allow a string to be prepended to
86 // names of translated functions. This makes it easier to create an 134 // names of translated functions. This makes it easier to create an
87 // execution test against a reference translator like llc, with both 135 // execution test against a reference translator like llc, with both
88 // translators using the same bitcode as input. 136 // translators using the same bitcode as input.
(...skipping 13 matching lines...) Expand all
102 Constant *getConstantDouble(double Value); 150 Constant *getConstantDouble(double Value);
103 // Returns a symbolic constant. 151 // Returns a symbolic constant.
104 Constant *getConstantSym(RelocOffsetT Offset, const IceString &Name, 152 Constant *getConstantSym(RelocOffsetT Offset, const IceString &Name,
105 bool SuppressMangling); 153 bool SuppressMangling);
106 // Returns an undef. 154 // Returns an undef.
107 Constant *getConstantUndef(Type Ty); 155 Constant *getConstantUndef(Type Ty);
108 // Returns a zero value. 156 // Returns a zero value.
109 Constant *getConstantZero(Type Ty); 157 Constant *getConstantZero(Type Ty);
110 // getConstantPool() returns a copy of the constant pool for 158 // getConstantPool() returns a copy of the constant pool for
111 // constants of a given type. 159 // constants of a given type.
112 ConstantList getConstantPool(Type Ty) const; 160 ConstantList getConstantPool(Type Ty);
113 // Returns a new function declaration, allocated in an internal 161 // Returns a new function declaration, allocated in an internal
114 // memory pool. Ownership of the function is maintained by this 162 // memory pool. Ownership of the function is maintained by this
115 // class instance. 163 // class instance.
116 FunctionDeclaration *newFunctionDeclaration(const FuncSigType *Signature, 164 FunctionDeclaration *newFunctionDeclaration(const FuncSigType *Signature,
117 unsigned CallingConv, 165 unsigned CallingConv,
118 unsigned Linkage, bool IsProto); 166 unsigned Linkage, bool IsProto);
119 167
120 // Returns a new global variable declaration, allocated in an 168 // Returns a new global variable declaration, allocated in an
121 // internal memory pool. Ownership of the function is maintained by 169 // internal memory pool. Ownership of the function is maintained by
122 // this class instance. 170 // this class instance.
123 VariableDeclaration *newVariableDeclaration(); 171 VariableDeclaration *newVariableDeclaration();
124 172
125 const ClFlags &getFlags() const { return Flags; } 173 const ClFlags &getFlags() const { return Flags; }
126 174
127 bool isIRGenerationDisabled() const { 175 bool isIRGenerationDisabled() const {
128 return ALLOW_DISABLE_IR_GEN ? getFlags().DisableIRGeneration : false; 176 return ALLOW_DISABLE_IR_GEN ? getFlags().DisableIRGeneration : false;
129 } 177 }
130 178
131 // Allocate data of type T using the global allocator. 179 // Allocate data of type T using the global allocator.
132 template <typename T> T *allocate() { return Allocator.Allocate<T>(); } 180 template <typename T> T *allocate() { return getAllocator()->Allocate<T>(); }
133 181
134 const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; } 182 const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; }
135 183
136 // TODO(wala,stichnot): Make the RNG play nicely with multithreaded 184 // TODO(wala,stichnot): Make the RNG play nicely with multithreaded
137 // translation. 185 // translation.
138 RandomNumberGenerator &getRNG() { return RNG; } 186 RandomNumberGenerator &getRNG() { return RNG; }
139 187
140 ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); } 188 ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); }
141 189
142 // Reset stats at the beginning of a function. 190 // Reset stats at the beginning of a function.
143 void resetStats() { 191 void resetStats() {
144 if (ALLOW_DUMP) 192 if (ALLOW_DUMP)
145 StatsFunction.reset(); 193 TLS->StatsFunction.reset();
146 } 194 }
147 void dumpStats(const IceString &Name, bool Final = false); 195 void dumpStats(const IceString &Name, bool Final = false);
148 void statsUpdateEmitted(uint32_t InstCount) { 196 void statsUpdateEmitted(uint32_t InstCount) {
149 if (!ALLOW_DUMP) 197 if (!ALLOW_DUMP || !getFlags().DumpStats)
150 return; 198 return;
151 StatsFunction.updateEmitted(InstCount); 199 TLS->StatsFunction.updateEmitted(InstCount);
152 StatsCumulative.updateEmitted(InstCount); 200 getStatsCumulative()->updateEmitted(InstCount);
153 } 201 }
154 void statsUpdateRegistersSaved(uint32_t Num) { 202 void statsUpdateRegistersSaved(uint32_t Num) {
155 if (!ALLOW_DUMP) 203 if (!ALLOW_DUMP || !getFlags().DumpStats)
156 return; 204 return;
157 StatsFunction.updateRegistersSaved(Num); 205 TLS->StatsFunction.updateRegistersSaved(Num);
158 StatsCumulative.updateRegistersSaved(Num); 206 getStatsCumulative()->updateRegistersSaved(Num);
159 } 207 }
160 void statsUpdateFrameBytes(uint32_t Bytes) { 208 void statsUpdateFrameBytes(uint32_t Bytes) {
161 if (!ALLOW_DUMP) 209 if (!ALLOW_DUMP || !getFlags().DumpStats)
162 return; 210 return;
163 StatsFunction.updateFrameBytes(Bytes); 211 TLS->StatsFunction.updateFrameBytes(Bytes);
164 StatsCumulative.updateFrameBytes(Bytes); 212 getStatsCumulative()->updateFrameBytes(Bytes);
165 } 213 }
166 void statsUpdateSpills() { 214 void statsUpdateSpills() {
167 if (!ALLOW_DUMP) 215 if (!ALLOW_DUMP || !getFlags().DumpStats)
168 return; 216 return;
169 StatsFunction.updateSpills(); 217 TLS->StatsFunction.updateSpills();
170 StatsCumulative.updateSpills(); 218 getStatsCumulative()->updateSpills();
171 } 219 }
172 void statsUpdateFills() { 220 void statsUpdateFills() {
173 if (!ALLOW_DUMP) 221 if (!ALLOW_DUMP || !getFlags().DumpStats)
174 return; 222 return;
175 StatsFunction.updateFills(); 223 TLS->StatsFunction.updateFills();
176 StatsCumulative.updateFills(); 224 getStatsCumulative()->updateFills();
177 } 225 }
178 226
179 // These are predefined TimerStackIdT values. 227 // These are predefined TimerStackIdT values.
180 enum TimerStackKind { 228 enum TimerStackKind {
181 TSK_Default = 0, 229 TSK_Default = 0,
182 TSK_Funcs, 230 TSK_Funcs,
183 TSK_Num 231 TSK_Num
184 }; 232 };
185 233
234 TimerStackIdT newTimerStackID(const IceString &Name);
186 TimerIdT getTimerID(TimerStackIdT StackID, const IceString &Name); 235 TimerIdT getTimerID(TimerStackIdT StackID, const IceString &Name);
187 TimerStackIdT newTimerStackID(const IceString &Name);
188 void pushTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default); 236 void pushTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default);
189 void popTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default); 237 void popTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default);
190 void resetTimer(TimerStackIdT StackID); 238 void resetTimer(TimerStackIdT StackID);
191 void setTimerName(TimerStackIdT StackID, const IceString &NewName); 239 void setTimerName(TimerStackIdT StackID, const IceString &NewName);
192 void dumpTimers(TimerStackIdT StackID = TSK_Default, 240 void dumpTimers(TimerStackIdT StackID = TSK_Default,
193 bool DumpCumulative = true); 241 bool DumpCumulative = true);
194 242
195 private: 243 private:
244 // Try to make sure the mutexes are allocated on separate cache
245 // lines, assuming the maximum cache line size is 64.
246 const static size_t MaxCacheLineSize = 64;
247 alignas(MaxCacheLineSize) GlobalLockType AllocLock;
248 alignas(MaxCacheLineSize) GlobalLockType ConstPoolLock;
249 alignas(MaxCacheLineSize) GlobalLockType StatsLock;
250 alignas(MaxCacheLineSize) GlobalLockType TimerLock;
251
252 // StrLock is a global lock on the dump and emit output streams.
253 typedef std::recursive_mutex StrLockType;
254 StrLockType StrLock;
255
196 Ostream *StrDump; // Stream for dumping / diagnostics 256 Ostream *StrDump; // Stream for dumping / diagnostics
197 Ostream *StrEmit; // Stream for code emission 257 Ostream *StrEmit; // Stream for code emission
198 258
199 ArenaAllocator<> Allocator; 259 ArenaAllocator<> Allocator;
200 VerboseMask VMask; 260 VerboseMask VMask;
201 std::unique_ptr<class ConstantPool> ConstPool; 261 std::unique_ptr<ConstantPool> ConstPool;
202 Intrinsics IntrinsicsInfo; 262 Intrinsics IntrinsicsInfo;
203 const TargetArch Arch; 263 const TargetArch Arch;
204 const OptLevel Opt; 264 const OptLevel Opt;
205 const IceString TestPrefix; 265 const IceString TestPrefix;
206 const ClFlags &Flags; 266 const ClFlags &Flags;
207 RandomNumberGenerator RNG; 267 RandomNumberGenerator RNG;
208 std::unique_ptr<ELFObjectWriter> ObjectWriter; 268 std::unique_ptr<ELFObjectWriter> ObjectWriter;
209 CodeStats StatsFunction;
210 CodeStats StatsCumulative; 269 CodeStats StatsCumulative;
211 std::vector<TimerStack> Timers; 270 std::vector<TimerStack> Timers;
212 std::vector<GlobalDeclaration *> GlobalDeclarations; 271 std::vector<GlobalDeclaration *> GlobalDeclarations;
213 272
273 LockedPtr<ArenaAllocator<> > getAllocator() {
274 return LockedPtr<ArenaAllocator<> >(&Allocator, &AllocLock);
JF 2015/01/20 17:20:21 Y u s p a c e ?
Jim Stichnoth 2015/01/20 18:14:53 Thanks, clang-format! Fixed here and in IceDefs.h
275 }
276 LockedPtr<ConstantPool> getConstPool() {
277 return LockedPtr<ConstantPool>(ConstPool.get(), &ConstPoolLock);
278 }
279 LockedPtr<CodeStats> getStatsCumulative() {
280 return LockedPtr<CodeStats>(&StatsCumulative, &StatsLock);
281 }
282 LockedPtr<std::vector<TimerStack> > getTimers() {
283 return LockedPtr<std::vector<TimerStack> >(&Timers, &TimerLock);
284 }
285
286 std::vector<ThreadContext *> AllThreadContexts;
287 // Each thread has its own TLS pointer which is also held in
288 // AllThreadContexts.
289 thread_local static ThreadContext *TLS;
290
214 // Private helpers for mangleName() 291 // Private helpers for mangleName()
215 typedef llvm::SmallVector<char, 32> ManglerVector; 292 typedef llvm::SmallVector<char, 32> ManglerVector;
216 void incrementSubstitutions(ManglerVector &OldName) const; 293 void incrementSubstitutions(ManglerVector &OldName) const;
217 }; 294 };
218 295
219 // Helper class to push and pop a timer marker. The constructor 296 // Helper class to push and pop a timer marker. The constructor
220 // pushes a marker, and the destructor pops it. This is for 297 // pushes a marker, and the destructor pops it. This is for
221 // convenient timing of regions of code. 298 // convenient timing of regions of code.
222 class TimerMarker { 299 class TimerMarker {
223 TimerMarker(const TimerMarker &) = delete; 300 TimerMarker(const TimerMarker &) = delete;
(...skipping 14 matching lines...) Expand all
238 if (ALLOW_DUMP && Active) 315 if (ALLOW_DUMP && Active)
239 Ctx->popTimer(ID); 316 Ctx->popTimer(ID);
240 } 317 }
241 318
242 private: 319 private:
243 TimerIdT ID; 320 TimerIdT ID;
244 GlobalContext *const Ctx; 321 GlobalContext *const Ctx;
245 bool Active; 322 bool Active;
246 }; 323 };
247 324
325 // Helper class for locking the streams and then automatically
326 // unlocking them.
327 class OstreamLocker {
328 private:
329 OstreamLocker() = delete;
330 OstreamLocker(const OstreamLocker &) = delete;
331 OstreamLocker &operator=(const OstreamLocker &) = delete;
332
333 public:
334 explicit OstreamLocker(GlobalContext *Ctx) : Ctx(Ctx) { Ctx->lockStr(); }
335 ~OstreamLocker() { Ctx->unlockStr(); }
336
337 private:
338 GlobalContext *const Ctx;
339 };
340
248 } // end of namespace Ice 341 } // end of namespace Ice
249 342
250 #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H 343 #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H
OLDNEW
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceGlobalContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698