Skip to content

Commit

Permalink
Minor shader debugging fixes
Browse files Browse the repository at this point in the history
Provide several small fix and improvement to shader debugging code.
  • Loading branch information
elvinw-intel authored and igcbot committed Nov 14, 2024
1 parent bbdf8df commit a7165ef
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 71 deletions.
30 changes: 21 additions & 9 deletions IGC/AdaptorCommon/customApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ namespace IGC
GetDebugFlag(DebugFlag::VISA_BINARY) ||
GetDebugFlag(DebugFlag::VISA_DUMPCOMMONISA) ||
IGC_IS_FLAG_ENABLED(EnableCapsDump) ||
IGC_IS_FLAG_ENABLED(ShaderOverride);
IGC_IS_FLAG_ENABLED(ShaderOverride) ||
IGC_IS_FLAG_ENABLED(GenerateOptionsFile);
}

OutputFolderName IGC_DEBUG_API_CALL GetBaseIGCOutputFolder()
Expand Down Expand Up @@ -494,7 +495,7 @@ namespace IGC
{
static std::mutex m;
std::lock_guard<std::mutex> lck(m);
if(g_shaderOutputFolder != "")
if(g_shaderOutputFolder != "" && doesRegexMatch(g_shaderOutputFolder, IGC_GET_REGKEYSTRING(ShaderDumpFilter)))
{
return g_shaderOutputFolder.c_str();
}
Expand Down Expand Up @@ -525,12 +526,19 @@ namespace IGC
sprintf_s(dumpPath, "%sunknownProcess_%d\\", dumpPath, _getpid());
}

if (GetFileAttributesA(dumpPath) != FILE_ATTRIBUTE_DIRECTORY && needMkDir())
if (needMkDir() && doesRegexMatch(dumpPath, IGC_GET_REGKEYSTRING(ShaderDumpFilter)))
{
_mkdir(dumpPath);
if (GetFileAttributesA(dumpPath) != FILE_ATTRIBUTE_DIRECTORY)
{
_mkdir(dumpPath);
}
g_shaderOutputFolder = dumpPath;
}
else
{
// To make the path always invalid.
g_shaderOutputFolder = "NUL\\";
}

g_shaderOutputFolder = dumpPath;
}
else if (IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
Expand All @@ -550,7 +558,7 @@ namespace IGC
#elif defined __linux__
if (!IGC_IS_FLAG_ENABLED(DumpToCurrentDir) && g_shaderOutputFolder == "" && !IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
bool needMkdir = needMkDir();
bool needMkdir = needMkDir() && doesRegexMatch(GetBaseIGCOutputFolder(), IGC_GET_REGKEYSTRING(ShaderDumpFilter));

char path[MAX_PATH] = { 0 };
bool pidEnabled = IGC_IS_FLAG_DISABLED(ShaderDumpPidDisable);
Expand All @@ -564,9 +572,13 @@ namespace IGC
false,
true,
pidEnabled);
g_shaderOutputFolder = path;
}
else
{
// To make the path always invalid.
g_shaderOutputFolder = "/dev/null/";
}

