10
10
from high_parser .pisa_operations import PIsaOp , Comment
11
11
12
12
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
+
13
43
def remove_comments (pisa_list : list [PIsaOp ]) -> list [PIsaOp ]:
14
44
"""Remove comments from a list of PIsaOp instructions.
15
45
@@ -22,40 +52,49 @@ def remove_comments(pisa_list: list[PIsaOp]) -> list[PIsaOp]:
22
52
return [pisa for pisa in pisa_list if not isinstance (pisa , Comment )]
23
53
24
54
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 ]:
26
56
"""Split a list of PIsaOp instructions into reorderable and non-reorderable groups.
27
57
28
58
Args:
29
59
pisa_list: List of PIsaOp instructions
30
60
31
61
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
35
63
"""
36
-
37
- reorderable = []
38
- non_reorderable = []
39
- is_reorderable = False
64
+ groups = []
65
+ current_group = PIsaOpGroup ([], is_reorderable = False )
66
+ no_reoderable_group = True
40
67
41
68
for pisa in pisa_list :
42
69
# if the pisa is a comment and it contains <reorderable> tag, treat the following pisa as reorderable until a </reorderable> tag is found.
43
70
if isinstance (pisa , Comment ):
44
71
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
46
78
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 )
51
84
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
53
96
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
59
98
60
99
61
100
def loop_interchange (
0 commit comments