Sanitize debug source location tracking for implicit branch and return

This patch tries to attach debug location of a branch/return instruction to its predecessor or the closing brace. If none could be found, no debug info should be emitted.
This commit is contained in:
Qingyuan Zheng 2024-08-26 03:53:07 +00:00 committed by arcady-lunarg
parent b1fac200c4
commit a496a34b43
30 changed files with 4713 additions and 4253 deletions

View file

@ -1558,7 +1558,7 @@ TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion,
else {
builder.setEmitSpirvDebugInfo();
}
builder.setDebugSourceFile(glslangIntermediate->getSourceFile());
builder.setDebugMainSourceFile(glslangIntermediate->getSourceFile());
// Set the source shader's text. If for SPV version 1.0, include
// a preamble in comments stating the OpModuleProcessed instructions.
@ -2967,6 +2967,12 @@ bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TInt
currentFunction->setDebugLineInfo(sourceFileId, loc.line, loc.column);
}
} else {
if (options.generateDebugInfo) {
if (glslangIntermediate->getSource() == glslang::EShSourceGlsl && node->getSequence().size() > 1) {
auto endLoc = node->getSequence()[1]->getAsAggregate()->getEndLoc();
builder.setDebugSourceLocation(endLoc.line, endLoc.getFilename());
}
}
if (inEntryPoint)
entryPointTerminated = true;
builder.leaveFunction();
@ -4120,7 +4126,7 @@ bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::T
if (codeSegments[s])
codeSegments[s]->traverse(this);
else
builder.addSwitchBreak();
builder.addSwitchBreak(true);
}
breakForLoop.pop();
@ -4144,7 +4150,7 @@ void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* n
bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
{
auto blocks = builder.makeNewLoop();
builder.createBranch(&blocks.head);
builder.createBranch(true, &blocks.head);
// Loop control:
std::vector<unsigned int> operands;
@ -4161,7 +4167,7 @@ bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIn
builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands);
if (node->testFirst() && node->getTest()) {
spv::Block& test = builder.makeNewBlock();
builder.createBranch(&test);
builder.createBranch(true, &test);
builder.setBuildPoint(&test);
node->getTest()->traverse(this);
@ -4172,22 +4178,22 @@ bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIn
breakForLoop.push(true);
if (node->getBody())
node->getBody()->traverse(this);
builder.createBranch(&blocks.continue_target);
builder.createBranch(true, &blocks.continue_target);
breakForLoop.pop();
builder.setBuildPoint(&blocks.continue_target);
if (node->getTerminal())
node->getTerminal()->traverse(this);
builder.createBranch(&blocks.head);
builder.createBranch(true, &blocks.head);
} else {
builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename());
builder.createBranch(&blocks.body);
builder.createBranch(true, &blocks.body);
breakForLoop.push(true);
builder.setBuildPoint(&blocks.body);
if (node->getBody())
node->getBody()->traverse(this);
builder.createBranch(&blocks.continue_target);
builder.createBranch(true, &blocks.continue_target);
breakForLoop.pop();
builder.setBuildPoint(&blocks.continue_target);
@ -4202,7 +4208,7 @@ bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIn
// TODO: unless there was a break/return/discard instruction
// somewhere in the body, this is an infinite loop, so we should
// issue a warning.
builder.createBranch(&blocks.head);
builder.createBranch(true, &blocks.head);
}
}
builder.setBuildPoint(&blocks.merge);
@ -4238,7 +4244,7 @@ bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::T
if (breakForLoop.top())
builder.createLoopExit();
else
builder.addSwitchBreak();
builder.addSwitchBreak(false);
break;
case glslang::EOpContinue:
builder.createLoopContinue();