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
9 changes: 9 additions & 0 deletions crates/celox-frontend-core/src/symbolic/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ fn collect_inputs_with_window<A: Hash + Eq + Clone + Debug>(
set: &mut crate::HashSet<VarAtomBase<A>>,
visited: &mut HashMap<NodeId, Vec<BitAccess>>,
) {
// IEEE 1800-2023 11.4.12.1: a zero-count replication contributes no
// bits to its enclosing concatenation. There is no dependency window
// to visit, and constructing one would underflow its inclusive MSB.
if get_width(expr, arena) == 0 {
return;
}
let requested = window.unwrap_or_else(|| BitAccess::new(0, get_width(expr, arena) - 1));
let uncovered = claim_uncovered_window(visited.entry(expr).or_default(), requested);

Expand Down Expand Up @@ -596,6 +602,9 @@ fn collect_inputs_with_window<A: Hash + Eq + Clone + Debug>(
// Walk from LSB side to map the requested window to each part.
let mut part_lsb = 0usize;
for (part, width) in parts.iter().rev() {
if *width == 0 {
continue;
}
let part_msb = part_lsb + width - 1;
if win.overlaps(&BitAccess::new(part_lsb, part_msb)) {
let ov_lsb = std::cmp::max(win.lsb, part_lsb);
Expand Down
4 changes: 3 additions & 1 deletion crates/celox-frontend-sv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ pub use lowering::{
prepare_external_hierarchy_with_foreign_resolver_with_options, schedule_sources_with_options,
};

pub use lowering::schedule_sources_with_force_signals;
pub use lowering::{
schedule_sources_with_force_signals, schedule_sources_with_parameter_expressions,
};
190 changes: 182 additions & 8 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) struct LoweredSvModule {
#[derive(Clone)]
struct AnalyzedSvModule {
name: String,
interface_member_paths: HashMap<String, Vec<String>>,
interface_member_paths: HashMap<String, sv::InterfaceMemberPath>,
source: std::rc::Rc<sv::ParsedSource>,
implicit_nets_allowed: bool,
/// The positional interface of every module in all sources, used to bind
Expand Down Expand Up @@ -1788,6 +1788,49 @@ pub fn schedule_sources_with_force_signals(
warnings: &mut Vec<crate::FrontendWarning>,
analysis_options: sv::AnalysisOptions,
force_signals: &[String],
) -> Result<ScheduledRtlOutput, FrontendError> {
let parameters = parameter_overrides
.iter()
.map(|(name, value)| (name.clone(), sv::ir::ConstExpr::Literal(value.to_string())))
.collect::<Vec<_>>();
schedule_sources_with_parameter_expressions(
sources,
top,
&parameters,
ignored_loops,
true_loops,
four_state,
parallel,
trace_options,
trace,
warnings,
analysis_options,
force_signals,
)
}

/// Schedule a design with typed SV constant expressions as top parameters.
#[allow(clippy::too_many_arguments)]
pub fn schedule_sources_with_parameter_expressions(
sources: &[(&str, &Path)],
top: &str,
parameter_overrides: &[(String, sv::ir::ConstExpr)],
ignored_loops: &[(
(Vec<(String, usize)>, Vec<String>),
(Vec<(String, usize)>, Vec<String>),
)],
true_loops: &[(
(Vec<(String, usize)>, Vec<String>),
(Vec<(String, usize)>, Vec<String>),
usize,
)],
four_state: bool,
parallel: &celox_frontend_core::ParallelScheduleOptions,
trace_options: &FrontendTraceOptions,
trace: Option<&mut FrontendTrace>,
warnings: &mut Vec<crate::FrontendWarning>,
analysis_options: sv::AnalysisOptions,
force_signals: &[String],
) -> Result<ScheduledRtlOutput, FrontendError> {
let mut found = Vec::new();
let scheduled = schedule_sources_collecting(
Expand All @@ -1813,7 +1856,7 @@ pub fn schedule_sources_with_force_signals(
fn schedule_sources_collecting(
sources: &[(&str, &Path)],
top: &str,
parameter_overrides: &[(String, u64)],
parameter_overrides: &[(String, sv::ir::ConstExpr)],
ignored_loops: &[(
(Vec<(String, usize)>, Vec<String>),
(Vec<(String, usize)>, Vec<String>),
Expand Down Expand Up @@ -1845,7 +1888,7 @@ fn schedule_sources_collecting(
.iter()
.map(|(name, value)| LoweredSvParameterOverride {
name: name.clone(),
value: Some(sv::ir::ConstExpr::Literal(value.to_string())),
value: Some(value.clone()),
bound_type: None,
bound_pattern: None,
})
Expand Down Expand Up @@ -1990,7 +2033,13 @@ fn restore_interface_member_paths(module: &mut SimModule, source: &AnalyzedSvMod
source
.interface_member_paths
.get(segment)
.cloned()
.and_then(|path| path.restore(segment))
.or_else(|| {
source
.interface_member_paths
.values()
.find_map(|path| path.restore(segment))
})
.unwrap_or_else(|| vec![segment.clone()])
})
.collect();
Expand Down Expand Up @@ -2091,7 +2140,7 @@ fn specialize_module(
module
.interface_member_paths
.iter()
.find(|(_, segments)| segments.join(".") == *path)
.find(|(_, segments)| segments.segments.join(".") == *path)
.map_or_else(|| path.clone(), |(flat, _)| flat.clone())
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -2313,7 +2362,38 @@ fn lower_module_with_overrides(
};
name_to_id.insert(port.name().to_string(), id);
port_order.push(id);
if port.is_net() || type_info.is_4state {
if let Some(default) = port.default() {
if !variable.array_dims.is_empty() {
warnings.push((
crate::FrontendWarningKind::UnpackedPortDefault {
module: name.clone(),
port: port.name().to_string(),
},
None,
));
}
let literal = sv::typecheck::context_size_const_integral_literal(
default,
&constants,
&parameter_types,
type_info.width,
type_info.signed,
)
.ok_or_else(|| {
sv::AnalyzerError::Unsupported(format!(
"nonconstant input port default `{}`",
port.name()
))
})?;
initial_memory_values.push(InitialStateValue {
address: id,
data: InitialStateData::Packed {
value: literal.value,
mask: literal.mask,
written_mask: (BigUint::from(1u8) << type_info.width) - BigUint::from(1u8),
},
});
} else if port.is_net() || type_info.is_4state {
let written_mask = (BigUint::from(1u8) << type_info.width) - BigUint::from(1u8);
let value = if port.is_net() {
BigUint::default()
Expand Down Expand Up @@ -2381,9 +2461,66 @@ fn lower_module_with_overrides(
variables.insert(id, variable);
}

// Retain integral parameters for external observation, without adding them
// to name_to_id: HDL expressions must still fold parameter references as
// constants rather than introduce runtime loads.
for parameter in module.parameters() {
let Some(width) = parameter.resolved_width().filter(|width| *width > 0) else {
continue;
};
let signed = parameter.resolved_signed().unwrap_or(false);
let expr = parameter.value().cloned().or_else(|| {
constants
.get(parameter.name())
.copied()
.map(const_expr_from_i128)
});
let Some(literal) = expr.and_then(|expr| {
sv::typecheck::context_size_const_integral_literal(
&expr,
&constants,
&parameter_types,
width,
signed,
)
}) else {
continue;
};
let id = next_var_id(&mut next_id);
let written_mask = (BigUint::from(1u8) << width) - BigUint::from(1u8);
initial_memory_values.push(InitialStateValue {
address: id,
data: InitialStateData::Packed {
value: literal.value,
mask: literal.mask,
written_mask,
},
});
variables.insert(
id,
SvVariable {
path: vec![parameter.name().to_string()],
width,
signed,
is_4state: true,
packed_ranges: vec![(width as i128 - 1, 0)],
array_dims: Vec::new(),
domain_kind: DomainKind::Other,
kind: VariableKind::Parameter,
type_kind: PortTypeKind::Logic,
source: None,
hidden: false,
force_control: None,
},
);
}

let forceable = variables
.iter()
.filter_map(|(&id, var)| {
if var.kind == VariableKind::Parameter {
return None;
}
let name = &var.path[0];
force_signals.iter().any(|path| name == path).then_some(id)
})
Expand Down Expand Up @@ -3914,6 +4051,39 @@ fn build_instance_glue(
),
)?;
(expr, sources, source_ids)
} else if let Some(default) = child
.source
.ports()
.iter()
.find(|port| port.name() == formal)
.and_then(|port| port.default())
{
// IEEE 1800-2023 23.2.2.4: defaults belong to the child's
// parameter scope, and apply only to omitted connections.
let literal = sv::typecheck::context_size_const_integral_literal(
default,
&child.constants,
&child.parameter_types,
width,
child_var.signed,
)
.ok_or_else(|| {
ParserError::illegal_context(
"systemverilog input port default",
formal.clone(),
None,
)
})?;
(
arena.alloc(SLTNode::Constant(
literal.value,
literal.mask,
width,
child_var.signed,
))?,
HashSet::default(),
Vec::new(),
)
} else {
let unknown_mask = (BigUint::from(1u8) << width) - BigUint::from(1u8);
(
Expand Down Expand Up @@ -4387,7 +4557,9 @@ fn lower_glue_parent_expr(
)?
};
let width = celox_slt::get_width(node, arena);
nodes.push((node, width));
if width != 0 {
nodes.push((node, width));
}
sources.extend(part_sources);
source_ids.extend(part_source_ids);
}
Expand Down Expand Up @@ -5574,7 +5746,9 @@ fn lower_expr_with_context(
None,
)?;
let width = celox_slt::get_width(node, arena);
nodes.push((node, width));
if width != 0 {
nodes.push((node, width));
}
sources.extend(part_sources);
}
Some((arena.alloc(SLTNode::Concat(nodes)).ok()?, sources))
Expand Down
7 changes: 7 additions & 0 deletions crates/celox-frontend-sv/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub enum FrontendWarningKind {
/// IEEE 1800-2023 3.14.2.3 requires consistent explicit timing declarations.
#[error("mixed explicit and default time units or precisions across design elements")]
MixedTimescale,
/// Aggregate defaults are a compatibility extension to IEEE 1800-2023 23.2.2.4.
#[error("default value on unpacked input port `{module}.{port}` is a language extension")]
UnpackedPortDefault { module: String, port: String },
/// A memory file without address directives holds a different number of
/// words than the address range of `$readmemh`/`$readmemb`
/// (IEEE 1800-2023 21.4).
Expand Down Expand Up @@ -141,6 +144,7 @@ impl miette::Diagnostic for FrontendWarning {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new(match self.kind {
FrontendWarningKind::MixedTimescale => "mixed_timescale",
FrontendWarningKind::UnpackedPortDefault { .. } => "unpacked_port_default",
FrontendWarningKind::MemoryFileWordCount { .. } => "memory_file_word_count",
FrontendWarningKind::ExitOutsideProgram => "exit_outside_program",
}))
Expand All @@ -155,6 +159,9 @@ impl miette::Diagnostic for FrontendWarning {
FrontendWarningKind::MixedTimescale => {
"declare timeunit/timeprecision consistently; undeclared scopes use 1ns/1ps when this check is relaxed"
}
FrontendWarningKind::UnpackedPortDefault { .. } => {
"IEEE 1800-2023 23.2.2.4 permits defaults on singular input ports; use unpacked_port_default=warn or allow to enable aggregate defaults"
}
FrontendWarningKind::MemoryFileWordCount { .. } => {
"give one word per address of the range, or place the words with `@address` directives"
}
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-runtime/src/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct ReflectionScope {

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReflectionSignal {
/// An elaboration constant exposed as a read-only VPI parameter.
pub is_parameter: bool,
pub name: String,
pub full_name: String,
pub parent: ReflectionScopeId,
Expand Down
27 changes: 15 additions & 12 deletions crates/celox-sv-analyzer/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,21 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
let mut ports = Vec::new();
for port in module.ports() {
port_table.insert(module, port)?;
ports.push(ir::Port::new(
port.name().to_string(),
port.direction().into(),
ir::Type::from_ast(
port.r#type().clone(),
&ast::const_functions::Environment {
constants: &constants,
functions: &state.const_functions,
},
),
port.is_net(),
));
ports.push(
ir::Port::new(
port.name().to_string(),
port.direction().into(),
ir::Type::from_ast(
port.r#type().clone(),
&ast::const_functions::Environment {
constants: &constants,
functions: &state.const_functions,
},
),
port.is_net(),
)
.with_default(port.default().cloned().map(Into::into)),
);
}
let signals = module
.signals()
Expand Down
Loading
Loading