Skip to content

Commit 9b1a7e7

Browse files
authored
grammar tables: emit good error when missing an operand type (KhronosGroup#6145)
If a grammar change introduces a new optional operand kind, then currently the error message is very unfriendly. It's a Python dictionary key error, with a call stack. With this change, we get a single friendly error line like this, but all on one line: error: unknown operand type SPV_OPERAND_TYPE_TENSOR_OPERANDS, from JSON grammar operand 'TensorOperands': consider updating spv_operand_type_t in spirv-tools/libspirv.h
1 parent eac930c commit 9b1a7e7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

utils/ggt.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
from Table.IndexRange import IndexRange
3131
from Table.Operand import Operand
3232

33+
class GrammarError(Exception):
34+
pass
35+
3336
# Extensions to recognize, but which don't necessarily come from the SPIR-V
3437
# core or KHR grammar files. Get this list from the SPIR-V registry web page.
3538
# NOTE: Only put things on this list if it is not in those grammar files.
@@ -429,9 +432,16 @@ def ShouldEmit(operand_kind_json: Dict[str,any]):
429432
kind_key,
430433
str(operands_by_value_by_kind[kind_key])))
431434
for kind in self.operand_kinds_needing_optional_variant:
432-
parts.append(" case {}: return {};".format(
433-
ctype(kind, '?'),
434-
str(operands_by_value_by_kind[ctype(kind,'')])))
435+
non_optional_kind = ctype(kind,'')
436+
if non_optional_kind in operands_by_value_by_kind:
437+
parts.append(" case {}: return {};".format(
438+
ctype(kind, '?'),
439+
str(operands_by_value_by_kind[ctype(kind,'')])))
440+
else:
441+
raise GrammarError(
442+
"error: unknown operand type {}, from JSON grammar operand '{}':".format(non_optional_kind, kind) +
443+
" consider updating spv_operand_type_t in spirv-tools/libspirv.h")
444+
435445
parts.append(" default: break;");
436446
parts.append(" }\n return IR(0,0);\n}\n")
437447
self.body_decls.extend(parts)
@@ -955,4 +965,8 @@ def main():
955965

956966

957967
if __name__ == '__main__':
958-
main()
968+
try:
969+
main()
970+
except GrammarError as ge:
971+
print(ge)
972+
sys.exit(1)

0 commit comments

Comments
 (0)