From 20c0e5cb36b8209752b20a51020bff2a500fde0b Mon Sep 17 00:00:00 2001 From: tignear Date: Thu, 19 Nov 2026 10:32:45 +0900 Subject: [PATCH] perf(sv): keep the packed dimensions of a scope's parameters as they are declared Each parameter declaration built the packed dimensions of every parameter the scope imports and of every one it had declared so far, for the contexts that bind its value and its type: as many tables as the scope has parameters, each as large as its packages' parameters. BlackParrot's modules import the parameters of large configuration packages and declare many of their own, so the copies of the dimensions of members of packed structures were a large part of its discovery. The parameter environment of a scope now builds the dimensions of the imported parameters once, for the imported symbols they come from, and each declaration adds those of the scope's parameters to a shared copy. BlackParrot (1x1 cmark) builds with 7.7% fewer instructions than on master (2,731.1 G to 2,521.3 G, perf stat instructions:u). A unit test checks that the environment gives the dimensions of `parameter_packed_dimensions_with_imported` for every prefix of a scope's parameters, with shadowed imported ones, through two tables of imported symbols. A first version that updated the table as parameters were declared broke BlackParrot ("constant cast operand") while the suites passed; BlackParrot's build is this change's regression check, and the nightly CodSpeed BlackParrot case (#4380) compiles it. Reducing that shape to a small regression test is #4384. Change-Id: I3afed5e1f43e5fc3656adb19bc67c23a86b61575 --- .../celox-sv-analyzer/src/ast/parameters.rs | 51 ++++++++++--- .../src/ast/parameters/tests.rs | 74 +++++++++++++++++++ 2 files changed, 114 insertions(+), 11 deletions(-) diff --git a/crates/celox-sv-analyzer/src/ast/parameters.rs b/crates/celox-sv-analyzer/src/ast/parameters.rs index 99ec5ec5365..212b79eecc0 100644 --- a/crates/celox-sv-analyzer/src/ast/parameters.rs +++ b/crates/celox-sv-analyzer/src/ast/parameters.rs @@ -529,6 +529,11 @@ pub(super) struct ParameterEnvironment<'a> { /// The shapes of the variables that hold the array parameters declared /// so far. array_shapes: HashMap, + /// The packed dimensions of the parameters a scope imports, with the + /// imported symbols they were built from: built once, + /// not again for each declaration of the scope (see + /// [`Self::packed_dimensions`]). + imported_dimensions: Option<(Arc, ScopedMap)>, } impl<'a> ParameterEnvironment<'a> { @@ -601,9 +606,38 @@ impl<'a> ParameterEnvironment<'a> { literals, arrays: HashMap::default(), array_shapes: HashMap::default(), + imported_dimensions: None, } } + /// The packed dimensions of the parameters that `tree` imports and of + /// `parameters`, which shadow imported ones of their names, as + /// [`module_elaboration::parameter_packed_dimensions_with_imported`] + /// gives them; those of the imported parameters are built once. + pub(super) fn packed_dimensions( + &mut self, + tree: &AnalysisContext<'_>, + parameters: &[Parameter], + ) -> ScopedMap { + let imported = scope::imported(tree); + let mut dimensions = match &self.imported_dimensions { + Some((cached, base)) if Arc::ptr_eq(cached, &imported) => base.shared(), + _ => { + let base: ScopedMap = + dimensions::parameter_packed_dimensions(&imported.parameters).into(); + self.imported_dimensions = Some((imported.clone(), base.shared())); + base + } + }; + // A local scalar or implicitly typed parameter also shadows an + // imported packed parameter. + for parameter in parameters { + dimensions.remove(parameter.name()); + } + dimensions.extend(dimensions::parameter_packed_dimensions(parameters)); + dimensions + } + fn append(&mut self, tree: &AnalysisContext<'_>, session: &mut Session, parameter: &Parameter) { let bound = bind_parameter( tree, @@ -784,7 +818,7 @@ pub(super) fn parameters_from_native_with_environment( // is a constant variable instead. let imported = scope::imported(syntax_tree); let mut context = PackedDimensions::new( - parameter_packed_dimensions_with_imported(parameters, &imported), + environment.packed_dimensions(syntax_tree, parameters), &environment.constants, type_aliases, ); @@ -881,6 +915,7 @@ pub(super) fn parameters_from_native_with_environment( environment.constants.remove(&key); } environment.literals.remove(&name); + let scope_dimensions = environment.packed_dimensions(syntax_tree, parameters); let const_env = &environment.constants; let replaced_pattern = !is_local && parameter_overrides.contains_key(&name) @@ -891,11 +926,8 @@ pub(super) fn parameters_from_native_with_environment( parameter_overrides.get(&name).cloned() } else if ownership::default(native).is_some() { let imported = scope::imported(syntax_tree); - let mut proof_context = PackedDimensions::new( - parameter_packed_dimensions_with_imported(parameters, &imported), - const_env, - type_aliases, - ); + let mut proof_context = + PackedDimensions::new(scope_dimensions.clone(), const_env, type_aliases); // Preliminary collection must not expand unresolved initializer // chains merely to validate a shape. Only resolved prefix values // are available here; imported bindings remain shadowed by locals. @@ -942,11 +974,8 @@ pub(super) fn parameters_from_native_with_environment( syntax_tree.declarations(), ) { - let mut dimensions = PackedDimensions::new( - parameter_packed_dimensions_with_imported(parameters, &imported), - const_env, - type_aliases, - ); + let mut dimensions = + PackedDimensions::new(scope_dimensions.clone(), const_env, type_aliases); dimensions.parameter_values = parameter_value_env_with_imported( syntax_tree, session, diff --git a/crates/celox-sv-analyzer/src/ast/parameters/tests.rs b/crates/celox-sv-analyzer/src/ast/parameters/tests.rs index 340a986d44a..3fbc583e673 100644 --- a/crates/celox-sv-analyzer/src/ast/parameters/tests.rs +++ b/crates/celox-sv-analyzer/src/ast/parameters/tests.rs @@ -1727,3 +1727,77 @@ fn member_constants_follow_their_parameter() { ); assert_eq!(member_literal("other", "a", &env), None); } + +/// The packed dimensions a parameter environment gives a scope are those of +/// `parameter_packed_dimensions_with_imported`: of the imported parameters, +/// shadowed by every parameter of the scope declared so far, also one +/// without packed dimensions, and of those parameters. The imported ones are +/// built once per table of imported symbols, also when a scope's +/// declarations go through another table. +#[test] +fn environment_packed_dimensions_are_those_of_the_imported_and_declared_parameters() { + let packed = |name: &str, msb: i128| { + let mut parameter = Parameter::new( + name.into(), + Some(ConstExpr::Literal("0".into())), + None, + None, + false, + false, + true, + ); + if msb >= 0 { + parameter.packed_ranges = vec![PackedRange::new( + ConstExpr::Literal(msb.to_string()), + ConstExpr::Literal("0".into()), + )]; + } + parameter + }; + let imported = |parameters: Vec| { + Arc::new(scope::ScopeSymbols { + parameters, + ..Default::default() + }) + }; + let first = imported(vec![packed("IMPORTED", 7), packed("SHADOWED", 3)]); + let second = imported(vec![packed("OTHER", 5)]); + let source = crate::source_tree::parse_source("", Path::new("environment.sv")).unwrap(); + let tree = AnalysisContext::unspecialized(&source); + let first_tree = tree.importing(first.clone()); + let second_tree = tree.importing(second.clone()); + let session = &mut Session::default(); + let base = ConstEnv::default(); + let mut environment = ParameterEnvironment::new(&first_tree, session, &[], &base, &[]); + // A scalar `SHADOWED` hides the imported packed one. + let declared = [packed("SHADOWED", -1), packed("OWN", 15)]; + let shown = |dimensions: ScopedMap| { + let mut entries: Vec<_> = dimensions + .iter() + .map(|(name, shape)| format!("{name}: {shape:?}")) + .collect(); + entries.sort(); + entries + }; + for (tree, symbols) in [ + (&first_tree, &first), + (&second_tree, &second), + (&first_tree, &first), + ] { + for prefix in 0..=declared.len() { + let expected: ScopedMap = + module_elaboration::parameter_packed_dimensions_with_imported( + &declared[..prefix], + symbols, + ) + .into(); + for _ in 0..2 { + assert_eq!( + shown(environment.packed_dimensions(tree, &declared[..prefix])), + shown(expected.shared()), + "prefix {prefix}" + ); + } + } + } +}