From 1bd8785a16c0edbbc86ab051e51e054af48c2709 Mon Sep 17 00:00:00 2001 From: tignear Date: Sun, 25 Oct 2026 18:25:46 +0900 Subject: [PATCH] fix(sir-opt): preserve live operands of shared bit-extract shifts When two bit-extract replacements share a dead shift, the cleanup visits that shift twice and decrements its operand use counts twice. This can delete the shift amount even when another live shift still uses it, producing invalid SSA. Only decrement a dead instruction's operands when it is first inserted into the removal set. Add a regression with two masked uses of one shift and a separate live shift sharing its amount; verify SSA before and after the actual pass. This isolates the concrete bug fix found during formal-verification work. It contains no extraction refactors and does not claim a universal pass proof. Validation: the new regression fails before the fix with SSA.USE_HAS_DEFINITION (r1 is used but never defined). After the fix, celox-sir-opt has 469 passing unit tests (1 ignored) and 1 passing doctest; cargo clippy --locked -p celox-sir-opt --all-targets -- -D warnings and cargo fmt --all -- --check pass. The existing masked_shift_fields_keep_expression_width regression also passes on Wasm, native, Cranelift, native parallel and Cranelift parallel backends (5 tests). Shared cases and external simulator adapters are unchanged. Change-Id: I9e413552cce93def93a0ef57f46d82ac323a8d7f --- .../dataflow/pass_bit_extract_peephole.rs | 69 ++++++++++++++++++- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/crates/celox-sir-opt/src/optimizer/passes/dataflow/pass_bit_extract_peephole.rs b/crates/celox-sir-opt/src/optimizer/passes/dataflow/pass_bit_extract_peephole.rs index 96eec63be7a..10ca02176c5 100644 --- a/crates/celox-sir-opt/src/optimizer/passes/dataflow/pass_bit_extract_peephole.rs +++ b/crates/celox-sir-opt/src/optimizer/passes/dataflow/pass_bit_extract_peephole.rs @@ -200,15 +200,15 @@ fn optimize_bit_extracts( | SIRInstruction::Load(dst, _, _, _) => *dst, _ => continue, }; - if use_count.get(&def_reg).copied().unwrap_or(0) == 0 { - // Also decrement use counts for operands of this dead instruction + if use_count.get(&def_reg).copied().unwrap_or(0) == 0 && dead_set.insert(dead_idx) { + // Multiple replacements may share this definition. Removing + // it consumes its operands once, not once per replacement. if let SIRInstruction::Binary(_, lhs, _, rhs) = &instructions[dead_idx] { *use_count.entry(*lhs).or_default() = use_count.get(lhs).copied().unwrap_or(0).saturating_sub(1); *use_count.entry(*rhs).or_default() = use_count.get(rhs).copied().unwrap_or(0).saturating_sub(1); } - dead_set.insert(dead_idx); } } } @@ -399,6 +399,69 @@ mod tests { } } + #[test] + fn preserves_ssa_when_replacements_share_a_shift() { + let addr = test_addr(); + let other = RegionedAbsoluteAddr { + instance_id: InstanceId(1), + ..addr + }; + let mut unit = ExecutionUnit { + entry_block_id: BlockId(0), + blocks: [( + BlockId(0), + BasicBlock { + id: BlockId(0), + params: vec![], + instructions: vec![ + SIRInstruction::Load(RegisterId(0), addr, SIROffset::Static(0), 8), + SIRInstruction::Load(RegisterId(8), other, SIROffset::Static(0), 8), + SIRInstruction::Imm(RegisterId(1), SIRValue::new(2u64)), + SIRInstruction::Binary( + RegisterId(2), + RegisterId(0), + BinaryOp::Shr, + RegisterId(1), + ), + SIRInstruction::Imm(RegisterId(3), SIRValue::new(3u64)), + SIRInstruction::Binary( + RegisterId(4), + RegisterId(2), + BinaryOp::And, + RegisterId(3), + ), + SIRInstruction::Imm(RegisterId(5), SIRValue::new(15u64)), + SIRInstruction::Binary( + RegisterId(6), + RegisterId(2), + BinaryOp::And, + RegisterId(5), + ), + SIRInstruction::Binary( + RegisterId(7), + RegisterId(8), + BinaryOp::Shr, + RegisterId(1), + ), + SIRInstruction::RuntimeEvent { + site_id: 0, + args: vec![RegisterId(4), RegisterId(6), RegisterId(7)], + }, + ], + terminator: SIRTerminator::Return, + }, + )] + .into_iter() + .collect(), + register_map: (0..9) + .map(|id| (RegisterId(id), RegisterType::Logic { width: 8 })) + .collect(), + }; + unit.verify_result().unwrap(); + BitExtractPeepholePass.run(&mut unit, &PassOptions::default()); + unit.verify_result().unwrap(); + } + #[test] fn keeps_shift_result_used_by_runtime_event() { let addr = test_addr();