diff --git a/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs b/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs index bc9d6de712d..407da9246c6 100644 --- a/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs +++ b/crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs @@ -148,6 +148,22 @@ fn shape(context: &mut PackedDimensions, name: String, ty: &Type, local: bool) { context.insert(name, value); } } +// Binding the declaration's type/ranges may reenter discovery and needs the +// completed prefix. The following table edit itself performs no queries. Drop +// only our own old snapshot during that edit, so ScopedMap need not copy its +// growing overlay on every declaration, then publish before the next query. +fn publish_shape( + tree: &AnalysisContext<'_>, + key: &Key, + context: &mut PackedDimensions, + name: String, + ty: &Type, + local: bool, +) { + tree.state.dimensions.partial.borrow_mut().remove(key); + shape(context, name, ty, local); + publish(&tree.state.dimensions, key, context); +} fn variables( declaration: &celox_sv_syntax::VariableDeclaration, tree: &AnalysisContext<'_>, @@ -162,8 +178,7 @@ fn variables( type_binding::ordinary_variable(variable, declaration.data_type, tree.declarations()) }) { let ty = type_with_dimensions(base.clone(), &variable.dimensions, tree, context)?; - shape(context, name(&variable.name), &ty, local); - publish(&tree.state.dimensions, key, context); + publish_shape(tree, key, context, name(&variable.name), &ty, local); } Ok(()) } @@ -206,7 +221,7 @@ fn parameter( if !scalar_binding && let Some(base) = type_binding::bind(*data_type, tree, context)? { let ty = type_with_dimensions(base, dimensions, tree, context)?; if !dimensions.is_empty() || !ty.packed_ranges().is_empty() { - shape(context, n, &ty, local); + publish_shape(tree, key, context, n, &ty, local); } } } @@ -236,8 +251,7 @@ fn declaration( for variable in &data.variables { let ty = type_with_dimensions(base.clone(), &variable.dimensions, tree, context)?; - shape(context, name(&variable.name), &ty, false); - publish(&tree.state.dimensions, key, context); + publish_shape(tree, key, context, name(&variable.name), &ty, false); } } } @@ -689,8 +703,7 @@ fn statement_scope( for declaration in declarations { if let Some(ty) = type_binding::bind(declaration.data_type, tree, context)? { for variable in declaration.variables { - shape(context, name(&variable.name), &ty, true); - publish(&tree.state.dimensions, key, context); + publish_shape(tree, key, context, name(&variable.name), &ty, true); } } } @@ -699,8 +712,7 @@ fn statement_scope( let ty = type_binding::builtin_type("int", None) .ok_or_else(|| unsupported("foreach index type"))?; for n in names { - shape(context, name(&n), &ty, true); - publish(&tree.state.dimensions, key, context); + publish_shape(tree, key, context, name(&n), &ty, true); } } } @@ -824,8 +836,7 @@ pub(in crate::ast) fn query_operand_dimensions_id( && let Ok(ty) = type_with_dimensions(base, &port.dimensions, tree, &context) && let celox_sv_syntax::PortKind::Named(n) = &port.kind { - shape(&mut context, name(n), &ty, false); - publish(&tree.state.dimensions, &key, &context); + publish_shape(tree, &key, &mut context, name(n), &ty, false); } } } @@ -842,8 +853,7 @@ pub(in crate::ast) fn query_operand_dimensions_id( if let Some(id) = owners.subroutine { let subroutine = native.subroutine(id); for (argument, ty) in formal_types(subroutine, tree, &context)? { - shape(&mut context, argument.name, &ty, true); - publish(&tree.state.dimensions, &key, &context); + publish_shape(tree, &key, &mut context, argument.name, &ty, true); } if contains(subroutine.body_tokens, target) && subroutine.kind == SubroutineKind::Function @@ -853,8 +863,7 @@ pub(in crate::ast) fn query_operand_dimensions_id( if matches!(native.data_type(id).kind, DataTypeKind::Implicit) { ty.kind = TypeKind::Logic; } - shape(&mut context, name(&subroutine.name), &ty, true); - publish(&tree.state.dimensions, &key, &context); + publish_shape(tree, &key, &mut context, name(&subroutine.name), &ty, true); } } statement_scope(&owners.statements, target, tree, &mut context, &key)?; diff --git a/crates/celox-sv-analyzer/src/ast/dimensions/tests.rs b/crates/celox-sv-analyzer/src/ast/dimensions/tests.rs index eeeac9bcae6..4263dd6f0a9 100644 --- a/crates/celox-sv-analyzer/src/ast/dimensions/tests.rs +++ b/crates/celox-sv-analyzer/src/ast/dimensions/tests.rs @@ -641,3 +641,39 @@ fn preliminary_literal_queries_do_not_discover_scope_declarations() { assert_eq!(direct, discovered); assert_eq!(SYNTAX_TYPE_DISCOVERIES.with(|count| count.get()), 0); } + +#[test] +fn discovery_prefix_does_not_copy_a_growing_symbol_overlay() { + const COUNT: usize = 128; + let declarations = (0..COUNT) + .map(|index| format!("logic [7:0] v{index};")) + .collect::(); + let code = format!( + "module Top; {declarations} localparam int WIDTH=$bits(v{}); endmodule", + COUNT - 1 + ); + let native = + crate::source_tree::parse_scope_fixture(&code, Path::new("query-prefix-copies.sv")) + .unwrap(); + let calls = native_calls(&native); + assert_eq!(calls.len(), 1); + let tree = AnalysisContext::unspecialized(&native); + super::super::scoped_map::COPIED_OVERLAY_BINDINGS.with(|count| count.set(0)); + SYNTAX_TYPE_DISCOVERIES.with(|count| count.set(0)); + assert_eq!( + query_number( + calls[0], + &tree, + &HashMap::default(), + &HashMap::default(), + None + ), + Some(8) + ); + assert!(SYNTAX_TYPE_DISCOVERIES.with(|count| count.get()) > 0); + let copied = super::super::scoped_map::COPIED_OVERLAY_BINDINGS.with(|count| count.get()); + assert!( + copied <= COUNT * 8, + "{copied} copied bindings for {COUNT} declarations" + ); +} diff --git a/crates/celox-sv-analyzer/src/ast/scoped_map.rs b/crates/celox-sv-analyzer/src/ast/scoped_map.rs index e39d271d8ed..a4749bb66f0 100644 --- a/crates/celox-sv-analyzer/src/ast/scoped_map.rs +++ b/crates/celox-sv-analyzer/src/ast/scoped_map.rs @@ -4,6 +4,11 @@ use std::sync::Arc; use fxhash::FxHashMap as HashMap; +#[cfg(test)] +thread_local! { + pub(super) static COPIED_OVERLAY_BINDINGS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + /// Cloning a scope shares both tables. Mutation copies only local changes when /// another scope still shares the overlay, never the complete module table. /// `None` in the overlay hides a symbol in the shared table. @@ -57,6 +62,11 @@ impl ScopedMap { return base.insert(name, value); } let old = self.get(&name).cloned(); + #[cfg(test)] + if Arc::strong_count(&self.overlay) > 1 { + COPIED_OVERLAY_BINDINGS.with(|count| count.set(count.get() + self.overlay.len())); + } + Arc::make_mut(&mut self.overlay).insert(name, Some(value)); old } @@ -68,6 +78,11 @@ impl ScopedMap { return base.remove(name); } let old = self.get(name).cloned(); + #[cfg(test)] + if Arc::strong_count(&self.overlay) > 1 { + COPIED_OVERLAY_BINDINGS.with(|count| count.set(count.get() + self.overlay.len())); + } + if self.base.contains_key(name) { Arc::make_mut(&mut self.overlay).insert(name.to_string(), None); } else {