-
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 12 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 |
---|---|---|
|
@@ -268,11 +268,6 @@ impl UnificationContext { | |
where | ||
T: HugrView, | ||
{ | ||
if hugr.root_type().signature().is_none() { | ||
let m_input = self.make_or_get_meta(hugr.root(), Direction::Incoming); | ||
self.variables.insert(m_input); | ||
} | ||
|
||
for node in hugr.nodes() { | ||
let m_input = self.make_or_get_meta(node, Direction::Incoming); | ||
let m_output = self.make_or_get_meta(node, Direction::Outgoing); | ||
|
@@ -312,6 +307,14 @@ impl UnificationContext { | |
match node_type.signature() { | ||
// Input extensions are open | ||
None => { | ||
if node == hugr.root() | ||
|| matches!( | ||
node_type.tag(), | ||
OpTag::Alias | OpTag::FuncDefn | OpTag::Function | ||
) | ||
{ | ||
self.variables.insert(m_input); | ||
} | ||
self.gen_union_constraint( | ||
m_input, | ||
m_output, | ||
|
@@ -338,16 +341,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 +730,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 +750,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 +906,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 +985,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 +1017,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 +1095,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 +1126,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 +1178,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 +1210,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 +1274,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 +1370,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 +1456,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 +1537,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.
The check on this line is equivalent to the one removed above (lines 271-275); the
||
on next line extends it and is a first stab at what nodes we'd expect to be unconnected. (An alternative might be to skip creatingm_input
andm_output
earlier in the loop...that might be better, happy to give that a go). Note #457 .This still won't address random unconnected bits of graph, which is to say, malformed graphs, tho. (Only really an issue for tests of validation - some of these have to jump through hoops to get the error they expect rather than "CantInfer")