1212
1313# Indicates whether a block could in theory be moved, based on the operations
1414# it contains (side effects, jumps). Does not consider data dependencies.
15- @enum _BlockMobility :: Int8 begin
15+ @enum BlockMobility :: Int8 begin
1616 # The block (or any ancestor) cannot be moved due to side effects.
17- _IMMOVABLE = 0
17+ IMMOVABLE = 0
1818 # The block itself can't be hoisted alone, but its containing loop can.
1919 # Happens when the block contains Continue or Break.
20- _CAN_MOVE_WITH_LOOP = 1
20+ CAN_MOVE_WITH_LOOP = 1
2121 # The block can move (subject to data dependencies).
22- _CAN_MOVE = 2
22+ CAN_MOVE = 2
2323end
2424
25- struct _BlockResult
26- mobility:: _BlockMobility
25+ struct BlockResult
26+ mobility:: BlockMobility
2727 min_depth:: Int # deepest outside dependency of any hoisted-out op
2828end
2929
3030# Helper for accumulating data dependency information per operation.
31- mutable struct _DepInfo
31+ mutable struct DepInfo
3232 must_stay:: Bool
3333 max_outside_depth:: Int
3434end
3535
36- function _update_dep ! (di:: _DepInfo , dep_depth:: Int , cur_depth:: Int )
36+ function update_dep ! (di:: DepInfo , dep_depth:: Int , cur_depth:: Int )
3737 if dep_depth >= cur_depth
3838 di. must_stay = true
3939 else
@@ -42,31 +42,31 @@ function _update_dep!(di::_DepInfo, dep_depth::Int, cur_depth::Int)
4242end
4343
4444# Update dependency info from an SSA value or literal.
45- function _check_val ! (di:: _DepInfo , val, def_depth:: Dict{Any,Int} , cur_depth:: Int )
45+ function check_val ! (di:: DepInfo , val, def_depth:: Dict{Any,Int} , cur_depth:: Int )
4646 d = get (def_depth, val, nothing )
4747 d === nothing && return # constants/literals always available
48- _update_dep ! (di, d, cur_depth)
48+ update_dep ! (di, d, cur_depth)
4949end
5050
5151# Extract all SSA dependencies from a statement.
52- function _check_stmt_deps ! (di:: _DepInfo , @nospecialize (s), def_depth:: Dict{Any,Int} ,
53- cur_depth:: Int )
52+ function check_stmt_deps ! (di:: DepInfo , @nospecialize (s), def_depth:: Dict{Any,Int} ,
53+ cur_depth:: Int )
5454 if s isa Expr
5555 start = s. head === :invoke ? 3 : 2
5656 for i in start: length (s. args)
57- _check_val ! (di, s. args[i], def_depth, cur_depth)
57+ check_val ! (di, s. args[i], def_depth, cur_depth)
5858 end
5959 elseif s isa JoinTokensNode
6060 for tok in s. tokens
61- _check_val ! (di, tok, def_depth, cur_depth)
61+ check_val ! (di, tok, def_depth, cur_depth)
6262 end
6363 elseif s isa TokenResultNode
64- _check_val ! (di, SSAValue (s. mem_op_ssa), def_depth, cur_depth)
64+ check_val ! (di, SSAValue (s. mem_op_ssa), def_depth, cur_depth)
6565 end
6666 # MakeTokenNode, PiNode, GlobalRef, literals: no SSA deps
6767end
6868
69- struct _StackItem
69+ struct StackItem
7070 entries:: Vector{Tuple{Int,Any,Any}} # (ssa_idx, stmt, type)
7171 is_loop_body:: Bool
7272end
@@ -81,54 +81,54 @@ function licm_pass!(sci::StructuredIRCode)
8181 for i in 1 : length (sci. argtypes)
8282 def_depth[Argument (i)] = 0
8383 end
84- _hoist ! (sci. entry, _StackItem [], def_depth, false )
84+ hoist ! (sci. entry, StackItem [], def_depth, false )
8585 return
8686end
8787
88- function _hoist ! (block:: Block , stack:: Vector{_StackItem } , def_depth:: Dict{Any,Int} ,
88+ function hoist ! (block:: Block , stack:: Vector{StackItem } , def_depth:: Dict{Any,Int} ,
8989 is_loop_body:: Bool )
9090 depth = length (stack)
91- push! (stack, _StackItem (Tuple{Int,Any,Any}[], is_loop_body))
91+ push! (stack, StackItem (Tuple{Int,Any,Any}[], is_loop_body))
9292
9393 for ba in block. args
9494 def_depth[ba] = depth
9595 end
9696
97- mobility = _CAN_MOVE
97+ mobility = CAN_MOVE
9898 min_depth = 0
9999
100100 for inst in instructions (block)
101101 s = stmt (inst)
102- di = _DepInfo (! is_loop_body, 0 )
102+ di = DepInfo (! is_loop_body, 0 )
103103
104104 if s isa ForOp
105105 def_depth[s. iv_arg] = depth + 1
106106 for ba in s. body. args
107107 def_depth[ba] = depth + 1
108108 end
109- body_res = _hoist ! (s. body, stack, def_depth, true )
110- if body_res. mobility == _IMMOVABLE
111- mobility = _IMMOVABLE
109+ body_res = hoist ! (s. body, stack, def_depth, true )
110+ if body_res. mobility == IMMOVABLE
111+ mobility = IMMOVABLE
112112 di. must_stay = true
113113 end
114114 for v in s. init_values
115- _check_val ! (di, v, def_depth, depth)
115+ check_val ! (di, v, def_depth, depth)
116116 end
117- _update_dep ! (di, body_res. min_depth, depth)
117+ update_dep ! (di, body_res. min_depth, depth)
118118
119119 elseif s isa LoopOp
120120 for ba in s. body. args
121121 def_depth[ba] = depth + 1
122122 end
123- body_res = _hoist ! (s. body, stack, def_depth, true )
124- if body_res. mobility == _IMMOVABLE
125- mobility = _IMMOVABLE
123+ body_res = hoist ! (s. body, stack, def_depth, true )
124+ if body_res. mobility == IMMOVABLE
125+ mobility = IMMOVABLE
126126 di. must_stay = true
127127 end
128128 for v in s. init_values
129- _check_val ! (di, v, def_depth, depth)
129+ check_val ! (di, v, def_depth, depth)
130130 end
131- _update_dep ! (di, body_res. min_depth, depth)
131+ update_dep ! (di, body_res. min_depth, depth)
132132
133133 elseif s isa WhileOp
134134 for ba in s. before. args
@@ -137,54 +137,54 @@ function _hoist!(block::Block, stack::Vector{_StackItem}, def_depth::Dict{Any,In
137137 for ba in s. after. args
138138 def_depth[ba] = depth + 1
139139 end
140- before_res = _hoist ! (s. before, stack, def_depth, true )
141- after_res = _hoist ! (s. after, stack, def_depth, true )
142- if min (before_res. mobility, after_res. mobility) == _IMMOVABLE
143- mobility = _IMMOVABLE
140+ before_res = hoist ! (s. before, stack, def_depth, true )
141+ after_res = hoist ! (s. after, stack, def_depth, true )
142+ if min (before_res. mobility, after_res. mobility) == IMMOVABLE
143+ mobility = IMMOVABLE
144144 di. must_stay = true
145145 end
146146 for v in s. init_values
147- _check_val ! (di, v, def_depth, depth)
147+ check_val ! (di, v, def_depth, depth)
148148 end
149- _update_dep ! (di, before_res. min_depth, depth)
150- _update_dep ! (di, after_res. min_depth, depth)
149+ update_dep ! (di, before_res. min_depth, depth)
150+ update_dep ! (di, after_res. min_depth, depth)
151151
152152 elseif s isa IfOp
153- _check_val ! (di, s. condition, def_depth, depth)
153+ check_val ! (di, s. condition, def_depth, depth)
154154 for region in (s. then_region, s. else_region)
155- branch_res = _hoist ! (region, stack, def_depth, false )
156- _update_dep ! (di, branch_res. min_depth, depth)
157- if branch_res. mobility != _CAN_MOVE
155+ branch_res = hoist ! (region, stack, def_depth, false )
156+ update_dep ! (di, branch_res. min_depth, depth)
157+ if branch_res. mobility != CAN_MOVE
158158 mobility = min (mobility, branch_res. mobility)
159159 di. must_stay = true
160160 end
161161 end
162162
163- elseif _is_store (block, s)
164- mobility = _IMMOVABLE
163+ elseif is_store (block, s)
164+ mobility = IMMOVABLE
165165 di. must_stay = true
166166
167167 elseif s isa ContinueOp || s isa BreakOp
168- mobility = min (mobility, _CAN_MOVE_WITH_LOOP )
168+ mobility = min (mobility, CAN_MOVE_WITH_LOOP )
169169 di. must_stay = true
170170
171171 elseif s isa YieldOp || s isa ConditionOp || s isa ReturnNode
172172 di. must_stay = true
173173 # Track deps for YieldOp/ConditionOp so min_depth is correct
174174 if s isa YieldOp
175175 for v in s. values
176- _check_val ! (di, v, def_depth, depth)
176+ check_val ! (di, v, def_depth, depth)
177177 end
178178 elseif s isa ConditionOp
179- _check_val ! (di, s. condition, def_depth, depth)
179+ check_val ! (di, s. condition, def_depth, depth)
180180 for v in s. args
181- _check_val ! (di, v, def_depth, depth)
181+ check_val ! (di, v, def_depth, depth)
182182 end
183183 end
184184
185185 else
186186 # Movable operation: loads, arithmetic, make_partition_view, etc.
187- _check_stmt_deps ! (di, s, def_depth, depth)
187+ check_stmt_deps ! (di, s, def_depth, depth)
188188 end
189189
190190 # Determine target depth
@@ -210,11 +210,11 @@ function _hoist!(block::Block, stack::Vector{_StackItem}, def_depth::Dict{Any,In
210210 push! (block, idx, s, typ)
211211 end
212212
213- return _BlockResult (mobility, min_depth)
213+ return BlockResult (mobility, min_depth)
214214end
215215
216216# Check if a statement is a store/atomic (side-effecting memory write).
217- function _is_store (block:: Block , @nospecialize (s))
217+ function is_store (block:: Block , @nospecialize (s))
218218 call = resolve_call (block, s)
219219 call === nothing && return false
220220 resolved_func, _ = call
0 commit comments