Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/test_spirv_asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,49 @@ def test_assembler_leaves_non_type_redefinitions_to_validator(self):
with self.subTest(body=body):
self.assertTrue(spirv_asm.assemble("%uint = OpTypeInt 32 0\n" + body))

def test_named_shader_debuginfo_with_forward_references(self):
# A function refers to its parent composite, which in turn lists the function as a member.
asm = """OpCapability Shader
OpExtension "SPV_KHR_non_semantic_info"
OpExtension "SPV_KHR_relaxed_extended_instruction"
%dbg = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
%name = OpString "demo"
%void = OpTypeVoid
%uint = OpTypeInt 32 0
%zero = OpConstant %uint 0
%fn = OpTypeFunction %void
%src = OpExtInst %void %dbg DebugSource %name %name
%unit = OpExtInst %void %dbg DebugCompilationUnit %zero %zero %src %zero
%type = OpExtInstWithForwardRefsKHR %void %dbg DebugTypeFunction %zero %class
%method = OpExtInstWithForwardRefsKHR %void %dbg DebugFunction %name %type %src %zero %zero %class %name %zero %zero
%class = OpExtInst %void %dbg DebugTypeComposite %name %zero %src %zero %zero %unit %name %zero %zero %method
%main = OpFunction %void None %fn
%entry = OpLabel
OpReturn
OpFunctionEnd
"""
numeric = asm
for name, opcode in (("DebugSource", 35), ("DebugCompilationUnit", 1), ("DebugTypeFunction", 8),
("DebugFunction", 20), ("DebugTypeComposite", 10)):
numeric = numeric.replace(f" %dbg {name} ", f" %dbg {opcode} ")
spv = spirv_asm.assemble(asm)
self.assertEqual(spv, spirv_asm.assemble(numeric))
_validate(spv)
if SPIRV_AS:
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "debug.spvasm")
out = os.path.join(tmp, "debug.spv")
with open(src, "w", encoding="utf-8", newline="\n") as f:
f.write(asm)
subprocess.run([SPIRV_AS, "--target-env", "vulkan1.2", src, "-o", out],
check=True, capture_output=True)
with open(out, "rb") as f:
reference = f.read()
self.assertEqual(spv[:8] + spv[12:], reference[:8] + reference[12:])

def test_header_and_target_env_versions(self):
asm = (
"OpCapability Shader\n"
Expand Down
6 changes: 4 additions & 2 deletions tools/spirv_asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
sys.path.insert(0, _HERE)

from spirv_asm_tables import ( # noqa: E402
BIT_ENUMS, GLSL_STD_450, INSTRUCTIONS, NONSEMANTIC_DEBUGPRINTF, VALUE_ENUMS,
BIT_ENUMS, GLSL_STD_450, INSTRUCTIONS, NONSEMANTIC_DEBUGPRINTF,
NONSEMANTIC_SHADER_DEBUGINFO_100, VALUE_ENUMS,
)

SPIRV_MAGIC = 0x07230203
Expand All @@ -54,6 +55,7 @@
_ID_KINDS = frozenset(("IdRef", "IdResult", "IdResultType", "IdScope", "IdMemorySemantics"))
_ID_RE = re.compile(r"%[A-Za-z0-9_]+")
_EXT_SETS = {
"NonSemantic.Shader.DebugInfo.100": NONSEMANTIC_SHADER_DEBUGINFO_100,
"GLSL.std.450": GLSL_STD_450,
"NonSemantic.DebugPrintf": NONSEMANTIC_DEBUGPRINTF, # spirv-dis writes its instruction by name
}
Expand Down Expand Up @@ -638,7 +640,7 @@ def encode_operand(kind: str, toks: list[str], idx: int, lineno: int,

cur_ext_table = None
switch_type = None # (width, signed) of the OpSwitch selector's integer type
if opname == "OpExtInst" and arg_idx < len(toks):
if opname in ("OpExtInst", "OpExtInstWithForwardRefsKHR") and arg_idx < len(toks):
cur_ext_table = ext_imports.get(get_id(toks[arg_idx], lineno))
elif opname == "OpSwitch" and arg_idx < len(toks):
sel_id = get_id(toks[arg_idx], lineno)
Expand Down
48 changes: 48 additions & 0 deletions tools/spirv_asm_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,3 +1111,51 @@
NONSEMANTIC_DEBUGPRINTF = {
'DebugPrintf': 1,
}

# extinst.nonsemantic.shader.debuginfo.100.grammar.json (SPIRV-Headers).
NONSEMANTIC_SHADER_DEBUGINFO_100 = {
'DebugBuildIdentifier': 105,
'DebugCompilationUnit': 1,
'DebugDeclare': 28,
'DebugEntryPoint': 107,
'DebugExpression': 31,
'DebugFunction': 20,
'DebugFunctionDeclaration': 19,
'DebugFunctionDefinition': 101,
'DebugGlobalVariable': 18,
'DebugImportedEntity': 34,
'DebugInfoNone': 0,
'DebugInlinedAt': 25,
'DebugInlinedVariable': 27,
'DebugLexicalBlock': 21,
'DebugLexicalBlockDiscriminator': 22,
'DebugLine': 103,
'DebugLocalVariable': 26,
'DebugMacroDef': 32,
'DebugMacroUndef': 33,
'DebugNoLine': 104,
'DebugNoScope': 24,
'DebugOperation': 30,
'DebugScope': 23,
'DebugSource': 35,
'DebugSourceContinued': 102,
'DebugStoragePath': 106,
'DebugTypeArray': 5,
'DebugTypeBasic': 2,
'DebugTypeComposite': 10,
'DebugTypedef': 7,
'DebugTypeEnum': 9,
'DebugTypeFunction': 8,
'DebugTypeInheritance': 12,
'DebugTypeMatrix': 108,
'DebugTypeMember': 11,
'DebugTypePointer': 3,
'DebugTypePtrToMember': 13,
'DebugTypeQualifier': 4,
'DebugTypeTemplate': 14,
'DebugTypeTemplateParameter': 15,
'DebugTypeTemplateParameterPack': 17,
'DebugTypeTemplateTemplateParameter': 16,
'DebugTypeVector': 6,
'DebugValue': 29,
}