Skip to content

Commit

Permalink
[SPIRV] Fix NonSemanticDebugInfo to include all files (#6935)
Browse files Browse the repository at this point in the history
Previously, we fixed the SPIRV OpenCL debug info to always represent all
include files, even those that only have preprocessor definitions. This
work was merged in
#6094. This PR
applies the same fix to the NonSemantic debug info, and resolves
#6907
  • Loading branch information
SteveUrquhart committed Sep 26, 2024
1 parent c0c01e9 commit 5c16ae7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
6 changes: 0 additions & 6 deletions tools/clang/lib/SPIRV/EmitVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,6 @@ void EmitVisitor::emitDebugLine(spv::Op op, const SourceLocation &loc,
debugColumnEnd = columnEnd;
}

if ((emittedSource[fileId] == 0) && (spvOptions.debugInfoVulkan)) {
SpirvDebugSource *src = new (context) SpirvDebugSource(fileName, "");
visit(src);
spvInstructions.push_back(src);
}

curInst.clear();
if (!spvOptions.debugInfoVulkan) {
curInst.push_back(static_cast<uint32_t>(spv::Op::OpLine));
Expand Down
24 changes: 17 additions & 7 deletions tools/clang/lib/SPIRV/SpirvEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,7 @@ SpirvEmitter::SpirvEmitter(CompilerInstance &ci)

// Rich DebugInfo DebugSource
if (spirvOptions.debugInfoRich) {
auto *dbgSrc =
spvBuilder.createDebugSource(mainSourceFile->getString(), source);
auto *dbgSrc = spvBuilder.createDebugSource(mainSourceFile->getString());
// spvContext.getDebugInfo().insert() inserts {string key, RichDebugInfo}
// pair and returns {{string key, RichDebugInfo}, true /*Success*/}.
// spvContext.getDebugInfo().insert().first->second is a RichDebugInfo.
Expand Down Expand Up @@ -839,17 +838,23 @@ void SpirvEmitter::HandleTranslationUnit(ASTContext &context) {
return;

// Add source instruction(s)
if ((spirvOptions.debugInfoSource || spirvOptions.debugInfoFile) &&
!spirvOptions.debugInfoVulkan) {
if (spirvOptions.debugInfoSource || spirvOptions.debugInfoFile) {
std::vector<llvm::StringRef> fileNames;
fileNames.clear();
const auto &sm = context.getSourceManager();
// Add each include file from preprocessor output
for (unsigned int i = 0; i < sm.getNumLineTableFilenames(); i++) {
fileNames.push_back(sm.getLineTableFilename(i));
llvm::StringRef file = sm.getLineTableFilename(i);
if (spirvOptions.debugInfoVulkan) {
getOrCreateRichDebugInfoImpl(file);
} else {
fileNames.push_back(file);
}
}
if (!spirvOptions.debugInfoVulkan) {
spvBuilder.setDebugSource(spvContext.getMajorVersion(),
spvContext.getMinorVersion(), fileNames);
}
spvBuilder.setDebugSource(spvContext.getMajorVersion(),
spvContext.getMinorVersion(), fileNames);
}

if (spirvOptions.enableMaximalReconvergence) {
Expand Down Expand Up @@ -1046,6 +1051,11 @@ RichDebugInfo *
SpirvEmitter::getOrCreateRichDebugInfo(const SourceLocation &loc) {
const StringRef file =
astContext.getSourceManager().getPresumedLoc(loc).getFilename();
return getOrCreateRichDebugInfoImpl(file);
}

RichDebugInfo *
SpirvEmitter::getOrCreateRichDebugInfoImpl(llvm::StringRef file) {
auto &debugInfo = spvContext.getDebugInfo();
auto it = debugInfo.find(file);
if (it != debugInfo.end())
Expand Down
1 change: 1 addition & 0 deletions tools/clang/lib/SPIRV/SpirvEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SpirvEmitter : public ASTConsumer {
/// created, we just return RichDebugInfo containing it. Otherwise,
/// create DebugSource and DebugCompilationUnit for loc and return it.
RichDebugInfo *getOrCreateRichDebugInfo(const SourceLocation &loc);
RichDebugInfo *getOrCreateRichDebugInfoImpl(llvm::StringRef file);

void doDecl(const Decl *decl);
void doStmt(const Stmt *stmt, llvm::ArrayRef<const Attr *> attrs = {});
Expand Down
24 changes: 24 additions & 0 deletions tools/clang/test/CodeGenSPIRV/shader.debug.opsource.include.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %dxc -T ps_6_0 -E main -Zi %s -spirv -fspv-debug=vulkan-with-source | FileCheck %s

// CHECK: [[main:%[0-9]+]] = OpString
// CHECK-SAME: shader.debug.opsource.include.hlsl
// CHECK: [[file1:%[0-9]+]] = OpString
// CHECK-SAME: spirv.debug.opsource.include-file.hlsli
// CHECK: [[file1string:%[0-9]+]] = OpString "#define UBER_TYPE(x) x ## Type
// CHECK: [[mainstring:%[0-9]+]] = OpString "// RUN: %dxc -T ps_6_0 -E main -Zi %s -spirv -fspv-debug=vulkan-with-source | FileCheck %s

// CHECK: DebugSource [[file1]] [[file1string]]
// CHECK: DebugSource [[main]] [[mainstring]]

#include "spirv.debug.opsource.include-file.hlsli"

struct ColorType
{
float4 position : SV_POSITION;
float4 color : COLOR;
};

float4 main(UBER_TYPE(Color) input) : SV_TARGET
{
return input.color;
}
3 changes: 1 addition & 2 deletions tools/clang/tools/dxcompiler/dxcompilerobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,7 @@ class DxcCompiler : public IDxcCompiler3,
// pre-seeding with #line directives. We invoke Preprocess() here
// first for such case. Then we invoke the compilation process over the
// preprocessed source code.
if (!isPreprocessing && opts.GenSPIRV && opts.DebugInfo &&
!opts.SpirvOptions.debugInfoVulkan) {
if (!isPreprocessing && opts.GenSPIRV && opts.DebugInfo) {
// Convert source code encoding
CComPtr<IDxcBlobUtf8> pOrigUtf8Source;
IFC(hlsl::DxcGetBlobAsUtf8(pSourceEncoding, m_pMalloc,
Expand Down

0 comments on commit 5c16ae7

Please sign in to comment.