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

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: Document the OstreamLocker class 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
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;
30 class FuncSigType; 31 class FuncSigType;
31 32
32 // This class collects rudimentary statistics during translation.
33 class CodeStats {
34 CodeStats(const CodeStats &) = delete;
35 CodeStats &operator=(const CodeStats &) = default;
36
37 public:
38 CodeStats()
39 : InstructionsEmitted(0), RegistersSaved(0), FrameBytes(0), Spills(0),
40 Fills(0) {}
41 void reset() { *this = CodeStats(); }
42 void updateEmitted(uint32_t InstCount) { InstructionsEmitted += InstCount; }
43 void updateRegistersSaved(uint32_t Num) { RegistersSaved += Num; }
44 void updateFrameBytes(uint32_t Bytes) { FrameBytes += Bytes; }
45 void updateSpills() { ++Spills; }
46 void updateFills() { ++Fills; }
47 void dump(const IceString &Name, Ostream &Str);
48
49 private:
50 uint32_t InstructionsEmitted;
51 uint32_t RegistersSaved;
52 uint32_t FrameBytes;
53 uint32_t Spills;
54 uint32_t Fills;
55 };
56
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 { 33 class GlobalContext {
61 GlobalContext(const GlobalContext &) = delete; 34 GlobalContext(const GlobalContext &) = delete;
62 GlobalContext &operator=(const GlobalContext &) = delete; 35 GlobalContext &operator=(const GlobalContext &) = delete;
63 36
37 // CodeStats collects rudimentary statistics during translation.
38 class CodeStats {
39 CodeStats(const CodeStats &) = delete;
40 CodeStats &operator=(const CodeStats &) = default;
41
42 public:
43 CodeStats()
44 : InstructionsEmitted(0), RegistersSaved(0), FrameBytes(0), Spills(0),
45 Fills(0) {}
46 void reset() { *this = CodeStats(); }
47 void updateEmitted(uint32_t InstCount) { InstructionsEmitted += InstCount; }
48 void updateRegistersSaved(uint32_t Num) { RegistersSaved += Num; }
49 void updateFrameBytes(uint32_t Bytes) { FrameBytes += Bytes; }
50 void updateSpills() { ++Spills; }
51 void updateFills() { ++Fills; }
52 void dump(const IceString &Name, Ostream &Str);
53
54 private:
55 uint32_t InstructionsEmitted;
56 uint32_t RegistersSaved;
57 uint32_t FrameBytes;
58 uint32_t Spills;
59 uint32_t Fills;
60 };
61
62 // ThreadContext contains thread-local data. This data can be
63 // combined/reduced as needed after all threads complete.
64 class ThreadContext {
65 ThreadContext(const ThreadContext &) = delete;
66 ThreadContext &operator=(const ThreadContext &) = delete;
67 public:
68 ThreadContext() {}
69 CodeStats StatsFunction;
70 std::vector<TimerStack> Timers;
71 };
72
73 typedef std::recursive_mutex GlobalLockType;
74 typedef std::recursive_mutex StrLockType;
JF 2015/01/15 01:31:01 Why recursive? I'd leave a TODO here to remove the
Jim Stichnoth 2015/01/15 07:16:29 (I moved this typedef down to where it's actually
75
64 public: 76 public:
65 GlobalContext(Ostream *OsDump, Ostream *OsEmit, ELFStreamer *ELFStreamer, 77 GlobalContext(Ostream *OsDump, Ostream *OsEmit, ELFStreamer *ELFStreamer,
66 VerboseMask Mask, TargetArch Arch, OptLevel Opt, 78 VerboseMask Mask, TargetArch Arch, OptLevel Opt,
67 IceString TestPrefix, const ClFlags &Flags); 79 IceString TestPrefix, const ClFlags &Flags);
68 ~GlobalContext(); 80 ~GlobalContext();
69 81
70 // Returns true if any of the specified options in the verbose mask 82 // 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 83 // are set. If the argument is omitted, it checks if any verbose
72 // options at all are set. 84 // options at all are set.
73 VerboseMask getVerbose() const { return VMask; } 85 VerboseMask getVerbose() const { return VMask; }
74 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; } 86 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; }
75 void setVerbose(VerboseMask Mask) { VMask = Mask; } 87 void setVerbose(VerboseMask Mask) { VMask = Mask; }
76 void addVerbose(VerboseMask Mask) { VMask |= Mask; } 88 void addVerbose(VerboseMask Mask) { VMask |= Mask; }
77 void subVerbose(VerboseMask Mask) { VMask &= ~Mask; } 89 void subVerbose(VerboseMask Mask) { VMask &= ~Mask; }
78 90
79 Ostream &getStrDump() { return *StrDump; } 91 // The dump and emit streams need to be used by only one thread at a
80 Ostream &getStrEmit() { return *StrEmit; } 92 // time. This is done by exclusively reserving the streams via
93 // lockStr() and unlockStr(). The OstreamLocker class can be used
94 // to conveniently manage this.
95 void lockStr() {
96 StrLock.lock();
97 assert(!IsStrLocked);
98 IsStrLocked = true;
99 }
100 void unlockStr() {
101 assert(IsStrLocked);
102 IsStrLocked = false;
103 StrLock.unlock();
104 }
105 // Test whether we are already holding StrLock, by doing a
106 // try_lock() and if it succeeds, checking that we didn't
107 // recursively lock it.
108 bool isStrLocked() {
109 if (!StrLock.try_lock())
110 return false;
111 bool WasLocked = IsStrLocked;
112 StrLock.unlock();
113 return WasLocked;
114 }
115 Ostream &getStrDump() {
116 assert(isStrLocked());
117 return *StrDump;
118 }
119 Ostream &getStrEmit() {
120 assert(isStrLocked());
121 return *StrEmit;
122 }
81 123
82 TargetArch getTargetArch() const { return Arch; } 124 TargetArch getTargetArch() const { return Arch; }
83 OptLevel getOptLevel() const { return Opt; } 125 OptLevel getOptLevel() const { return Opt; }
84 126
85 // When emitting assembly, we allow a string to be prepended to 127 // When emitting assembly, we allow a string to be prepended to
86 // names of translated functions. This makes it easier to create an 128 // names of translated functions. This makes it easier to create an
87 // execution test against a reference translator like llc, with both 129 // execution test against a reference translator like llc, with both
88 // translators using the same bitcode as input. 130 // translators using the same bitcode as input.
89 IceString getTestPrefix() const { return TestPrefix; } 131 IceString getTestPrefix() const { return TestPrefix; }
90 IceString mangleName(const IceString &Name) const; 132 IceString mangleName(const IceString &Name) const;
(...skipping 11 matching lines...) Expand all
102 Constant *getConstantDouble(double Value); 144 Constant *getConstantDouble(double Value);
103 // Returns a symbolic constant. 145 // Returns a symbolic constant.
104 Constant *getConstantSym(RelocOffsetT Offset, const IceString &Name, 146 Constant *getConstantSym(RelocOffsetT Offset, const IceString &Name,
105 bool SuppressMangling); 147 bool SuppressMangling);
106 // Returns an undef. 148 // Returns an undef.
107 Constant *getConstantUndef(Type Ty); 149 Constant *getConstantUndef(Type Ty);
108 // Returns a zero value. 150 // Returns a zero value.
109 Constant *getConstantZero(Type Ty); 151 Constant *getConstantZero(Type Ty);
110 // getConstantPool() returns a copy of the constant pool for 152 // getConstantPool() returns a copy of the constant pool for
111 // constants of a given type. 153 // constants of a given type.
112 ConstantList getConstantPool(Type Ty) const; 154 ConstantList getConstantPool(Type Ty);
113 // Returns a new function declaration, allocated in an internal 155 // Returns a new function declaration, allocated in an internal
114 // memory pool. Ownership of the function is maintained by this 156 // memory pool. Ownership of the function is maintained by this
115 // class instance. 157 // class instance.
116 FunctionDeclaration *newFunctionDeclaration(const FuncSigType *Signature, 158 FunctionDeclaration *newFunctionDeclaration(const FuncSigType *Signature,
117 unsigned CallingConv, 159 unsigned CallingConv,
118 unsigned Linkage, bool IsProto); 160 unsigned Linkage, bool IsProto);
119 161
120 // Returns a new global variable declaration, allocated in an 162 // Returns a new global variable declaration, allocated in an
121 // internal memory pool. Ownership of the function is maintained by 163 // internal memory pool. Ownership of the function is maintained by
122 // this class instance. 164 // this class instance.
123 VariableDeclaration *newVariableDeclaration(); 165 VariableDeclaration *newVariableDeclaration();
124 166
125 const ClFlags &getFlags() const { return Flags; } 167 const ClFlags &getFlags() const { return Flags; }
126 168
127 bool isIRGenerationDisabled() const { 169 bool isIRGenerationDisabled() const {
128 return ALLOW_DISABLE_IR_GEN ? getFlags().DisableIRGeneration : false; 170 return ALLOW_DISABLE_IR_GEN ? getFlags().DisableIRGeneration : false;
129 } 171 }
130 172
131 // Allocate data of type T using the global allocator. 173 // Allocate data of type T using the global allocator.
132 template <typename T> T *allocate() { return Allocator.Allocate<T>(); } 174 template <typename T> T *allocate() {
175 std::lock_guard<GlobalLockType> L(GlobalLock);
176 return Allocator.Allocate<T>();
177 }
133 178
134 const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; } 179 const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; }
135 180
136 // TODO(wala,stichnot): Make the RNG play nicely with multithreaded 181 // TODO(wala,stichnot): Make the RNG play nicely with multithreaded
137 // translation. 182 // translation.
138 RandomNumberGenerator &getRNG() { return RNG; } 183 RandomNumberGenerator &getRNG() { return RNG; }
139 184
140 ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); } 185 ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); }
141 186
142 // Reset stats at the beginning of a function. 187 // Reset stats at the beginning of a function.
143 void resetStats() { 188 void resetStats() {
144 if (ALLOW_DUMP) 189 if (ALLOW_DUMP)
145 StatsFunction.reset(); 190 TLS->StatsFunction.reset();
146 } 191 }
147 void dumpStats(const IceString &Name, bool Final = false); 192 void dumpStats(const IceString &Name, bool Final = false);
148 void statsUpdateEmitted(uint32_t InstCount) { 193 void statsUpdateEmitted(uint32_t InstCount) {
149 if (!ALLOW_DUMP) 194 if (!ALLOW_DUMP || !getFlags().DumpStats)
150 return; 195 return;
151 StatsFunction.updateEmitted(InstCount); 196 TLS->StatsFunction.updateEmitted(InstCount);
197 std::lock_guard<GlobalLockType> L(GlobalLock);
152 StatsCumulative.updateEmitted(InstCount); 198 StatsCumulative.updateEmitted(InstCount);
153 } 199 }
154 void statsUpdateRegistersSaved(uint32_t Num) { 200 void statsUpdateRegistersSaved(uint32_t Num) {
155 if (!ALLOW_DUMP) 201 if (!ALLOW_DUMP || !getFlags().DumpStats)
156 return; 202 return;
157 StatsFunction.updateRegistersSaved(Num); 203 TLS->StatsFunction.updateRegistersSaved(Num);
204 std::lock_guard<GlobalLockType> L(GlobalLock);
158 StatsCumulative.updateRegistersSaved(Num); 205 StatsCumulative.updateRegistersSaved(Num);
159 } 206 }
160 void statsUpdateFrameBytes(uint32_t Bytes) { 207 void statsUpdateFrameBytes(uint32_t Bytes) {
161 if (!ALLOW_DUMP) 208 if (!ALLOW_DUMP || !getFlags().DumpStats)
162 return; 209 return;
163 StatsFunction.updateFrameBytes(Bytes); 210 TLS->StatsFunction.updateFrameBytes(Bytes);
211 std::lock_guard<GlobalLockType> L(GlobalLock);
164 StatsCumulative.updateFrameBytes(Bytes); 212 StatsCumulative.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();
218 std::lock_guard<GlobalLockType> L(GlobalLock);
170 StatsCumulative.updateSpills(); 219 StatsCumulative.updateSpills();
171 } 220 }
172 void statsUpdateFills() { 221 void statsUpdateFills() {
173 if (!ALLOW_DUMP) 222 if (!ALLOW_DUMP || !getFlags().DumpStats)
174 return; 223 return;
175 StatsFunction.updateFills(); 224 TLS->StatsFunction.updateFills();
225 std::lock_guard<GlobalLockType> L(GlobalLock);
176 StatsCumulative.updateFills(); 226 StatsCumulative.updateFills();
177 } 227 }
178 228
179 // These are predefined TimerStackIdT values. 229 // These are predefined TimerStackIdT values.
180 enum TimerStackKind { 230 enum TimerStackKind {
181 TSK_Default = 0, 231 TSK_Default = 0,
182 TSK_Funcs, 232 TSK_Funcs,
183 TSK_Num 233 TSK_Num
184 }; 234 };
185 235
236 TimerStackIdT newTimerStackID(const IceString &Name);
186 TimerIdT getTimerID(TimerStackIdT StackID, const IceString &Name); 237 TimerIdT getTimerID(TimerStackIdT StackID, const IceString &Name);
187 TimerStackIdT newTimerStackID(const IceString &Name);
188 void pushTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default); 238 void pushTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default);
189 void popTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default); 239 void popTimer(TimerIdT ID, TimerStackIdT StackID = TSK_Default);
190 void resetTimer(TimerStackIdT StackID); 240 void resetTimer(TimerStackIdT StackID);
191 void setTimerName(TimerStackIdT StackID, const IceString &NewName); 241 void setTimerName(TimerStackIdT StackID, const IceString &NewName);
192 void dumpTimers(TimerStackIdT StackID = TSK_Default, 242 void dumpTimers(TimerStackIdT StackID = TSK_Default,
193 bool DumpCumulative = true); 243 bool DumpCumulative = true);
194 244
195 private: 245 private:
246 // GlobalLock is the default coarse-grain lock for accessing members
247 // of GlobalContext. As contention becomes an issue, more
248 // fine-grain locks can be added.
249 GlobalLockType GlobalLock;
250 // StrLock is a global lock on the dump and emit output streams.
251 // IsStrLocked is used to validate the locking protocol.
252 StrLockType StrLock;
253 bool IsStrLocked;
JF 2015/01/15 01:31:01 If the intent is to detect races then IsStrLocked
Jim Stichnoth 2015/01/15 07:16:29 I changed it so that IsStrLocked is only read or w
JF 2015/01/15 16:28:39 The goal of IsStrLocked is to guarantee thread-saf
Jim Stichnoth 2015/01/20 16:22:53 I removed the IsStrLocked field. The stream lock
254
196 Ostream *StrDump; // Stream for dumping / diagnostics 255 Ostream *StrDump; // Stream for dumping / diagnostics
197 Ostream *StrEmit; // Stream for code emission 256 Ostream *StrEmit; // Stream for code emission
198 257
199 ArenaAllocator<> Allocator; 258 ArenaAllocator<> Allocator;
200 VerboseMask VMask; 259 VerboseMask VMask;
201 std::unique_ptr<class ConstantPool> ConstPool; 260 std::unique_ptr<class ConstantPool> ConstPool;
202 Intrinsics IntrinsicsInfo; 261 Intrinsics IntrinsicsInfo;
203 const TargetArch Arch; 262 const TargetArch Arch;
204 const OptLevel Opt; 263 const OptLevel Opt;
205 const IceString TestPrefix; 264 const IceString TestPrefix;
206 const ClFlags &Flags; 265 const ClFlags &Flags;
207 RandomNumberGenerator RNG; 266 RandomNumberGenerator RNG;
208 std::unique_ptr<ELFObjectWriter> ObjectWriter; 267 std::unique_ptr<ELFObjectWriter> ObjectWriter;
209 CodeStats StatsFunction;
210 CodeStats StatsCumulative; 268 CodeStats StatsCumulative;
211 std::vector<TimerStack> Timers; 269 std::vector<TimerStack> Timers;
212 std::vector<GlobalDeclaration *> GlobalDeclarations; 270 std::vector<GlobalDeclaration *> GlobalDeclarations;
213 271
272 std::vector<ThreadContext *> AllThreadContexts;
273 // Each thread has its own TLS pointer which is also held in
274 // AllThreadContexts.
275 thread_local static ThreadContext *TLS;
276
214 // Private helpers for mangleName() 277 // Private helpers for mangleName()
215 typedef llvm::SmallVector<char, 32> ManglerVector; 278 typedef llvm::SmallVector<char, 32> ManglerVector;
216 void incrementSubstitutions(ManglerVector &OldName) const; 279 void incrementSubstitutions(ManglerVector &OldName) const;
217 }; 280 };
218 281
219 // Helper class to push and pop a timer marker. The constructor 282 // Helper class to push and pop a timer marker. The constructor
220 // pushes a marker, and the destructor pops it. This is for 283 // pushes a marker, and the destructor pops it. This is for
221 // convenient timing of regions of code. 284 // convenient timing of regions of code.
222 class TimerMarker { 285 class TimerMarker {
223 TimerMarker(const TimerMarker &) = delete; 286 TimerMarker(const TimerMarker &) = delete;
(...skipping 14 matching lines...) Expand all
238 if (ALLOW_DUMP && Active) 301 if (ALLOW_DUMP && Active)
239 Ctx->popTimer(ID); 302 Ctx->popTimer(ID);
240 } 303 }
241 304
242 private: 305 private:
243 TimerIdT ID; 306 TimerIdT ID;
244 GlobalContext *const Ctx; 307 GlobalContext *const Ctx;
245 bool Active; 308 bool Active;
246 }; 309 };
247 310
311 // Helper class for locking the streams and then automatically
312 // unlocking them.
313 class OstreamLocker {
314 private:
315 OstreamLocker(const OstreamLocker &) = delete;
316 OstreamLocker &operator=(const OstreamLocker &) = delete;
JF 2015/01/15 01:31:01 OStreamLocker() = delete;
Jim Stichnoth 2015/01/15 07:16:29 Done.
317
318 public:
319 explicit OstreamLocker(GlobalContext *Ctx) : Ctx(Ctx) { Ctx->lockStr(); }
320 ~OstreamLocker() { Ctx->unlockStr(); }
321
322 private:
323 GlobalContext *const Ctx;
324 };
325
248 } // end of namespace Ice 326 } // end of namespace Ice
249 327
250 #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H 328 #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H
OLDNEW
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceGlobalContext.cpp » ('j') | src/IceGlobalContext.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698