1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#ifndef DEBUG_INFO_H
#define DEBUG_INFO_H
#include "BuildCISAIR.h"
#include "Common_BinaryEncoding.h"
#include "FlowGraph.h"
#include "GraphColor.h"
#include "Mem_Manager.h"
// clang-format off
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Support/Allocator.h"
#include "common/LLVMWarningsPop.hpp"
// clang-format on
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace vISA {
class G4_Declare;
class LiveRange;
}
class CISA_IR_Builder;
class VISAKernelImpl;
struct VarnameMap;
namespace vISA {
class DebugInfoState;
}
int decodeAndDumpDebugInfo(char *filename, TARGET_PLATFORM platform);
void emitDebugInfo(VISAKernelImpl *kernel,
CISA_IR_Builder::KernelListTy &functions,
std::string filename);
void emitDebugInfoToMem(VISAKernelImpl *kernel,
CISA_IR_Builder::KernelListTy &functions, void *&info,
unsigned &size);
struct IDX_VDbgCisaByte2Gen {
unsigned CisaByteOffset;
unsigned GenOffset;
};
struct IDX_VDbgCisaIndex2Gen {
unsigned CisaIndex;
unsigned GenOffset;
};
struct IDX_VDbgGen2CisaIndex {
unsigned GenOffset;
unsigned VisaIndex;
};
void generateCISAByteOffsetFromOffset(
std::map<unsigned int, unsigned int> &mapCISAIndexCISAOffset,
std::vector<IDX_VDbgCisaIndex2Gen> &mapCISAIndexGenOffset,
std::vector<IDX_VDbgCisaByte2Gen> &mapCISAOffsetGenOffset);
void generateByteOffsetMapping(
vISA::G4_Kernel &kernel,
std::vector<std::pair<unsigned int, unsigned int>> &mapping,
std::list<vISA::G4_BB *> &stackCallEntryBBs);
void updateRelocOffset(VISAKernelImpl &kernel);
void resetGenOffsets(vISA::G4_Kernel &kernel);
void addCallFrameInfo(VISAKernelImpl *kernel);
// For ranges colored during graph coloring
void updateDebugInfo(vISA::G4_Kernel &kernel, vISA::G4_INST *inst,
const vISA::LivenessAnalysis &liveAnalysis,
const LiveRangeVec& lrs, SparseBitVector &live,
vISA::DebugInfoState *state, bool closeAllOpenIntervals);
// For ranges allocated by local RA
void updateDebugInfo(vISA::G4_Kernel &kernel,
std::vector<vISA::LocalLiveRange *> &liveIntervals);
// For ranges allocated by global linear scan
void updateDebugInfo(vISA::G4_Kernel &kernel,
std::vector<vISA::LSLiveRange *> &liveIntervals);
// For ranges updated by augmentation
void updateDebugInfo(vISA::G4_Kernel &kernel,
std::vector<std::tuple<vISA::G4_Declare *, vISA::G4_INST *,
vISA::G4_INST *>>
augmentationLiveIntervals);
// For ranges assigned using unique assignments in local RA
void updateDebugInfo(vISA::G4_Kernel &kernel, vISA::G4_Declare *dcl,
uint32_t start, uint32_t end);
void updateDebugInfo(vISA::G4_Kernel &kernel, vISA::G4_Declare *dcl,
uint32_t offset);
void updateCallStackLiveIntervals(vISA::G4_Kernel &kernel);
#define DEBUG_MAGIC_NUMBER ((unsigned int)0xdeadd010)
// Format of debug info
struct VarnameMap {
#define VARMAP_VREG_FILE_ADDRESS 0
#define VARMAP_VREG_FILE_FLAG 1
#define VARMAP_VREG_FILE_GRF 2
// 0 - address, 1 - flag, 2 - GRF
uint8_t virtualType;
#define VARMAP_PREG_FILE_ADDRESS 0
#define VARMAP_PREG_FILE_FLAG 1
#define VARMAP_PREG_FILE_GRF 2
#define VARMAP_PREG_FILE_MEMORY 3
// Physical register type allocated by RA: 0 - address, 1 - flag, 2 - GRF, 3 -
// memory
uint8_t physicalType;
union Mapping {
struct Register {
uint16_t regNum;
// For GRF, sub reg num is in byte granularity
uint16_t subRegNum;
} Register;
struct Memory {
// For globals, spill offset is absolute whereas for frame auto vars,
// it is BE_FP relative.
uint8_t isAbs : 1;
int32_t memoryOffset : 31;
} Memory;
} Mapping;
vISA::G4_Declare *dcl;
};
// Instance of this class is created and stored in G4_Kernel.
// Any debug info related members will go here and necessary
// getters/setters should be added here. One level of indirection
// will be required from G4_Kernel instance to get to this
// instance, but this means no space/perf penalty is imposed
// on G4_Kernel.
namespace vISA {
class LiveIntervalInfo;
using LIIAlloc = llvm::SpecificBumpPtrAllocator<LiveIntervalInfo>;
class LiveIntervalInfo {
public:
enum DebugLiveIntervalState { Open = 0, Closed = 1 };
private:
std::list<std::pair<uint32_t, uint32_t>> liveIntervals;
uint32_t cleanedAt;
DebugLiveIntervalState state;
uint32_t openIntervalVISAIndex;
public:
void *operator new(size_t sz, LIIAlloc &Allocator) {
return Allocator.Allocate(sz / sizeof(LiveIntervalInfo));
}
void addLiveInterval(uint32_t start, uint32_t end);
void liveAt(uint32_t cisaOff);
void getLiveIntervals(std::vector<std::pair<uint32_t, uint32_t>> &intervals);
void clearLiveIntervals() { liveIntervals.clear(); }
DebugLiveIntervalState getState() const { return state; }
void setStateOpen(uint32_t VISAIndex) {
// MUST_BE_TRUE(state == Closed, "Cannot open internal in Open state");
state = Open;
openIntervalVISAIndex = VISAIndex;
}
void setStateClosed(uint32_t VISAIndex) {
// MUST_BE_TRUE(state == Open, "Cannot close interval in Close state");
state = Closed;
addLiveInterval(VISAIndex, openIntervalVISAIndex);
}
bool isLiveAt(uint32_t VISAIndex) const {
for (auto &k : liveIntervals) {
if (k.first <= VISAIndex && k.second >= VISAIndex)
return true;
}
return false;
}
LiveIntervalInfo() {
cleanedAt = 0;
state = Closed;
openIntervalVISAIndex = 0;
}
};
class KernelDebugInfo {
private:
G4_Kernel *kernel = nullptr;
VISAKernelImpl *visaKernel;
std::unordered_map<G4_Declare *, LiveIntervalInfo *> debugInfoLiveIntervalMap;
// Instruction that saves caller BE_FP to callerbpfpdcl
G4_INST *saveCallerFP;
// Instruction that restores caller BE_FP prior to return
G4_INST *restoreCallerFP;
// Instruction that updates BE_FP for current frame
G4_INST *setupFP;
// Instruction that destroys BE_FP for current frame
G4_INST *restoreSP;
// Instruction that stores CE in prolog
G4_INST *saveCE;
// Current frame size in bytes
uint32_t frameSize;
// Store declare used holding return value for stack call functions,
// NULL for kernel.
G4_Declare *fretVar;
// Offset from FP where CE is stored
uint16_t CEStoreOffset;
// Caller save/restore
// std::vector<std::pair<fcall inst BB, std::pair<first caller save, last
// caller restore>>> One entry per fcall inst in current compilation unit
using SaveRestore = std::pair<std::vector<G4_INST *>, std::vector<G4_INST *>>;
std::unordered_map<G4_BB *, SaveRestore> callerSaveRestore;
SaveRestore calleeSaveRestore;
// Cache used to quickly check if intrinsic is in
// callerSaveRestore/calleeSaveRestore.
struct SaveRestoreIndex {
bool isSave;
G4_BB *bb;
};
std::unordered_map<vISA::G4_INST *, SaveRestoreIndex> isSaveRestoreInst;
std::unordered_set<vISA::G4_INST *> oldInsts;
// Store pair of cisa byte offset and gen byte offset in vector
std::vector<IDX_VDbgCisaByte2Gen> mapCISAOffsetGenOffset;
// Store pair of cisa index and gen byte offset in vector
std::vector<IDX_VDbgCisaIndex2Gen> mapCISAIndexGenOffset;
// Store varname map instance for each dcl
std::vector<VarnameMap *> varsMap;
// Store map between CISA bytecode index and CISA offset
std::map<unsigned int, unsigned int> mapCISAOffset;
// Store reloc_offset of gen binary. This is emitted out to debug info.
uint32_t reloc_offset;
// Store set of missing VISA ids as this helps consolidate live-intervals
// to save compile time.
std::set<unsigned int> missingVISAIds;
bool missingVISAIdsComputed;
std::vector<IDX_VDbgGen2CisaIndex> genISAOffsetToVISAIndex;
// Store all dcls that are from stack call function
std::unordered_set<G4_Declare *> stackCallDcls;
LIIAlloc LIIAllocator;
Mem_Manager varNameMapAlloc;
public:
LiveIntervalInfo *getLiveIntervalInfo(G4_Declare *dcl,
bool createIfNULL = true);
G4_Kernel &getKernel() { return *kernel; }
VISAKernelImpl *getVISAKernel() const { return visaKernel; }
void setVISAKernel(VISAKernelImpl *k);
KernelDebugInfo();
~KernelDebugInfo() = default;
void reset() {
mapCISAOffsetGenOffset.clear();
mapCISAIndexGenOffset.clear();
varsMap.clear();
resetRelocOffset();
missingVISAIds.clear();
missingVISAIdsComputed = false;
}
G4_Declare *getBEFP() {
G4_Declare *ret = nullptr;
if (!kernel->fg.getIsStackCallFunc()) {
ret = kernel->fg.builder->getBEFP();
} else if (saveCallerFP) {
ret = GetTopDclFromRegRegion(saveCallerFP->getSrc(0));
}
return ret;
}
G4_Declare *getCallerBEFP() {
G4_Declare *ret = nullptr;
if (saveCallerFP) {
ret = GetTopDclFromRegRegion(saveCallerFP->getDst());
}
return ret;
}
G4_INST *getCallerBEFPSaveInst() const { return saveCallerFP; }
void setCallerBEFPSaveInst(G4_INST *i) { saveCallerFP = i; }
G4_INST *getCallerBEFPRestoreInst() const { return restoreCallerFP; }
void setCallerBEFPRestoreInst(G4_INST *i) { restoreCallerFP = i; }
G4_INST *getBEFPSetupInst() const { return setupFP; }
void setBEFPSetupInst(G4_INST *i) { setupFP = i; }
G4_INST *getCallerSPRestoreInst() const { return restoreSP; }
void setCallerSPRestoreInst(G4_INST *i) { restoreSP = i; }
uint32_t getFrameSize() const { return frameSize; }
void setFrameSize(uint32_t sz) { frameSize = sz; }
G4_Declare *getFretVar() const { return fretVar; }
void setFretVar(G4_Declare *dcl) { fretVar = dcl; }
G4_INST *getCESaveInst() const { return saveCE; }
void setSaveCEInst(G4_INST *i) { saveCE = i; }
void setCESaveOffset(uint16_t Off) { CEStoreOffset = Off; }
uint16_t getCESaveOffset() const { return CEStoreOffset; }
void updateExpandedIntrinsic(G4_InstIntrinsic *spillOrFill, INST_LIST &insts);
void addCallerSaveInst(G4_BB *fcallBB, G4_INST *inst);
void addCallerRestoreInst(G4_BB *fcallBB, G4_INST *inst);
void addCalleeSaveInst(G4_INST *inst);
void addCalleeRestoreInst(G4_INST *inst);
bool isFcallWithSaveRestore(G4_BB *bb);
std::vector<G4_INST *> &getCallerSaveInsts(G4_BB *fcallBB);
std::vector<G4_INST *> &getCallerRestoreInsts(G4_BB *fcallBB);
std::vector<G4_INST *> &getCalleeSaveInsts();
std::vector<G4_INST *> &getCalleeRestoreInsts();
void setOldInstList(G4_BB *bb) { oldInsts.insert(bb->begin(), bb->end()); }
void clearOldInstList() { oldInsts.clear(); }
INST_LIST getDeltaInstructions(G4_BB *bb);
void resetRelocOffset() { reloc_offset = 0; }
void updateMapping(std::list<G4_BB *> &stackCallEntryBBs);
void generateByteOffsetMapping(std::list<G4_BB *> &stackCallEntryBBs);
void emitRegisterMapping();
void generateCISAByteOffsetFromOffset();
void updateRelocOffset();
void updateCallStackLiveIntervals();
void updateCallStackMain();
void generateGenISAToVISAIndex();
void computeDebugInfo(std::list<G4_BB *> &stackCallEntryBBs);
uint32_t getRelocOffset() const { return reloc_offset; }
void mapCISAOffsetInsert(unsigned int a, unsigned int b) {
mapCISAOffset.insert(std::make_pair(a, b));
}
unsigned int getGenOffsetFromVISAIndex(unsigned int v) const {
for (auto &item : mapCISAIndexGenOffset) {
if (item.CisaIndex == v)
return item.GenOffset;
}
return 0;
}
std::vector<IDX_VDbgCisaByte2Gen> &getMapCISAOffsetGenOffset() {
return mapCISAOffsetGenOffset;
}
std::vector<IDX_VDbgCisaIndex2Gen> &getMapCISAIndexGenOffset() {
return mapCISAIndexGenOffset;
}
std::vector<IDX_VDbgGen2CisaIndex> &getMapGenISAOffsetToCISAIndex() {
return genISAOffsetToVISAIndex;
}
std::vector<VarnameMap *> &getVarsMap() { return varsMap; }
uint32_t getVarIndex(G4_Declare *dcl);
void computeMissingVISAIds();
bool isMissingVISAId(unsigned int);
void markStackCallFuncDcls(G4_Kernel &function);
};
class SaveRestoreInfo {
G4_INST *i = nullptr;
public:
// Map src GRF->GRF/Memory
union RegMap {
uint32_t regNum;
struct {
int32_t offset : 31;
uint32_t isAbs : 1;
};
int32_t memOff;
};
enum RegOrMem { Reg = 1, MemAbs = 2, MemOffBEFP = 3 };
std::map<uint32_t, std::pair<RegOrMem, RegMap>> saveRestoreMap;
bool isEqual(SaveRestoreInfo &other) {
auto otherMapIt = other.saveRestoreMap.begin();
if (this->saveRestoreMap.size() != other.saveRestoreMap.size()) {
return false;
}
for (auto &thisMap : saveRestoreMap) {
if (thisMap.first != otherMapIt->first ||
thisMap.second.first != otherMapIt->second.first ||
thisMap.second.second.memOff != otherMapIt->second.second.memOff) {
return false;
}
otherMapIt++;
}
return true;
}
void update(G4_INST *inst, int32_t memOffset = 0xffff,
uint32_t regWithMemOffset = 0xffff, bool absOffset = false);
G4_INST *getInst() const { return i; }
};
class SaveRestoreManager {
VISAKernelImpl *visaKernel;
std::vector<SaveRestoreInfo> srInfo;
int32_t memOffset;
uint32_t regWithMemOffset;
bool absOffset;
public:
enum CallerOrCallee { Caller = 1, Callee = 2 };
void addInst(G4_INST *inst);
SaveRestoreManager(VISAKernelImpl *k) {
visaKernel = k;
memOffset = 0xffff;
regWithMemOffset = 0xffff;
absOffset = false;
}
std::vector<SaveRestoreInfo> &getSRInfo() { return srInfo; }
void sieveInstructions(CallerOrCallee c);
void emitAll();
};
class DbgDecoder {
private:
enum CallerCallee {
CALLER = 0,
CALLEE = 1
};
const char *const filename;
std::FILE *dbgFile = nullptr;
const PlatformInfo *platInfo = nullptr;
void ddName();
template <class T> void ddLiveInterval();
void ddCalleeCallerSave(uint32_t relocOffset, CallerCallee callCase);
public:
DbgDecoder(const char *f, TARGET_PLATFORM platform) : filename(f) {
dbgFile = fopen(filename, "rb");
platInfo = PlatformInfo::LookupPlatformInfo(platform);
vISA_ASSERT(platInfo != nullptr, "failed to look up platform");
}
~DbgDecoder() {
if (dbgFile)
fclose(dbgFile);
}
DbgDecoder() = delete;
DbgDecoder(const DbgDecoder &) = delete;
DbgDecoder operator=(const DbgDecoder &) = delete;
int ddDbg();
};
class DebugInfoState {
// Class used to store state during RA.
public:
void setPrevBitset(const SparseBitVector &b) { prevBitset = b; }
void setPrevInst(G4_INST *i) {
if (i->getVISAId() != UNMAPPABLE_VISA_INDEX) {
prevInst = i;
}
}
SparseBitVector *getPrevBitset() {
if (prevBitset.empty())
return nullptr;
return &prevBitset;
}
G4_INST *getPrevInst() { return prevInst; }
private:
SparseBitVector prevBitset;
G4_INST *prevInst = nullptr;
};
} // namespace vISA
/* Debug info format:
struct DebugFormatHeader
{
uint32_t magic;
uint16_t numCompiledObjects;
DebugInfoFormat debugInfo[numCompiledObjectsObjects];
}
struct DebugInfoFormat
{
uint16_t kernelNameLen;
char kernelName[kernelNameLen];
uint32_t reloc_offset; // 0 for kernel, non-zero for stack call functions
struct CISAOffsetMap
{
uint32_t numElements;
CISAMap data[numElements];
}
struct CISAIndexMap
{
uint32_t numElements;
CISAMap data[numElements];
}
struct VarInfoMap
{
uint32_t numElements;
struct VarInfo[numElements]
{
VarName name;
VarLiveIntervalVISA lr;
}
}
uint16_t numSubs
SubroutineInfo subInfo[numSubs];
CallFrameInfo frameInfo;
}
struct CISAMap
{
uint32_t cisaOffset/cisaIndex;
uint32_t genOffset;
}
struct SubroutineInfo
{
VarName subName;
uint32_t startVISAOffset;
uint32_t endVISAOffset;
VarLiveIntervalVISA retVal;
}
struct CallFrameInfo
{
uint16_t frameSizeInBytes;
uint8_t befpValid;
VarLiveIntervalGenISA befp; // Validity depends on flag befpValid
uint8_t callerbefpValid;
VarLiveIntervalGenISA callerbefp; // Validity depends on flag callerbefpValid
uint8_t retAddrValid;
VarLiveIntervalGenISA retAddr; // Validity depends on flag retAddrValid
uint16_t CEOffsetFromFPOff; // -1 means CE not saved offset
uint16_t CEStoreIP; // This field stores IP where CE is stored.
// Valid only if CEOffsetFromFPOff != -1.
uint16_t numCalleeSaveEntries;
PhyRegSaveInfoPerIP calleeSaveEntry[numCalleeSaveEntries];
// Need this because of following:
//
// V10 -> r2, r3, r4, r5, r6, r7
//
// send (16) null:w r1 <-- Writes 4 GRFs (r1, r2, r3, r4)
// r4 = r0
// r4.2 =...
// send (16) null:w r4 <-- Writes 4 GRFs (r5, r6, r7, r8)
//
uint32_t numCallerSaveEntries;
PhyRegSaveInfoPerIP callerSaveEntry[numCallerSaveEntries];
}
struct VarName
{
uint16_t varNameLen;
char varName[varNameLen];
}
struct VarAlloc
{
uint8_t virtualType; // Virtual register type from CISA file: 0 - address, 1
- flag, 2 - GRF uint8_t physicalType; // Physical register type allocated by RA:
0 - address, 1 - flag, 2 - GRF, 3 - memory Mapping mapping;
}
union Mapping
{
struct Register
{
uint16_t regNum;
uint16_t subRegNum; // for GRF, in byte offset
}
struct Memory
{
uint32_t isBaseOffBEFP : 1; // MSB of 32-bit field denotes whether base
if off BE_FP (0) or absolute (1) int32_t memoryOffset : 31; // memory offset
}
}
struct VarLiveIntervalVISA
{
uint16_t numIntervals;
LiveIntervalVISA interval[numIntervals];
}
struct VarLiveIntervalGenISA
{
uint16_t numIntervals;
LiveIntervalGenISA interval[numIntervals];
}
struct LiveIntervalVISA
{
uint16_t start;
uint16_t end;
VarAlloc alloc;
}
struct LiveIntervalGenISA
{
uint32_t start;
uint32_t end;
VarAlloc alloc;
}
struct PhyRegSaveInfoPerIP
{
uint32_t genIPOffset;
uint16_t numEntries;
RegInfoMapping data[numEntries];
}
struct RegInfoMapping
{
RegInfo src;
uint8_t dstInReg;
Mapping dst;
}
struct RegInfo
{
// GRF file r0.0 -> srcRegOff = 0, r1.0 -> srcRegOff = 32.
// Addr and flag registers can be represented beyond GRF file size of 4k.
But not currently required since they are not callee save. uint16_t srcRegOff;
uint16_t numBytes;
}
*/
#endif
|