Skip to content

Commit 60c1bbe

Browse files
daniel-manigcbot
authored andcommitted
Minor fixes for RA metadata
Minor changes to MetadataDumpRA
1 parent 8c3fb49 commit 60c1bbe

File tree

5 files changed

+144
-67
lines changed

5 files changed

+144
-67
lines changed

IGC/Compiler/CISACodeGen/CISABuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4670,6 +4670,10 @@ namespace IGC
46704670
if (dumpJSON != 0)
46714671
SaveOption(vISA_dumpIgaJson, dumpJSON);
46724672

4673+
auto dumpRAMetadata = (bool)IGC_GET_FLAG_VALUE(ShaderDumpEnableRAMetadata);
4674+
if (dumpRAMetadata)
4675+
SaveOption(vISA_dumpRAMetadata, dumpRAMetadata);
4676+
46734677
if (EnableBarrierInstCounterBits)
46744678
{
46754679
SaveOption(VISA_EnableBarrierInstCounterBits, true);

IGC/common/igc_flags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ DECLARE_IGC_REGKEY(bool, ShaderDumpEnable, false, "dump LLVM IR, vi
337337
DECLARE_IGC_REGKEY(bool, ShaderDumpEnableAll, false, "dump all LLVM IR passes, visaasm, and GenISA", true)
338338
DECLARE_IGC_REGKEY(DWORD, ShaderDumpEnableG4, false, "same as ShaderDumpEnable but adds G4 dumps (0 = off, 1 = some, 2 = all)", 0)
339339
DECLARE_IGC_REGKEY(DWORD, ShaderDumpEnableIGAJSON, false, "adds IGA JSON output to shader dumps (0 = off, 1 = enabled, 2 = include def/use info but causes longer compile times)", 0)
340+
DECLARE_IGC_REGKEY(bool, ShaderDumpEnableRAMetadata, false, "adds RA Metadata file to shader dumps", true)
340341
DECLARE_IGC_REGKEY(bool, ShaderDumpInstNamer, false, "dump all unnamed LLVM IR instruction with variable names 'tmp' which makes easier for shaderoverriding", true)
341342
DECLARE_IGC_REGKEY(debugString, ShaderDumpFilter, 0, "Only dump files matching the given regex", true)
342343
DECLARE_IGC_REGKEY(bool, ElfDumpEnable, false, "dump ELF file", true)

visa/BuildCISAIRImpl.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,10 +1856,16 @@ int CISA_IR_Builder::Compile(const char *isaasmFileName, bool emit_visa_only) {
18561856

18571857
// initialize new metadata object
18581858
std::unique_ptr<MetadataDumpRA> metadata;
1859+
const char* raFileName = nullptr;
1860+
const char* kernelName = nullptr;
18591861
bool dumpMetadata = m_options.getOption(vISA_dumpRAMetadata);
1862+
bool decodeMetadata = m_options.getOption(vISA_DecodeRAMetadata);
18601863
if (dumpMetadata) {
18611864
metadata = std::make_unique<vISA::MetadataDumpRA>();
18621865
}
1866+
if (dumpMetadata || decodeMetadata) {
1867+
m_options.getOption(VISA_AsmFileName, raFileName);
1868+
}
18631869

18641870
// stitch functions and compile to gen binary
18651871
for (auto func : mainFunctions) {
@@ -1991,6 +1997,9 @@ int CISA_IR_Builder::Compile(const char *isaasmFileName, bool emit_visa_only) {
19911997
if (dumpMetadata) {
19921998
metadata->addKernelMD(func->getKernel());
19931999
}
2000+
if (dumpMetadata || decodeMetadata) {
2001+
kernelName = func->getKernel()->getName();
2002+
}
19942003

19952004
func->setGenxBinaryBuffer(genxBuffer, genxBufferSize);
19962005
if (m_options.getOption(vISA_GenerateDebugInfo)) {
@@ -2002,14 +2011,16 @@ int CISA_IR_Builder::Compile(const char *isaasmFileName, bool emit_visa_only) {
20022011

20032012
if (dumpMetadata) {
20042013
// emit the metadata file
2005-
metadata->emitMetadataFile();
2014+
metadata->emitMetadataFile(raFileName, kernelName);
20062015
}
2007-
20082016
// output metadata to console
2009-
bool decodeMetadata = m_options.getOption(vISA_DecodeRAMetadata);
2017+
20102018
if (decodeMetadata) {
2019+
stringstream ssInit;
2020+
ssInit << raFileName << "_" << kernelName << ".ra_metadata";
2021+
20112022
MetadataReader MDReader;
2012-
MDReader.readMetadata();
2023+
MDReader.readMetadata(ssInit.str());
20132024
}
20142025

20152026
}

visa/MetadataDumpRA.cpp

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,64 @@ Def::Def(G4_DstRegRegion* dst) {
1919
G4_RegVar* dstRegVar = dstBase->asRegVar();
2020
G4_Declare* dstDecl = dstRegVar->getDeclare();
2121

22-
this->name = dstRegVar->getName();
23-
this->nameLen = strnlen(this->name, MAX_STRLEN);
24-
25-
this->hstride = dst->getHorzStride();
26-
this->typeSize = dst->getTypeSize();
27-
28-
this->reg = dstRegVar->getPhyReg()->asGreg()->getRegNum();
29-
this->subreg = dstRegVar->getPhyRegOff() + dst->getSubRegOff();
30-
31-
unsigned int offsetFromR0 = dstDecl->getGRFOffsetFromR0();
32-
this->leftBound = offsetFromR0 + dst->getLeftBound();
33-
this->rightBound = offsetFromR0 + dst->getRightBound();
22+
// handle the case where this VV is aliased to another VV
23+
unsigned int rootOffset = 0;
24+
G4_Declare* rootDecl = dstDecl->getRootDeclare(rootOffset);
25+
G4_RegVar* rootRegVar = rootDecl->getRegVar();
26+
27+
regFileKind = rootDecl->getRegFile();
28+
reg = rootRegVar->getPhyReg()->asGreg()->getRegNum();
29+
subreg = rootRegVar->getPhyRegOff();
30+
byteSize = rootDecl->getByteSize();
31+
aliasOffset = rootOffset;
32+
33+
rowOffset = dst->getRegOff();
34+
colOffset = dst->getSubRegOff();
35+
36+
hstride = dst->getHorzStride();
37+
typeSize = dst->getTypeSize();
38+
39+
unsigned int offsetFromR0 = rootDecl->getGRFOffsetFromR0();
40+
rootBound = offsetFromR0 + rootOffset;
41+
leftBound = dst->getLeftBound();
42+
rightBound = dst->getRightBound();
43+
44+
name = rootRegVar->getName();
45+
nameLen = strnlen(this->name, MAX_STRLEN);
3446
}
3547

3648
Use::Use(G4_SrcRegRegion* src) {
3749
G4_VarBase* srcBase = src->getBase();
3850
G4_RegVar* srcRegVar = srcBase->asRegVar();
3951
G4_Declare* srcDecl = srcRegVar->getDeclare();
4052

41-
this->name = srcRegVar->getName();
42-
this->nameLen = strnlen(this->name, MAX_STRLEN);
53+
// handle the case where this VV is aliased to another VV
54+
unsigned int rootOffset = 0;
55+
G4_Declare* rootDecl = srcDecl->getRootDeclare(rootOffset);
56+
G4_RegVar* rootRegVar = rootDecl->getRegVar();
4357

44-
const RegionDesc* srcRegion = src->getRegion();
45-
this->hstride = srcRegion->horzStride;
46-
this->vstride = srcRegion->vertStride;
47-
this->width = srcRegion->width;
58+
regFileKind = rootDecl->getRegFile();
59+
reg = rootRegVar->getPhyReg()->asGreg()->getRegNum();
60+
subreg = rootRegVar->getPhyRegOff();
61+
byteSize = rootDecl->getByteSize();
62+
aliasOffset = rootOffset;
4863

49-
this->typeSize = src->getTypeSize();
50-
this->reg = srcRegVar->getPhyReg()->asGreg()->getRegNum();
51-
this->subreg = srcRegVar->getPhyRegOff() + src->getSubRegOff();
64+
rowOffset = src->getRegOff();
65+
colOffset = src->getSubRegOff();
5266

53-
unsigned int offsetFromR0 = srcDecl->getGRFOffsetFromR0();
54-
this->leftBound = offsetFromR0 + src->getLeftBound();
55-
this->rightBound = offsetFromR0 + src->getRightBound();
67+
const RegionDesc* srcRegion = src->getRegion();
68+
hstride = srcRegion->horzStride;
69+
vstride = srcRegion->vertStride;
70+
width = srcRegion->width;
71+
typeSize = src->getTypeSize();
72+
73+
unsigned int offsetFromR0 = rootDecl->getGRFOffsetFromR0();
74+
rootBound = offsetFromR0 + rootOffset;
75+
leftBound = src->getLeftBound();
76+
rightBound = src->getRightBound();
77+
78+
name = rootRegVar->getName();
79+
nameLen = strnlen(this->name, MAX_STRLEN);
5680
}
5781

5882
void MetadataDumpRA::addKernelMD(G4_Kernel* kernel) {
@@ -155,12 +179,14 @@ void MetadataDumpRA::addKernelMD(G4_Kernel* kernel) {
155179
// |--- ... ... ...
156180
// <repeats numKernels times, for each Kernel>
157181
// |--- ... ... ...
158-
void MetadataDumpRA::emitMetadataFile() {
182+
void MetadataDumpRA::emitMetadataFile(std::string asmName, std::string kernelName) {
159183
using namespace std;
160-
ofstream MDFile;
161184

162-
// create metadata file in CWD -- FIXME: emit to a specific dump dir
163-
MDFile.open("RA_METADATA_DUMP", ios::trunc);
185+
// create metadata file in shader dump
186+
ofstream MDFile;
187+
stringstream ssInit;
188+
ssInit << asmName << "_" << kernelName << ".ra_metadata";
189+
MDFile.open(ssInit.str(), std::ofstream::binary);
164190

165191
// write number of kernels to file
166192
assert(numKernels == kernelMetadatas.size());
@@ -184,13 +210,15 @@ void MetadataDumpRA::emitMetadataFile() {
184210

185211
for (Def def : iMD.instDefs) {
186212

213+
// uchar (1) : regFileKind
187214
// ushort (1) : typeSize
188-
// uint (6) : leftBound, rightBound, reg, subreg, hstride, nameLen
189-
streamsize defOffset = sizeof(unsigned short) + sizeof(unsigned int) * 6;
215+
// uint (10) : reg, subreg, byteSize, aliasOffset, row/colOffset, hstride, root/l/rBound, nameLen
216+
streamsize defOffset = sizeof(unsigned char) + sizeof(unsigned short) + sizeof(unsigned int) * 11;
190217
MDFile.write((char*)&def, defOffset);
191218

192219
// write virtual variable name to file
193220
MDFile.write(def.name, def.nameLen);
221+
vISA_ASSERT(MDFile.good(), "RA metadata file write stream error for Def");
194222
}
195223

196224
// write number of uses in this inst to file
@@ -199,15 +227,18 @@ void MetadataDumpRA::emitMetadataFile() {
199227

200228
for (Use use : iMD.instUses) {
201229

230+
// uchar (1) : regFileKind
202231
// ushort (1) : typeSize
203-
// uint (8) : leftBound, rightBound, reg, subreg, hstride, vstride, width, nameLen
204-
streamsize useOffset = sizeof(unsigned short) + sizeof(unsigned int) * 8;
232+
// uint (12) : reg, subreg, byteSize, aliasOffset, row/colOffset, h/vstride, width, root/l/rBound, nameLen
233+
streamsize useOffset = sizeof(unsigned char) + sizeof(unsigned short) + sizeof(unsigned int) * 13;
205234
MDFile.write((char*)&use, useOffset);
206235

207236
// write virtual variable name to file
208237
MDFile.write(use.name, use.nameLen);
209-
238+
vISA_ASSERT(MDFile.good(), "RA metadata file write stream error for Use");
210239
}
240+
241+
MDFile.flush();
211242
}
212243
}
213244
MDFile.close();
@@ -221,10 +252,10 @@ void MetadataReader::printName(const char* name, unsigned int nameLen) {
221252
}
222253

223254
// Reads a metadata file from the dump directory
224-
void MetadataReader::readMetadata() {
255+
void MetadataReader::readMetadata(std::string fileName) {
225256
ifstream MDFile;
226257

227-
MDFile.open("RA_METADATA_DUMP", ios::in);
258+
MDFile.open(fileName, ios::binary);
228259

229260
unsigned int numKernels;
230261
MDFile.read((char*)&numKernels, sizeof(unsigned int));
@@ -252,20 +283,25 @@ void MetadataReader::readMetadata() {
252283

253284
for (unsigned int d = 0; d < numDefs; d++) {
254285
Def def;
286+
287+
// uchar (1) : regFileKind
255288
// ushort (1) : typeSize
256-
// uint (6) : leftBound, rightBound, reg, subreg, hstride, nameLen
257-
streamsize defOffset = sizeof(unsigned short) + sizeof(unsigned int) * 6;
289+
// uint (10) : reg, subreg, byteSize, aliasOffset, row/colOffset, hstride, root/l/rBound, nameLen
290+
streamsize defOffset = sizeof(unsigned char) + sizeof(unsigned short) + sizeof(unsigned int) * 11;
291+
258292
MDFile.read((char*)&def, defOffset);
259293

260-
char* name = new char[def.nameLen];
294+
char name[MAX_NAMELEN];
261295
MDFile.read(name, def.nameLen);
262296
def.name = name;
263297

298+
vISA_ASSERT(MDFile.good(), "RA metadata file read stream error for Def");
299+
264300
printf("| ");
265301
this->printName(def.name, def.nameLen);
266302
printf(" \t\t|");
267303
printf("%5u -> %-5u| ", def.leftBound, def.rightBound);
268-
printf(" r%-5u.%-5u<%-5u%-5s%-5s >:%-5u", def.reg, def.subreg, def.hstride, "", "", def.typeSize);
304+
printf(" r%-5u.%-5u(%-5u,%-5u)<%-5u%-5s%-5s >:%-5u_%-5u_%-5u", def.reg, def.subreg, def.rowOffset, def.colOffset, def.hstride, "", "", def.typeSize, def.byteSize, (unsigned int)def.regFileKind);
269305
printf("\n");
270306
}
271307

@@ -274,20 +310,25 @@ void MetadataReader::readMetadata() {
274310

275311
for (unsigned int u = 0; u < numUses; u++) {
276312
Use use;
313+
314+
// uchar (1) : regFileKind
277315
// ushort (1) : typeSize
278-
// uint (8) : leftBound, rightBound, reg, subreg, hstride, vstride, width, nameLen
279-
streamsize useOffset = sizeof(unsigned short) + sizeof(unsigned int) * 8;
316+
// uint (12) : reg, subreg, byteSize, aliasOffset, row/colOffset, h/vstride, width, root/l/rBound, nameLen
317+
streamsize useOffset = sizeof(unsigned char) + sizeof(unsigned short) + sizeof(unsigned int) * 13;
318+
280319
MDFile.read((char*)&use, useOffset);
281320

282-
char* name = new char[use.nameLen];
321+
char name[MAX_NAMELEN];
283322
MDFile.read(name, use.nameLen);
284323
use.name = name;
285324

325+
vISA_ASSERT(MDFile.good(), "RA metadata file read stream error for Use");
326+
286327
printf("| ");
287328
this->printName(use.name, use.nameLen);
288329
printf(" \t\t|");
289330
printf("%5u -> %-5u| ", use.leftBound, use.rightBound);
290-
printf(" r%-5u.%-5u<%-5u;%-5u,%-5u>:%-5u", use.reg, use.subreg, use.vstride, use.width, use.hstride, use.typeSize);
331+
printf(" r%-5u.%-5u(%-5u,%-5u)<%-5u;%-5u,%-5u>:%-5u_%-5u_%-5u", use.reg, use.subreg, use.rowOffset, use.colOffset, use.vstride, use.width, use.hstride, use.typeSize, use.byteSize, (unsigned int)use.regFileKind);
291332
printf("\n");
292333
}
293334
}

visa/MetadataDumpRA.h

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,35 @@ SPDX-License-Identifier: MIT
2525

2626
// global var for strnlen call
2727
static constexpr size_t MAX_STRLEN = 100;
28+
29+
// global var for VV name length
30+
static constexpr unsigned int MAX_NAMELEN = 1024;
31+
2832
using namespace std;
2933

3034
namespace vISA {
3135

3236
class Def {
3337
public:
3438

35-
unsigned short typeSize = 0;
36-
unsigned int leftBound = 0;
37-
unsigned int rightBound = 0;
38-
unsigned int reg = 0;
39-
unsigned int subreg = 0;
39+
unsigned char regFileKind; // G4_RegFileKind of this operand's G4_Declare
40+
unsigned short typeSize = 0; // size of each element
41+
unsigned int reg = 0; // register number of the start of this VV's register allocation
42+
unsigned int subreg = 0; // subregister number of the start of this VV's register allocation
43+
unsigned int byteSize = 0; // total number of bytes that this VV is allocated in register file
44+
unsigned int aliasOffset = 0; // number of bytes that this VV's allocation is offset from its root VV's
45+
46+
unsigned int rowOffset = 0; // number of registers that this region is offset from the base by
47+
unsigned int colOffset = 0; // number of subregisters/elements that this region is offset from the base by
4048

41-
unsigned int hstride = 0;
49+
unsigned int hstride = 0; // step size (in units of 1 element size) btwn the starts of two elems in a row
4250

43-
unsigned int nameLen = 0;
44-
const char* name = nullptr;
51+
unsigned int rootBound = 0; // byte offset into the register file (VV start)
52+
unsigned int leftBound = 0; // byte offset into the register file (VV start + operand row/col offset)
53+
unsigned int rightBound = 0; // byte offset into the register file (VV start + operand total size)
54+
55+
unsigned int nameLen = 0; // number of characters in this VV's name
56+
const char* name = nullptr; // note that for aliased virtual variables, this is the name of the root VV
4557

4658
Def() = default;
4759

@@ -51,18 +63,26 @@ class Def {
5163
class Use {
5264
public:
5365

54-
unsigned short typeSize = 0;
55-
unsigned int leftBound = 0;
56-
unsigned int rightBound = 0;
57-
unsigned int reg = 0;
58-
unsigned int subreg = 0;
66+
unsigned char regFileKind; // G4_RegFileKind of this operand's G4_Declare
67+
unsigned short typeSize = 0; // size of each element
68+
unsigned int reg = 0; // register number of the start of this VV's register allocation
69+
unsigned int subreg = 0; // subregister number of the start of this VV's register allocation
70+
unsigned int byteSize = 0; // total number of bytes that this VV is allocated in register file
71+
unsigned int aliasOffset = 0; // number of bytes that this VV's allocation is offset from its root VV's
72+
73+
unsigned int rowOffset = 0; // number of registers that this region is offset from the base by
74+
unsigned int colOffset = 0; // number of subregisters/elements that this region is offset from the base by
75+
76+
unsigned int hstride = 0; // step size (in units of 1 element size) btwn the starts of two elems in a row
77+
unsigned int vstride = 0; // step size (in units of 1 element size) btwn the starts of two rows
78+
unsigned int width = 0; // number of elements in one row
5979

60-
unsigned int hstride = 0;
61-
unsigned int vstride = 0;
62-
unsigned int width = 0;
80+
unsigned int rootBound = 0; // byte offset into the register file (VV start)
81+
unsigned int leftBound = 0; // byte offset into the register file (VV start + operand row/col offset)
82+
unsigned int rightBound = 0; // byte offset into the register file (VV start + operand total size)
6383

64-
unsigned int nameLen = 0;
65-
const char* name = nullptr;
84+
unsigned int nameLen = 0; // number of characters in this VV's name
85+
const char* name = nullptr; // note that for aliased virtual variables, this is the name of the root VV
6686

6787
Use() = default;
6888

@@ -95,13 +115,13 @@ class MetadataDumpRA {
95115
void addKernelMD(G4_Kernel* kernel);
96116

97117
// Emits a metadata file to the dump directory
98-
void emitMetadataFile();
118+
void emitMetadataFile(std::string asmName, std::string kernelName);
99119
};
100120

101121
class MetadataReader {
102122
public:
103123
void printName(const char* name, unsigned int nameLen);
104-
void readMetadata();
124+
void readMetadata(std::string fileName);
105125
};
106126

107127
}

0 commit comments

Comments
 (0)