From 0dc51ae4251edfe2a852e6fd36f7fe7f529bf0a1 Mon Sep 17 00:00:00 2001 From: Achille Date: Wed, 7 Feb 2024 18:17:18 -0800 Subject: [PATCH] return 0 is compare function of sortBlocks when blocks are equal (#2026) Signed-off-by: Achille Roussel --- internal/engine/wazevo/ssa/basic_block_sort.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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 }) }