Skip to content

Conversation

pvelesko
Copy link
Contributor

The translator was incorrectly generating SPIR-V debug information for functions. Specifically:

  1. DebugFunction Declaration Issue: When a function definition didn't have a separate declaration, the DebugFunction instruction was using DebugInfoNone instead of a proper DebugFunctionDeclaration for its Declaration operand.
  2. Void Type Handling: The translator was not properly handling void types in debug information, which could cause issues with function return types.

The PR implements two main fixes:

1. Proper DebugFunctionDeclaration Creation (transDbgFunction)

  • When a function definition lacks a separate declaration (Func->getDeclaration() returns null), instead of using DebugInfoNone, the code now:
  • Creates a proper DebugFunctionDeclaration with all required operands (name, type, source, line, column, parent scope, linkage name, flags)
  • Uses this created declaration as the Declaration operand for the DebugFunction

2. Proper Void Debug Type Handling (getVoidDebugType and transDbgSubroutineType)

  • Adds a new getVoidDebugType() method that creates a proper TypeBasic debug type for void (with size 0 and unspecified encoding)
  • Updates transDbgSubroutineType to use this proper void debug type instead of raw void type or DebugInfoNone

Added a test and validated that test fails wtihout the fix and passes with it.

[5/6] Running the LLVM-SPIRV regression tests
llvm-lit: /space/pvelesko/.local/lib/python3.10/site-packages/lit/llvm/config.py:502: note: using clang: /space/pvelesko/install/llvm/21.0-upstream/bin/clang
FAIL: LLVM_SPIRV :: DebugInfo/DebugFunctionDeclarationBug.cl (414 of 918)
******************** TEST 'LLVM_SPIRV :: DebugInfo/DebugFunctionDeclarationBug.cl' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 5: /space/pvelesko/install/llvm/21.0-upstream/bin/clang -cc1 -internal-isystem /space/pvelesko/install/llvm/21.0-upstream/lib/clang/21/include -nostdsysteminc /space/pvelesko/SPIRV-LLVM-Translator/test/DebugInfo/DebugFunctionDeclarationBug.cl -emit-llvm-bc -triple spir -debug-info-kind=limited -O0 -o - | /space/pvelesko/SPIRV-LLVM-Translator/build/tools/llvm-spirv/llvm-spirv -o /space/pvelesko/SPIRV-LLVM-Translator/build/test/test_output/DebugInfo/Output/DebugFunctionDeclarationBug.cl.tmp.spv
+ /space/pvelesko/install/llvm/21.0-upstream/bin/clang -cc1 -internal-isystem /space/pvelesko/install/llvm/21.0-upstream/lib/clang/21/include -nostdsysteminc /space/pvelesko/SPIRV-LLVM-Translator/test/DebugInfo/DebugFunctionDeclarationBug.cl -emit-llvm-bc -triple spir -debug-info-kind=limited -O0 -o -
+ /space/pvelesko/SPIRV-LLVM-Translator/build/tools/llvm-spirv/llvm-spirv -o /space/pvelesko/SPIRV-LLVM-Translator/build/test/test_output/DebugInfo/Output/DebugFunctionDeclarationBug.cl.tmp.spv
RUN: at line 6: /usr/local/bin/spirv-val /space/pvelesko/SPIRV-LLVM-Translator/build/test/test_output/DebugInfo/Output/DebugFunctionDeclarationBug.cl.tmp.spv
+ /usr/local/bin/spirv-val /space/pvelesko/SPIRV-LLVM-Translator/build/test/test_output/DebugInfo/Output/DebugFunctionDeclarationBug.cl.tmp.spv
error: line 49: OpenCL.DebugInfo.100 DebugTypeFunction: expected operand Return Type is not a valid debug type
  %36 = OpExtInst %void %2 DebugTypeFunction None %11

@MrSidims
Copy link
Contributor

Restarting CI with a new HEAD

@MrSidims MrSidims closed this Jul 25, 2025
@MrSidims MrSidims reopened this Jul 25, 2025
Copy link
Contributor

@MrSidims MrSidims left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to split this patch into 2. First is addressing an issue with DebugFunction Type. And another is addressing null translation for function declaration.

@pvelesko please also note, that currently existing tests are failing with the patch, I believe their failure is related due to null handling part of the patch.

