-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sc-481] Add transformation passes to hvm-core.
- Add `transform` subcommand to `hvm-core`. - Add `pre-reduce`-related options. - Add `Net::init_heap_bytes` method.
- Loading branch information
1 parent
0ba064c
commit 18d2ff8
Showing
7 changed files
with
272 additions
and
58 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
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 @@ | ||
pub mod pre_reduce; |
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,63 @@ | ||
// Reduce the compiled networks, solving any annihilations and commutations. | ||
|
||
use std::sync::Mutex; | ||
|
||
use crate::{ast::Book, host::Host, run, run::Def}; | ||
|
||
/// A Def that pushes all interactions to its inner Vec. | ||
struct InertDef(Mutex<Vec<(run::Port, run::Port)>>); | ||
|
||
impl run::AsDef for InertDef { | ||
unsafe fn call<M: run::Mode>(def: *const run::Def<Self>, _: &mut run::Net<M>, port: run::Port) { | ||
let def = unsafe { &*def }; | ||
def.data.0.lock().unwrap().push((run::Port::new_ref(def), port)); | ||
} | ||
} | ||
|
||
impl Book { | ||
/// Reduces the definitions in the book individually, except for the skipped | ||
/// ones. | ||
/// | ||
/// Defs in `dont_interact` are skipped and treated as inert defs. | ||
pub fn pre_reduce( | ||
&mut self, | ||
skip: &dyn Fn(&str) -> bool, | ||
dont_interact: &mut dyn Iterator<Item = &str>, | ||
max_memory: usize, | ||
max_rwts: u64, | ||
) -> Result<(), String> { | ||
// Create a host | ||
// with inert definitions in the place | ||
// of core builtins, to prevent them from being reduced | ||
let mut host = Host::default(); | ||
for builtin in dont_interact { | ||
let def = InertDef(Default::default()); | ||
host.insert_def(builtin, crate::host::DefRef::Owned(Box::new(Def::new(run::LabSet::ALL, def)))); | ||
} | ||
host.insert_book(self); | ||
let area = run::Net::<run::Strict>::init_heap_bytes(max_memory); | ||
|
||
for (nam, net) in self.nets.iter_mut() { | ||
// Skip unnecessary work | ||
if net.rdex.is_empty() || skip(nam) { | ||
continue; | ||
} | ||
|
||
let mut rt = run::Net::<run::Strict>::new(&area); | ||
rt.boot(host.defs.get(nam).expect("No function.")); | ||
rt.expand(); | ||
rt.reduce(max_rwts as usize); | ||
|
||
// Move interactions with inert defs back into the net rdex array | ||
for def in host.defs.values() { | ||
if let Some(def) = def.downcast_ref::<InertDef>() { | ||
let mut stored_redexes = def.data.0.lock().unwrap(); | ||
rt.rdex.extend(core::mem::take(&mut *stored_redexes)); | ||
} | ||
} | ||
// Place the reduced net back into the def map | ||
*net = host.readback(&mut rt); | ||
} | ||
Ok(()) | ||
} | ||
} |
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.