@@ -602,6 +602,9 @@ def write_body(self, out: Formatter) -> None:
602602 out .assign (var , oeffect )
603603
604604
605+ MacroParts = list [Component | parser .CacheEffect ]
606+
607+
605608@dataclasses .dataclass
606609class MacroInstruction :
607610 """A macro instruction."""
@@ -613,7 +616,7 @@ class MacroInstruction:
613616 instr_fmt : str
614617 instr_flags : InstructionFlags
615618 macro : parser .Macro
616- parts : list [ Component | parser . CacheEffect ]
619+ parts : MacroParts
617620 cache_offset : int
618621 predicted : bool = False
619622
@@ -906,7 +909,7 @@ def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:
906909 components = self .check_macro_components (macro )
907910 stack , initial_sp = self .stack_analysis (components )
908911 sp = initial_sp
909- parts : list [ Component | parser . CacheEffect ] = []
912+ parts : MacroParts = []
910913 flags = InstructionFlags .newEmpty ()
911914 offset = 0
912915 for component in components :
@@ -1253,17 +1256,14 @@ def write_metadata(self) -> None:
12531256 pass
12541257 case parser .InstDef (name = name ):
12551258 instr = self .instrs [name ]
1256- # Since an 'op' is not a bytecode, it has no expansion
1257- if instr .kind != "op" and instr .is_viable_uop ():
1258- # Double check there aren't any used cache effects.
1259- # If this fails, see write_macro_expansions().
1260- assert not instr .active_caches , (instr .name , instr .cache_effects )
1261- self .out .emit (
1262- f"[{ name } ] = "
1263- f"{{ .nuops = 1, .uops = {{ {{ { name } , 0, 0 }} }} }},"
1264- )
1259+ # Since an 'op' is not a bytecode, it has no expansion; but 'inst' is
1260+ if instr .kind == "inst" and instr .is_viable_uop ():
1261+ # Construct a dummy Component -- input/output mappings are not used
1262+ part = Component (instr , [], [], instr .active_caches )
1263+ self .write_macro_expansions (instr .name , [part ])
12651264 case parser .Macro ():
1266- self .write_macro_expansions (self .macro_instrs [thing .name ])
1265+ mac = self .macro_instrs [thing .name ]
1266+ self .write_macro_expansions (mac .name , mac .parts )
12671267 case parser .Pseudo ():
12681268 pass
12691269 case _:
@@ -1328,29 +1328,29 @@ def add(name: str) -> None:
13281328 if instr .kind == "op" and instr .is_viable_uop ():
13291329 add (instr .name )
13301330
1331- def write_macro_expansions (self , mac : MacroInstruction ) -> None :
1331+ def write_macro_expansions (self , name : str , parts : MacroParts ) -> None :
13321332 """Write the macro expansions for a macro-instruction."""
13331333 # TODO: Refactor to share code with write_cody(), is_viaible_uop(), etc.
13341334 offset = 0 # Cache effect offset
13351335 expansions : list [tuple [str , int , int ]] = [] # [(name, size, offset), ...]
1336- for part in mac . parts :
1336+ for part in parts :
13371337 if isinstance (part , Component ):
13381338 # All component instructions must be viable uops
13391339 if not part .instr .is_viable_uop ():
1340- print (f"NOTE: Part { part .instr .name } of { mac . name } is not a viable uop" )
1340+ print (f"NOTE: Part { part .instr .name } of { name } is not a viable uop" )
13411341 return
13421342 if part .instr .instr_flags .HAS_ARG_FLAG or not part .active_caches :
13431343 size , offset = 0 , 0
13441344 else :
13451345 # If this assert triggers, is_viable_uops() lied
1346- assert len (part .active_caches ) == 1 , (mac . name , part .instr .name )
1346+ assert len (part .active_caches ) == 1 , (name , part .instr .name )
13471347 cache = part .active_caches [0 ]
13481348 size , offset = cache .effect .size , cache .offset
13491349 expansions .append ((part .instr .name , size , offset ))
1350- assert len (expansions ) > 0 , f"Macro { mac . name } has empty expansion?!"
1350+ assert len (expansions ) > 0 , f"Macro { name } has empty expansion?!"
13511351 pieces = [f"{{ { name } , { size } , { offset } }}" for name , size , offset in expansions ]
13521352 self .out .emit (
1353- f"[{ mac . name } ] = "
1353+ f"[{ name } ] = "
13541354 f"{{ .nuops = { len (expansions )} , .uops = {{ { ', ' .join (pieces )} }} }},"
13551355 )
13561356
0 commit comments