Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add let = ? binding for temporary Filament automation frontend #461

Merged
merged 2 commits into from
Sep 20, 2024
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
2 changes: 1 addition & 1 deletion crates/ast/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl Bundle {
pub struct ParamLet {
pub name: Loc<Id>,
/// The expression for the parameter binding
pub expr: Expr,
pub expr: Option<Expr>,
}

#[derive(Clone)]
Expand Down
3 changes: 2 additions & 1 deletion crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ impl FilamentParser {
fn param_let(input: Node) -> ParseResult<ast::ParamLet> {
Ok(match_nodes!(
input.into_children();
[param_var(name), expr(expr)] => ast::ParamLet { name, expr: expr.take() }
[param_var(name), expr(expr)] => ast::ParamLet { name, expr: Some(expr.take()) },
[param_var(name)] => ast::ParamLet { name, expr: None }
))
}

Expand Down
1 change: 1 addition & 0 deletions crates/ast/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ if_stmt = {
// ===== let-bound parameters ========
param_let = {
"let" ~ param_var ~ "=" ~ expr ~ ";"
| "let" ~ param_var ~ "=" ~ "?" ~ ";"
}

// ====== Loops ==========
Expand Down
20 changes: 11 additions & 9 deletions crates/filament/src/ir_passes/build_domination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ impl BuildDomination {
for (id, cmd) in cmds.iter().enumerate() {
match cmd {
ir::Command::Let(ir::Let { expr, param }) => {
for idx in expr.relevant_vars(comp) {
log::trace!(
"param {} is used by {}",
comp.display(idx),
comp.display(*param)
);
if let Some(pid) = param_map.get(&idx) {
// Add a dependency from the let to the parameter owner, if it is defined in this scope
topo.add_dependency(*pid, id);
if let Some(expr) = expr {
for idx in expr.relevant_vars(comp) {
log::trace!(
"param {} is used by {}",
comp.display(idx),
comp.display(*param)
);
if let Some(pid) = param_map.get(&idx) {
// Add a dependency from the let to the parameter owner, if it is defined in this scope
topo.add_dependency(*pid, id);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/filament/src/ir_passes/discharge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ impl Visitor for Discharge {

// Assert bindings for all let-bound parameters
for (idx, p) in data.comp.params().iter() {
let ir::ParamOwner::Let { bind } = &p.owner else {
let ir::ParamOwner::Let { bind: Some(bind) } = &p.owner else {
continue;
};
let param_s = self.param_map[idx];
Expand Down
9 changes: 8 additions & 1 deletion crates/filament/src/ir_passes/mono/monodeferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,14 @@ impl MonoDeferred<'_, '_> {
let p = param.ul();
let e = self
.monosig
.expr(&self.underlying, expr.ul(), self.pass)
.expr(
&self.underlying,
expr.unwrap_or_else(|| {
unreachable!("Let binding was bound to `?`")
})
.ul(),
self.pass,
)
.get();
let Some(v) = e.as_concrete(self.monosig.base.comp()) else {
unreachable!(
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,5 @@ pub struct Let {
/// The parameter
pub param: ParamIdx,
/// The binding for the parameter
pub expr: ExprIdx,
pub expr: Option<ExprIdx>,
}
13 changes: 10 additions & 3 deletions crates/ir/src/from_ast/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ impl<'prog> BuildCtx<'prog> {
ast::Command::ParamLet(ast::ParamLet { name, expr }) => {
// Declare the parameter since it may be used in instance or
// invocation definitions.
let bind = self.expr(expr.clone())?;
let owner = ir::ParamOwner::Let { bind };
let owner = ir::ParamOwner::Let {
bind: match expr {
Some(expr) => Some(self.expr(expr.clone())?),
None => None,
},
};
self.param(name.clone(), owner);
Ok(())
}
Expand Down Expand Up @@ -979,7 +983,10 @@ impl<'prog> BuildCtx<'prog> {
"let-bound parameter was rewritten to expression"
)
};
let expr = self.expr(expr)?;
let expr = match expr {
Some(e) => Some(self.expr(e)?),
None => None,
};
// The declare phase already added the rewrite for this binding
vec![ir::Let { param, expr }.into()]
}
Expand Down
5 changes: 4 additions & 1 deletion crates/ir/src/printer/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ impl<'a, 'b> Printer<'a, 'b> {
write!(f, "{:indent$}let ", "")?;
self.comp.write(*param, f)?;
write!(f, " = ")?;
self.comp.write(*expr, f)
match expr {
Some(expr) => self.comp.write(*expr, f),
None => write!(f, "?"),
}
}
ir::Command::ForLoop(ir::Loop {
index,
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub enum ParamOwner {
/// A `let`-bound variable
Let {
/// The value of the parameter in its corresponding binding
bind: ExprIdx,
bind: Option<ExprIdx>,
},
/// Owned by an `exists` binding
Exists {
Expand Down
10 changes: 8 additions & 2 deletions crates/ir/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,14 @@ impl<'a> Validate<'a> {
self.comp.internal_error(format!(
"let binding for `{}' binds it to {} but the owner defines it to {}",
self.comp.display(*param),
self.comp.display(*expr),
self.comp.display(*bind)
match expr {
Some(expr) => self.comp.display(*expr),
None => "?".to_string(),
},
match bind {
Some(bind) => self.comp.display(*bind),
None => "?".to_string(),
}
))
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/errors/binding/let-unbound.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---CODE---
101
---STDERR---
thread 'main' panicked at crates/filament/src/ir_passes/mono/monodeferred.rs:348:29:
internal error: entered unreachable code: Let binding was bound to `?`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
3 changes: 3 additions & 0 deletions tests/errors/binding/let-unbound.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
comp main<'G: 1>() -> () {
let value = ?;
}
Loading