|
6 | 6 | # The algorithm walks the IR recursively while tracking the definition depth |
7 | 7 | # of each value. An operation whose data dependencies all resolve to depths |
8 | 8 | # *less than* its containing loop can be hoisted above that loop. A stack of |
9 | | -# SSAMaps collects operations at their target depth; at the end of each block, |
10 | | -# the original body is replaced with the (filtered) rebuilt map. |
| 9 | +# instruction lists collects operations at their target depth; at the end of |
| 10 | +# each block, the original body is rebuilt from the filtered list. |
11 | 11 |
|
12 | 12 | # Whether a block can be moved, based on the operations it contains. |
13 | 13 | @enum BlockMobility begin |
@@ -35,7 +35,7 @@ function update!(di::DependencyInfo, dep_depth::Int, cur_depth::Int) |
35 | 35 | end |
36 | 36 |
|
37 | 37 | struct StackItem |
38 | | - new_body::SSAMap |
| 38 | + entries::Vector{Tuple{Int,Any,Any}} # (ssa_idx, stmt, typ) triples |
39 | 39 | is_loop_body::Bool |
40 | 40 | end |
41 | 41 |
|
|
57 | 57 | function _hoist!(block::Block, stack::Vector{StackItem}, def_depth::Dict{Any,Int}, |
58 | 58 | is_loop_body::Bool) |
59 | 59 | depth = length(stack) |
60 | | - new_body = SSAMap() |
61 | | - push!(stack, StackItem(new_body, is_loop_body)) |
| 60 | + push!(stack, StackItem(Tuple{Int,Any,Any}[], is_loop_body)) |
62 | 61 |
|
63 | 62 | mobility = CAN_MOVE |
64 | 63 | min_depth = 0 |
@@ -148,20 +147,25 @@ function _hoist!(block::Block, stack::Vector{StackItem}, def_depth::Dict{Any,Int |
148 | 147 | end |
149 | 148 |
|
150 | 149 | # Place at target depth |
151 | | - push!(stack[target_depth + 1].new_body, (inst.ssa_idx, s, inst.typ)) |
| 150 | + push!(stack[target_depth + 1].entries, (inst.ssa_idx, s, inst.typ)) |
152 | 151 |
|
153 | 152 | # Record definition depth AFTER hoisting (enables cascading hoists) |
154 | 153 | def_depth[SSAValue(inst.ssa_idx)] = target_depth |
155 | 154 | end |
156 | 155 |
|
157 | | - # Handle terminator operands for min_depth computation |
| 156 | + # Handle terminator for mobility |
158 | 157 | term = block.terminator |
159 | 158 | if term isa ContinueOp || term isa BreakOp |
160 | 159 | mobility = min(mobility, CAN_MOVE_WITH_LOOP) |
161 | 160 | end |
162 | 161 |
|
163 | | - pop!(stack) |
164 | | - block.body = new_body |
| 162 | + # Rebuild block body from collected entries |
| 163 | + entries = pop!(stack).entries |
| 164 | + empty!(block) |
| 165 | + for (idx, s, typ) in entries |
| 166 | + push!(block, idx, s, typ) |
| 167 | + end |
| 168 | + |
165 | 169 | return BlockResult(mobility, min_depth) |
166 | 170 | end |
167 | 171 |
|
|
0 commit comments