Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to simplify and change register count #175

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.3.4 (unreleased)
- Add `GenericVmFunction::simplify_with` to simultaneously simplify a function
and pick a new register count for the resulting tape
- Add bidirectional conversions between `JitFunction` and `GenericVmFunction`
(exposing the inner data member)

# 0.3.3
- `Function` and evaluator types now produce multiple outputs
- `MathFunction::new` now takes a slice of nodes, instead of a single node
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fidget/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fidget"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
license = "MPL-2.0"
repository = "https://github.com/mkeeter/fidget"
Expand Down
14 changes: 12 additions & 2 deletions fidget/src/core/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ impl<const N: usize> GenericVmFunction<N> {
pub fn choice_count(&self) -> usize {
self.0.choice_count()
}

/// Simplifies the function with the given trace and a new register count
pub fn simplify_with<const M: usize>(
&self,
trace: &VmTrace,
storage: VmData<M>,
workspace: &mut VmWorkspace<M>,
) -> Result<GenericVmFunction<M>, Error> {
let d = self.0.simplify::<M>(trace.as_slice(), workspace, storage)?;
Ok(GenericVmFunction(Arc::new(d)))
}
}

impl<const N: usize> Function for GenericVmFunction<N> {
Expand Down Expand Up @@ -166,8 +177,7 @@ impl<const N: usize> Function for GenericVmFunction<N> {
storage: Self::Storage,
workspace: &mut Self::Workspace,
) -> Result<Self, Error> {
let d = self.0.simplify(trace.as_slice(), workspace, storage)?;
Ok(Self(Arc::new(d)))
self.simplify_with(trace, storage, workspace)
}

fn recycle(self) -> Option<Self::Storage> {
Expand Down
24 changes: 18 additions & 6 deletions fidget/src/jit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,24 @@ impl RenderHints for JitFunction {
}
}

impl MathFunction for JitFunction {
fn new(ctx: &Context, nodes: &[Node]) -> Result<Self, Error> {
GenericVmFunction::new(ctx, nodes).map(JitFunction)
}
}

impl From<GenericVmFunction<REGISTER_LIMIT>> for JitFunction {
fn from(v: GenericVmFunction<REGISTER_LIMIT>) -> Self {
Self(v)
}
}

impl<'a> From<&'a JitFunction> for &'a GenericVmFunction<REGISTER_LIMIT> {
fn from(v: &'a JitFunction) -> Self {
&v.0
}
}

////////////////////////////////////////////////////////////////////////////////

// Selects the calling convention based on platform; this is forward-looking for
Expand Down Expand Up @@ -1278,12 +1296,6 @@ impl BulkEvaluator for JitGradSliceEval {
}
}

impl MathFunction for JitFunction {
fn new(ctx: &Context, nodes: &[Node]) -> Result<Self, Error> {
GenericVmFunction::new(ctx, nodes).map(JitFunction)
}
}

/// A [`Shape`](crate::shape::Shape) which uses the JIT evaluator
pub type JitShape = crate::shape::Shape<JitFunction>;

Expand Down