1010from high_parser .pisa_operations import PIsaOp , Comment
1111
1212
13+ class PIsaOpGroup :
14+ """A group of PIsaOp instructions with reorderable flag.
15+
16+ Attributes:
17+ pisa_list: List of PIsaOp instructions
18+ is_reorderable: Boolean indicating if the instructions can be reordered
19+ """
20+
21+ pisa_list : list [PIsaOp ]
22+ is_reorderable : bool
23+
24+ def __init__ (self , pisa_list : list [PIsaOp ], is_reorderable : bool = False ):
25+ """Initialize PIsaOpGroup.
26+
27+ Args:
28+ pisa_list: List of PIsaOp instructions
29+ is_reorderable: Boolean indicating if instructions can be reordered
30+ """
31+ self .pisa_list = pisa_list
32+ self .is_reorderable = is_reorderable
33+
34+ def __len__ (self ) -> int :
35+ """Return the number of instructions in the group."""
36+ return len (self .pisa_list )
37+
38+ def __iter__ (self ):
39+ """Allow iteration over the PIsaOp instructions."""
40+ return iter (self .pisa_list )
41+
42+
1343def remove_comments (pisa_list : list [PIsaOp ]) -> list [PIsaOp ]:
1444 """Remove comments from a list of PIsaOp instructions.
1545
@@ -22,40 +52,49 @@ def remove_comments(pisa_list: list[PIsaOp]) -> list[PIsaOp]:
2252 return [pisa for pisa in pisa_list if not isinstance (pisa , Comment )]
2353
2454
25- def split_by_reorderable (pisa_list : list [PIsaOp ]) -> tuple [ list [PIsaOp ], list [ PIsaOp ] ]:
55+ def split_by_reorderable (pisa_list : list [PIsaOp ]) -> list [PIsaOpGroup ]:
2656 """Split a list of PIsaOp instructions into reorderable and non-reorderable groups.
2757
2858 Args:
2959 pisa_list: List of PIsaOp instructions
3060
3161 Returns:
32- Tuple containing two lists:
33- - reorderable: Instructions that can be reordered
34- - non_reorderable: Instructions that cannot be reordered
62+ List of PIsaOpGroup objects containing grouped instructions with their reorderable status
3563 """
36-
37- reorderable = []
38- non_reorderable = []
39- is_reorderable = False
64+ groups = []
65+ current_group = PIsaOpGroup ([], is_reorderable = False )
66+ no_reoderable_group = True
4067
4168 for pisa in pisa_list :
4269 # if the pisa is a comment and it contains <reorderable> tag, treat the following pisa as reorderable until a </reorderable> tag is found.
4370 if isinstance (pisa , Comment ):
4471 if "<reorderable>" in pisa .line :
45- is_reorderable = True
72+ # If current group has instructions, append it to groups first
73+ if current_group .pisa_list :
74+ groups .append (current_group )
75+ # Create a new reorderable group
76+ current_group = PIsaOpGroup ([], is_reorderable = True )
77+ no_reoderable_group = False
4678 elif "</reorderable>" in pisa .line :
47- is_reorderable = False
48-
49- if is_reorderable :
50- reorderable .append (pisa )
79+ # End reorderable section, append current group to groups
80+ if current_group .pisa_list :
81+ groups .append (current_group )
82+ # Create a new non-reorderable group
83+ current_group = PIsaOpGroup ([], is_reorderable = False )
5184 else :
52- non_reorderable .append (pisa )
85+ # Add non-comment instruction to current group
86+ current_group .pisa_list .append (pisa )
87+
88+ # Add any remaining instructions in current_group
89+ if current_group .pisa_list :
90+ groups .append (current_group )
91+
92+ # If there are no reorderable groups, set reorderable to True for the entire groups
93+ if no_reoderable_group :
94+ for group in groups :
95+ group .is_reorderable = True
5396
54- # if reoderable is empty, return non_reorderable as reorderable
55- if not reorderable :
56- reorderable = non_reorderable
57- non_reorderable = []
58- return remove_comments (reorderable ), remove_comments (non_reorderable )
97+ return groups
5998
6099
61100def loop_interchange (
0 commit comments