g_shaderOutputFolder = path;
}
else if (IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
Expand Down
17 changes: 11 additions & 6 deletions IGC/AdaptorOCL/OCL/sp/spp_g8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ static void dumpZEInfo(const IGC::CodeGenContext &Ctx,
.Hash(Ctx.hash)
.Type(ShaderType::OPENCL_SHADER)
.Extension("zeinfo");

ZEBuilder.printZEInfo(filename.str());
if (filename.allow())
{
ZEBuilder.printZEInfo(filename.str());
}
}

void dumpOCLKernelBinary(
Expand All @@ -306,10 +308,13 @@ void dumpOCLKernelBinary(
const auto &KernBin = data.kernelBinary;
IGC_ASSERT(KernBin);

std::error_code EC;
llvm::raw_fd_ostream f(name.str(), EC);
if (!EC)
f.write(KernBin->GetLinearPointer(), (size_t)KernBin->Size());
if (name.allow())
{
std::error_code EC;
llvm::raw_fd_ostream f(name.str(), EC);
if (!EC)
f.write(KernBin->GetLinearPointer(), (size_t)KernBin->Size());
}
}

void overrideOCLKernelBinary(
Expand Down
29 changes: 18 additions & 11 deletions IGC/AdaptorOCL/dllInterfaceCompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,34 +363,38 @@ void DumpShaderFile(
const UINT bufferSize,
const QWORD hash,
const std::string& ext,
std::string* fileName = nullptr)
std::string* fullFilePath = nullptr)
{
if (!pBuffer || bufferSize == 0)
{
return;
}

std::ostringstream fullPath(dstDir, std::ostringstream::ate);
fullPath << "OCL_asm"
std::ostringstream fileName(std::ostringstream::ate);
fileName << "OCL_asm"
<< std::hex
<< std::setfill('0')
<< std::setw(sizeof(hash) * CHAR_BIT / 4)
<< hash
<< std::dec
<< std::setfill(' ')
<< ext;
std::string fullFilePathStr = dstDir + fileName.str();

FILE* pFile = NULL;
fopen_s(&pFile, fullPath.str().c_str(), "wb");
if (pFile)
if (doesRegexMatch(fileName.str(), IGC_GET_REGKEYSTRING(ShaderDumpFilter)))
{
fwrite(pBuffer, 1, bufferSize, pFile);
fclose(pFile);
FILE* pFile = NULL;
fopen_s(&pFile, fullFilePathStr.c_str(), "wb");
if (pFile)
{
fwrite(pBuffer, 1, bufferSize, pFile);
fclose(pFile);
}
}

if (fileName != nullptr)
if (fullFilePath != nullptr)
{
*fileName = fullPath.str();
*fullFilePath = std::move(fullFilePathStr);
}
}

Expand Down Expand Up @@ -1159,7 +1163,10 @@ void dumpOCLProgramBinary(OpenCLProgramContext &Ctx, const char *binaryOutput,
.Type(ShaderType::OPENCL_SHADER)
.Extension("progbin");

dumpOCLProgramBinary(name.str().data(), binaryOutput, binarySize);
if (name.allow())
{
dumpOCLProgramBinary(name.str().data(), binaryOutput, binarySize);
}
}

static void WriteSpecConstantsDump(
Expand Down
15 changes: 9 additions & 6 deletions IGC/Compiler/CISACodeGen/CISABuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6774,12 +6774,15 @@ namespace IGC
if (dbgSize > 0)
{
// dump dbg file only if it not empty
std::string debugFileNameStr = IGC::Debug::GetDumpName(m_program, "dbg");
FILE* const dbgFile = fopen(debugFileNameStr.c_str(), "wb+");
if (nullptr != dbgFile)
auto debugFileNameObj = IGC::Debug::GetDumpNameObj(m_program, "dbg");
if (debugFileNameObj.allow())
{
fwrite(genxdbgInfo, dbgSize, 1, dbgFile);
fclose(dbgFile);
FILE* const dbgFile = fopen(debugFileNameObj.str().c_str(), "wb+");
if (nullptr != dbgFile)
{
fwrite(genxdbgInfo, dbgSize, 1, dbgFile);
fclose(dbgFile);
}
}
}
}
Expand Down Expand Up @@ -8426,7 +8429,7 @@ namespace IGC

std::string CEncoder::GetDumpFileName(std::string extension)
{
std::string filename = IGC::Debug::GetDumpName(m_program, extension.c_str());
std::string filename = IGC::Debug::GetDumpNameObj(m_program, extension.c_str()).str();
return filename;
}

Expand Down
14 changes: 9 additions & 5 deletions IGC/Compiler/CISACodeGen/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,16 @@ static void debugDump(const CShader* Shader, llvm::StringRef Ext,
DumpNameObj = DumpNameObj.Hash(hash);
DumpName = DumpNameObj.AbsolutePath(IGC_GET_REGKEYSTRING(DebugDumpNamePrefix));
}
FILE* const DumpFile = fopen(DumpName.c_str(), "wb+");
if (nullptr == DumpFile)
return;

fwrite(Blob.data(), Blob.size(), 1, DumpFile);
fclose(DumpFile);
if (DumpNameObj.allow())
{
FILE* const DumpFile = fopen(DumpName.c_str(), "wb+");
if (nullptr == DumpFile)
return;

fwrite(Blob.data(), Blob.size(), 1, DumpFile);
fclose(DumpFile);
}
}

void DebugInfoPass::EmitDebugInfo(bool finalize,
Expand Down
15 changes: 9 additions & 6 deletions IGC/Compiler/CISACodeGen/OpenCLKernelCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3319,15 +3319,18 @@ namespace IGC

dumpName = dumpName.PostFix(shaderName);

std::ostringstream FullPath(dumpName.str(), std::ostringstream::ate);
FullPath << "_previous_kernel_pick.txt";
if (dumpName.allow())
{
std::ostringstream FullPath(dumpName.str(), std::ostringstream::ate);
FullPath << "_previous_kernel_pick.txt";

std::ofstream OutF(FullPath.str(), std::ofstream::out);
std::ofstream OutF(FullPath.str(), std::ofstream::out);


if (OutF)
OutF.write(reason.str().c_str(),
reason.str().length());
if (OutF)
OutF.write(reason.str().c_str(),
reason.str().length());
}
}

pSelectedKernel =
Expand Down
23 changes: 7 additions & 16 deletions IGC/common/debug/Dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ SPDX-License-Identifier: MIT
#include <iomanip>
#include <mutex>
#include <algorithm>
#include <regex>
#include "Probe/Assertion.h"

using namespace IGC;
Expand Down Expand Up @@ -440,13 +439,7 @@ std::string DumpName::RelativePath() const

bool DumpName::allow() const
{
const char* regex = IGC_GET_REGKEYSTRING(ShaderDumpFilter);
if (!regex || *regex == '\0')
return true;

std::regex fileRegex(regex);

return std::regex_search(RelativePath(), fileRegex);
return doesRegexMatch(RelativePath(), IGC_GET_REGKEYSTRING(ShaderDumpFilter));
}

namespace {
Expand Down Expand Up @@ -649,9 +642,12 @@ void Dump::flush()
}
if (!isText(m_type))
mode |= std::ios_base::binary;
std::ofstream asmFile(m_name.str(), mode);
asmFile << m_string;
asmFile.close();
if (m_name.allow())
{
std::ofstream asmFile(m_name.str(), mode);
asmFile << m_string;
asmFile.close();
}
m_string.clear();
}

