Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_>,
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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
Expand All @@ -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)?;
Expand Down
36 changes: 36 additions & 0 deletions crates/celox-sv-analyzer/src/ast/dimensions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
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"
);
}
15 changes: 15 additions & 0 deletions crates/celox-sv-analyzer/src/ast/scoped_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = 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.
Expand Down Expand Up @@ -57,6 +62,11 @@ impl<V: Clone> ScopedMap<V> {
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
}
Expand All @@ -68,6 +78,11 @@ impl<V: Clone> ScopedMap<V> {
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 {
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# The xezim reference simulator for the shared test suite, pinned by
# revision so that Renovate proposes updates.
xezim = {
url = "github:aionhw/xezim/a059e87dc05dd85dc7b38a07f0091f20a6964ba2";
url = "github:aionhw/xezim/8082de3cd5bc717e96145699c787a247fd6cf2e3";
flake = false;
};
};
Expand Down
Loading