diff --git a/cranelift/codegen/src/legalizer/mod.rs b/cranelift/codegen/src/legalizer/mod.rs index 775c65be45..32f3331214 100644 --- a/cranelift/codegen/src/legalizer/mod.rs +++ b/cranelift/codegen/src/legalizer/mod.rs @@ -304,6 +304,9 @@ fn expand_cond_trap( let new_block_trap = func.dfg.make_block(); let new_block_resume = func.dfg.make_block(); + // Trapping is a rare event, mark the trapping block as cold. + func.layout.set_cold(new_block_trap); + // Replace trap instruction by the inverted condition. if trapz { func.dfg.replace(inst).brnz(arg, new_block_resume, &[]); diff --git a/cranelift/filetests/filetests/legalizer/conditional-traps.clif b/cranelift/filetests/filetests/legalizer/conditional-traps.clif new file mode 100644 index 0000000000..e4440efd32 --- /dev/null +++ b/cranelift/filetests/filetests/legalizer/conditional-traps.clif @@ -0,0 +1,40 @@ +;; Test the legalizations of `trap[n]z`. + +test legalizer +target aarch64 +target x86_64 +target riscv64 +target s390x + +function %trapnz(i32) -> i32 { +block0(v0: i32): + trapnz v0, heap_oob + return v0 +} + +; check: block0(v0: i32): +; nextln: brz v0, block2 +; nextln: jump block1 +; nextln: +; nextln: block1 cold: +; nextln: trap heap_oob +; nextln: +; nextln: block2: +; nextln: return v0 + +function %trapz(i32) -> i32 { +block0(v0: i32): + trapz v0, heap_oob + return v0 +} + +; check: block0(v0: i32): +; nextln: brnz v0, block2 +; nextln: jump block1 +; nextln: +; nextln: block1 cold: +; nextln: trap heap_oob +; nextln: +; nextln: block2: +; nextln: return v0 +