Skip to content
Merged
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
13 changes: 8 additions & 5 deletions crates/celox-frontend-veryl/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,22 @@ pub fn external_signatures(
ExternalModule {
sim_module,
port_order,
// Veryl children link through the IR; only SV children are
// left for the SV frontend to resolve by name.
unresolved_instances: module
.declarations
.iter()
.filter_map(|declaration| {
let veryl_analyzer::ir::Declaration::Inst(instance) = declaration else {
return None;
};
let name = match instance.component.as_ref() {
Component::Module(module) => module.name,
Component::SystemVerilog(module) => module.name,
Component::Interface(_) => return None,
let Component::SystemVerilog(module) = instance.component.as_ref() else {
return None;
Comment on lines +95 to +96

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve native dependencies when preparing SV-rooted hierarchies

When an SV top instantiates a Veryl module whose Veryl child later instantiates an SV module, filtering the parent signature down to direct SystemVerilog children prevents the SV frontend from discovering that descendant. reachable_external_sv_roots cannot seed it because the selected top is not in the Veryl IR, so the external hierarchy contains the SV top and first Veryl module but not the eventual SV leaf; when the IR linker traverses the native child, its lookup in external.roots fails with MissingExternalModule. Native children therefore still need to contribute their transitive SV dependencies during hierarchy preparation, even though their bodies are linked through the IR.

Useful? React with 👍 / 👎.

};
Some(veryl_parser::resource_table::get_str_value(name).unwrap_or_default())
Some(
veryl_parser::resource_table::get_str_value(module.name)
.unwrap_or_default(),
)
})
.collect(),
},
Expand Down
21 changes: 15 additions & 6 deletions crates/celox-frontend-veryl/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ pub fn parse_ir_with_external_hierarchy<'a>(
}

/// Link the explicitly identified Veryl exports without matching SV namesakes.
/// `native_modules` maps each external module name that stands for a Veryl
/// body to the name of that body in `ir`.
pub fn parse_ir_with_external_hierarchy_and_native_modules<'a>(
ir: &'a veryl_analyzer::ir::Ir,
loop_provenance: &LoopProvenance,
external: &'a ExternalHierarchy,
config: &BuildConfig,
top: &StrId,
native_modules: &HashSet<String>,
native_modules: &HashMap<String, StrId>,
) -> Result<VerylSymbolicRtl<'a>, ParserError> {
parse_external_hierarchy(
ir,
Expand All @@ -72,7 +74,7 @@ fn parse_external_hierarchy<'a>(
external: &'a ExternalHierarchy,
config: &BuildConfig,
top: &StrId,
native_modules: Option<&HashSet<String>>,
native_modules: Option<&HashMap<String, StrId>>,
) -> Result<VerylSymbolicRtl<'a>, ParserError> {
// Token-to-scope resolution uses process-global analyzer state and is not
// safe while independent compilations run on other test threads. Snapshot
Expand Down Expand Up @@ -116,7 +118,7 @@ fn parse_external_hierarchy<'a>(
let top_name = resource_table::get_str_value(*top).unwrap_or_default();
let root_ir = name_to_ir.get(top).copied();
let external_root = external.roots.get(&top_name).copied().filter(|_| {
root_ir.is_none() || native_modules.is_none_or(|names| names.contains(&top_name))
root_ir.is_none() || native_modules.is_none_or(|names| names.contains_key(&top_name))
});
if root_ir.is_none() && external_root.is_none() {
return Err(ParserError::TopNotFound { name: top_name });
Expand Down Expand Up @@ -172,11 +174,18 @@ fn parse_external_hierarchy<'a>(
binding.package = external_ids[&binding.package];
}
module_names.insert(global_id, external_module.sim_module.name.clone());
let native_name = resource_table::insert_str(&external_module.sim_module.name);
if let Some(&native_module) = name_to_ir.get(&native_name)
&& native_modules.is_none_or(|names| names.contains(&external_module.sim_module.name))
let native_name = match native_modules {
Some(names) => names.get(&external_module.sim_module.name).copied(),
None => Some(resource_table::insert_str(&external_module.sim_module.name)),
};
if let Some(native_name) = native_name
&& let Some(&native_module) = name_to_ir.get(&native_name)
&& !module_ir.contains_key(&global_id)
{
module_names.insert(
global_id,
resource_table::get_str_value(native_name).unwrap_or_default(),
);
module_ir.insert(global_id, native_module);
signature_to_id.insert(native_module.signature.clone(), global_id);
worklist.push((global_id, native_module));
Expand Down
51 changes: 37 additions & 14 deletions crates/celox/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ pub fn parse_mixed(
requests: &[celox_frontend_sv::SvModuleRequest],
sv_interfaces: &[celox_frontend_sv::InterfaceSignature],
used_names: &mut fxhash::FxHashSet<String>,
names: crate::simulator::mixed::EmittedNames,
config: &BuildConfig,
ignored_loops: &[(
(Vec<(String, usize)>, Vec<String>),
Expand Down Expand Up @@ -699,8 +700,23 @@ pub fn parse_mixed(
external_roots.insert(top_name.clone());
}
let mut foreign = celox_frontend_veryl::external_signatures(ir, config)?;
let authored = celox_frontend_sv::module_interfaces(sv_sources).map_err(sv_analysis_error)?;
foreign.roots.retain(|name, _| !authored.contains_key(name));
// SV instantiates a Veryl module by its emitted name. The linker binds
// that name back to the Veryl body; an SV module declaring the same name
// is a duplicate the SV analyzer reports.
let mut native_modules = crate::HashMap::default();
let mut roots = std::mem::take(&mut foreign.roots);
for component in &ir.components {
let veryl_analyzer::ir::Component::Module(module) = component else {
continue;
};
let Some(id) = roots.remove(&module.name.to_string()) else {
continue;
Comment on lines +712 to +713

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve namespace identity while renaming foreign roots

When the root project and a dependency both define a module with the same terminal name, their Veryl namespaces are distinct and the emitter gives them distinct names such as prj_Child and dep_Child, but this lookup still consumes roots by module.name alone. external_signatures has only a source-name-keyed Child entry, so the first component removes it and the other component is silently skipped; consequently SV written against the emitted code cannot instantiate both declarations and may report one as missing or bind the surviving body. The emitted-name mapping needs to retain the module's symbol/signature identity rather than round-tripping through the unqualified source name.

Useful? React with 👍 / 👎.

};
let emitted = names.module(module);
foreign.modules.get_mut(&id).unwrap().sim_module.name = emitted.clone();
foreign.roots.insert(emitted.clone(), id);
native_modules.insert(emitted, module.name);
}
let sv_top_parameters = (!native_top).then_some((top_name.as_str(), top_parameters));
let base = ir.clone();
let mut specializations = Vec::new();
Expand Down Expand Up @@ -743,8 +759,19 @@ pub fn parse_mixed(
));
}
let mut prepare = || -> Result<_, ParserError> {
let native = native_modules.get(name).ok_or_else(|| {
ParserError::illegal_context(
"foreign module specialization",
format!("`{name}` is not a Veryl module"),
None,
)
})?;
let module = crate::simulator::mixed::specialize_foreign_module(
&base, context, name, parameters, used_names,
&base,
context,
&native.to_string(),
parameters,
used_names,
)?;
let mut specialized = veryl_analyzer::ir::Ir {
components: vec![veryl_analyzer::ir::Component::Module(module)],
Expand All @@ -767,6 +794,7 @@ pub fn parse_mixed(
&joint_sources,
context,
used_names,
names,
)?;
let exported = celox_frontend_veryl::external_signatures(&specialized, config)?;
let signature = exported.modules.values().next().cloned().ok_or_else(|| {
Expand Down Expand Up @@ -818,17 +846,12 @@ pub fn parse_mixed(
.map(|source| (source, std::path::PathBuf::from("<sv-interface-behavior>"))),
);
};
ir.components.extend(specializations);
let mut native_modules = foreign.roots.keys().cloned().collect::<HashSet<_>>();
native_modules.extend(ir.components.iter().filter_map(|component| {
if let veryl_analyzer::ir::Component::Module(module) = component
&& !authored.contains_key(&module.name.to_string())
{
Some(module.name.to_string())
} else {
None
for component in &specializations {
if let veryl_analyzer::ir::Component::Module(module) = component {
native_modules.insert(module.name.to_string(), module.name);
}
}));
}
ir.components.extend(specializations);
parse_with_external_hierarchy(
top,
ir,
Expand Down Expand Up @@ -856,7 +879,7 @@ pub fn parse_with_external_hierarchy(
ir: &veryl_analyzer::ir::Ir,
loop_provenance: &loop_provenance::LoopProvenance,
external: &celox_frontend_core::symbolic::artifact::ExternalHierarchy,
native_modules: Option<&HashSet<String>>,
native_modules: Option<&crate::HashMap<String, StrId>>,
config: &BuildConfig,
ignored_loops: &[(
(Vec<(String, usize)>, Vec<String>),
Expand Down
6 changes: 5 additions & 1 deletion crates/celox/src/simulator/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ fn analyze(
{
if let Some(sv_sources) = sv_sources {
(|| {
let mut used_names = super::mixed::component_names(sv_sources)?;
let names = super::mixed::EmittedNames::new(&metadata);
let mut used_names = super::mixed::component_names(sv_sources, names)?;
let behavior_sources = super::mixed::attach_sv_interface_behavior(
&mut ir,
&sv_interface_signatures,
Expand All @@ -515,12 +516,14 @@ fn analyze(
&joint_sources,
&mut context,
&mut used_names,
names,
)?;
let declarations = super::mixed::sv_declaration_contracts(
&ir,
&mut context,
&joint_sources,
&requests,
names,
)?;
parser::parse_mixed(
&top,
Expand All @@ -536,6 +539,7 @@ fn analyze(
&requests,
&sv_interface_signatures,
&mut used_names,
names,
&build_config,
ignored_loops,
true_loops,
Expand Down
Loading
Loading