1 //===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===//
3 // The Subzero Code Generator
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines aspects of the compilation that persist across
11 // multiple functions.
13 //===----------------------------------------------------------------------===//
15 #include <ctype.h> // isdigit(), isupper()
16 #include <locale> // locale
17 #include <unordered_map>
19 #include "llvm/Support/Timer.h"
22 #include "IceClFlags.h"
24 #include "IceELFObjectWriter.h"
25 #include "IceGlobalContext.h"
26 #include "IceGlobalInits.h"
27 #include "IceOperand.h"
28 #include "IceTargetLowering.h"
29 #include "IceTimerTree.h"
33 template <> struct hash<Ice::RelocatableTuple> {
34 size_t operator()(const Ice::RelocatableTuple &Key) const {
35 return hash<Ice::IceString>()(Key.Name) +
36 hash<Ice::RelocOffsetT>()(Key.Offset);
39 } // end of namespace std
43 // TypePool maps constants of type KeyType (e.g. float) to pointers to
44 // type ValueType (e.g. ConstantFloat).
45 template <Type Ty, typename KeyType, typename ValueType> class TypePool {
46 TypePool(const TypePool &) = delete;
47 TypePool &operator=(const TypePool &) = delete;
50 TypePool() : NextPoolID(0) {}
51 ValueType *getOrAdd(GlobalContext *Ctx, KeyType Key) {
52 auto Iter = Pool.find(Key);
53 if (Iter != Pool.end())
55 ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++);
59 ConstantList getConstantPool() const {
60 ConstantList Constants;
61 Constants.reserve(Pool.size());
63 Constants.push_back(I.second);
68 typedef std::unordered_map<KeyType, ValueType *> ContainerType;
73 // UndefPool maps ICE types to the corresponding ConstantUndef values.
75 UndefPool(const UndefPool &) = delete;
76 UndefPool &operator=(const UndefPool &) = delete;
79 UndefPool() : NextPoolID(0), Pool(IceType_NUM) {}
81 ConstantUndef *getOrAdd(GlobalContext *Ctx, Type Ty) {
82 if (Pool[Ty] == nullptr)
83 Pool[Ty] = ConstantUndef::create(Ctx, Ty, NextPoolID++);
89 std::vector<ConstantUndef *> Pool;
92 // The global constant pool bundles individual pools of each type of
95 ConstantPool(const ConstantPool &) = delete;
96 ConstantPool &operator=(const ConstantPool &) = delete;
100 TypePool<IceType_f32, float, ConstantFloat> Floats;
101 TypePool<IceType_f64, double, ConstantDouble> Doubles;
102 TypePool<IceType_i1, int8_t, ConstantInteger32> Integers1;
103 TypePool<IceType_i8, int8_t, ConstantInteger32> Integers8;
104 TypePool<IceType_i16, int16_t, ConstantInteger32> Integers16;
105 TypePool<IceType_i32, int32_t, ConstantInteger32> Integers32;
106 TypePool<IceType_i64, int64_t, ConstantInteger64> Integers64;
107 TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable> Relocatables;
108 TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable>
113 void GlobalContext::CodeStats::dump(const IceString &Name, Ostream &Str) {
116 #define X(str, tag) \
117 Str << "|" << Name << "|" str "|" << Stats[CS_##tag] << "\n";
120 Str << "|" << Name << "|Spills+Fills|"
121 << Stats[CS_NumSpills] + Stats[CS_NumFills] << "\n";
122 Str << "|" << Name << "|Memory Usage|";
123 if (ssize_t MemUsed = llvm::TimeRecord::getCurrentTime(false).getMemUsed())
126 Str << "(requires '-track-memory')";
130 GlobalContext::GlobalContext(Ostream *OsDump, Ostream *OsEmit,
131 ELFStreamer *ELFStr, VerboseMask Mask,
132 TargetArch Arch, OptLevel Opt,
133 IceString TestPrefix, const ClFlags &Flags)
134 : ConstPool(new ConstantPool()), ErrorStatus(), StrDump(OsDump),
135 StrEmit(OsEmit), VMask(Mask), Arch(Arch), Opt(Opt),
136 TestPrefix(TestPrefix), Flags(Flags), RNG(""), ObjectWriter(),
137 CfgQ(/*MaxSize=*/Flags.NumTranslationThreads,
138 /*Sequential=*/(Flags.NumTranslationThreads == 0)) {
139 // Make sure thread_local fields are properly initialized before any
140 // accesses are made. Do this here instead of at the start of
141 // main() so that all clients (e.g. unit tests) can benefit for
143 GlobalContext::TlsInit();
145 // Create a new ThreadContext for the current thread. No need to
146 // lock AllThreadContexts at this point since no other threads have
147 // access yet to this GlobalContext object.
148 ThreadContext *MyTLS = new ThreadContext();
149 AllThreadContexts.push_back(MyTLS);
150 ICE_TLS_SET_FIELD(TLS, MyTLS);
151 // Pre-register built-in stack names.
153 // TODO(stichnot): There needs to be a strong relationship between
154 // the newTimerStackID() return values and TSK_Default/TSK_Funcs.
155 newTimerStackID("Total across all functions");
156 newTimerStackID("Per-function summary");
158 Timers.initInto(MyTLS->Timers);
159 if (Flags.UseELFWriter) {
160 ObjectWriter.reset(new ELFObjectWriter(*this, *ELFStr));
164 void GlobalContext::translateFunctions() {
165 while (std::unique_ptr<Cfg> Func = cfgQueueBlockingPop()) {
166 // Install Func in TLS for Cfg-specific container allocators.
167 Cfg::setCurrentCfg(Func.get());
168 // Reset per-function stats being accumulated in TLS.
170 // Set verbose level to none if the current function does NOT
171 // match the -verbose-focus command-line option.
172 if (!matchSymbolName(Func->getFunctionName(), getFlags().VerboseFocusOn))
173 Func->setVerbose(IceV_None);
174 // Disable translation if -notranslate is specified, or if the
175 // current function matches the -translate-only option. If
176 // translation is disabled, just dump the high-level IR and
178 if (getFlags().DisableTranslation ||
179 !matchSymbolName(Func->getFunctionName(), getFlags().TranslateOnly)) {
183 if (Func->hasError()) {
184 getErrorStatus()->assign(EC_Translation);
185 OstreamLocker L(this);
186 getStrDump() << "ICE translation error: " << Func->getError() << "\n";
188 if (getFlags().UseIntegratedAssembler)
192 // TODO(stichnot): actually add to emit queue
194 dumpStats(Func->getFunctionName());
196 Cfg::setCurrentCfg(nullptr);
197 // The Cfg now gets deleted as Func goes out of scope.
201 // Scan a string for S[0-9A-Z]*_ patterns and replace them with
202 // S<num>_ where <num> is the next base-36 value. If a type name
203 // legitimately contains that pattern, then the substitution will be
204 // made in error and most likely the link will fail. In this case,
205 // the test classes can be rewritten not to use that pattern, which is
206 // much simpler and more reliable than implementing a full demangling
207 // parser. Another substitution-in-error may occur if a type
208 // identifier ends with the pattern S[0-9A-Z]*, because an immediately
209 // following substitution string like "S1_" or "PS1_" may be combined
210 // with the previous type.
211 void GlobalContext::incrementSubstitutions(ManglerVector &OldName) const {
212 const std::locale CLocale("C");
213 // Provide extra space in case the length of <num> increases.
214 ManglerVector NewName(OldName.size() * 2);
217 size_t OldLen = OldName.size();
218 for (; OldPos < OldLen; ++OldPos, ++NewPos) {
219 if (OldName[OldPos] == '\0')
221 if (OldName[OldPos] == 'S') {
222 // Search forward until we find _ or invalid character (including \0).
226 for (Last = OldPos + 1; Last < OldLen; ++Last) {
227 char Ch = OldName[Last];
231 } else if (std::isdigit(Ch) || std::isupper(Ch, CLocale)) {
235 // Invalid character, stop searching.
240 NewName[NewPos++] = OldName[OldPos++]; // 'S'
241 size_t Length = Last - OldPos;
242 // NewPos and OldPos point just past the 'S'.
243 assert(NewName[NewPos - 1] == 'S');
244 assert(OldName[OldPos - 1] == 'S');
245 assert(OldName[OldPos + Length] == '_');
247 // Replace N 'Z' characters with a '0' (if N=0) or '1' (if
248 // N>0) followed by N '0' characters.
249 NewName[NewPos++] = (Length ? '1' : '0');
250 for (size_t i = 0; i < Length; ++i) {
251 NewName[NewPos++] = '0';
254 // Iterate right-to-left and increment the base-36 number.
256 for (size_t i = 0; i < Length; ++i) {
257 size_t Offset = Length - 1 - i;
258 char Ch = OldName[OldPos + Offset];
274 NewName[NewPos + Offset] = Ch;
279 // Fall through and let the '_' be copied across.
282 NewName[NewPos] = OldName[OldPos];
284 assert(NewName[NewPos] == '\0');
288 // In this context, name mangling means to rewrite a symbol using a
289 // given prefix. For a C++ symbol, nest the original symbol inside
290 // the "prefix" namespace. For other symbols, just prepend the
292 IceString GlobalContext::mangleName(const IceString &Name) const {
293 // An already-nested name like foo::bar() gets pushed down one
294 // level, making it equivalent to Prefix::foo::bar().
295 // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
296 // A non-nested but mangled name like bar() gets nested, making it
297 // equivalent to Prefix::bar().
298 // _Z3barxyz ==> ZN6Prefix3barExyz
299 // An unmangled, extern "C" style name, gets a simple prefix:
301 if (!ALLOW_DUMP || getTestPrefix().empty())
304 unsigned PrefixLength = getTestPrefix().length();
305 ManglerVector NameBase(1 + Name.length());
306 const size_t BufLen = 30 + Name.length() + PrefixLength;
307 ManglerVector NewName(BufLen);
308 uint32_t BaseLength = 0; // using uint32_t due to sscanf format string
310 int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data());
311 if (ItemsParsed == 1) {
312 // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
313 // (splice in "6Prefix") ^^^^^^^
314 snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength,
315 getTestPrefix().c_str(), NameBase.data());
316 // We ignore the snprintf return value (here and below). If we
317 // somehow miscalculated the output buffer length, the output will
318 // be truncated, but it will be truncated consistently for all
319 // mangleName() calls on the same input string.
320 incrementSubstitutions(NewName);
321 return NewName.data();
324 // Artificially limit BaseLength to 9 digits (less than 1 billion)
325 // because sscanf behavior is undefined on integer overflow. If
326 // there are more than 9 digits (which we test by looking at the
327 // beginning of NameBase), then we consider this a failure to parse
328 // a namespace mangling, and fall back to the simple prefixing.
329 ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data());
330 if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) &&
331 !isdigit(NameBase[0])) {
332 // Transform _Z3barxyz ==> _ZN6Prefix3barExyz
334 // (splice in "N6Prefix", and insert "E" after "3bar")
335 // But an "I" after the identifier indicates a template argument
336 // list terminated with "E"; insert the new "E" before/after the
338 // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz
340 // (splice in "N6Prefix", and insert "E" after "3barIabcE")
341 ManglerVector OrigName(Name.length());
342 ManglerVector OrigSuffix(Name.length());
343 uint32_t ActualBaseLength = BaseLength;
344 if (NameBase[ActualBaseLength] == 'I') {
346 while (NameBase[ActualBaseLength] != 'E' &&
347 NameBase[ActualBaseLength] != '\0')
350 strncpy(OrigName.data(), NameBase.data(), ActualBaseLength);
351 OrigName[ActualBaseLength] = '\0';
352 strcpy(OrigSuffix.data(), NameBase.data() + ActualBaseLength);
353 snprintf(NewName.data(), BufLen, "_ZN%u%s%u%sE%s", PrefixLength,
354 getTestPrefix().c_str(), BaseLength, OrigName.data(),
356 incrementSubstitutions(NewName);
357 return NewName.data();
360 // Transform bar ==> Prefixbar
362 return getTestPrefix() + Name;
365 GlobalContext::~GlobalContext() {
366 llvm::DeleteContainerPointers(AllThreadContexts);
369 // TODO(stichnot): Consider adding thread-local caches of constant
370 // pool entries to reduce contention.
372 // All locking is done by the getConstantInt[0-9]+() target function.
373 Constant *GlobalContext::getConstantInt(Type Ty, int64_t Value) {
376 return getConstantInt1(Value);
378 return getConstantInt8(Value);
380 return getConstantInt16(Value);
382 return getConstantInt32(Value);
384 return getConstantInt64(Value);
386 llvm_unreachable("Bad integer type for getConstant");
391 Constant *GlobalContext::getConstantInt1(int8_t ConstantInt1) {
392 ConstantInt1 &= INT8_C(1);
393 return getConstPool()->Integers1.getOrAdd(this, ConstantInt1);
396 Constant *GlobalContext::getConstantInt8(int8_t ConstantInt8) {
397 return getConstPool()->Integers8.getOrAdd(this, ConstantInt8);
400 Constant *GlobalContext::getConstantInt16(int16_t ConstantInt16) {
401 return getConstPool()->Integers16.getOrAdd(this, ConstantInt16);
404 Constant *GlobalContext::getConstantInt32(int32_t ConstantInt32) {
405 return getConstPool()->Integers32.getOrAdd(this, ConstantInt32);
408 Constant *GlobalContext::getConstantInt64(int64_t ConstantInt64) {
409 return getConstPool()->Integers64.getOrAdd(this, ConstantInt64);
412 Constant *GlobalContext::getConstantFloat(float ConstantFloat) {
413 return getConstPool()->Floats.getOrAdd(this, ConstantFloat);
416 Constant *GlobalContext::getConstantDouble(double ConstantDouble) {
417 return getConstPool()->Doubles.getOrAdd(this, ConstantDouble);
420 Constant *GlobalContext::getConstantSym(RelocOffsetT Offset,
421 const IceString &Name,
422 bool SuppressMangling) {
423 return getConstPool()->Relocatables.getOrAdd(
424 this, RelocatableTuple(Offset, Name, SuppressMangling));
427 Constant *GlobalContext::getConstantExternSym(const IceString &Name) {
428 const RelocOffsetT Offset = 0;
429 const bool SuppressMangling = true;
430 return getConstPool()->ExternRelocatables.getOrAdd(
431 this, RelocatableTuple(Offset, Name, SuppressMangling));
434 Constant *GlobalContext::getConstantUndef(Type Ty) {
435 return getConstPool()->Undefs.getOrAdd(this, Ty);
438 // All locking is done by the getConstant*() target function.
439 Constant *GlobalContext::getConstantZero(Type Ty) {
442 return getConstantInt1(0);
444 return getConstantInt8(0);
446 return getConstantInt16(0);
448 return getConstantInt32(0);
450 return getConstantInt64(0);
452 return getConstantFloat(0);
454 return getConstantDouble(0);
461 case IceType_v4f32: {
463 llvm::raw_string_ostream BaseOS(Str);
464 BaseOS << "Unsupported constant type: " << Ty;
465 llvm_unreachable(BaseOS.str().c_str());
471 llvm_unreachable("Unknown type");
474 ConstantList GlobalContext::getConstantPool(Type Ty) {
480 return getConstPool()->Integers32.getConstantPool();
482 return getConstPool()->Integers64.getConstantPool();
484 return getConstPool()->Floats.getConstantPool();
486 return getConstPool()->Doubles.getConstantPool();
493 case IceType_v4f32: {
495 llvm::raw_string_ostream BaseOS(Str);
496 BaseOS << "Unsupported constant type: " << Ty;
497 llvm_unreachable(BaseOS.str().c_str());
503 llvm_unreachable("Unknown type");
506 ConstantList GlobalContext::getConstantExternSyms() {
507 return getConstPool()->ExternRelocatables.getConstantPool();
510 TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) {
513 auto Timers = getTimers();
514 TimerStackIdT NewID = Timers->size();
515 Timers->push_back(TimerStack(Name));
519 TimerIdT GlobalContext::getTimerID(TimerStackIdT StackID,
520 const IceString &Name) {
521 auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers;
522 assert(StackID < Timers->size());
523 return Timers->at(StackID).getTimerID(Name);
526 void GlobalContext::pushTimer(TimerIdT ID, TimerStackIdT StackID) {
527 auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers;
528 assert(StackID < Timers->size());
529 Timers->at(StackID).push(ID);
532 void GlobalContext::popTimer(TimerIdT ID, TimerStackIdT StackID) {
533 auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers;
534 assert(StackID < Timers->size());
535 Timers->at(StackID).pop(ID);
538 void GlobalContext::resetTimer(TimerStackIdT StackID) {
539 auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers;
540 assert(StackID < Timers->size());
541 Timers->at(StackID).reset();
544 void GlobalContext::setTimerName(TimerStackIdT StackID,
545 const IceString &NewName) {
546 auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers;
547 assert(StackID < Timers->size());
548 Timers->at(StackID).setName(NewName);
551 // Note: cfgQueueBlockingPush and cfgQueueBlockingPop use unique_ptr
552 // at the interface to take and transfer ownership, but they
553 // internally store the raw Cfg pointer in the work queue. This
554 // allows e.g. future queue optimizations such as the use of atomics
555 // to modify queue elements.
556 void GlobalContext::cfgQueueBlockingPush(std::unique_ptr<Cfg> Func) {
557 CfgQ.blockingPush(Func.release());
560 std::unique_ptr<Cfg> GlobalContext::cfgQueueBlockingPop() {
561 return std::unique_ptr<Cfg>(CfgQ.blockingPop());
564 void GlobalContext::dumpStats(const IceString &Name, bool Final) {
565 if (!ALLOW_DUMP || !getFlags().DumpStats)
567 OstreamLocker OL(this);
569 getStatsCumulative()->dump(Name, getStrDump());
571 ICE_TLS_GET_FIELD(TLS)->StatsFunction.dump(Name, getStrDump());
575 void GlobalContext::dumpTimers(TimerStackIdT StackID, bool DumpCumulative) {
578 auto Timers = getTimers();
579 assert(Timers->size() > StackID);
580 OstreamLocker L(this);
581 Timers->at(StackID).dump(getStrDump(), DumpCumulative);
584 void TimerMarker::push() {
586 case GlobalContext::TSK_Default:
587 Active = Ctx->getFlags().SubzeroTimingEnabled;
589 case GlobalContext::TSK_Funcs:
590 Active = Ctx->getFlags().TimeEachFunction;
596 Ctx->pushTimer(ID, StackID);
599 void TimerMarker::pushCfg(const Cfg *Func) {
600 Ctx = Func->getContext();
601 Active = Func->getFocusedTiming() || Ctx->getFlags().SubzeroTimingEnabled;
603 Ctx->pushTimer(ID, StackID);
606 ICE_TLS_DEFINE_FIELD(GlobalContext::ThreadContext *, GlobalContext, TLS);
608 } // end of namespace Ice