diff options
| author | ettehn <141814824+ettehn@users.noreply.github.com> | 2026-11-24 11:02:23 +0800 |
|---|---|---|
| committer | ettehn <141814824+ettehn@users.noreply.github.com> | 2026-11-24 11:28:30 +0800 |
| commit | f8a88299e1dfd09195e129a1d14970e2b532ef2b (patch) | |
| tree | 5938ae2bf4a64083bebccae8da44c44724448bf8 | |
| parent | d478a92d4d7eae1d7b512cb4553ddc4fbc300e85 (diff) | |
Support named SPIR-V shader debug instructions
Carry forward old Leonida PR #4744 for BUILD-SPIRV-DEBUGINFO.
| -rw-r--r-- | tests/test_spirv_asm.py | 43 | ||||
| -rw-r--r-- | tools/spirv_asm.py | 6 | ||||
| -rw-r--r-- | tools/spirv_asm_tables.py | 48 |
3 files changed, 95 insertions, 2 deletions
diff --git a/tests/test_spirv_asm.py b/tests/test_spirv_asm.py index cd15b0dba..608f20ffe 100644 --- a/tests/test_spirv_asm.py +++ b/tests/test_spirv_asm.py | |||
| @@ -168,6 +168,49 @@ class SpirvAsmUnitTests(unittest.TestCase): | |||
| 168 | with self.subTest(body=body): | 168 | with self.subTest(body=body): |
| 169 | self.assertTrue(spirv_asm.assemble("%uint = OpTypeInt 32 0\n" + body)) | 169 | self.assertTrue(spirv_asm.assemble("%uint = OpTypeInt 32 0\n" + body)) |
| 170 | 170 | ||
| 171 | def test_named_shader_debuginfo_with_forward_references(self): | ||
| 172 | # A function refers to its parent composite, which in turn lists the function as a member. | ||
| 173 | asm = """OpCapability Shader | ||
| 174 | OpExtension "SPV_KHR_non_semantic_info" | ||
| 175 | OpExtension "SPV_KHR_relaxed_extended_instruction" | ||
| 176 | %dbg = OpExtInstImport "NonSemantic.Shader.DebugInfo.100" | ||
| 177 | OpMemoryModel Logical GLSL450 | ||
| 178 | OpEntryPoint GLCompute %main "main" | ||
| 179 | OpExecutionMode %main LocalSize 1 1 1 | ||
| 180 | %name = OpString "demo" | ||
| 181 | %void = OpTypeVoid | ||
| 182 | %uint = OpTypeInt 32 0 | ||
| 183 | %zero = OpConstant %uint 0 | ||
| 184 | %fn = OpTypeFunction %void | ||
| 185 | %src = OpExtInst %void %dbg DebugSource %name %name | ||
| 186 | %unit = OpExtInst %void %dbg DebugCompilationUnit %zero %zero %src %zero | ||
| 187 | %type = OpExtInstWithForwardRefsKHR %void %dbg DebugTypeFunction %zero %class | ||
| 188 | %method = OpExtInstWithForwardRefsKHR %void %dbg DebugFunction %name %type %src %zero %zero %class %name %zero %zero | ||
| 189 | %class = OpExtInst %void %dbg DebugTypeComposite %name %zero %src %zero %zero %unit %name %zero %zero %method | ||
| 190 | %main = OpFunction %void None %fn | ||
| 191 | %entry = OpLabel | ||
| 192 | OpReturn | ||
| 193 | OpFunctionEnd | ||
| 194 | """ | ||
| 195 | numeric = asm | ||
| 196 | for name, opcode in (("DebugSource", 35), ("DebugCompilationUnit", 1), ("DebugTypeFunction", 8), | ||
| 197 | ("DebugFunction", 20), ("DebugTypeComposite", 10)): | ||
| 198 | numeric = numeric.replace(f" %dbg {name} ", f" %dbg {opcode} ") | ||
| 199 | spv = spirv_asm.assemble(asm) | ||
| 200 | self.assertEqual(spv, spirv_asm.assemble(numeric)) | ||
| 201 | _validate(spv) | ||
| 202 | if SPIRV_AS: | ||
| 203 | with tempfile.TemporaryDirectory() as tmp: | ||
| 204 | src = os.path.join(tmp, "debug.spvasm") | ||
| 205 | out = os.path.join(tmp, "debug.spv") | ||
| 206 | with open(src, "w", encoding="utf-8", newline="\n") as f: | ||
| 207 | f.write(asm) | ||
| 208 | subprocess.run([SPIRV_AS, "--target-env", "vulkan1.2", src, "-o", out], | ||
| 209 | check=True, capture_output=True) | ||
| 210 | with open(out, "rb") as f: | ||
| 211 | reference = f.read() | ||
| 212 | self.assertEqual(spv[:8] + spv[12:], reference[:8] + reference[12:]) | ||
| 213 | |||
| 171 | def test_header_and_target_env_versions(self): | 214 | def test_header_and_target_env_versions(self): |
| 172 | asm = ( | 215 | asm = ( |
| 173 | "OpCapability Shader\n" | 216 | "OpCapability Shader\n" |
diff --git a/tools/spirv_asm.py b/tools/spirv_asm.py index 49d0809e1..e32dd83af 100644 --- a/tools/spirv_asm.py +++ b/tools/spirv_asm.py | |||
| @@ -30,7 +30,8 @@ if _HERE not in sys.path: | |||
| 30 | sys.path.insert(0, _HERE) | 30 | sys.path.insert(0, _HERE) |
| 31 | 31 | ||
| 32 | from spirv_asm_tables import ( # noqa: E402 | 32 | from spirv_asm_tables import ( # noqa: E402 |
| 33 | BIT_ENUMS, GLSL_STD_450, INSTRUCTIONS, NONSEMANTIC_DEBUGPRINTF, VALUE_ENUMS, | 33 | BIT_ENUMS, GLSL_STD_450, INSTRUCTIONS, NONSEMANTIC_DEBUGPRINTF, |
| 34 | NONSEMANTIC_SHADER_DEBUGINFO_100, VALUE_ENUMS, | ||
| 34 | ) | 35 | ) |
| 35 | 36 | ||
| 36 | SPIRV_MAGIC = 0x07230203 | 37 | SPIRV_MAGIC = 0x07230203 |
| @@ -54,6 +55,7 @@ TARGET_ENV_VERSION = { | |||
| 54 | _ID_KINDS = frozenset(("IdRef", "IdResult", "IdResultType", "IdScope", "IdMemorySemantics")) | 55 | _ID_KINDS = frozenset(("IdRef", "IdResult", "IdResultType", "IdScope", "IdMemorySemantics")) |
| 55 | _ID_RE = re.compile(r"%[A-Za-z0-9_]+") | 56 | _ID_RE = re.compile(r"%[A-Za-z0-9_]+") |
| 56 | _EXT_SETS = { | 57 | _EXT_SETS = { |
| 58 | "NonSemantic.Shader.DebugInfo.100": NONSEMANTIC_SHADER_DEBUGINFO_100, | ||
| 57 | "GLSL.std.450": GLSL_STD_450, | 59 | "GLSL.std.450": GLSL_STD_450, |
| 58 | "NonSemantic.DebugPrintf": NONSEMANTIC_DEBUGPRINTF, # spirv-dis writes its instruction by name | 60 | "NonSemantic.DebugPrintf": NONSEMANTIC_DEBUGPRINTF, # spirv-dis writes its instruction by name |
| 59 | } | 61 | } |
| @@ -638,7 +640,7 @@ def _assemble(instructions, version: int, track_switch_types: bool) -> bytes: | |||
| 638 | 640 | ||
| 639 | cur_ext_table = None | 641 | cur_ext_table = None |
| 640 | switch_type = None # (width, signed) of the OpSwitch selector's integer type | 642 | switch_type = None # (width, signed) of the OpSwitch selector's integer type |
| 641 | if opname == "OpExtInst" and arg_idx < len(toks): | 643 | if opname in ("OpExtInst", "OpExtInstWithForwardRefsKHR") and arg_idx < len(toks): |
| 642 | cur_ext_table = ext_imports.get(get_id(toks[arg_idx], lineno)) | 644 | cur_ext_table = ext_imports.get(get_id(toks[arg_idx], lineno)) |
| 643 | elif opname == "OpSwitch" and arg_idx < len(toks): | 645 | elif opname == "OpSwitch" and arg_idx < len(toks): |
| 644 | sel_id = get_id(toks[arg_idx], lineno) | 646 | sel_id = get_id(toks[arg_idx], lineno) |
diff --git a/tools/spirv_asm_tables.py b/tools/spirv_asm_tables.py index a58311c14..7714d734f 100644 --- a/tools/spirv_asm_tables.py +++ b/tools/spirv_asm_tables.py | |||
| @@ -1111,3 +1111,51 @@ GLSL_STD_450 = { | |||
| 1111 | NONSEMANTIC_DEBUGPRINTF = { | 1111 | NONSEMANTIC_DEBUGPRINTF = { |
| 1112 | 'DebugPrintf': 1, | 1112 | 'DebugPrintf': 1, |
| 1113 | } | 1113 | } |
| 1114 | |||
| 1115 | # extinst.nonsemantic.shader.debuginfo.100.grammar.json (SPIRV-Headers). | ||
| 1116 | NONSEMANTIC_SHADER_DEBUGINFO_100 = { | ||
| 1117 | 'DebugBuildIdentifier': 105, | ||
| 1118 | 'DebugCompilationUnit': 1, | ||
| 1119 | 'DebugDeclare': 28, | ||
| 1120 | 'DebugEntryPoint': 107, | ||
| 1121 | 'DebugExpression': 31, | ||
| 1122 | 'DebugFunction': 20, | ||
| 1123 | 'DebugFunctionDeclaration': 19, | ||
| 1124 | 'DebugFunctionDefinition': 101, | ||
| 1125 | 'DebugGlobalVariable': 18, | ||
| 1126 | 'DebugImportedEntity': 34, | ||
| 1127 | 'DebugInfoNone': 0, | ||
| 1128 | 'DebugInlinedAt': 25, | ||
| 1129 | 'DebugInlinedVariable': 27, | ||
| 1130 | 'DebugLexicalBlock': 21, | ||
| 1131 | 'DebugLexicalBlockDiscriminator': 22, | ||
| 1132 | 'DebugLine': 103, | ||
| 1133 | 'DebugLocalVariable': 26, | ||
| 1134 | 'DebugMacroDef': 32, | ||
| 1135 | 'DebugMacroUndef': 33, | ||
| 1136 | 'DebugNoLine': 104, | ||
| 1137 | 'DebugNoScope': 24, | ||
| 1138 | 'DebugOperation': 30, | ||
| 1139 | 'DebugScope': 23, | ||
| 1140 | 'DebugSource': 35, | ||
| 1141 | 'DebugSourceContinued': 102, | ||
| 1142 | 'DebugStoragePath': 106, | ||
| 1143 | 'DebugTypeArray': 5, | ||
| 1144 | 'DebugTypeBasic': 2, | ||
| 1145 | 'DebugTypeComposite': 10, | ||
| 1146 | 'DebugTypedef': 7, | ||
| 1147 | 'DebugTypeEnum': 9, | ||
| 1148 | 'DebugTypeFunction': 8, | ||
| 1149 | 'DebugTypeInheritance': 12, | ||
| 1150 | 'DebugTypeMatrix': 108, | ||
| 1151 | 'DebugTypeMember': 11, | ||
| 1152 | 'DebugTypePointer': 3, | ||
| 1153 | 'DebugTypePtrToMember': 13, | ||
| 1154 | 'DebugTypeQualifier': 4, | ||
| 1155 | 'DebugTypeTemplate': 14, | ||
| 1156 | 'DebugTypeTemplateParameter': 15, | ||
| 1157 | 'DebugTypeTemplateParameterPack': 17, | ||
| 1158 | 'DebugTypeTemplateTemplateParameter': 16, | ||
| 1159 | 'DebugTypeVector': 6, | ||
| 1160 | 'DebugValue': 29, | ||
| 1161 | } | ||