Skip to content

Commit

Permalink
refactor: remove ResolvedTarget::Cached variant
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Jul 23, 2024
1 parent 6396054 commit 6730fc3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 123 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Introduced `MastForestError` to enforce `MastForest` node count invariant (#1394)
- Added functions to `MastForestBuilder` to allow ensuring of nodes with fewer LOC (#1404)
- Make `Assembler` single-use (#1409)
- Remove `ProcedureCache` from the assembler (#1411).

#### Changed

Expand Down
2 changes: 1 addition & 1 deletion assembly/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl Assembler {
};
let resolved = self.module_graph.resolve_target(&caller, target)?;
match resolved {
ResolvedTarget::Phantom(digest) | ResolvedTarget::Cached { digest, .. } => Ok(digest),
ResolvedTarget::Phantom(digest) => Ok(digest),
ResolvedTarget::Exact { gid } | ResolvedTarget::Resolved { gid, .. } => {
Ok(mast_forest_builder
.get_procedure(gid)
Expand Down
7 changes: 0 additions & 7 deletions assembly/src/assembler/module_graph/analysis/rewrite_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ impl<'a, 'b: 'a> RewriteCheckVisitor<'a, 'b> {
Ok(ResolvedTarget::Exact { .. } | ResolvedTarget::Phantom(_)) => {
ControlFlow::Continue(())
}
Ok(ResolvedTarget::Cached { .. }) => {
if let InvocationTarget::MastRoot(_) = target {
ControlFlow::Continue(())
} else {
ControlFlow::Break(Ok(true))
}
}
}
}
}
Expand Down
35 changes: 0 additions & 35 deletions assembly/src/assembler/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ pub struct ModuleGraph {
/// The set of MAST roots which have procedure definitions in this graph. There can be
/// multiple procedures bound to the same root due to having identical code.
roots: BTreeMap<RpoDigest, SmallVec<[GlobalProcedureIndex; 1]>>,
/// The set of procedures in this graph which have known MAST roots
digests: BTreeMap<GlobalProcedureIndex, RpoDigest>,
kernel_index: Option<ModuleIndex>,
kernel: Kernel,
}
Expand Down Expand Up @@ -344,33 +342,13 @@ impl ModuleGraph {
self.modules.get(id.module.as_usize()).and_then(|m| m.get(id.index))
}

/// Fetches a [Procedure] by [RpoDigest].
///
/// NOTE: This implicitly chooses the first definition for a procedure if the same digest is
/// shared for multiple definitions.
#[allow(unused)]
pub fn get_procedure_by_digest(&self, digest: &RpoDigest) -> Option<&Procedure> {
self.roots
.get(digest)
.and_then(|indices| match self.get_procedure(indices[0])? {
Export::Procedure(ref proc) => Some(proc),
Export::Alias(_) => None,
})
}

pub fn get_procedure_index_by_digest(
&self,
digest: &RpoDigest,
) -> Option<GlobalProcedureIndex> {
self.roots.get(digest).map(|indices| indices[0])
}

/// Look up the [RpoDigest] associated with the given [GlobalProcedureIndex], if one is known
/// at this point in time.
pub fn get_mast_root(&self, id: GlobalProcedureIndex) -> Option<&RpoDigest> {
self.digests.get(&id)
}

#[allow(unused)]
pub fn callees(&self, gid: GlobalProcedureIndex) -> &[GlobalProcedureIndex] {
self.callgraph.out_edges(gid)
Expand Down Expand Up @@ -435,19 +413,6 @@ impl ModuleGraph {
}
}

match self.digests.entry(id) {
Entry::Occupied(ref entry) => {
assert_eq!(
entry.get(),
&digest,
"attempted to register the same procedure with different digests!"
);
}
Entry::Vacant(entry) => {
entry.insert(digest);
}
}

Ok(())
}

Expand Down
103 changes: 31 additions & 72 deletions assembly/src/assembler/module_graph/name_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ pub struct CallerInfo {
/// Represents the output of the [NameResolver] when it resolves a procedure name.
#[derive(Debug)]
pub enum ResolvedTarget {
/// The callee is available in the procedure cache, so we know its exact hash.
Cached {
digest: RpoDigest,
/// If the procedure was compiled from source, this is its identifier in the [ModuleGraph]
gid: Option<GlobalProcedureIndex>,
},
/// The callee was resolved to a known procedure in the [ModuleGraph]
Exact { gid: GlobalProcedureIndex },
/// The callee was resolved to a concrete procedure definition, and can be referenced as
Expand All @@ -73,7 +67,6 @@ impl ResolvedTarget {
pub fn into_global_id(self) -> Option<GlobalProcedureIndex> {
match self {
ResolvedTarget::Exact { gid } | ResolvedTarget::Resolved { gid, .. } => Some(gid),
ResolvedTarget::Cached { gid, .. } => gid,
ResolvedTarget::Phantom(_) => None,
}
}
Expand Down Expand Up @@ -140,51 +133,31 @@ impl<'a> NameResolver<'a> {
module: self.graph.kernel_index.unwrap(),
index: index.into_inner(),
};
match self.graph.get_mast_root(gid) {
Some(digest) => Ok(ResolvedTarget::Cached {
digest: *digest,
gid: Some(gid),
}),
None => Ok(ResolvedTarget::Exact { gid }),
}
Ok(ResolvedTarget::Exact { gid })
}
Some(ResolvedProcedure::Local(index)) => {
let gid = GlobalProcedureIndex {
module: caller.module,
index: index.into_inner(),
};
match self.graph.get_mast_root(gid) {
Some(digest) => Ok(ResolvedTarget::Cached {
digest: *digest,
gid: Some(gid),
}),
None => Ok(ResolvedTarget::Exact { gid }),
}
Ok(ResolvedTarget::Exact { gid })
}
Some(ResolvedProcedure::External(ref fqn)) => {
let gid = self.find(caller, fqn)?;
match self.graph.get_mast_root(gid) {
Some(digest) => Ok(ResolvedTarget::Cached {
digest: *digest,
gid: Some(gid),
}),
None => {
let path = self.module_path(gid.module);
let pending_offset = self.graph.modules.len();
let name = if gid.module.as_usize() >= pending_offset {
self.pending[gid.module.as_usize() - pending_offset]
.resolver
.get_name(gid.index)
.clone()
} else {
self.graph[gid].name().clone()
};
Ok(ResolvedTarget::Resolved {
gid,
target: InvocationTarget::AbsoluteProcedurePath { name, path },
})
}
}
let path = self.module_path(gid.module);
let pending_offset = self.graph.modules.len();
let name = if gid.module.as_usize() >= pending_offset {
self.pending[gid.module.as_usize() - pending_offset]
.resolver
.get_name(gid.index)
.clone()
} else {
self.graph[gid].name().clone()
};
Ok(ResolvedTarget::Resolved {
gid,
target: InvocationTarget::AbsoluteProcedurePath { name, path },
})
}
Some(ResolvedProcedure::MastRoot(ref digest)) => {
match self.graph.get_procedure_index_by_digest(digest) {
Expand Down Expand Up @@ -241,28 +214,20 @@ impl<'a> NameResolver<'a> {
name: name.clone(),
};
let gid = self.find(caller, &fqn)?;
match self.graph.get_mast_root(gid) {
Some(digest) => Ok(ResolvedTarget::Cached {
digest: *digest,
gid: Some(gid),
}),
None => {
let path = self.module_path(gid.module);
let pending_offset = self.graph.modules.len();
let name = if gid.module.as_usize() >= pending_offset {
self.pending[gid.module.as_usize() - pending_offset]
.resolver
.get_name(gid.index)
.clone()
} else {
self.graph[gid].name().clone()
};
Ok(ResolvedTarget::Resolved {
gid,
target: InvocationTarget::AbsoluteProcedurePath { name, path },
})
}
}
let path = self.module_path(gid.module);
let pending_offset = self.graph.modules.len();
let name = if gid.module.as_usize() >= pending_offset {
self.pending[gid.module.as_usize() - pending_offset]
.resolver
.get_name(gid.index)
.clone()
} else {
self.graph[gid].name().clone()
};
Ok(ResolvedTarget::Resolved {
gid,
target: InvocationTarget::AbsoluteProcedurePath { name, path },
})
}
None => Err(AssemblyError::UndefinedModule {
span: target.span(),
Expand All @@ -280,13 +245,7 @@ impl<'a> NameResolver<'a> {
name: name.clone(),
};
let gid = self.find(caller, &fqn)?;
match self.graph.get_mast_root(gid) {
Some(digest) => Ok(ResolvedTarget::Cached {
digest: *digest,
gid: Some(gid),
}),
None => Ok(ResolvedTarget::Exact { gid }),
}
Ok(ResolvedTarget::Exact { gid })
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions assembly/src/assembler/module_graph/rewrites/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
InvocationTarget, Invoke, InvokeKind, Module, Procedure,
},
diagnostics::SourceFile,
AssemblyError, Span, Spanned,
AssemblyError, Spanned,
};

/// A [ModuleRewriter] handles applying all of the module-wide rewrites to a [Module] that is being
Expand Down Expand Up @@ -67,13 +67,6 @@ impl<'a, 'b: 'a> ModuleRewriter<'a, 'b> {
};
match self.resolver.resolve_target(&caller, target) {
Err(err) => return ControlFlow::Break(err),
Ok(ResolvedTarget::Cached { digest, .. }) => {
*target = InvocationTarget::MastRoot(Span::new(target.span(), digest));
self.invoked.insert(Invoke {
kind,
target: target.clone(),
});
}
Ok(ResolvedTarget::Phantom(_)) => (),
Ok(ResolvedTarget::Exact { .. }) => {
self.invoked.insert(Invoke {
Expand Down

0 comments on commit 6730fc3

Please sign in to comment.