-
Notifications
You must be signed in to change notification settings - Fork 7
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
fix!: OpDef serialisation #1013
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -122,7 +122,7 @@ pub struct CustomValidator { | |
#[serde(flatten)] | ||
poly_func: PolyFuncType, | ||
#[serde(skip)] | ||
validate: Box<dyn ValidateTypeArgs>, | ||
pub(crate) validate: Box<dyn ValidateTypeArgs>, | ||
} | ||
|
||
impl CustomValidator { | ||
|
@@ -265,11 +265,19 @@ impl Debug for SignatureFunc { | |
/// Different ways that an [OpDef] can lower operation nodes i.e. provide a Hugr | ||
/// that implements the operation using a set of other extensions. | ||
#[derive(serde::Deserialize, serde::Serialize)] | ||
#[serde(untagged)] | ||
pub enum LowerFunc { | ||
/// Lowering to a fixed Hugr. Since this cannot depend upon the [TypeArg]s, | ||
/// this will generally only be applicable if the [OpDef] has no [TypeParam]s. | ||
#[serde(rename = "hugr")] | ||
FixedHugr(ExtensionSet, Hugr), | ||
FixedHugr { | ||
/// The extensions required by the [`Hugr`] | ||
extensions: ExtensionSet, | ||
/// The [`Hugr`] to be used to replace [CustomOp]s matching the parent | ||
/// [OpDef] | ||
/// | ||
/// [CustomOp]: crate::ops::CustomOp | ||
hugr: Hugr, | ||
}, | ||
/// Custom binary function that can (fallibly) compute a Hugr | ||
/// for the particular instance and set of available extensions. | ||
#[serde(skip)] | ||
|
@@ -279,7 +287,7 @@ pub enum LowerFunc { | |
impl Debug for LowerFunc { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::FixedHugr(_, _) => write!(f, "FixedHugr"), | ||
Self::FixedHugr { .. } => write!(f, "FixedHugr"), | ||
Self::CustomFunc(_) => write!(f, "<custom lower>"), | ||
} | ||
} | ||
|
@@ -305,8 +313,7 @@ pub struct OpDef { | |
signature_func: SignatureFunc, | ||
// Some operations cannot lower themselves and tools that do not understand them | ||
// can only treat them as opaque/black-box ops. | ||
#[serde(flatten)] | ||
lower_funcs: Vec<LowerFunc>, | ||
pub(crate) lower_funcs: Vec<LowerFunc>, | ||
|
||
/// Operations can optionally implement [`ConstFold`] to implement constant folding. | ||
#[serde(skip)] | ||
|
@@ -360,9 +367,9 @@ impl OpDef { | |
self.lower_funcs | ||
.iter() | ||
.flat_map(|f| match f { | ||
LowerFunc::FixedHugr(req_res, h) => { | ||
if available_extensions.is_superset(req_res) { | ||
Some(h.clone()) | ||
LowerFunc::FixedHugr { extensions, hugr } => { | ||
if available_extensions.is_superset(extensions) { | ||
Some(hugr.clone()) | ||
} else { | ||
None | ||
} | ||
|
@@ -495,7 +502,10 @@ mod test { | |
let type_scheme = PolyFuncType::new(vec![TP], FunctionType::new_endo(vec![list_of_var])); | ||
|
||
let def = e.add_op(OP_NAME, "desc".into(), type_scheme)?; | ||
def.add_lower_func(LowerFunc::FixedHugr(ExtensionSet::new(), Hugr::default())); | ||
def.add_lower_func(LowerFunc::FixedHugr { | ||
extensions: ExtensionSet::new(), | ||
hugr: Hugr::default(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is the test I should look at - no this doesn't look good! This is a Hugr that we can splice in in place of an Op (Node), so should be a DFG-rooted thing with the same inputs/outputs as specified in the OpDef's type_scheme.... |
||
}); | ||
def.add_misc("key", Default::default()); | ||
assert_eq!(def.description(), "desc"); | ||
assert_eq!(def.lower_funcs.len(), 1); | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmm, perhaps to think about in the future, but if we insist that the
Hugr
is DFG-rooted, then we can extract the ExtensionSet from the root node'sextension_reqs
, so I'm not sure we really need this...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, but let's leave until later.