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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ num-bigint = { version = "0.5", features = ["serde"] }
num-traits = "0.2.19"
rand = { version = "0.10", default-features = false }
rand_pcg = "0.10"
regex = "1.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tempfile = "3.26"
Expand Down
54 changes: 42 additions & 12 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ fn kernel_window_writes(
foreign_writes: &HashMap<String, hierarchy::ForeignWrites>,
) -> HashSet<String> {
let subroutines = module.subroutines();
let scan = DriverScan::of(module);
let mut names = HashSet::default();
for process in module
.initial_processes()
Expand All @@ -1387,7 +1388,7 @@ fn kernel_window_writes(
.unwrap_or(body.len())
};
kernel_written_names(
subroutines,
&scan,
&body[first..],
&mut names,
&mut HashSet::default(),
Expand Down Expand Up @@ -1435,7 +1436,7 @@ fn stmt_may_suspend(subroutines: &[sv::ir::Subroutine], stmt: &sv::ir::Stmt) ->
/// Adds the names of the variables `body` writes, directly or in the
/// subroutines it calls, to `names` (see [`DriverScan`]).
fn kernel_written_names(
subroutines: &[sv::ir::Subroutine],
scan: &DriverScan<'_>,
body: &[sv::ir::Stmt],
names: &mut HashSet<String>,
visited: &mut HashSet<String>,
Expand Down Expand Up @@ -1488,7 +1489,16 @@ fn kernel_written_names(
| sv::ir::Callee::Stream(_) => continue,
sv::ir::Callee::Subroutine(name) => name,
};
let Some(subroutine) = subroutines
if let Some(import) = scan.dpi_import(&name) {
names.extend(
procedural::written_dpi_arguments(import, &args)
.iter()
.map(|lvalue| lvalue.name().to_string()),
);
continue;
}
let Some(subroutine) = scan
.subroutines
.iter()
.find(|subroutine| subroutine.name == name)
else {
Expand All @@ -1503,7 +1513,7 @@ fn kernel_written_names(
}
}
if visited.insert(name) {
kernel_written_names(subroutines, &subroutine.body, names, visited, depth + 1);
kernel_written_names(scan, &subroutine.body, names, visited, depth + 1);
}
}
}
Expand Down Expand Up @@ -1644,9 +1654,21 @@ fn validate_variable_driver_ranges(
/// the calling process (IEEE 1800-2023 9.2.2.2).
struct DriverScan<'a> {
subroutines: &'a [sv::ir::Subroutine],
dpi_imports: &'a [sv::ir::DpiImport],
}

impl DriverScan<'_> {
impl<'a> DriverScan<'a> {
fn of(module: &'a sv::ir::Module) -> Self {
Self {
subroutines: module.subroutines(),
dpi_imports: module.dpi_imports(),
}
}

fn dpi_import(&self, name: &str) -> Option<&sv::ir::DpiImport> {
self.dpi_imports.iter().find(|import| import.name() == name)
}

/// Calls `record` with each assignment target of `body`.
fn body(&self, record: &mut dyn FnMut(&sv::ir::LValue), body: &[sv::ir::Stmt]) {
self.body_at(record, body, &mut HashSet::default(), 0);
Expand Down Expand Up @@ -1694,6 +1716,12 @@ impl DriverScan<'_> {
| sv::ir::Callee::Stream(_) => continue,
sv::ir::Callee::Subroutine(name) => name,
};
if let Some(import) = self.dpi_import(&name) {
for lvalue in procedural::written_dpi_arguments(import, &args) {
record(&lvalue);
}
continue;
}
let Some(subroutine) = self
.subroutines
.iter()
Expand Down Expand Up @@ -1822,9 +1850,7 @@ impl ModuleDrivers {
// run-time element.
let mut shapes: HashMap<String, Option<(usize, usize)>> = HashMap::default();
let mut writes: HashMap<String, Vec<DriverWrite>> = HashMap::default();
let scan = DriverScan {
subroutines: module.subroutines(),
};
let scan = DriverScan::of(module);
let mut walk = |body: &[sv::ir::Stmt],
local: Option<usize>,
continuous: Option<usize>,
Expand Down Expand Up @@ -3282,9 +3308,7 @@ fn demote_level_lists(
else {
continue;
};
let scan = DriverScan {
subroutines: lowered.source.subroutines(),
};
let scan = DriverScan::of(&lowered.source);
for process in lowered.source.comb_processes() {
let creation_order = process.creation_order();
if process.kind() != sv::ir::CombProcessKind::AlwaysStar
Expand Down Expand Up @@ -4065,7 +4089,13 @@ fn lower_module_with_overrides(
}
}

if !cfg!(feature = "dpi") && !module.dpi_imports().is_empty() {
// The functions Celox provides need no C library.
if !cfg!(feature = "dpi")
&& module
.dpi_imports()
.iter()
.any(|import| import.builtin().is_none())
{
return Err(sv::AnalyzerError::Unsupported(
"DPI-C import (enable the `sv-dpi` feature of `celox`)".to_string(),
));
Expand Down
16 changes: 16 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! four-state semantics identical to the combinational lowering.

mod containers;
mod dpi_builtins;
mod file_io;
mod format;
mod fread;
Expand Down Expand Up @@ -941,6 +942,12 @@ impl<'p, 'a> Ff<'p, 'a> {
| sv::ir::Callee::Stream(_) => continue,
sv::ir::Callee::Subroutine(name) => name,
};
if let Some(import) = self.m.dpi_imports.get(&name) {
for lvalue in written_dpi_arguments(import, &args) {
self.record_target(&lvalue, true);
}
continue;
}
let Some(subroutine) = self.m.subroutine(&name).cloned() else {
continue;
};
Expand Down Expand Up @@ -1825,6 +1832,10 @@ impl<'p, 'a> Ff<'p, 'a> {
r#type.is_signed(),
r#type.is_4state(),
);
if r#type == sv::ir::DpiType::String {
self.m.variables.get_mut(&temp).unwrap().value_kind =
celox_design::ValueKind::StrRef;
}
self.store(temp, SIROffset::Static(0), r#type.width(), result);
Expr::Ident(temp_name)
}
Expand Down Expand Up @@ -2005,6 +2016,11 @@ impl<'p, 'a> Ff<'p, 'a> {
import.arguments().len()
)));
}
if let Some(builtin) = import.builtin() {
// Keeps a process whose only effect is the call (see `extern_calls`).
self.m.extern_calls += 1;
return self.builtin_dpi_call(import, builtin, args, arg_order);
Comment thread
tignear marked this conversation as resolved.
Resolved
}
let mut registers = vec![None; args.len()];
let positions: Vec<_> = if arg_order.is_empty() {
(0..args.len()).collect()
Expand Down
Loading
Loading