From 0bbfaa140318e28358ac623238d662b0a97dd253 Mon Sep 17 00:00:00 2001 From: tignear Date: Wed, 21 Oct 2026 08:49:06 +0900 Subject: [PATCH] perf(sv): reuse constants when expression contexts match Constant evaluation first computed each operand in its own context and then walked the same subtree again even when width and signedness were unchanged. Nested casts repeated this work, causing pathological growth in deep trees. Reuse the computed value for matching contexts and for casts that do not widen their operand. Reevaluate operands when width or signedness changes, preserving context-dependent carries, division and four-state values under IEEE 1800-2023 11.8.2. Taxi BASE-R compilation with the debug host and generated O1 code decreases from 174 seconds to 55 seconds. The release host improves from 30.9 seconds to 8.4 seconds (user CPU 25.95s to 8.01s); generated code remains at O1. The release baseline attributes about 60% of sampled CPU time to context_binary_operands. The unchanged selected upstream cocotb cases both pass after the optimization (2 executed, 238 skipped). The full BASE-R matrix still needs the upstream 1fs precision override, which the current CLI harness does not support; no all-matrix pass is claimed. Validation: deep 48-layer constant casts including X/Z, widening carries and signedness-changing division; 36 focused literal tests; full analyzer tests (762 passed, 3 ignored); full SystemVerilog suite across five backends (3242 passed, 90 ignored); formatting and affected-crate Clippy. Change-Id: I2bdbf133a10a898df17646cf566fa5a24496ff15 --- crates/celox-sv-analyzer/src/typecheck.rs | 79 +++++++++++++++++++++-- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/crates/celox-sv-analyzer/src/typecheck.rs b/crates/celox-sv-analyzer/src/typecheck.rs index 5eb7cb7aa24..ae2df0d402f 100644 --- a/crates/celox-sv-analyzer/src/typecheck.rs +++ b/crates/celox-sv-analyzer/src/typecheck.rs @@ -615,9 +615,21 @@ fn context_binary_operands( let (left_type, right_type) = integral_binary_operands(left, right)?; let width = left_type.width.max(right_type.width); let signed = left_type.signed && right_type.signed; - let left = eval_integral_in_context(left, width, signed)?; - let right = eval_integral_in_context(right, width, signed)?; - Some((left, right, signed)) + // These values were already evaluated in their own expression contexts. + // IEEE 1800-2023 11.8.2 requires another descent only when the enclosing + // context changes their size or signedness. + let in_context = |expr, literal: IntegralLiteral| { + if literal.width == width && literal.signed == signed { + Some(literal) + } else { + eval_integral_in_context(expr, width, signed) + } + }; + Some(( + in_context(left, left_type)?, + in_context(right, right_type)?, + signed, + )) } fn eval_literal_binary( @@ -987,8 +999,11 @@ fn integral_literal_from_const_expr(expr: &ConstExpr) -> Option return Some(literal); } let operand = self_determined_integral_literal(expr)?; - let operand = - eval_integral_in_context(expr, (*width).max(operand.width), operand.signed)?; + let operand = if *width <= operand.width { + operand + } else { + eval_integral_in_context(expr, *width, operand.signed)? + }; let extension = signed_extension(&operand, operand.signed); Some(resize_integral_literal(operand, *width, *signed, extension)) } @@ -1802,6 +1817,60 @@ fn extension_for_leading_digit(ch: char) -> (bool, bool) { mod literal_tests { use super::*; + #[test] + fn evaluates_deep_constant_casts_without_repeated_context_walks() { + for (value, expected) in [("32'h12", "32'h12"), ("32'h1z", "32'h1x")] { + let mut expr = ConstExpr::Literal(value.into()); + // Each layer used to evaluate every inner layer twice. Keep this + // deep enough to catch that growth without a wall-clock assertion. + for _ in 0..48 { + expr = ConstExpr::Resize { + expr: Box::new(ConstExpr::Binary { + left: Box::new(expr), + op: BinaryOp::BitXor, + right: Box::new(ConstExpr::Literal("32'hf".into())), + }), + width: 32, + signed: false, + }; + } + assert_eq!( + integral_literal_from_const_expr(&expr), + parse_integral_literal(expected) + ); + } + } + + #[test] + fn changed_constant_contexts_recompute_width_and_signedness() { + let binary = |left, op, right| ConstExpr::Binary { + left: Box::new(left), + op, + right: Box::new(right), + }; + let carry = binary( + ConstExpr::Literal("32'hffffffff".into()), + BinaryOp::Add, + ConstExpr::Literal("32'd1".into()), + ); + let widened = binary(carry, BinaryOp::Add, ConstExpr::Literal("64'd0".into())); + assert_eq!( + integral_literal_from_const_expr(&widened), + parse_integral_literal("64'h100000000") + ); + + let quotient = binary( + ConstExpr::Literal("32'shffffffff".into()), + BinaryOp::Div, + ConstExpr::Literal("32'sd2".into()), + ); + let unsigned = binary(quotient, BinaryOp::Add, ConstExpr::Literal("32'd0".into())); + assert_eq!( + integral_literal_from_const_expr(&unsigned), + parse_integral_literal("32'h7fffffff") + ); + } + #[test] fn evaluates_nested_logical_parameter_chains() { let constants = HashMap::from_iter([("P".to_string(), 1)]);