diff --git a/tests/test_spirv_asm.py b/tests/test_spirv_asm.py index cd15b0dba8..608f20ffef 100644 --- a/tests/test_spirv_asm.py +++ b/tests/test_spirv_asm.py @@ -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" diff --git a/tools/spirv_asm.py b/tools/spirv_asm.py index 49d0809e17..e32dd83aff 100644 --- a/tools/spirv_asm.py +++ b/tools/spirv_asm.py @@ -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 @@ -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 } @@ -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) diff --git a/tools/spirv_asm_tables.py b/tools/spirv_asm_tables.py index a58311c147..7714d734f4 100644 --- a/tools/spirv_asm_tables.py +++ b/tools/spirv_asm_tables.py @@ -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, +}