Skip to content

Commit

Permalink
Fixes bundle-elim leaving references to deleted parameters (#463)
Browse files Browse the repository at this point in the history
* fix bundle_elim bugs

* remove unused import
  • Loading branch information
UnsignedByte authored Oct 1, 2024
1 parent 1f95254 commit 43e0a10
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/filament/src/ir_passes/bundle_elim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use fil_ir::{
self as ir, Access, AddCtx, Bind, Command, Component, Connect, Ctx,
DenseIndexInfo, DisplayCtx, Expr, Foreign, Info, InvIdx, Invoke, Liveness,
MutCtx, Port, PortIdx, PortOwner, Range, Subst, Time,
MutCtx, Port, PortIdx, PortOwner, Range, SparseInfoMap, Subst, Time,
};
use fil_utils as utils;
use itertools::Itertools;
Expand All @@ -17,7 +17,7 @@ pub type PortInfo =
// Eliminates bundle ports by breaking them into multiple len-1 ports, and eliminates local ports altogether.
pub struct BundleElim {
/// Mapping from component to the map from signature bundle port to generated port.
context: DenseIndexInfo<Component, HashMap<PortIdx, PortInfo>>,
context: DenseIndexInfo<Component, SparseInfoMap<Port, PortInfo>>,
/// Mapping from index into a dst port to an index of the src port.
local_map: HashMap<
(PortIdx, /*idxs=*/ Vec<usize>),
Expand Down Expand Up @@ -54,7 +54,7 @@ impl BundleElim {
None => break group,
}
};
let (lens, sig_ports) = &comp_info[port];
let (lens, sig_ports) = &comp_info[*port];
ports.push(sig_ports[utils::flat_idx(idxs, lens)])
}

Expand Down Expand Up @@ -118,7 +118,7 @@ impl BundleElim {

// creates a new liveness with the new start and end times and length one
let live = Liveness {
idxs: vec![idxs[0]], // this should technically be some null parameter, as it will refer to a deleted parameter now.
idxs: vec![],
lens: vec![one],
range: Range { start, end },
};
Expand All @@ -136,7 +136,7 @@ impl BundleElim {
base: Foreign::new(
// maps the foreign to the corresponding single port
// this works because all signature ports are compiled first.
self.context[owner][&key].1[i],
self.context[owner][key].1[i],
owner,
),
}
Expand Down Expand Up @@ -165,7 +165,7 @@ impl BundleElim {
}

/// Compiles the signature of a component and adds the new ports to the context mapping.
fn sig(&self, comp: &mut Component) -> HashMap<PortIdx, PortInfo> {
fn sig(&self, comp: &mut Component) -> SparseInfoMap<Port, PortInfo> {
// loop through signature ports and compile them
comp.ports()
.idx_iter()
Expand All @@ -181,7 +181,7 @@ impl BundleElim {
&self,
idx: InvIdx,
comp: &mut Component,
) -> HashMap<PortIdx, PortInfo> {
) -> SparseInfoMap<Port, PortInfo> {
let Invoke { ports, .. } = comp.get_mut(idx);
// first take all the old ports and split them up
let mappings = std::mem::take(ports)
Expand Down Expand Up @@ -230,7 +230,7 @@ impl Visitor for BundleElim {
) -> Action {
let Connect { src, dst, .. } = connect;

if !self.context.get(data.idx).contains_key(&dst.port) {
if !self.context.get(data.idx).contains(dst.port) {
// we are writing to a local port here.
return Action::Change(vec![]);
}
Expand Down
17 changes: 17 additions & 0 deletions crates/ir/src/utils/sparse_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ where
pub fn iter(&self) -> impl Iterator<Item = (Key, &Info)> + '_ {
self.map.iter().map(|(idx, val)| (*idx, val))
}

/// Extend the map with the values from another map.
pub fn extend(&mut self, other: Self) {
self.map.extend(other.map);
}
}

impl<Assoc, Info, Key> Default for SparseInfoMap<Assoc, Info, Key>
Expand Down Expand Up @@ -98,6 +103,18 @@ impl<T, V, Idx: IdxLike<T>> FromIterator<(Idx, V)>
}
}

impl<Assoc, Info, Key> IntoIterator for SparseInfoMap<Assoc, Info, Key>
where
Key: IdxLike<Assoc>,
{
type Item = (Key, Info);
type IntoIter = std::collections::hash_map::IntoIter<Key, Info>;

fn into_iter(self) -> Self::IntoIter {
self.map.into_iter()
}
}

impl<T, V, Idx: IdxLike<T>> std::ops::Index<Idx> for SparseInfoMap<T, V, Idx> {
type Output = V;

Expand Down

0 comments on commit 43e0a10

Please sign in to comment.