-
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
feat: Builder and HugrMut add_op_xxx default to open extensions #622
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
51f3a02
add_op_with_parent uses open_extensions; some test fixes
acl-cqc 15074dc
Use open_extensions in other add_op methods too
acl-cqc c92a5e2
Fix simple_alias: define unconstrained Metas as variables
acl-cqc 584e7f2
Fix most validate tests by doing update_validate
acl-cqc b5c6ffb
Fix cfg_children_restrictions
acl-cqc 85f2321
Lift m_tgt invariant
acl-cqc 46e567c
Change some callers of add_node_before to use add_op_before
acl-cqc ed4afa1
Remove add_node_before(?!)
acl-cqc 9bb1b49
No, keep add_node_before, maintain the parallel; remove TODO
acl-cqc bb79165
Change many add_node+open_extensions to use add_op
acl-cqc 9234154
Fix cfg_children_restrictions test in a different way (instantiate_ex…
acl-cqc 59eb260
Better fix to gen_constraints. Fix children_restrictions other way to…
acl-cqc dde1ff7
Simpler solution! - add_solution w/ empty ExtensionSet
acl-cqc 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 |
---|---|---|
|
@@ -317,6 +317,12 @@ impl UnificationContext { | |
m_output, | ||
node_type.op_signature().extension_reqs, | ||
); | ||
if matches!( | ||
node_type.tag(), | ||
OpTag::Alias | OpTag::Function | OpTag::FuncDefn | ||
) { | ||
self.add_solution(m_input, ExtensionSet::new()); | ||
} | ||
} | ||
// We have a solution for everything! | ||
Some(sig) => { | ||
|
@@ -338,16 +344,16 @@ impl UnificationContext { | |
| Some(EdgeKind::ControlFlow) | ||
) | ||
}) { | ||
let m_tgt = *self | ||
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. This is a driveby lifting of a loop invariant, nothing more |
||
.extensions | ||
.get(&(tgt_node, Direction::Incoming)) | ||
.unwrap(); | ||
for (src_node, _) in hugr.linked_ports(tgt_node, port) { | ||
let m_src = self | ||
.extensions | ||
.get(&(src_node, Direction::Outgoing)) | ||
.unwrap(); | ||
let m_tgt = self | ||
.extensions | ||
.get(&(tgt_node, Direction::Incoming)) | ||
.unwrap(); | ||
self.add_constraint(*m_src, Constraint::Equal(*m_tgt)); | ||
self.add_constraint(*m_src, Constraint::Equal(m_tgt)); | ||
} | ||
} | ||
} | ||
|
@@ -727,11 +733,11 @@ mod test { | |
let root_node = NodeType::open_extensions(op); | ||
let mut hugr = Hugr::new(root_node); | ||
|
||
let input = NodeType::open_extensions(ops::Input::new(type_row![NAT, NAT])); | ||
let output = NodeType::open_extensions(ops::Output::new(type_row![NAT])); | ||
let input = ops::Input::new(type_row![NAT, NAT]); | ||
let output = ops::Output::new(type_row![NAT]); | ||
|
||
let input = hugr.add_node_with_parent(hugr.root(), input)?; | ||
let output = hugr.add_node_with_parent(hugr.root(), output)?; | ||
let input = hugr.add_op_with_parent(hugr.root(), input)?; | ||
let output = hugr.add_op_with_parent(hugr.root(), output)?; | ||
|
||
assert_matches!(hugr.get_io(hugr.root()), Some(_)); | ||
|
||
|
@@ -747,29 +753,29 @@ mod test { | |
let mult_c_sig = FunctionType::new(type_row![NAT, NAT], type_row![NAT]) | ||
.with_extension_delta(&ExtensionSet::singleton(&C)); | ||
|
||
let add_a = hugr.add_node_with_parent( | ||
let add_a = hugr.add_op_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { | ||
ops::DFG { | ||
signature: add_a_sig, | ||
}), | ||
}, | ||
)?; | ||
let add_b = hugr.add_node_with_parent( | ||
let add_b = hugr.add_op_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { | ||
ops::DFG { | ||
signature: add_b_sig, | ||
}), | ||
}, | ||
)?; | ||
let add_ab = hugr.add_node_with_parent( | ||
let add_ab = hugr.add_op_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { | ||
ops::DFG { | ||
signature: add_ab_sig, | ||
}), | ||
}, | ||
)?; | ||
let mult_c = hugr.add_node_with_parent( | ||
let mult_c = hugr.add_op_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { | ||
ops::DFG { | ||
signature: mult_c_sig, | ||
}), | ||
}, | ||
)?; | ||
|
||
hugr.connect(input, 0, add_a, 0)?; | ||
|
@@ -903,29 +909,26 @@ mod test { | |
let [input, output] = hugr.get_io(hugr.root()).unwrap(); | ||
let add_r_sig = FunctionType::new(type_row![NAT], type_row![NAT]).with_extension_delta(&rs); | ||
|
||
let add_r = hugr.add_node_with_parent( | ||
let add_r = hugr.add_op_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { | ||
ops::DFG { | ||
signature: add_r_sig, | ||
}), | ||
}, | ||
)?; | ||
|
||
// Dangling thingy | ||
let src_sig = FunctionType::new(type_row![], type_row![NAT]) | ||
.with_extension_delta(&ExtensionSet::new()); | ||
|
||
let src = hugr.add_node_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { signature: src_sig }), | ||
)?; | ||
let src = hugr.add_op_with_parent(hugr.root(), ops::DFG { signature: src_sig })?; | ||
|
||
let mult_sig = FunctionType::new(type_row![NAT, NAT], type_row![NAT]); | ||
// Mult has open extension requirements, which we should solve to be "R" | ||
let mult = hugr.add_node_with_parent( | ||
let mult = hugr.add_op_with_parent( | ||
hugr.root(), | ||
NodeType::open_extensions(ops::DFG { | ||
ops::DFG { | ||
signature: mult_sig, | ||
}), | ||
}, | ||
)?; | ||
|
||
hugr.connect(input, 0, add_r, 0)?; | ||
|
@@ -985,18 +988,18 @@ mod test { | |
) -> Result<[Node; 3], Box<dyn Error>> { | ||
let op: OpType = op.into(); | ||
|
||
let node = hugr.add_node_with_parent(parent, NodeType::open_extensions(op))?; | ||
let input = hugr.add_node_with_parent( | ||
let node = hugr.add_op_with_parent(parent, op)?; | ||
let input = hugr.add_op_with_parent( | ||
node, | ||
NodeType::open_extensions(ops::Input { | ||
ops::Input { | ||
types: op_sig.input, | ||
}), | ||
}, | ||
)?; | ||
let output = hugr.add_node_with_parent( | ||
let output = hugr.add_op_with_parent( | ||
node, | ||
NodeType::open_extensions(ops::Output { | ||
ops::Output { | ||
types: op_sig.output, | ||
}), | ||
}, | ||
)?; | ||
Ok([node, input, output]) | ||
} | ||
|
@@ -1017,20 +1020,20 @@ mod test { | |
Into::<OpType>::into(op).signature(), | ||
)?; | ||
|
||
let lift1 = hugr.add_node_with_parent( | ||
let lift1 = hugr.add_op_with_parent( | ||
case, | ||
NodeType::open_extensions(ops::LeafOp::Lift { | ||
ops::LeafOp::Lift { | ||
type_row: type_row![NAT], | ||
new_extension: first_ext, | ||
}), | ||
}, | ||
)?; | ||
|
||
let lift2 = hugr.add_node_with_parent( | ||
let lift2 = hugr.add_op_with_parent( | ||
case, | ||
NodeType::open_extensions(ops::LeafOp::Lift { | ||
ops::LeafOp::Lift { | ||
type_row: type_row![NAT], | ||
new_extension: second_ext, | ||
}), | ||
}, | ||
)?; | ||
|
||
hugr.connect(case_in, 0, lift1, 0)?; | ||
|
@@ -1095,17 +1098,17 @@ mod test { | |
})); | ||
|
||
let root = hugr.root(); | ||
let input = hugr.add_node_with_parent( | ||
let input = hugr.add_op_with_parent( | ||
root, | ||
NodeType::open_extensions(ops::Input { | ||
ops::Input { | ||
types: type_row![NAT], | ||
}), | ||
}, | ||
)?; | ||
let output = hugr.add_node_with_parent( | ||
let output = hugr.add_op_with_parent( | ||
root, | ||
NodeType::open_extensions(ops::Output { | ||
ops::Output { | ||
types: type_row![NAT], | ||
}), | ||
}, | ||
)?; | ||
|
||
// Make identical dataflow nodes which add extension requirement "A" or "B" | ||
|
@@ -1126,12 +1129,12 @@ mod test { | |
.unwrap(); | ||
|
||
let lift = hugr | ||
.add_node_with_parent( | ||
.add_op_with_parent( | ||
node, | ||
NodeType::open_extensions(ops::LeafOp::Lift { | ||
ops::LeafOp::Lift { | ||
type_row: type_row![NAT], | ||
new_extension: ext, | ||
}), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
|
@@ -1178,7 +1181,7 @@ mod test { | |
|
||
let [bb, bb_in, bb_out] = create_with_io(hugr, bb_parent, dfb, dfb_sig)?; | ||
|
||
let dfg = hugr.add_node_with_parent(bb, NodeType::open_extensions(op))?; | ||
let dfg = hugr.add_op_with_parent(bb, op)?; | ||
|
||
hugr.connect(bb_in, 0, dfg, 0)?; | ||
hugr.connect(dfg, 0, bb_out, 0)?; | ||
|
@@ -1210,23 +1213,20 @@ mod test { | |
extension_delta: entry_extensions, | ||
}; | ||
|
||
let exit = hugr.add_node_with_parent( | ||
let exit = hugr.add_op_with_parent( | ||
root, | ||
NodeType::open_extensions(ops::BasicBlock::Exit { | ||
ops::BasicBlock::Exit { | ||
cfg_outputs: exit_types.into(), | ||
}), | ||
}, | ||
)?; | ||
|
||
let entry = hugr.add_node_before(exit, NodeType::open_extensions(dfb))?; | ||
let entry_in = hugr.add_node_with_parent( | ||
let entry = hugr.add_op_before(exit, dfb)?; | ||
let entry_in = hugr.add_op_with_parent(entry, ops::Input { types: inputs })?; | ||
let entry_out = hugr.add_op_with_parent( | ||
entry, | ||
NodeType::open_extensions(ops::Input { types: inputs }), | ||
)?; | ||
let entry_out = hugr.add_node_with_parent( | ||
entry, | ||
NodeType::open_extensions(ops::Output { | ||
ops::Output { | ||
types: vec![entry_tuple_sum].into(), | ||
}), | ||
}, | ||
)?; | ||
|
||
Ok(([entry, entry_in, entry_out], exit)) | ||
|
@@ -1277,12 +1277,12 @@ mod test { | |
type_row![NAT], | ||
)?; | ||
|
||
let mkpred = hugr.add_node_with_parent( | ||
let mkpred = hugr.add_op_with_parent( | ||
entry, | ||
NodeType::open_extensions(make_opaque( | ||
make_opaque( | ||
A, | ||
FunctionType::new(vec![NAT], twoway(NAT)).with_extension_delta(&a), | ||
)), | ||
), | ||
)?; | ||
|
||
// Internal wiring for DFGs | ||
|
@@ -1373,12 +1373,9 @@ mod test { | |
type_row![NAT], | ||
)?; | ||
|
||
let entry_mid = hugr.add_node_with_parent( | ||
let entry_mid = hugr.add_op_with_parent( | ||
entry, | ||
NodeType::open_extensions(make_opaque( | ||
UNKNOWN_EXTENSION, | ||
FunctionType::new(vec![NAT], twoway(NAT)), | ||
)), | ||
make_opaque(UNKNOWN_EXTENSION, FunctionType::new(vec![NAT], twoway(NAT))), | ||
)?; | ||
|
||
hugr.connect(entry_in, 0, entry_mid, 0)?; | ||
|
@@ -1462,12 +1459,12 @@ mod test { | |
type_row![NAT], | ||
)?; | ||
|
||
let entry_dfg = hugr.add_node_with_parent( | ||
let entry_dfg = hugr.add_op_with_parent( | ||
entry, | ||
NodeType::open_extensions(make_opaque( | ||
make_opaque( | ||
UNKNOWN_EXTENSION, | ||
FunctionType::new(vec![NAT], oneway(NAT)).with_extension_delta(&entry_ext), | ||
)), | ||
), | ||
)?; | ||
|
||
hugr.connect(entry_in, 0, entry_dfg, 0)?; | ||
|
@@ -1543,12 +1540,9 @@ mod test { | |
type_row![NAT], | ||
)?; | ||
|
||
let entry_mid = hugr.add_node_with_parent( | ||
let entry_mid = hugr.add_op_with_parent( | ||
entry, | ||
NodeType::open_extensions(make_opaque( | ||
UNKNOWN_EXTENSION, | ||
FunctionType::new(vec![NAT], oneway(NAT)), | ||
)), | ||
make_opaque(UNKNOWN_EXTENSION, FunctionType::new(vec![NAT], oneway(NAT))), | ||
)?; | ||
|
||
hugr.connect(entry_in, 0, entry_mid, 0)?; | ||
|
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.
I've tried a few things here - see e.g. commit 59eb260 and this comment. Think this is better than that...
but a better-still way (???) might be to define
OpType::default_extensions() -> Option<ExtensionSet>
and then makeadd_op_xxxx
use notopen_extensions
butdefault
. (Wheredefault_extensions
isNone
i.e. open for most OpTypes, butExtensionSet::new()
i.e. pure for Alias/Function/FuncDefn.) Thoughts?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.
(Could be good for a later refactor - at the same time as sorting out
NodeType::pure
etc. as that should benew_pure
orOpType::pure(self)
, likeFunctionType::pure(self) -> Signature
)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.
Good ideas, both imo! The first one, making "open_extensions" a more well supported "default" option was hampered previously by the lack of extension inference so now we can reconsider