Expand Down Expand Up @@ -730,11 +726,6 @@ ShaderHash ShaderHashOGL(QWORD glslHash, QWORD nosHash)
return shaderHash;
}

std::string GetDumpName(const IGC::CShader* pProgram, const char* ext)
{
return GetDumpNameObj(pProgram, ext).str();
}

DumpName GetDumpNameObj(const IGC::CShader* pProgram, const char* ext)
{
IGC::CodeGenContext* context = pProgram->GetContext();
Expand Down
10 changes: 3 additions & 7 deletions IGC/common/debug/Dump.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ class DumpName
std::optional<unsigned> m_retryId;
};

/// return the name of the file to dump
std::string GetDumpName(const CShader* pProgram, const char* ext);

/// return the name of the file to dump
DumpName GetDumpNameObj(const CShader* pProgram, const char* ext);

Expand Down Expand Up @@ -211,12 +208,11 @@ inline FILE* OpenDumpFile(
const ShaderHash& hash,
const char* flag)
{
std::string name =
auto name =
IGC::Debug::DumpName(fileNamePrefix)
.Hash(hash)
.Extension(fileNameExt)
.str();
FILE* fp = fopen(name.c_str(), flag);
.Extension(fileNameExt);
FILE* fp = name.allow() ? fopen(name.str().c_str(), flag) : NULL;
return fp;
}

Expand Down
1 change: 0 additions & 1 deletion IGC/common/igc_regkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ static void setImpliedIGCKeys()

