forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rebase * More tests updated for viewing * reorganize logic into Bindings struct * typos * additional tests for Common Subexpression Elimination (CSE) Co-authored-by: Philip Stoev <[email protected]>
- Loading branch information
1 parent
0a0483d
commit c793609
Showing
13 changed files
with
2,068 additions
and
472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
//! Common subexpression elimination. | ||
pub mod map; | ||
pub mod relation_cse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright Materialize, Inc. and contributors. All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
//! Identifies common relation subexpressions and places them behind `Let` bindings. | ||
//! | ||
//! All structurally equivalent expressions, defined recursively as having structurally | ||
//! equivalent inputs, and identical parameters, will be placed behind `Let` bindings. | ||
//! The resulting expressions likely have an excess of `Let` expressions, and should be | ||
//! subjected to the `InlineLet` transformation to remove those that are not necessary. | ||
use std::collections::HashMap; | ||
|
||
use expr::{Id, LocalId, MirRelationExpr}; | ||
|
||
use crate::TransformArgs; | ||
|
||
/// Identifies common relation subexpressions and places them behind `Let` bindings. | ||
#[derive(Debug)] | ||
pub struct RelationCSE; | ||
|
||
impl crate::Transform for RelationCSE { | ||
fn transform( | ||
&self, | ||
relation: &mut MirRelationExpr, | ||
_: TransformArgs, | ||
) -> Result<(), crate::TransformError> { | ||
let mut bindings = Bindings::default(); | ||
bindings.intern_expression(relation); | ||
bindings.populate_expression(relation); | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Maintains `Let` bindings in a compact, explicit representation. | ||
/// | ||
/// The `bindings` map contains neither `Let` bindings nor two structurally | ||
/// equivalent expressions. | ||
/// | ||
/// The bindings can be interpreted as an ordered sequence of let bindings, | ||
/// ordered by their identifier, that should be applied in order before the | ||
/// use of the expression from which they have been extracted. | ||
#[derive(Debug, Default)] | ||
pub struct Bindings { | ||
/// A list of let-bound expressions and their order / identifier. | ||
bindings: HashMap<MirRelationExpr, u64>, | ||
/// Mapping from conventional local `Get` identifiers to new ones. | ||
rebindings: HashMap<LocalId, LocalId>, | ||
} | ||
|
||
impl Bindings { | ||
/// Replace `relation` with an equivalent `Get` expression referencing a location in `bindings`. | ||
/// | ||
/// The algorithm performs a post-order traversal of the expression tree, binding each distinct | ||
/// expression to a new local identifier. It maintains the invariant that `bindings` contains no | ||
/// `Let` expressions, nor any two structurally equivalent expressions. | ||
/// | ||
/// Once each sub-expression is replaced by a canonical `Get` expression, each expression is also | ||
/// in a canonical representation, which is used to check for prior instances and drives re-use. | ||
fn intern_expression(&mut self, relation: &mut MirRelationExpr) { | ||
match relation { | ||
MirRelationExpr::Let { id, value, body } => { | ||
self.intern_expression(value); | ||
let new_id = if let MirRelationExpr::Get { | ||
id: Id::Local(x), .. | ||
} = **value | ||
{ | ||
x | ||
} else { | ||
panic!("Invariant violated") | ||
}; | ||
self.rebindings.insert(*id, new_id); | ||
self.intern_expression(body); | ||
let body = body.take_dangerous(); | ||
self.rebindings.remove(id); | ||
*relation = body; | ||
} | ||
MirRelationExpr::Get { id, .. } => { | ||
if let Id::Local(id) = id { | ||
*id = self.rebindings[id]; | ||
} | ||
} | ||
|
||
_ => { | ||
// All other expressions just need to apply the logic recursively. | ||
relation.visit1_mut(&mut |expr| { | ||
self.intern_expression(expr); | ||
}) | ||
} | ||
}; | ||
|
||
// This should be fast, as it depends directly on only `Get` expressions. | ||
let typ = relation.typ(); | ||
// We want to maintain the invariant that `relation` ends up as a local `Get`. | ||
if let MirRelationExpr::Get { | ||
id: Id::Local(_), .. | ||
} = relation | ||
{ | ||
// Do nothing, as the expression is already a local `Get` expression. | ||
} else { | ||
// Either find an instance of `relation` or insert this one. | ||
let bindings_len = self.bindings.len() as u64; | ||
let id = self | ||
.bindings | ||
.entry(relation.take_dangerous()) | ||
.or_insert(bindings_len); | ||
*relation = MirRelationExpr::Get { | ||
id: Id::Local(LocalId::new(*id)), | ||
typ, | ||
} | ||
} | ||
} | ||
|
||
/// Populates `expression` with necessary `Let` bindings. | ||
/// | ||
/// This population may result in substantially more `Let` bindings that one | ||
/// might expect. It is very appropriate to run the `InlineLet` transformation | ||
/// afterwards to remove `Let` bindings that it deems unhelpful. | ||
fn populate_expression(self, expression: &mut MirRelationExpr) { | ||
// Convert the bindings in to a sequence, by the local identifier. | ||
let mut bindings = self.bindings.into_iter().collect::<Vec<_>>(); | ||
bindings.sort_by_key(|(_, i)| *i); | ||
|
||
for (value, index) in bindings.into_iter().rev() { | ||
let new_expression = MirRelationExpr::Let { | ||
id: LocalId::new(index), | ||
value: Box::new(value), | ||
body: Box::new(expression.take_dangerous()), | ||
}; | ||
*expression = new_expression; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.