Skip to content

Commit

Permalink
Support NonSemantic DebugValue and generate it for const arg
Browse files Browse the repository at this point in the history
For passing-by-value semantic, DebugDeclare cannot be used for parameters because there's no pointer.
  • Loading branch information
qingyuanzNV authored and jeremy-lunarg committed Nov 14, 2023
1 parent 1dcb072 commit 109b597
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
46 changes: 32 additions & 14 deletions SPIRV/SpvBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,13 +1182,26 @@ Id Builder::makeDebugExpression()
return debugExpression;
}

Id Builder::makeDebugDeclare(Id const debugLocalVariable, Id const localVariable)
Id Builder::makeDebugDeclare(Id const debugLocalVariable, Id const pointer)
{
Instruction* inst = new Instruction(getUniqueId(), makeVoidType(), OpExtInst);
inst->addIdOperand(nonSemanticShaderDebugInfo);
inst->addImmediateOperand(NonSemanticShaderDebugInfo100DebugDeclare);
inst->addIdOperand(debugLocalVariable); // debug local variable id
inst->addIdOperand(localVariable); // local variable id
inst->addIdOperand(pointer); // pointer to local variable id
inst->addIdOperand(makeDebugExpression()); // expression id
buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));

return inst->getResultId();
}

Id Builder::makeDebugValue(Id const debugLocalVariable, Id const value)
{
Instruction* inst = new Instruction(getUniqueId(), makeVoidType(), OpExtInst);
inst->addIdOperand(nonSemanticShaderDebugInfo);
inst->addImmediateOperand(NonSemanticShaderDebugInfo100DebugValue);
inst->addIdOperand(debugLocalVariable); // debug local variable id
inst->addIdOperand(value); // value of local variable id
inst->addIdOperand(makeDebugExpression()); // expression id
buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));

Expand Down Expand Up @@ -2148,20 +2161,25 @@ void Builder::setupDebugFunctionEntry(Function* function, const char* name, int
Id firstParamId = function->getParamId(0);

for (size_t p = 0; p < paramTypes.size(); ++p) {
auto getParamTypeId = [this](Id const& typeId) {
if (isPointerType(typeId) || isArrayType(typeId)) {
return getContainedTypeId(typeId);
} else {
return typeId;
}
};
auto const& paramName = paramNames[p];
auto const debugLocalVariableId =
createDebugLocalVariable(debugId[getParamTypeId(paramTypes[p])], paramName, p + 1);
bool passByRef = false;
Id paramTypeId = paramTypes[p];

debugId[firstParamId + p] = debugLocalVariableId;
// For pointer-typed parameters, they are actually passed by reference and we need unwrap the pointer to get the actual parameter type.
if (isPointerType(paramTypeId) || isArrayType(paramTypeId)) {
passByRef = true;
paramTypeId = getContainedTypeId(paramTypeId);
}

makeDebugDeclare(debugLocalVariableId, firstParamId + p);
auto const& paramName = paramNames[p];
auto const debugLocalVariableId = createDebugLocalVariable(debugId[paramTypeId], paramName, p + 1);
auto const paramId = static_cast<Id>(firstParamId + p);
debugId[paramId] = debugLocalVariableId;

if (passByRef) {
makeDebugDeclare(debugLocalVariableId, paramId);
} else {
makeDebugValue(debugLocalVariableId, paramId);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion SPIRV/SpvBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Builder {
Id createDebugGlobalVariable(Id const type, char const*const name, Id const variable);
Id createDebugLocalVariable(Id type, char const*const name, size_t const argNumber = 0);
Id makeDebugExpression();
Id makeDebugDeclare(Id const debugLocalVariable, Id const localVariable);
Id makeDebugDeclare(Id const debugLocalVariable, Id const pointer);
Id makeDebugValue(Id const debugLocalVariable, Id const value);
Id makeDebugFunctionType(Id returnType, const std::vector<Id>& paramTypes);
Id makeDebugFunction(Function* function, Id nameId, Id funcTypeId);
Expand Down
26 changes: 13 additions & 13 deletions Test/baseResults/spv.debuginfo.const_params.glsl.comp.out
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ spv.debuginfo.const_params.glsl.comp
Return
FunctionEnd
Line 1 7 18
33(function(f1;vf2;vf3;vf4;): 4 Function None 27
29(f): 16(float) FunctionParameter
30(f2): 19(fvec2) FunctionParameter
31(f3): 22(fvec3) FunctionParameter
32(f4): 24(fvec4) FunctionParameter
34: Label
42: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 23(DebugScope) 36
43: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 37 39 39 12 12
46: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 28(DebugDeclare) 44 29(f) 47
50: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 28(DebugDeclare) 48 30(f2) 47
53: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 28(DebugDeclare) 51 31(f3) 47
56: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 28(DebugDeclare) 54 32(f4) 47
60: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 101(DebugFunctionDefinition) 36 33(function(f1;vf2;vf3;vf4;)
39(function(f1;vf2;vf3;vf4;): 4 Function None 33
35(f): 24(float) FunctionParameter
36(f2): 27(fvec2) FunctionParameter
37(f3): 29(fvec3) FunctionParameter
38(f4): 31(fvec4) FunctionParameter
42: Label
43: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 23(DebugScope) 41
44: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 17 12 12 12 12
47: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 29(DebugValue) 45 35(f) 48
51: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 29(DebugValue) 49 36(f2) 48
54: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 29(DebugValue) 52 37(f3) 48
57: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 29(DebugValue) 55 38(f4) 48
58: 4 ExtInst 2(NonSemantic.Shader.DebugInfo.100) 101(DebugFunctionDefinition) 41 39(function(f1;vf2;vf3;vf4;)
Return
FunctionEnd

0 comments on commit 109b597

Please sign in to comment.