diff --git a/internal/engine/wazevo/ssa/basic_block_sort.go b/internal/engine/wazevo/ssa/basic_block_sort.go index e21cc8fb99..e1471edc37 100644 --- a/internal/engine/wazevo/ssa/basic_block_sort.go +++ b/internal/engine/wazevo/ssa/basic_block_sort.go @@ -8,16 +8,27 @@ import ( func sortBlocks(blocks []*basicBlock) { slices.SortFunc(blocks, func(i, j *basicBlock) int { - if j.ReturnBlock() { + jIsReturn := j.ReturnBlock() + iIsReturn := i.ReturnBlock() + if iIsReturn && jIsReturn { + return 0 + } + if jIsReturn { return 1 } - if i.ReturnBlock() { + if iIsReturn { return -1 } iRoot, jRoot := i.rootInstr, j.rootInstr - if iRoot == nil || jRoot == nil { // For testing. + if iRoot == nil && jRoot == nil { // For testing. + return 0 + } + if jRoot == nil { return 1 } + if iRoot == nil { + return -1 + } return i.rootInstr.id - j.rootInstr.id }) }