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
8 changes: 8 additions & 0 deletions conformance/sv-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ parameters, as in `8.26.3--type_access_extends` and
`6.24.2--cast_fn`, `6.24.2--cast_task` and `8.16--cast_func` keep failing
because they cast to integral and enum variables.

Tagged packed union support changes `chapter-7/unions/tagged/packed.sv` from
fail to pass in both state modes. The official runner selects simulation: the
probe switches the active member with two tagged constructors and checks the
packed value with its `:assert:` output. Both executions return zero and pass
the assertion. The [reviewed full CI run](https://github.com/celox-sim/celox/actions/runs/40084353934)
contains 2054 case/mode verdicts; these are the only two changed rows, and all
verdicts match the updated expectations.

The report contains each case’s state mode, verdict, selected execution mode,
tags, tool return code and path to the official log. The output directory retains
both the official log and runner diagnostics for each case and state mode.
Expand Down
4 changes: 2 additions & 2 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1457,8 +1457,8 @@ chapter-7/unions/packed/basic.sv 2state pass
chapter-7/unions/packed/basic.sv 4state pass
chapter-7/unions/tagged/basic.sv 2state fail
chapter-7/unions/tagged/basic.sv 4state fail
chapter-7/unions/tagged/packed.sv 2state fail
chapter-7/unions/tagged/packed.sv 4state fail
chapter-7/unions/tagged/packed.sv 2state pass
chapter-7/unions/tagged/packed.sv 4state pass
chapter-7/unions/unpacked/basic.sv 2state fail
chapter-7/unions/unpacked/basic.sv 4state fail
chapter-8/8.10--static_methods.sv 2state pass
Expand Down
90 changes: 88 additions & 2 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,9 @@ fn kernel_written_names(
);
continue;
}
sv::ir::Callee::String(_) | sv::ir::Callee::Object(_) => continue,
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = subroutines
Expand Down Expand Up @@ -1372,7 +1374,9 @@ impl DriverScan<'_> {
}
continue;
}
sv::ir::Callee::String(_) | sv::ir::Callee::Object(_) => continue,
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = self
Expand Down Expand Up @@ -3708,6 +3712,87 @@ fn array_element_connections(
.collect()
}

