From cdb5218de18132257de01aa876e34be2da7f3ea6 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 17 Nov 2026 09:00:42 -0800 Subject: [PATCH] Cranelift: critical-edge blocks inherit coldness from predecessors They already inherited coldness from their successor, but this commit makes them also inherit it from their predecessor. If either is cold, then that implies that the critical-edge block is also cold. This improves code quality by better falling though hot paths and better register allocation after https://github.com/bytecodealliance/regalloc2/pull/279 lands. --- cranelift/codegen/src/machinst/blockorder.rs | 8 +-- .../filetests/isa/x64/cold-blocks.clif | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/cranelift/codegen/src/machinst/blockorder.rs b/cranelift/codegen/src/machinst/blockorder.rs index 6e7c088c2bbd..e10b3d7e4f98 100644 --- a/cranelift/codegen/src/machinst/blockorder.rs +++ b/cranelift/codegen/src/machinst/blockorder.rs @@ -286,14 +286,14 @@ impl BlockLoweringOrder { // Critical edges won't have successor information in block_succ_range, but // they only have a single known successor to record anyway. - &LoweredBlock::CriticalEdge { succ, .. } => { + &LoweredBlock::CriticalEdge { succ, pred, .. } => { let succ_index = lb_to_bindex[&LoweredBlock::Orig { block: succ }]; lowered_succ_indices.push(succ_index); - // Edges inherit indirect branch and cold block metadata from their - // successor. + // Edges inherit indirect branch and cold block metadata + // from their successor and predecessor. - if f.is_effectively_cold(succ) { + if f.is_effectively_cold(succ) || f.is_effectively_cold(pred) { cold_blocks.insert(bindex); } diff --git a/cranelift/filetests/filetests/isa/x64/cold-blocks.clif b/cranelift/filetests/filetests/isa/x64/cold-blocks.clif index 92ff03821611..fc77e81af30e 100644 --- a/cranelift/filetests/filetests/isa/x64/cold-blocks.clif +++ b/cranelift/filetests/filetests/isa/x64/cold-blocks.clif @@ -94,3 +94,75 @@ block2 cold: ; movl $0x61, %eax ; jmp 0xf + +;; The edge `block1 -> block3` is a critical edge (`block1` has multiple +;; successors and `block3` has multiple predecessors) so it gets split into its +;; own block. Because its predecessor, `block1`, is cold, that split edge block +;; should be cold as well and placed at the end of the function, alongside +;; `block1`. +function %cold_critical_edge_pred(i32, i32, i32) -> i32 { +block0(v0: i32, v1: i32, v2: i32): + brif v0, block1, block2 + +block1 cold: + brif v0, block3(v2, v1), block4 + +block2: + jump block3(v1, v2) + +block3(v3: i32, v4: i32): + v5 = isub v3, v4 + return v5 + +block4 cold: + trap user1 +} + +; VCode: +; pushq %rbp +; movq %rsp, %rbp +; block0: +; testl %edi, %edi +; jnz label2; j label1 +; block1: +; movq %rsi, %rax +; movq %rdx, %rsi +; jmp label5 +; block5: +; subl %esi, %eax +; movq %rbp, %rsp +; popq %rbp +; retq +; block2: +; testl %edi, %edi +; jnz label3; j label4 +; block3: +; movq %rdx, %rax +; jmp label5 +; block4: +; ud2 +; +; Disassembled: +; block0: ; offset 0x0 +; pushq %rbp +; movq %rsp, %rbp +; block1: ; offset 0x4 +; testl %edi, %edi +; jne 0x19 +; block2: ; offset 0xc +; movq %rsi, %rax +; movq %rdx, %rsi +; block3: ; offset 0x12 +; subl %esi, %eax +; movq %rbp, %rsp +; popq %rbp +; retq +; block4: ; offset 0x19 +; testl %edi, %edi +; je 0x29 +; block5: ; offset 0x21 +; movq %rdx, %rax +; jmp 0x12 +; block6: ; offset 0x29 +; ud2 ; trap: user1 +