Skip to content

Commit

Permalink
Refactor and extend shader dumps in VC
Browse files Browse the repository at this point in the history
Add zebin metadata and compiled module binary into the dumps.
  • Loading branch information
vmustya authored and igcbot committed Sep 20, 2023
1 parent cffcddb commit dca2074
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 86 deletions.
12 changes: 6 additions & 6 deletions IGC/VectorCompiler/igcdeps/include/vc/igcdeps/cmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ class CGen8CMProgram : public iOpenCL::CGen8OpenCLProgramBase {
bool KernelIsDebuggable = false;
};

explicit CGen8CMProgram(PLATFORM platform, const WA_TABLE &WATable,
llvm::ArrayRef<char> SPIRV = llvm::None,
llvm::Optional<llvm::StringRef> Opts = llvm::None);
explicit CGen8CMProgram(const CompileOptions &Opts, PLATFORM platform,
const WA_TABLE &WATable,
llvm::ArrayRef<char> SPIRV = llvm::None);

// Produce the final ELF binary with the given CM kernels
// in OpenCL format.
void CreateKernelBinaries(CompileOptions& Opts);
void CreateKernelBinaries();
void GetZEBinary(llvm::raw_pwrite_stream &programBinary,
unsigned pointerSizeInBytes) override;
bool HasErrors() const { return !m_ErrorLog.empty(); };
Expand All @@ -157,8 +157,8 @@ class CGen8CMProgram : public iOpenCL::CGen8OpenCLProgramBase {
std::string m_ErrorLog;

private:
llvm::ArrayRef<char> m_spirv;
llvm::Optional<llvm::StringRef> m_opts;
const CompileOptions &m_opts;
const llvm::ArrayRef<char> m_spirv;

TmpFilesStorage extractRawDebugInfo(llvm::raw_ostream &ErrStream);
std::unique_ptr<llvm::MemoryBuffer> buildZeDebugInfo();
Expand Down
28 changes: 14 additions & 14 deletions IGC/VectorCompiler/igcdeps/src/PatchTokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ struct DebugInfo {
};

// Implementation of CGen8CMProgram.
CGen8CMProgram::CGen8CMProgram(PLATFORM platform, const WA_TABLE &WATable,
llvm::ArrayRef<char> SPIRV,
llvm::Optional<llvm::StringRef> Opts)
CGen8CMProgram::CGen8CMProgram(const CompileOptions &Opts, PLATFORM platform,
const WA_TABLE &WATable,
llvm::ArrayRef<char> SPIRV)
: CGen8OpenCLProgramBase(platform, m_ContextProvider, WATable),
m_programInfo(new IGC::SOpenCLProgramInfo), m_spirv(SPIRV), m_opts(Opts) {
m_programInfo(new IGC::SOpenCLProgramInfo), m_opts(Opts), m_spirv(SPIRV) {
}

TmpFilesStorage
Expand Down Expand Up @@ -141,7 +141,7 @@ llvm::Error CGen8CMProgram::GetError() const {
return llvm::make_error<vc::OutputBinaryCreationError>(m_ErrorLog);
}

void CGen8CMProgram::CreateKernelBinaries(CompileOptions& Opts) {
void CGen8CMProgram::CreateKernelBinaries() {
CreateProgramScopePatchStream(*m_programInfo);
for (const auto &kernel : m_kernels) {
// Create the kernel binary streams.
Expand All @@ -157,9 +157,9 @@ void CGen8CMProgram::CreateKernelBinaries(CompileOptions& Opts) {
kernel->getProgramOutput().m_unpaddedProgramSize);

if (IGC_IS_FLAG_ENABLED(ShaderDumpEnable))
Opts.Dumper->dumpCos(m_StateProcessor.m_oclStateDebugMessagePrintOut,
vc::legalizeShaderDumpName(
kernel->m_kernelInfo.m_kernelName + ".cos"));
m_opts.Dumper->dumpCos(
m_StateProcessor.m_oclStateDebugMessagePrintOut,
vc::legalizeShaderDumpName(kernel->m_kernelInfo.m_kernelName));

if (kernel->getProgramOutput().m_debugDataSize) {
data.vcKernelDebugData = std::make_unique<Util::BinaryStream>();
Expand All @@ -186,12 +186,9 @@ void CGen8CMProgram::GetZEBinary(llvm::raw_pwrite_stream &programBinary,
const uint8_t *SpirvData = reinterpret_cast<const uint8_t *>(m_spirv.data());
size_t SpirvSize = m_spirv.size();

const uint8_t *OptsData = nullptr;
size_t OptsSize = 0;
if (m_opts) {
OptsData = reinterpret_cast<const uint8_t *>(m_opts->data());
OptsSize = m_opts->size();
}
const auto &ApiOpts = m_opts.ApiOptions;
const uint8_t *OptsData = reinterpret_cast<const uint8_t *>(ApiOpts.data());
size_t OptsSize = ApiOpts.size();

iOpenCL::ZEBinaryBuilder zebuilder(m_Platform, pointerSizeInBytes == 8,
*m_programInfo, SpirvData, SpirvSize,
Expand Down Expand Up @@ -224,6 +221,9 @@ void CGen8CMProgram::GetZEBinary(llvm::raw_pwrite_stream &programBinary,
}
dumpElfKernelMapFile();
zebuilder.getBinaryObject(programBinary);

if (IGC_IS_FLAG_ENABLED(ShaderDumpEnable))
zebuilder.printZEInfo(m_opts.Dumper->composeDumpPath("", "zeinfo"));
}

bool CGen8CMProgram::HasCrossThreadOffsetRelocations() {
Expand Down
68 changes: 47 additions & 21 deletions IGC/VectorCompiler/igcdeps/src/ShaderDump.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*========================== begin_copyright_notice ============================
Copyright (C) 2020-2021 Intel Corporation
Copyright (C) 2020-2023 Intel Corporation
SPDX-License-Identifier: MIT
Expand Down Expand Up @@ -47,19 +47,27 @@ class VC_IGCFileDumper : public vc::ShaderDumper {
public:
VC_IGCFileDumper(const ShaderHash &Hash);

void dumpBinary(llvm::ArrayRef<char> Binary,
llvm::StringRef DumpName) override;
void dumpText(llvm::StringRef Text, llvm::StringRef DumpName) override;
void dumpBinary(llvm::ArrayRef<char> Binary, llvm::StringRef DumpName,
llvm::StringRef DumpExtension) override;
void dumpText(llvm::StringRef Text, llvm::StringRef DumpName,
llvm::StringRef DumpExtension) override;

void dumpModule(const llvm::Module &M, llvm::StringRef DumpName) override;
void dumpModule(const llvm::Module &M, llvm::StringRef DumpName,
llvm::StringRef DumpExtension) override;

void dumpCos(llvm::StringRef Contents, llvm::StringRef DumpName) override;
void dumpCos(llvm::StringRef Contents, llvm::StringRef DumpName,
llvm::StringRef DumpExtension) override;

std::string composeDumpPath(llvm::StringRef DumpName) const override;
std::string composeDumpPath(llvm::StringRef DumpName,
llvm::StringRef DumpExtension) const override;

private:
IGC::Debug::DumpName getName(llvm::StringRef DumpName,
llvm::StringRef DumpExtension) const;

template <IGC::Debug::DumpType DumpTy, typename F>
void writeToFile(llvm::StringRef DumpName, F Writer) const;
void writeToFile(llvm::StringRef DumpName, llvm::StringRef DumpExtension,
F Writer) const;
};
} // namespace

Expand All @@ -71,40 +79,58 @@ static IGC::Debug::DumpName addDumpPostfix(const IGC::Debug::DumpName &Name,
return Name.PostFix(Postfix.str());
}

IGC::Debug::DumpName
VC_IGCFileDumper::getName(llvm::StringRef DumpName,
llvm::StringRef DumpExtension) const {
auto Name = DumpPrefix.PostFix(DumpName.str());
if (!DumpExtension.empty())
Name = Name.Extension(DumpExtension.str());
return Name;
}

template <IGC::Debug::DumpType DumpTy, typename F>
void VC_IGCFileDumper::writeToFile(llvm::StringRef DumpName, F Writer) const {
IGC::Debug::Dump Dumper{addDumpPostfix(DumpPrefix, DumpName), DumpTy};
void VC_IGCFileDumper::writeToFile(llvm::StringRef DumpName,
llvm::StringRef DumpExtension,
F Writer) const {
IGC::Debug::Dump Dumper{getName(DumpName, DumpExtension), DumpTy};
Writer(Dumper.stream());
}

void VC_IGCFileDumper::dumpBinary(llvm::ArrayRef<char> Binary,
llvm::StringRef DumpName) {
llvm::StringRef DumpName,
llvm::StringRef DumpExtension) {
writeToFile<IGC::Debug::DumpType::ASM_BC>(
DumpName, [Binary](llvm::raw_ostream &OS) {
DumpName, DumpExtension, [Binary](llvm::raw_ostream &OS) {
OS.write(Binary.data(), Binary.size());
});
}

void VC_IGCFileDumper::dumpText(llvm::StringRef Text,
llvm::StringRef DumpName) {
void VC_IGCFileDumper::dumpText(llvm::StringRef Text, llvm::StringRef DumpName,
llvm::StringRef DumpExtension) {
writeToFile<IGC::Debug::DumpType::DBG_MSG_TEXT>(
DumpName, [Text](llvm::raw_ostream &OS) { OS << Text; });
DumpName, DumpExtension, [Text](llvm::raw_ostream &OS) { OS << Text; });
}

void VC_IGCFileDumper::dumpModule(const llvm::Module &M,
llvm::StringRef DumpName) {
llvm::StringRef DumpName,
llvm::StringRef DumpExtension) {
writeToFile<IGC::Debug::DumpType::TRANSLATED_IR_TEXT>(
DumpName, [&M](llvm::raw_ostream &OS) { M.print(OS, nullptr); });
DumpName, DumpExtension,
[&M](llvm::raw_ostream &OS) { M.print(OS, nullptr); });
}

void VC_IGCFileDumper::dumpCos(llvm::StringRef Contents,
llvm::StringRef DumpName) {
llvm::StringRef DumpName,
llvm::StringRef DumpExtension) {
writeToFile<IGC::Debug::DumpType::COS_TEXT>(
DumpName, [Contents](llvm::raw_ostream &OS) { OS << Contents; });
DumpName, DumpExtension,
[Contents](llvm::raw_ostream &OS) { OS << Contents; });
}

std::string VC_IGCFileDumper::composeDumpPath(llvm::StringRef DumpName) const {
return addDumpPostfix(DumpPrefix, DumpName).str();
std::string
VC_IGCFileDumper::composeDumpPath(llvm::StringRef DumpName,
llvm::StringRef DumpExtension) const {
return getName(DumpName, DumpExtension).str();
}

namespace vc {
Expand Down
24 changes: 12 additions & 12 deletions IGC/VectorCompiler/igcdeps/src/TranslationInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ static void dumpInputData(vc::ShaderDumper &Dumper, llvm::StringRef ApiOptions,
if (!IGC_IS_FLAG_ENABLED(ShaderDumpEnable))
return;

Dumper.dumpText(ApiOptions, IsRaw ? "options_raw.txt" : "options.txt");
Dumper.dumpText(ApiOptions, IsRaw ? "options_raw" : "options");
Dumper.dumpText(InternalOptions,
IsRaw ? "internal_options_raw.txt" : "internal_options.txt");
Dumper.dumpBinary(Input, IsRaw ? "igc_input_raw.spv" : "igc_input.spv");
IsRaw ? "internal_options_raw" : "internal_options");
Dumper.dumpBinary(Input, IsRaw ? "igc_input_raw" : "igc_input", "spv");
}

static bool tryAddAuxiliaryOptions(llvm::StringRef AuxOpt,
Expand Down Expand Up @@ -564,7 +564,7 @@ static void dumpPlatform(const vc::CompileOptions &Opts, PLATFORM Platform,
<< (Opts.CPUStr.empty() ? "(empty)" : Opts.CPUStr) << ", " << Opts.RevId
<< "\n";

Dumper.dumpText(Os.str(), "platform.be.txt");
Dumper.dumpText(Os.str(), "platform.be");
#endif
}

Expand Down Expand Up @@ -663,13 +663,14 @@ std::error_code vc::translateBuild(const TC::STB_TranslateInputArgs *InputArgs,
return getError(ExpOutput.takeError(), OutputArgs);
auto &CompileResult = ExpOutput.get();

vc::CGen8CMProgram CMProgram{Opts, IGCPlatform.getPlatformInfo(),
IGCPlatform.getWATable(), Input};
vc::createBinary(CMProgram, CompileResult);

switch (Opts.Binary) {
case vc::BinaryKind::OpenCL: {
vc::CGen8CMProgram CMProgram{IGCPlatform.getPlatformInfo(),
IGCPlatform.getWATable()};
vc::createBinary(CMProgram, CompileResult);
validateCMProgramForOCLBin(CMProgram);
CMProgram.CreateKernelBinaries(Opts);
CMProgram.CreateKernelBinaries();
Util::BinaryStream ProgramBinary;
CMProgram.GetProgramBinary(ProgramBinary, CompileResult.PointerSizeInBytes);
llvm::StringRef BinaryRef{ProgramBinary.GetLinearPointer(),
Expand All @@ -688,17 +689,16 @@ std::error_code vc::translateBuild(const TC::STB_TranslateInputArgs *InputArgs,
break;
}
case vc::BinaryKind::ZE: {
vc::CGen8CMProgram CMProgram{IGCPlatform.getPlatformInfo(),
IGCPlatform.getWATable(), Input,
llvm::StringRef(Opts.ApiOptions)};
vc::createBinary(CMProgram, CompileResult);
llvm::SmallVector<char, 0> ProgramBinary;
llvm::raw_svector_ostream ProgramBinaryOS{ProgramBinary};
CMProgram.GetZEBinary(ProgramBinaryOS, CompileResult.PointerSizeInBytes);

if (CMProgram.HasErrors())
return getError(CMProgram.GetError(), OutputArgs);

if (IGC_IS_FLAG_ENABLED(ShaderDumpEnable))
Opts.Dumper->dumpBinary(ProgramBinary, "", "progbin");

llvm::StringRef BinaryRef{ProgramBinary.data(), ProgramBinary.size()};
outputBinary(BinaryRef, {}, Diag, OutputArgs);
break;
Expand Down
35 changes: 22 additions & 13 deletions IGC/VectorCompiler/include/vc/Support/ShaderDump.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*========================== begin_copyright_notice ============================
Copyright (C) 2020-2021 Intel Corporation
Copyright (C) 2020-2023 Intel Corporation
SPDX-License-Identifier: MIT
Expand All @@ -25,20 +25,29 @@ namespace vc {
class ShaderDumper {
public:
// Main methods, just dump either binary data or text.
virtual void dumpBinary(llvm::ArrayRef<char> Binary,
llvm::StringRef DumpName) = 0;
virtual void dumpText(llvm::StringRef Text, llvm::StringRef DumpName) = 0;
virtual void dumpBinary(llvm::ArrayRef<char> Binary, llvm::StringRef DumpName,
llvm::StringRef DumpExtension = {}) = 0;
virtual void dumpText(llvm::StringRef Text, llvm::StringRef DumpName,
llvm::StringRef DumpExtension = "txt") = 0;

// Convenience method to dump module.
virtual void dumpModule(const llvm::Module &M, llvm::StringRef DumpName) = 0;

virtual void dumpCos(llvm::StringRef Contents, llvm::StringRef DumpName) = 0;

// Hack required for finalizer dumps since it can dump only to
// specified file and not to generic stream.
// Return the same string by default.
virtual std::string composeDumpPath(llvm::StringRef DumpName) const {
return DumpName.str();
virtual void dumpModule(const llvm::Module &M, llvm::StringRef DumpName,
llvm::StringRef DumpExtension = "ll") = 0;

virtual void dumpCos(llvm::StringRef Contents, llvm::StringRef DumpName,
llvm::StringRef DumpExtension = "cos") = 0;

// Hack required for finalizer and zebin writer dumps since it can dump only
// to specified file instead of generic stream.
virtual std::string
composeDumpPath(llvm::StringRef DumpName,
llvm::StringRef DumpExtension = {}) const {
auto Path = DumpName.str();
if (!DumpExtension.empty()) {
Path += ".";
Path += DumpExtension;
}
return Path;
}

virtual ~ShaderDumper() = default;
Expand Down
10 changes: 5 additions & 5 deletions IGC/VectorCompiler/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static void printLLVMTimers(const vc::CompileOptions &Opts) {
return;

if (Opts.Dumper)
Opts.Dumper->dumpText(OutStr, "time_passes.txt");
Opts.Dumper->dumpText(OutStr, "time_passes");

// FIXME: it's not quite clear why we need to print stats to errs(),
// if we have shader dumper
Expand Down Expand Up @@ -497,7 +497,7 @@ Expected<vc::CompileOutput> vc::Compile(ArrayRef<char> Input,
Module &M = *ExpModule.get();

if (Opts.DumpIR && Opts.Dumper)
Opts.Dumper->dumpModule(M, "after_spirv_reader.ll");
Opts.Dumper->dumpModule(M, "after_spirv_reader");

if (Opts.StripDebugInfoCtrl == DebugInfoStripControl::All)
llvm::StripDebugInfo(M);
Expand Down Expand Up @@ -539,17 +539,17 @@ Expected<vc::CompileOutput> vc::Compile(ArrayRef<char> Input,
llvm::EnableStatistics(false /*DoPrintOnExit = false */);

if (Opts.DumpIR && Opts.Dumper)
Opts.Dumper->dumpModule(M, "after_ir_adaptors.ll");
Opts.Dumper->dumpModule(M, "after_ir_adaptors");

optimizeIR(Opts, ExtData, TM, M);

if (Opts.DumpIR && Opts.Dumper)
Opts.Dumper->dumpModule(M, "optimized.ll");
Opts.Dumper->dumpModule(M, "optimized");

vc::CompileOutput Output = runCodeGen(Opts, ExtData, TM, M);

if (Opts.DumpIR && Opts.Dumper)
Opts.Dumper->dumpModule(M, "final.ll");
Opts.Dumper->dumpModule(M, "final");

printLLVMStats(Opts);
printLLVMTimers(Opts);
Expand Down
8 changes: 5 additions & 3 deletions IGC/VectorCompiler/lib/GenXCodeGen/GenXCisaBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ void handleInlineAsmParseError(const GenXBackendConfig &BC, StringRef VisaErr,
if (!VisaErr.empty())
SS << VisaErr << '\n';
if (BC.hasShaderDumper() && BC.asmDumpsEnabled()) {
const char *DumpModuleName = "inline_asm_text.visaasm";
SS << "Full module dumped as '" << DumpModuleName << "'\n";
BC.getShaderDumper().dumpText(VisaText, DumpModuleName);
const char *DumpModuleName = "inline_asm_text";
const char *DumpModuleExt = "visaasm";
SS << "Full module dumped as '" << DumpModuleName << '.' << DumpModuleExt
<< "'\n";
BC.getShaderDumper().dumpText(VisaText, DumpModuleName, DumpModuleExt);
} else {
SS << "Enable dumps to see failed visa module\n";
}
Expand Down
Loading

0 comments on commit dca2074

Please sign in to comment.