/// Tagged unions cannot acquire an arbitrary tag through an integral port connection.
fn check_tagged_port_connections(
parent: &sv::ir::Module,
child: &sv::ir::Module,
connections: &[LoweredSvPortConnection],
) -> Result<(), ParserError> {
let type_of = |name: &str| {
parent
.ports()
.iter()
.find(|port| port.name() == name)
.map(|port| port.r#type())
.or_else(|| {
parent
.signals()
.iter()
.find(|signal| signal.name() == name)
.map(|signal| signal.r#type())
})
};
for connection in connections {
let Some(port) = child
.ports()
.iter()
.find(|port| port.name() == connection.formal)
else {
continue;
};
let Some(expr) = &connection.actual_expr else {
continue;
};
let actual = if let sv::ir::Expr::Ident(name) = expr {
type_of(name)
} else {
None
};
let writes_actual = matches!(
port.direction(),
sv::ir::PortDirection::Output
| sv::ir::PortDirection::Inout
| sv::ir::PortDirection::Ref
);
let mut writes_tagged = false;
if writes_actual {
let mut pending = vec![expr];
while let Some(part) = pending.pop() {
match part {
sv::ir::Expr::Ident(name) => {
writes_tagged |=
type_of(name).is_some_and(|ty| ty.tagged_identity().is_some());
}
sv::ir::Expr::Concat(parts) => pending.extend(parts),
sv::ir::Expr::Select { expr, .. } | sv::ir::Expr::Resize { expr, .. } => {
pending.push(expr)
}
_ => {}
}
}
}
let target = port.r#type();
if target.tagged_identity().is_none() && !writes_tagged {
continue;
}
if !actual.is_some_and(|ty| {
ty.tagged_identity() == target.tagged_identity()
&& ty.resolved_width() == target.resolved_width()
&& ty.unpacked_ranges() == target.unpacked_ranges()
}) {
return Err(ParserError::illegal_context(
"systemverilog tagged union port",
format!(
"port `{}` requires a signal of the same tagged union type",
connection.formal
),
None,
));
}
}
Ok(())
}

fn bind_port_patterns(
connections: &mut [LoweredSvPortConnection],
child: &LoweredSvModule,
Expand Down Expand Up @@ -3802,6 +3887,7 @@ pub(crate) fn attach_instance_glue(
}
None => authored_connections,
};
check_tagged_port_connections(&lowered.source, &child.source, &connections)?;
ensure_parent_output_signals(
module,
&mut parent_variables,
Expand Down
7 changes: 7 additions & 0 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3192,6 +3192,13 @@ impl<'p, 'a> Comb<'p, 'a> {
.subroutine(name)
.cloned()
.ok_or_else(|| unsupported(format!("call of `{name}`")))?;
if args
.iter()
.flatten()
.any(|arg| !tagged_actual_guards(arg).is_empty())
{
return Err(unsupported("checked tagged union argument"));
}
// Symbolic execution cannot represent state shared by invocations.
// Fall back to Ff, including for runtime declaration initializers.
if !subroutine.persistent_locals.is_empty() {
Expand Down
90 changes: 72 additions & 18 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ impl<'p, 'a> Ff<'p, 'a> {
self.urandom_state();
continue;
}
sv::ir::Callee::String(_) | sv::ir::Callee::Object(_) => continue,
sv::ir::Callee::String(_)
| sv::ir::Callee::Object(_)
| sv::ir::Callee::Checked { .. } => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = self.m.subroutine(&name).cloned() else {
Expand Down Expand Up @@ -938,6 +940,28 @@ impl<'p, 'a> Ff<'p, 'a> {
return Ok(expr.clone());
}
Ok(match expr {
Expr::Call {
callee: sv::ir::Callee::Checked { width, signed },
args,
..
} => {
let [Some(condition), Some(value)] = args.as_slice() else {
return Err(unsupported("invalid tagged access check"));
};
self.system_task(
sv::ir::SystemTask::Assert,
&[
sv::ir::SystemTaskArg::Expr(condition.clone()),
sv::ir::SystemTaskArg::Str(
"tagged union member does not match the active tag".into(),
),
],
None,
)?;
let (_, name) = self.m.temp("tagged_member", *width, *signed, true);
self.assign(&sv::ir::LValue::Ident(name.clone()), value)?;
Expr::Ident(name)
}
Expr::Call {
callee: sv::ir::Callee::System(sv::ir::SystemFunction::Fread),
args,
Expand Down Expand Up @@ -1733,9 +1757,8 @@ impl<'p, 'a> Ff<'p, 'a> {
use sv::ir::{BinaryOp, Expr};
let (current, store_back) = match seed {
Some(seed) => {
let lvalues = lvalue_from_expr(seed)
.ok_or_else(|| unsupported("seed variable of `$random`"))?;
(self.hoist(seed)?, Some(lvalues))
let (lvalues, read, guards) = self.capture_system_actual(seed)?;
(self.hoist(&read)?, Some((lvalues, guards)))
}
None => (Expr::Ident(self.dist_seed().1), None),
};
Expand Down Expand Up @@ -1802,10 +1825,13 @@ impl<'p, 'a> Ff<'p, 'a> {
};
let result = self.hidden("random_result", &value, true)?;
match store_back {
Some(lvalues) => match lvalues.as_slice() {
[lvalue] => self.assign(lvalue, &next)?,
parts => self.assign_concat(parts, &next)?,
},
Some((lvalues, guards)) => {
self.check_tagged_guards(&guards)?;
match lvalues.as_slice() {
[lvalue] => self.assign(lvalue, &next)?,
parts => self.assign_concat(parts, &next)?,
}
}
None => {
let (id, _) = self.dist_seed();
let next = self.eval(&next, Some((32, false)))?;
Expand All @@ -1826,7 +1852,7 @@ impl<'p, 'a> Ff<'p, 'a> {
.first()
.and_then(Option::as_ref)
.ok_or_else(|| unsupported("seed of `$dist_uniform`"))?;
let (lvalues, seed_read) = self.capture_system_actual(seed)?;
let (lvalues, seed_read, guards) = self.capture_system_actual(seed)?;
let seed_width = self
.m
.comparison_width(&seed_read, &seed_read)
Expand Down Expand Up @@ -1974,6 +2000,7 @@ impl<'p, 'a> Ff<'p, 'a> {
signed: true,
};
let restored = mux(invalid, original, next);
self.check_tagged_guards(&guards)?;
match lvalues.as_slice() {
[lvalue] => self.assign(lvalue, &restored)?,
parts => self.assign_concat(parts, &restored)?,
Expand All @@ -1985,15 +2012,33 @@ impl<'p, 'a> Ff<'p, 'a> {
})
}

fn check_tagged_guards(&mut self, guards: &[sv::ir::Expr]) -> Result<(), sv::AnalyzerError> {
for guard in guards {
self.system_task(
sv::ir::SystemTask::Assert,
&[
sv::ir::SystemTaskArg::Expr(guard.clone()),
sv::ir::SystemTaskArg::Str(
"tagged union member does not match the active tag".into(),
),
],
None,
)?;
}
Ok(())
}

/// Capture an inout/output actual's reference before reading or writing
/// it, including selectors changed by the call or by other selectors.
fn capture_system_actual(
&mut self,
expr: &sv::ir::Expr,
) -> Result<(Vec<sv::ir::LValue>, sv::ir::Expr), sv::AnalyzerError> {
) -> Result<(Vec<sv::ir::LValue>, sv::ir::Expr, Vec<sv::ir::Expr>), sv::AnalyzerError> {
use sv::ir::{Expr, LValue};
let parts =
lvalue_from_expr(expr).ok_or_else(|| unsupported("integral output/inout actual"))?;
let guards = tagged_actual_guards(expr);
self.check_tagged_guards(&guards)?;
let signed = self.expr_signed(expr);
let mut captured = Vec::new();
for part in parts {
Expand All @@ -2017,7 +2062,7 @@ impl<'p, 'a> Ff<'p, 'a> {
} else {
Expr::Concat(reads)
};
Ok((captured, read))
Ok((captured, read, guards))
}

fn host_hidden(
Expand Down Expand Up @@ -2062,7 +2107,7 @@ impl<'p, 'a> Ff<'p, 'a> {
.ok_or_else(|| unsupported("distribution seed"))?;
// Capture selectors once before evaluating the other actuals, and
// suspend with an int formal rather than writing the caller's storage.
let (lvalues, captured) = self.capture_system_actual(seed)?;
let (lvalues, captured, guards) = self.capture_system_actual(seed)?;
let formal = self.int_formal(captured);
let (seed_id, seed_result) =
self.host_hidden("distribution_seed", &formal, 32, true, true)?;
Expand Down Expand Up @@ -2099,6 +2144,7 @@ impl<'p, 'a> Ff<'p, 'a> {
});
self.with_kernel(|kernel| kernel.host_request(HostRequestKind::Distribution, site))
.map_err(kernel_error)?;
self.check_tagged_guards(&guards)?;
match lvalues.as_slice() {
[lvalue] => self.assign(lvalue, &seed_result)?,
parts => self.assign_concat(parts, &seed_result)?,
Expand Down Expand Up @@ -2144,7 +2190,7 @@ impl<'p, 'a> Ff<'p, 'a> {
.get(1)
.and_then(Option::as_ref)
.ok_or_else(|| unsupported("plusargs destination"))?;
let (lvalues, captured) = self.capture_system_actual(expr)?;
let (lvalues, captured, guards) = self.capture_system_actual(expr)?;
let width = self
.m
.comparison_width(expr, expr)
Expand All @@ -2157,7 +2203,7 @@ impl<'p, 'a> Ff<'p, 'a> {
signed: false,
is_string: false,
});
Some((lvalues, result))
Some((lvalues, result, guards))
} else {
None
};
Expand All @@ -2182,7 +2228,8 @@ impl<'p, 'a> Ff<'p, 'a> {
});
self.with_kernel(|kernel| kernel.host_request(HostRequestKind::Plusargs, site))
.map_err(kernel_error)?;
if let Some((lvalues, result)) = target {
if let Some((lvalues, result, guards)) = target {
self.check_tagged_guards(&guards)?;
match lvalues.as_slice() {
[lvalue] => self.assign(lvalue, &result)?,
parts => self.assign_concat(parts, &result)?,
Expand Down Expand Up @@ -3647,13 +3694,18 @@ impl<'p, 'a> Ff<'p, 'a> {
.flatten()
.or_else(|| param.default.clone());
if param.direction.is_written() {
let lvalues = actual.as_ref().and_then(lvalue_from_expr).ok_or_else(|| {
let reference = actual
.as_ref()
.ok_or_else(|| unsupported("missing output argument"))?;
let lvalues = lvalue_from_expr(reference).ok_or_else(|| {
unsupported(format!(
"output argument `{}` of `{name}`",
param.source_name
))
})?;
outputs.push((param.name.clone(), id, lvalues));
let guards = tagged_actual_guards(reference);
self.check_tagged_guards(&guards)?;
outputs.push((param.name.clone(), id, lvalues, guards));
}
if param.direction.is_read() {
let actual = actual.ok_or_else(|| {
Expand Down Expand Up @@ -3741,7 +3793,8 @@ impl<'p, 'a> Ff<'p, 'a> {
self.jump(return_block);
}
self.b.switch_to_block(return_block);
for (formal, id, lvalues) in outputs {
for (formal, id, lvalues, guards) in outputs {
self.check_tagged_guards(&guards)?;
let signed = self.m.var(id).signed;
if let [lvalue] = lvalues.as_slice() {
self.assign(lvalue, &sv::ir::Expr::Ident(formal))?;
Expand Down Expand Up @@ -5716,6 +5769,7 @@ fn impure_call(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option<String> {
sv::ir::Callee::String(_) => continue,
// A property can change without the handle changing.
sv::ir::Callee::Object(op) => return Some(op.to_string()),
sv::ir::Callee::Checked { .. } => return Some("tagged union access".into()),
sv::ir::Callee::Subroutine(name) => name,
};
if !visited.insert(name.clone()) {
Expand Down
3 changes: 2 additions & 1 deletion crates/celox-frontend-sv/src/lowering/ff/fread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Ff<'_, '_> {
};
// Evaluate the reference once, then all value operands, then snapshot
// the referenced storage so partial reads retain argument side effects.
let (lvalues, captured) = self.capture_system_actual(destination)?;
let (lvalues, captured, guards) = self.capture_system_actual(destination)?;
let mut values = Vec::new();
for (position, arg) in args.iter().enumerate().skip(1) {
if let Some(arg) = arg {
Expand Down Expand Up @@ -84,6 +84,7 @@ impl Ff<'_, '_> {
u8::from(args.get(3).is_some_and(Option::is_some)).to_string(),
],
)?;
self.check_tagged_guards(&guards)?;
match lvalues.as_slice() {
[target] => self.assign(target, &data)?,
targets => self.assign_concat(targets, &data)?,
Expand Down
Loading
Loading