IGC_SET_IMPLIED_REGKEY(ShaderDumpEnableAll, 1, ShaderDumpEnable, true);
IGC_SET_IMPLIED_REGKEY(ShaderDumpEnableAll, 1, EnableVISASlowpath, true);
IGC_SET_IMPLIED_REGKEY(ShaderDumpEnableAll, 1, EnableVISADumpCommonISA, true);
IGC_SET_IMPLIED_REGKEY(ShaderDumpEnable, 1, DumpLLVMIR, true);
IGC_SET_IMPLIED_REGKEY(ShaderDumpEnable, 1, EnableCosDump, true);
IGC_SET_IMPLIED_REGKEY(ShaderDumpEnable, 1, DumpOCLProgramInfo, true);
Expand Down
7 changes: 7 additions & 0 deletions IGC/common/igc_regkeys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PURPOSE: Defines meta data for holding/defining regkey variables
#include "common/shaderHash.hpp"
#include "Probe/Assertion.h"
#include <string>
#include <regex>
#include "CommonMacros.h"

typedef char debugString[1024];
Expand Down Expand Up @@ -154,6 +155,11 @@ void setImpliedRegkey(SRegKeyVariableMetaData& name,
SRegKeyVariableMetaData& subname,
const unsigned value);

inline bool doesRegexMatch(const std::string& src, const char* regex)
{
return (regex && *regex != '\0') ? std::regex_search(src, std::regex(regex)) : true;
}

extern SRegKeysList g_RegKeyList;
#if defined(LINUX_RELEASE_MODE)
#define IGC_GET_FLAG_VALUE(name) \
Expand Down Expand Up @@ -267,6 +273,7 @@ bool IsEnabled(const T& value)
(IGC_IS_FLAG_ENABLED(name) || IGC::Debug::GetDebugFlag(IGC::Debug::DebugFlag::flag))
#define IGC_SET_IMPLIED_REGKEY(name, setOnValue, subname, subvalue) {}
#define IGC_SET_IMPLIED_REGKEY_ANY(name, subname, subvalue) {}
inline bool doesRegexMatch(const std::string& src, const char* regex) { return false; }
#endif

// unset: Unlimited/Enable - return true
Expand Down
18 changes: 14 additions & 4 deletions visa/Common_ISA_framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,26 @@ int CisaInst::createCisaInstruction(ISA_Opcode opcode, unsigned char exec_size,
return VISA_SUCCESS;
}

bool allowDump(const Options &options, const std::string &fileName) {
bool allowDump(const Options &options, const std::string &fullPath) {
const char *regex = options.getOptionCstr(vISA_ShaderDumpFilter);
if (!regex || *regex == '\0')
return true;

std::regex fileRegex(regex);

return std::regex_search(fileName, fileRegex);
const auto fileNameIdx = fullPath.find_last_of("/\\");
if (std::string::npos != fileNameIdx)
{
const auto fileNamePos = 1 + fileNameIdx;
const auto fileName = fullPath.substr(fileNamePos, fullPath.size() - fileNamePos);

return std::regex_search(fileName, fileRegex);
}
else
{
// Do NOT dump if there's no prefix path.
return false;
}
}

} // namespace CisaFramework
Expand Down Expand Up @@ -231,7 +243,6 @@ int CISA_IR_Builder::isaDump(const char *combinedIsaasmName) const {
} else {
std::ofstream out(asmFileName);
out << isaDump(kTemp, repKernel);
vISA_ASSERT(!out.fail(), "Failed to write CISA ASM to file");
}
}
}
Expand All @@ -243,7 +254,6 @@ int CISA_IR_Builder::isaDump(const char *combinedIsaasmName) const {
vASSERT(combinedIsaasmName);
std::ofstream out(combinedIsaasmName);
out << ss.rdbuf();
vISA_ASSERT(!out.fail(), "Failed to write combined CISA ASM to file");
}
}
// Return early exit if emitting isaasm to console.
Expand Down

0 comments on commit a7165ef

Please sign in to comment.