OLD | NEW |
---|---|
1 //===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===// | 1 //===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===// |
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 defines aspects of the compilation that persist across | 10 // This file defines aspects of the compilation that persist across |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 TypePool<IceType_f64, double, ConstantDouble> Doubles; | 101 TypePool<IceType_f64, double, ConstantDouble> Doubles; |
102 TypePool<IceType_i1, int8_t, ConstantInteger32> Integers1; | 102 TypePool<IceType_i1, int8_t, ConstantInteger32> Integers1; |
103 TypePool<IceType_i8, int8_t, ConstantInteger32> Integers8; | 103 TypePool<IceType_i8, int8_t, ConstantInteger32> Integers8; |
104 TypePool<IceType_i16, int16_t, ConstantInteger32> Integers16; | 104 TypePool<IceType_i16, int16_t, ConstantInteger32> Integers16; |
105 TypePool<IceType_i32, int32_t, ConstantInteger32> Integers32; | 105 TypePool<IceType_i32, int32_t, ConstantInteger32> Integers32; |
106 TypePool<IceType_i64, int64_t, ConstantInteger64> Integers64; | 106 TypePool<IceType_i64, int64_t, ConstantInteger64> Integers64; |
107 TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable> Relocatables; | 107 TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable> Relocatables; |
108 UndefPool Undefs; | 108 UndefPool Undefs; |
109 }; | 109 }; |
110 | 110 |
111 void CodeStats::dump(const IceString &Name, Ostream &Str) { | 111 void GlobalContext::CodeStats::dump(const IceString &Name, Ostream &Str) { |
112 if (!ALLOW_DUMP) | 112 if (!ALLOW_DUMP) |
113 return; | 113 return; |
114 Str << "|" << Name << "|Inst Count |" << InstructionsEmitted << "\n"; | 114 Str << "|" << Name << "|Inst Count |" << InstructionsEmitted << "\n"; |
115 Str << "|" << Name << "|Regs Saved |" << RegistersSaved << "\n"; | 115 Str << "|" << Name << "|Regs Saved |" << RegistersSaved << "\n"; |
116 Str << "|" << Name << "|Frame Bytes |" << FrameBytes << "\n"; | 116 Str << "|" << Name << "|Frame Bytes |" << FrameBytes << "\n"; |
117 Str << "|" << Name << "|Spills |" << Spills << "\n"; | 117 Str << "|" << Name << "|Spills |" << Spills << "\n"; |
118 Str << "|" << Name << "|Fills |" << Fills << "\n"; | 118 Str << "|" << Name << "|Fills |" << Fills << "\n"; |
119 Str << "|" << Name << "|Spills+Fills|" << Spills + Fills << "\n"; | 119 Str << "|" << Name << "|Spills+Fills|" << Spills + Fills << "\n"; |
120 Str << "|" << Name << "|Memory Usage|"; | 120 Str << "|" << Name << "|Memory Usage|"; |
121 if (ssize_t MemUsed = llvm::TimeRecord::getCurrentTime(false).getMemUsed()) | 121 if (ssize_t MemUsed = llvm::TimeRecord::getCurrentTime(false).getMemUsed()) |
122 Str << MemUsed; | 122 Str << MemUsed; |
123 else | 123 else |
124 Str << "(requires '-track-memory')"; | 124 Str << "(requires '-track-memory')"; |
125 Str << "\n"; | 125 Str << "\n"; |
126 } | 126 } |
127 | 127 |
128 GlobalContext::GlobalContext(Ostream *OsDump, Ostream *OsEmit, | 128 GlobalContext::GlobalContext(Ostream *OsDump, Ostream *OsEmit, |
129 ELFStreamer *ELFStr, VerboseMask Mask, | 129 ELFStreamer *ELFStr, VerboseMask Mask, |
130 TargetArch Arch, OptLevel Opt, | 130 TargetArch Arch, OptLevel Opt, |
131 IceString TestPrefix, const ClFlags &Flags) | 131 IceString TestPrefix, const ClFlags &Flags) |
132 : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask), | 132 : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask), |
133 ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt), | 133 ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt), |
134 TestPrefix(TestPrefix), Flags(Flags), RNG(""), ObjectWriter() { | 134 TestPrefix(TestPrefix), Flags(Flags), RNG(""), ObjectWriter() { |
135 // Create a new ThreadContext for the current thread. No need to | |
136 // lock AllThreadContexts at this point since no other threads have | |
137 // access yet to this GlobalContext object. | |
138 AllThreadContexts.push_back(new ThreadContext()); | |
139 TLS = AllThreadContexts.back(); | |
135 // Pre-register built-in stack names. | 140 // Pre-register built-in stack names. |
136 if (ALLOW_DUMP) { | 141 if (ALLOW_DUMP) { |
142 // TODO(stichnot): There needs to be a strong relationship between | |
143 // the newTimerStackID() return values and TSK_Default/TSK_Funcs. | |
137 newTimerStackID("Total across all functions"); | 144 newTimerStackID("Total across all functions"); |
138 newTimerStackID("Per-function summary"); | 145 newTimerStackID("Per-function summary"); |
139 } | 146 } |
140 if (Flags.UseELFWriter) { | 147 if (Flags.UseELFWriter) { |
141 ObjectWriter.reset(new ELFObjectWriter(*this, *ELFStr)); | 148 ObjectWriter.reset(new ELFObjectWriter(*this, *ELFStr)); |
142 } | 149 } |
143 } | 150 } |
144 | 151 |
145 // Scan a string for S[0-9A-Z]*_ patterns and replace them with | 152 // Scan a string for S[0-9A-Z]*_ patterns and replace them with |
146 // S<num>_ where <num> is the next base-36 value. If a type name | 153 // S<num>_ where <num> is the next base-36 value. If a type name |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 return NewName.data(); | 308 return NewName.data(); |
302 } | 309 } |
303 | 310 |
304 // Transform bar ==> Prefixbar | 311 // Transform bar ==> Prefixbar |
305 // ^^^^^^ | 312 // ^^^^^^ |
306 return getTestPrefix() + Name; | 313 return getTestPrefix() + Name; |
307 } | 314 } |
308 | 315 |
309 GlobalContext::~GlobalContext() { | 316 GlobalContext::~GlobalContext() { |
310 llvm::DeleteContainerPointers(GlobalDeclarations); | 317 llvm::DeleteContainerPointers(GlobalDeclarations); |
318 llvm::DeleteContainerPointers(AllThreadContexts); | |
311 } | 319 } |
312 | 320 |
321 // TODO(stichnot): Consider adding thread-local caches of constant | |
322 // pool entries to reduce contention. | |
323 | |
324 // All locking is done by the getConstantInt[0-9]+() target function. | |
313 Constant *GlobalContext::getConstantInt(Type Ty, int64_t Value) { | 325 Constant *GlobalContext::getConstantInt(Type Ty, int64_t Value) { |
314 switch (Ty) { | 326 switch (Ty) { |
315 case IceType_i1: | 327 case IceType_i1: |
316 return getConstantInt1(Value); | 328 return getConstantInt1(Value); |
317 case IceType_i8: | 329 case IceType_i8: |
318 return getConstantInt8(Value); | 330 return getConstantInt8(Value); |
319 case IceType_i16: | 331 case IceType_i16: |
320 return getConstantInt16(Value); | 332 return getConstantInt16(Value); |
321 case IceType_i32: | 333 case IceType_i32: |
322 return getConstantInt32(Value); | 334 return getConstantInt32(Value); |
323 case IceType_i64: | 335 case IceType_i64: |
324 return getConstantInt64(Value); | 336 return getConstantInt64(Value); |
325 default: | 337 default: |
326 llvm_unreachable("Bad integer type for getConstant"); | 338 llvm_unreachable("Bad integer type for getConstant"); |
327 } | 339 } |
328 return nullptr; | 340 return nullptr; |
329 } | 341 } |
330 | 342 |
331 Constant *GlobalContext::getConstantInt1(int8_t ConstantInt1) { | 343 Constant *GlobalContext::getConstantInt1(int8_t ConstantInt1) { |
332 ConstantInt1 &= INT8_C(1); | 344 ConstantInt1 &= INT8_C(1); |
333 return ConstPool->Integers1.getOrAdd(this, ConstantInt1); | 345 return getConstPool()->Integers1.getOrAdd(this, ConstantInt1); |
334 } | 346 } |
335 | 347 |
336 Constant *GlobalContext::getConstantInt8(int8_t ConstantInt8) { | 348 Constant *GlobalContext::getConstantInt8(int8_t ConstantInt8) { |
337 return ConstPool->Integers8.getOrAdd(this, ConstantInt8); | 349 return getConstPool()->Integers8.getOrAdd(this, ConstantInt8); |
338 } | 350 } |
339 | 351 |
340 Constant *GlobalContext::getConstantInt16(int16_t ConstantInt16) { | 352 Constant *GlobalContext::getConstantInt16(int16_t ConstantInt16) { |
341 return ConstPool->Integers16.getOrAdd(this, ConstantInt16); | 353 return getConstPool()->Integers16.getOrAdd(this, ConstantInt16); |
342 } | 354 } |
343 | 355 |
344 Constant *GlobalContext::getConstantInt32(int32_t ConstantInt32) { | 356 Constant *GlobalContext::getConstantInt32(int32_t ConstantInt32) { |
345 return ConstPool->Integers32.getOrAdd(this, ConstantInt32); | 357 return getConstPool()->Integers32.getOrAdd(this, ConstantInt32); |
346 } | 358 } |
347 | 359 |
348 Constant *GlobalContext::getConstantInt64(int64_t ConstantInt64) { | 360 Constant *GlobalContext::getConstantInt64(int64_t ConstantInt64) { |
349 return ConstPool->Integers64.getOrAdd(this, ConstantInt64); | 361 return getConstPool()->Integers64.getOrAdd(this, ConstantInt64); |
350 } | 362 } |
351 | 363 |
352 Constant *GlobalContext::getConstantFloat(float ConstantFloat) { | 364 Constant *GlobalContext::getConstantFloat(float ConstantFloat) { |
353 return ConstPool->Floats.getOrAdd(this, ConstantFloat); | 365 return getConstPool()->Floats.getOrAdd(this, ConstantFloat); |
354 } | 366 } |
355 | 367 |
356 Constant *GlobalContext::getConstantDouble(double ConstantDouble) { | 368 Constant *GlobalContext::getConstantDouble(double ConstantDouble) { |
357 return ConstPool->Doubles.getOrAdd(this, ConstantDouble); | 369 return getConstPool()->Doubles.getOrAdd(this, ConstantDouble); |
358 } | 370 } |
359 | 371 |
360 Constant *GlobalContext::getConstantSym(RelocOffsetT Offset, | 372 Constant *GlobalContext::getConstantSym(RelocOffsetT Offset, |
361 const IceString &Name, | 373 const IceString &Name, |
362 bool SuppressMangling) { | 374 bool SuppressMangling) { |
363 return ConstPool->Relocatables.getOrAdd( | 375 return getConstPool()->Relocatables.getOrAdd( |
364 this, RelocatableTuple(Offset, Name, SuppressMangling)); | 376 this, RelocatableTuple(Offset, Name, SuppressMangling)); |
365 } | 377 } |
366 | 378 |
367 Constant *GlobalContext::getConstantUndef(Type Ty) { | 379 Constant *GlobalContext::getConstantUndef(Type Ty) { |
368 return ConstPool->Undefs.getOrAdd(this, Ty); | 380 return getConstPool()->Undefs.getOrAdd(this, Ty); |
369 } | 381 } |
370 | 382 |
383 // All locking is done by the getConstant*() target function. | |
371 Constant *GlobalContext::getConstantZero(Type Ty) { | 384 Constant *GlobalContext::getConstantZero(Type Ty) { |
372 switch (Ty) { | 385 switch (Ty) { |
373 case IceType_i1: | 386 case IceType_i1: |
374 return getConstantInt1(0); | 387 return getConstantInt1(0); |
375 case IceType_i8: | 388 case IceType_i8: |
376 return getConstantInt8(0); | 389 return getConstantInt8(0); |
377 case IceType_i16: | 390 case IceType_i16: |
378 return getConstantInt16(0); | 391 return getConstantInt16(0); |
379 case IceType_i32: | 392 case IceType_i32: |
380 return getConstantInt32(0); | 393 return getConstantInt32(0); |
(...skipping 15 matching lines...) Expand all Loading... | |
396 BaseOS << "Unsupported constant type: " << Ty; | 409 BaseOS << "Unsupported constant type: " << Ty; |
397 llvm_unreachable(BaseOS.str().c_str()); | 410 llvm_unreachable(BaseOS.str().c_str()); |
398 } break; | 411 } break; |
399 case IceType_void: | 412 case IceType_void: |
400 case IceType_NUM: | 413 case IceType_NUM: |
401 break; | 414 break; |
402 } | 415 } |
403 llvm_unreachable("Unknown type"); | 416 llvm_unreachable("Unknown type"); |
404 } | 417 } |
405 | 418 |
406 ConstantList GlobalContext::getConstantPool(Type Ty) const { | 419 ConstantList GlobalContext::getConstantPool(Type Ty) { |
407 switch (Ty) { | 420 switch (Ty) { |
408 case IceType_i1: | 421 case IceType_i1: |
409 case IceType_i8: | 422 case IceType_i8: |
410 case IceType_i16: | 423 case IceType_i16: |
411 case IceType_i32: | 424 case IceType_i32: |
412 return ConstPool->Integers32.getConstantPool(); | 425 return getConstPool()->Integers32.getConstantPool(); |
413 case IceType_i64: | 426 case IceType_i64: |
414 return ConstPool->Integers64.getConstantPool(); | 427 return getConstPool()->Integers64.getConstantPool(); |
415 case IceType_f32: | 428 case IceType_f32: |
416 return ConstPool->Floats.getConstantPool(); | 429 return getConstPool()->Floats.getConstantPool(); |
417 case IceType_f64: | 430 case IceType_f64: |
418 return ConstPool->Doubles.getConstantPool(); | 431 return getConstPool()->Doubles.getConstantPool(); |
419 case IceType_v4i1: | 432 case IceType_v4i1: |
420 case IceType_v8i1: | 433 case IceType_v8i1: |
421 case IceType_v16i1: | 434 case IceType_v16i1: |
422 case IceType_v16i8: | 435 case IceType_v16i8: |
423 case IceType_v8i16: | 436 case IceType_v8i16: |
424 case IceType_v4i32: | 437 case IceType_v4i32: |
425 case IceType_v4f32: { | 438 case IceType_v4f32: { |
426 IceString Str; | 439 IceString Str; |
427 llvm::raw_string_ostream BaseOS(Str); | 440 llvm::raw_string_ostream BaseOS(Str); |
428 BaseOS << "Unsupported constant type: " << Ty; | 441 BaseOS << "Unsupported constant type: " << Ty; |
429 llvm_unreachable(BaseOS.str().c_str()); | 442 llvm_unreachable(BaseOS.str().c_str()); |
430 } break; | 443 } break; |
431 case IceType_void: | 444 case IceType_void: |
432 case IceType_NUM: | 445 case IceType_NUM: |
433 break; | 446 break; |
434 } | 447 } |
435 llvm_unreachable("Unknown type"); | 448 llvm_unreachable("Unknown type"); |
436 } | 449 } |
437 | 450 |
451 // No locking because only the bitcode parser thread calls it. | |
452 // TODO(stichnot,kschimpf): GlobalContext::GlobalDeclarations actually | |
453 // seems to be unused. If so, remove that field and this method. | |
Karl
2015/01/20 20:00:36
Feel free to remove.
| |
438 FunctionDeclaration * | 454 FunctionDeclaration * |
439 GlobalContext::newFunctionDeclaration(const FuncSigType *Signature, | 455 GlobalContext::newFunctionDeclaration(const FuncSigType *Signature, |
440 unsigned CallingConv, unsigned Linkage, | 456 unsigned CallingConv, unsigned Linkage, |
441 bool IsProto) { | 457 bool IsProto) { |
442 FunctionDeclaration *Func = new FunctionDeclaration( | 458 FunctionDeclaration *Func = new FunctionDeclaration( |
443 *Signature, static_cast<llvm::CallingConv::ID>(CallingConv), | 459 *Signature, static_cast<llvm::CallingConv::ID>(CallingConv), |
444 static_cast<llvm::GlobalValue::LinkageTypes>(Linkage), IsProto); | 460 static_cast<llvm::GlobalValue::LinkageTypes>(Linkage), IsProto); |
445 GlobalDeclarations.push_back(Func); | 461 GlobalDeclarations.push_back(Func); |
446 return Func; | 462 return Func; |
447 } | 463 } |
448 | 464 |
465 // No locking because only the bitcode parser thread calls it. | |
466 // TODO(stichnot,kschimpf): GlobalContext::GlobalDeclarations actually | |
467 // seems to be unused. If so, remove that field and this method. | |
Karl
2015/01/20 20:00:36
Feel free to remove.
| |
449 VariableDeclaration *GlobalContext::newVariableDeclaration() { | 468 VariableDeclaration *GlobalContext::newVariableDeclaration() { |
450 VariableDeclaration *Var = new VariableDeclaration(); | 469 VariableDeclaration *Var = new VariableDeclaration(); |
451 GlobalDeclarations.push_back(Var); | 470 GlobalDeclarations.push_back(Var); |
452 return Var; | 471 return Var; |
453 } | 472 } |
454 | 473 |
455 TimerIdT GlobalContext::getTimerID(TimerStackIdT StackID, | |
456 const IceString &Name) { | |
457 assert(StackID < Timers.size()); | |
458 return Timers[StackID].getTimerID(Name); | |
459 } | |
460 | |
461 TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) { | 474 TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) { |
462 if (!ALLOW_DUMP) | 475 if (!ALLOW_DUMP) |
463 return 0; | 476 return 0; |
464 TimerStackIdT NewID = Timers.size(); | 477 auto Timers = getTimers(); |
465 Timers.push_back(TimerStack(Name)); | 478 TimerStackIdT NewID = Timers->size(); |
479 Timers->push_back(TimerStack(Name)); | |
466 return NewID; | 480 return NewID; |
467 } | 481 } |
468 | 482 |
483 TimerIdT GlobalContext::getTimerID(TimerStackIdT StackID, | |
484 const IceString &Name) { | |
485 auto Timers = getTimers(); | |
486 assert(StackID < Timers->size()); | |
487 return Timers->at(StackID).getTimerID(Name); | |
488 } | |
489 | |
469 void GlobalContext::pushTimer(TimerIdT ID, TimerStackIdT StackID) { | 490 void GlobalContext::pushTimer(TimerIdT ID, TimerStackIdT StackID) { |
470 assert(StackID < Timers.size()); | 491 auto Timers = getTimers(); |
471 Timers[StackID].push(ID); | 492 assert(StackID < Timers->size()); |
493 Timers->at(StackID).push(ID); | |
472 } | 494 } |
473 | 495 |
474 void GlobalContext::popTimer(TimerIdT ID, TimerStackIdT StackID) { | 496 void GlobalContext::popTimer(TimerIdT ID, TimerStackIdT StackID) { |
475 assert(StackID < Timers.size()); | 497 auto Timers = getTimers(); |
476 Timers[StackID].pop(ID); | 498 assert(StackID < Timers->size()); |
499 Timers->at(StackID).pop(ID); | |
477 } | 500 } |
478 | 501 |
479 void GlobalContext::resetTimer(TimerStackIdT StackID) { | 502 void GlobalContext::resetTimer(TimerStackIdT StackID) { |
480 assert(StackID < Timers.size()); | 503 auto Timers = getTimers(); |
481 Timers[StackID].reset(); | 504 assert(StackID < Timers->size()); |
505 Timers->at(StackID).reset(); | |
482 } | 506 } |
483 | 507 |
484 void GlobalContext::setTimerName(TimerStackIdT StackID, | 508 void GlobalContext::setTimerName(TimerStackIdT StackID, |
485 const IceString &NewName) { | 509 const IceString &NewName) { |
486 assert(StackID < Timers.size()); | 510 auto Timers = getTimers(); |
487 Timers[StackID].setName(NewName); | 511 assert(StackID < Timers->size()); |
512 Timers->at(StackID).setName(NewName); | |
488 } | 513 } |
489 | 514 |
490 void GlobalContext::dumpStats(const IceString &Name, bool Final) { | 515 void GlobalContext::dumpStats(const IceString &Name, bool Final) { |
491 if (!ALLOW_DUMP) | 516 if (!ALLOW_DUMP || !getFlags().DumpStats) |
492 return; | 517 return; |
493 if (Flags.DumpStats) { | 518 OstreamLocker OL(this); |
494 if (Final) { | 519 if (Final) { |
495 StatsCumulative.dump(Name, getStrDump()); | 520 getStatsCumulative()->dump(Name, getStrDump()); |
496 } else { | 521 } else { |
497 StatsFunction.dump(Name, getStrDump()); | 522 TLS->StatsFunction.dump(Name, getStrDump()); |
498 StatsCumulative.dump("_TOTAL_", getStrDump()); | 523 getStatsCumulative()->dump("_TOTAL_", getStrDump()); |
499 } | |
500 } | 524 } |
501 } | 525 } |
502 | 526 |
503 void GlobalContext::dumpTimers(TimerStackIdT StackID, bool DumpCumulative) { | 527 void GlobalContext::dumpTimers(TimerStackIdT StackID, bool DumpCumulative) { |
504 if (!ALLOW_DUMP) | 528 if (!ALLOW_DUMP) |
505 return; | 529 return; |
506 assert(Timers.size() > StackID); | 530 auto Timers = getTimers(); |
507 Timers[StackID].dump(getStrDump(), DumpCumulative); | 531 assert(Timers->size() > StackID); |
532 OstreamLocker L(this); | |
533 Timers->at(StackID).dump(getStrDump(), DumpCumulative); | |
508 } | 534 } |
509 | 535 |
510 TimerMarker::TimerMarker(TimerIdT ID, const Cfg *Func) | 536 TimerMarker::TimerMarker(TimerIdT ID, const Cfg *Func) |
511 : ID(ID), Ctx(Func->getContext()), Active(false) { | 537 : ID(ID), Ctx(Func->getContext()), Active(false) { |
512 if (ALLOW_DUMP) { | 538 if (ALLOW_DUMP) { |
513 Active = Func->getFocusedTiming() || Ctx->getFlags().SubzeroTimingEnabled; | 539 Active = Func->getFocusedTiming() || Ctx->getFlags().SubzeroTimingEnabled; |
514 if (Active) | 540 if (Active) |
515 Ctx->pushTimer(ID); | 541 Ctx->pushTimer(ID); |
516 } | 542 } |
517 } | 543 } |
518 | 544 |
545 thread_local GlobalContext::ThreadContext *GlobalContext::TLS; | |
546 | |
519 } // end of namespace Ice | 547 } // end of namespace Ice |
OLD | NEW |