MCPcopy Create free account
hub / github.com/pytorch/executorch / generate_cpp_loader_h

Function generate_cpp_loader_h

backends/mlx/serialization/generate.py:956–996  ·  view source on GitHub ↗

Generate MLXLoader.h from parsed FBS using template.

(schema: FBSSchema)

Source from the content-addressed store, hash-verified

954
955
956def generate_cpp_loader_h(schema: FBSSchema) -> str:
957 """Generate MLXLoader.h from parsed FBS using template."""
958 op_nodes = schema.get_op_nodes()
959
960 struct_lines = []
961 for table in op_nodes:
962 struct_lines.append(f"struct {table.name} {{")
963 if not table.fields:
964 struct_lines.append("};")
965 else:
966 for fld in table.fields:
967 if fld.name.endswith("_is_set"):
968 continue
969 cpp_type = _fbs_type_to_cpp(fld.type_str, fld.required, table, fld)
970 struct_lines.append(f" {cpp_type} {fld.name};")
971 struct_lines.append("};")
972 struct_lines.append("")
973
974 enum_lines = []
975 for table in op_nodes:
976 enum_lines.append(f" {_table_name_to_opcode(table.name)},")
977
978 name_lines = []
979 for table in op_nodes:
980 op_code = _table_name_to_opcode(table.name)
981 name_lines.append(f" case OpCode::{op_code}:")
982 name_lines.append(f' return "{op_code}";')
983
984 variant_lines = []
985 for i, table in enumerate(op_nodes):
986 comma = "," if i < len(op_nodes) - 1 else ""
987 variant_lines.append(f" {table.name}{comma}")
988
989 # Read template and fill placeholders
990 header = "\n".join(_file_header("//")) + "\n//\n"
991 tmpl = LOADER_H_TMPL.read_text()
992 result = tmpl.replace("{{OP_NODE_STRUCTS}}", "\n".join(struct_lines))
993 result = result.replace("{{OPCODE_ENUM_VALUES}}", "\n".join(enum_lines))
994 result = result.replace("{{OP_NAME_CASES}}", "\n".join(name_lines))
995 result = result.replace("{{NODE_VARIANT_TYPES}}", "\n".join(variant_lines))
996 return header + result
997
998
999def _fbs_type_to_cpp(

Callers

nothing calls this directly

Calls 5

_fbs_type_to_cppFunction · 0.85
_table_name_to_opcodeFunction · 0.85
_file_headerFunction · 0.85
get_op_nodesMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected