"""gpu/vk/spirv_operands.h from SPIR-V's core grammar (SPIRV-Headers, spirv.core.grammar.json): for each opcode, its operands as one letter each, for code that renames ids in SPIR-V it did not write (gpu/vk/fsr4_tune.h). python -I scripts/gen_spirv_operands.py gpu/vk/spirv_operands.h Letters: T result type, R result, i an id (IdRef, IdScope, IdMemorySemantics), l a literal word, s a literal string, c a literal number of the instruction's type (the rest of the words), P / Q / q pairs (id id, literal id, id literal), M MemoryAccess, G ImageOperands, L LoopControl (masks whose parameters are read by their bits), e an enum of one word with no parameters, X anything else (an instruction with one is not renamed). After a letter: * repeated to the end, ? optional. """ import json import sys g = json.load(open(sys.argv[1], encoding="utf-8")) kinds = {k["kind"]: k for k in g["operand_kinds"]} def letter(kind): fixed = {"IdResultType": "T", "IdResult": "R", "IdRef": "i", "IdScope": "i", "IdMemorySemantics": "i", "LiteralInteger": "l", "LiteralExtInstInteger": "l", "LiteralSpecConstantOpInteger": "l", "LiteralFloat": "l", "LiteralString": "s", "LiteralContextDependentNumber": "c", "PairIdRefIdRef": "P", "PairLiteralIntegerIdRef": "Q", "PairIdRefLiteralInteger": "q", "MemoryAccess": "M", "ImageOperands": "G", "LoopControl": "L"} if kind in fixed: return fixed[kind] k = kinds.get(kind) if k and k.get("category") in ("ValueEnum", "BitEnum"): if not any(e.get("parameters") for e in k.get("enumerants", [])): return "e" return "X" rows = {} for ins in g["instructions"]: s = "" for o in ins.get("operands", []): s += letter(o["kind"]) + {"*": "*", "?": "?"}.get(o.get("quantifier", ""), "") rows[ins["opcode"]] = (ins["opname"], s) with open(sys.argv[2], "w", encoding="utf-8", newline="\n") as f: f.write("// Generated by scripts/gen_spirv_operands.py from SPIR-V's core grammar (SPIRV-Headers %s.%s, revision %s): do not edit.\n" % (g.get("major_version"), g.get("minor_version"), g.get("revision"))) f.write("// Each opcode's operands, one letter each (the script says what they mean); nullptr: an opcode not in the grammar.\n") f.write("#pragma once\n\n#include \n\nnamespace spirv_operands {\n\n") f.write("inline const char* of(uint32_t op) {\n switch (op) {\n") for op in sorted(rows): name, s = rows[op] f.write(f' case {op}: return "{s}"; // {name}\n') f.write(" default: return nullptr;\n }\n}\n\n} // namespace spirv_operands\n") print(len(rows), "opcodes")