Skip to content

Commit

Permalink
Tidy up, move and improve some comments.
Browse files Browse the repository at this point in the history
Don't take into account rwts.dref in pre reduce until #73 is merged.
  • Loading branch information
FranchuFranchu committed Mar 5, 2024
1 parent bac1d6f commit bb22e55
Show file tree
Hide file tree
Showing 35 changed files with 91 additions and 83 deletions.
6 changes: 4 additions & 2 deletions src/host/encode_def.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::util::maybe_grow;

use super::*;

/// Converts an ast net to a list of instructions to create the net.
Expand Down Expand Up @@ -79,7 +81,7 @@ impl<'a, F: FnMut(&str) -> Port> State<'a, F> {
self.visit_tree(tree, trg);
}
fn visit_tree(&mut self, tree: &'a Tree, trg: TrgId) {
match tree {
maybe_grow(move || match tree {
Tree::Era => {
self.instr.push(Instruction::LinkConst { trg, port: Port::ERA });
>>>>>>> cc29e4c ([sc-484] Optimize pre-reduce pass)
Expand Down Expand Up @@ -135,7 +137,7 @@ impl<'a, F: FnMut(&str) -> Port> State<'a, F> {
self.visit_tree(sel, l);
self.visit_tree(ret, r);
}
}
})
}
>>>>>>> cc29e4c ([sc-484] Optimize pre-reduce pass)
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
compile_executable(output, &host.lock().unwrap()).unwrap();
}
CliMode::Run { run_opts, mut transform_opts, file, args } => {
// Don't pre-reduce the main reduction
// Don't pre-reduce the entry point
transform_opts.pre_reduce_skip.push(args.entry_point.clone());
let host = create_host(&load_book(&[file], &transform_opts));
run(&host.lock().unwrap(), run_opts, args);
Expand Down Expand Up @@ -285,6 +285,7 @@ fn load_book(files: &[String], transform_opts: &TransformOpts) -> Book {
acc
});
let transform_passes = TransformPass::passes_from_cli(&transform_opts.transform_passes);

if transform_passes.pre_reduce {
book.pre_reduce(
&|x| transform_opts.pre_reduce_skip.iter().any(|y| x == y),
Expand Down
4 changes: 2 additions & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ impl AtomicRewrites {
}
}

impl<T: Add<Output = U>, U> Add for Rewrites<T> {
type Output = Rewrites<U>;
impl<T: Add> Add for Rewrites<T> {
type Output = Rewrites<T::Output>;

fn add(self, rhs: Self) -> Self::Output {
Rewrites {
Expand Down
33 changes: 17 additions & 16 deletions src/transform/pre_reduce.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
//! Reduce the solving any annihilations and commutations.
//! Reduce the nets in the book, to avoid repeating work at runtime.
//!
//! Here's how it works:
//! - Each definition is visited in topological order (dependencies before
//! dependents). In the case of cycles, one will be arbitrarily selected to be
//! first.
//! - The definition is reduced in a [`run::Net`]
//! - The reduced [`run::Net`] is readback into an [`ast::Net`]
//! - The [`ast::Net`] is encoded into a [`Vec<Instruction>`]
//! - The [`ast::Net`] is stored in the [`State`], as it will be used later.
//! - The [`InterpretedDef`] corresponding to the definition is mutated in-place
//! and its instructions are replaced with the generated [`Vec<Instruction>`]
//!
//! At the end, each mutated [`ast::Net`] is placed into the [`Book`],
//! overriding the previous one.
use std::{
collections::HashMap,
Expand Down Expand Up @@ -77,20 +91,6 @@ impl run::AsDef for InertDef {
}

/// State of the pre-reduction algorithm.
///
/// Here's how it works:
/// - Each definition is visited in topological order (dependencies before
/// dependents). In the case of cycles, one will be arbitrarily selected to be
/// first.
/// - The definition is reduced in a [`run::Net`]
/// - The reduced [`run::Net`] is readback into an [`ast::Net`]
/// - The [`ast::Net`] is encoded into a [`Vec<Instruction>`]
/// - The [`ast::Net`] is stored in the [`State`], as it will be used later.
/// - The [`InterpretedDef`] corresponding to the definition is mutated in-place
/// and its instructions are replaced with the generated [`Vec<Instruction>`]
///
/// At the end, each mutated [`ast::Net`] is placed into the [`Book`],
/// overriding the previous one.
struct State<'a> {
book: &'a Book,

Expand Down Expand Up @@ -131,6 +131,7 @@ impl<'a> State<'a> {
if self.seen.contains_key(nam) || (self.skip)(nam) || self.book.get(nam).is_none() {
return;
}

self.seen.insert(nam.to_owned(), SeenState::Cycled);
// First, pre-reduce all nets referenced by this net by walking the tree
self.visit_net(self.book.get(nam).unwrap());
Expand All @@ -143,7 +144,7 @@ impl<'a> State<'a> {
self.rewrites += rt.rwts;

// Move interactions with inert defs back into the net redexes array
rt.redexes.extend(core::mem::take::<Vec<_>>(self.captured_redexes.lock().unwrap().as_mut()).into_iter());
rt.redexes.extend(self.captured_redexes.lock().unwrap().drain(..));

let net = self.host.readback(&mut rt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 20_495
- DREF : 43
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 114
- DREF : 40
- OPER : 0
run:
RWTS : 2
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 2
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 12
- DREF : 8
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@chained_ops.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 47
- DREF : 1
- OPER : 40
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 44
- DREF : 18
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@church_exp.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 857
- DREF : 76
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@church_mul.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 50
- DREF : 6
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@dec_bits.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 71_716
- DREF : 10_301
- OPER : 0
run:
RWTS : 2
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 2
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@dec_bits_tree.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 18_916
- DREF : 2_763
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 53_148
- DREF : 55
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 805
- DREF : 213
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@list_put_got.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 774
- DREF : 657
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 2
- DREF : 2
- OPER : 0
run:
RWTS : 7
RWTS : 3
- ANNI : 2
- COMM : 0
- ERAS : 1
- DREF : 4
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 7
- DREF : 3
- OPER : 2
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 8
- DREF : 3
- OPER : 1
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/tests__pre_reduce_run@neg_fusion.hvmc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 160
- DREF : 39
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
4 changes: 2 additions & 2 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 63
- DREF : 19
- OPER : 0
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 1_623_642
- DREF : 209_486
- OPER : 151_031
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 454
- DREF : 43
- OPER : 25
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 1_167_065_250
- DREF : 171_966_556
- OPER : 92_274_713
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 503_316_491
- DREF : 50_331_667
- OPER : 33_554_431
run:
RWTS : 2
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 2
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 34_000_727
- DREF : 5_000_075
- OPER : 9_000_256
run:
RWTS : 1
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 1
- DREF : 0
- OPER : 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RWTS : 134_217_925
- DREF : 16_777_258
- OPER : 0
run:
RWTS : 2
RWTS : 0
- ANNI : 0
- COMM : 0
- ERAS : 0
- DREF : 2
- DREF : 0
- OPER : 0
Loading

0 comments on commit bb22e55

Please sign in to comment.