Comment on lines +1339 to +1343
DeclOps[SPIRVDebug::Operand::FunctionDeclaration::NameIdx] = BM->getString(Func->getName().str())->getId();
DeclOps[SPIRVDebug::Operand::FunctionDeclaration::TypeIdx] = transDbgEntry(Func->getType())->getId();
DeclOps[SPIRVDebug::Operand::FunctionDeclaration::SourceIdx] = getSource(Func)->getId();
DeclOps[SPIRVDebug::Operand::FunctionDeclaration::LineIdx] = Func->getLine();
DeclOps[SPIRVDebug::Operand::FunctionDeclaration::ColumnIdx] = 0; // This version of DISubprogram has no column number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we are already using namespace SPIRVDebug::Operand::Function few lines above, so it's not really needed to specify it here.

@pvelesko
Copy link
Contributor Author

@MrSidims I now realize that I ran the unit tests incorrectly on my machine and that DebugInfoNone problem is not that trivial.

When translating LLVM IR to SPIR-V, functions without explicit declarations need a value for their Declaration operand in the DebugFunction instruction. We use DebugInfoNone, but spirv-val rejects this.

LLVM IR: Functions can exist without separate declaration metadata
SPIR-V Translation: We use DebugInfoNone for the missing declaration
spirv-val: Enforces OpenCL.DebugInfo.100 spec which requires Declaration to be a DebugFunctionDeclaration, not DebugInfoNone

Main problem:
my fix for spirv-val: Create synthetic DebugFunctionDeclaration entries
Side effect: This adds declaration: !13 to the LLVM IR when converting back
Result: 25+ tests fail because they expect no declaration field

The issue is fundamentally a mismatch between LLVM's flexible debug model and SPIR-V's strict validation requirements.

@MrSidims I guess I can strip debugInfo for when I am validating SPIR-V in my application but I'm not sure how to properly fix this discrepancy. Any ideas?

@MrSidims
Copy link
Contributor

@pvelesko IMHO current DebugInfo specification is too strict in terms of forbidding DebugInfoNone usages. I still have a homework to do to lift few of them off in KhronosGroup/SPIRV-Registry#186 and may be try to push for changes in .100 version. In the translator, as you have noticed, the rules are relaxed, may be even too relaxed, which is not a good thing as well.

So, about this specifically, I think we should be somewhat leaning not even to LLVM IR debug information specification, but to DWARF, when defining SPIR-V debug info. And unless I'm misinterpreting it: DW_AT_declaration attribute can be unspecified, especially in cases if a debug entity has DW_AT_specification attribute or DW_AT_abstract_origin (at least such interpretation makes sense to me, as why do we need to keep declaration attribute, when we have definition?).

I'll do a bit of more research and will open an issue in SPIR-V registry, proposing to relax rule(s) for DebugInfoNone (I could imagine relaxing them, for example, for mem2reged variables), but I can't promise to do it soon. Alternatively (if you agree with the statement, that declaration can be DebugInfoNone) you may open the issue yourself :)

@MrSidims
Copy link
Contributor

@pvelesko hi, sorry it took a bit longer, but eventually I'm returning to DebugInfoNone placement in SPIR-V. And for this very case it seem to be a straight-forward bug in llvm-spirv. Per both https://registry.khronos.org/SPIR-V/specs/unified1/OpenCL.DebugInfo.100.html#DebugFunction and https://github.com/KhronosGroup/SPIRV-Registry/blob/main/nonsemantic/NonSemantic.Shader.DebugInfo.100.asciidoc#DebugFunction - Declaration operand is optional. Hence the translator mustn't put DebugInfoNone as the last operand in case of declaration's absence.

@MrSidims
Copy link
Contributor

Created #3354

MrSidims added a commit that referenced this pull request Sep 23, 2025
In case of missing declaration we shouldn't place DebugInfoNone for
OpenCL and Shader.DebugInfo.100 instruction sets.

Solves issue from
#3275

Signed-off-by: Sidorov, Dmitry <[email protected]>
iclsrc pushed a commit to intel/llvm that referenced this pull request Sep 27, 2025
In case of missing declaration we shouldn't place DebugInfoNone for
OpenCL and Shader.DebugInfo.100 instruction sets.

Solves issue from
KhronosGroup/SPIRV-LLVM-Translator#3275

Signed-off-by: Sidorov, Dmitry <[email protected]>

Original commit:
KhronosGroup/SPIRV-LLVM-Translator@fa9bc14efcd68e0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants