-
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: add verification to constant folding #1030
Merged
+223
−94
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6951a2e
Add `validate` to `HugrView`
doug-q 02a1a27
Add verification to `constant_fold_pass`. Add a failing test
doug-q b2c815b
fix constant folding using invalid rewrites
doug-q 862419f
fix inarrow const folding tests
doug-q 1e7a6f5
address review
doug-q 20b35d6
remove `VerifyLevel` etc, assert! -> debug_assert!
doug-q 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,52 +61,38 @@ fn test_fold_iwiden_s() { | |
assert_fully_folded(&h, &expected); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_fold_inarrow_u() { | ||
// pseudocode: | ||
// | ||
// x0 := int_u<5>(13); | ||
// x1 := inarrow_u<5, 4>(x0); | ||
// output x1 == int_u<4>(13); | ||
let sum_type = sum_with_error(INT_TYPES[4].to_owned()); | ||
let mut build = DFGBuilder::new(FunctionType::new( | ||
type_row![], | ||
vec![sum_type.clone().into()], | ||
)) | ||
.unwrap(); | ||
let x0 = build.add_load_const(Value::extension(ConstInt::new_u(5, 13).unwrap())); | ||
let x1 = build | ||
.add_dataflow_op(IntOpDef::inarrow_u.with_two_log_widths(5, 4), [x0]) | ||
.unwrap(); | ||
let reg = ExtensionRegistry::try_new([ | ||
PRELUDE.to_owned(), | ||
arithmetic::int_types::EXTENSION.to_owned(), | ||
]) | ||
.unwrap(); | ||
let mut h = build.finish_hugr_with_outputs(x1.outputs(), ®).unwrap(); | ||
constant_fold_pass(&mut h, ®); | ||
let expected = Value::extension(ConstInt::new_u(4, 13).unwrap()); | ||
assert_fully_folded(&h, &expected); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_fold_inarrow_s() { | ||
#[rstest] | ||
#[case(ConstInt::new_s, IntOpDef::inarrow_s, 5, 4, -3, true)] | ||
#[case(ConstInt::new_s, IntOpDef::inarrow_s, 5, 5, -3, true)] | ||
#[case(ConstInt::new_s, IntOpDef::inarrow_s, 5, 1, -3, false)] | ||
#[case(ConstInt::new_u, IntOpDef::inarrow_u, 5, 4, 13, true)] | ||
#[case(ConstInt::new_u, IntOpDef::inarrow_u, 5, 5, 13, true)] | ||
#[case(ConstInt::new_u, IntOpDef::inarrow_u, 5, 0, 3, false)] | ||
fn test_fold_inarrow<I: Copy, C: Into<Value>, E: std::fmt::Debug>( | ||
#[case] mk_const: impl Fn(u8, I) -> Result<C, E>, | ||
#[case] op_def: IntOpDef, | ||
#[case] from_log_width: u8, | ||
#[case] to_log_width: u8, | ||
#[case] val: I, | ||
#[case] succeeds: bool, | ||
) { | ||
// pseudocode: | ||
// | ||
// x0 := int_s<5>(-3); | ||
// x1 := inarrow_s<5, 4>(x0); | ||
// output x1 == int_s<4>(-3); | ||
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 comment should be removed or rewritten in more general terms. |
||
let sum_type = sum_with_error(INT_TYPES[4].to_owned()); | ||
let sum_type = sum_with_error(INT_TYPES[to_log_width as usize].to_owned()); | ||
let mut build = DFGBuilder::new(FunctionType::new( | ||
type_row![], | ||
vec![sum_type.clone().into()], | ||
)) | ||
.unwrap(); | ||
let x0 = build.add_load_const(Value::extension(ConstInt::new_s(5, -3).unwrap())); | ||
let x0 = build.add_load_const(mk_const(from_log_width, val).unwrap().into()); | ||
let x1 = build | ||
.add_dataflow_op(IntOpDef::inarrow_s.with_two_log_widths(5, 4), [x0]) | ||
.add_dataflow_op( | ||
op_def.with_two_log_widths(from_log_width, to_log_width), | ||
[x0], | ||
) | ||
.unwrap(); | ||
let reg = ExtensionRegistry::try_new([ | ||
PRELUDE.to_owned(), | ||
|
@@ -115,7 +101,11 @@ fn test_fold_inarrow_s() { | |
.unwrap(); | ||
let mut h = build.finish_hugr_with_outputs(x1.outputs(), ®).unwrap(); | ||
constant_fold_pass(&mut h, ®); | ||
let expected = Value::extension(ConstInt::new_s(4, -3).unwrap()); | ||
let expected = if succeeds { | ||
Value::sum(0, [mk_const(to_log_width, val).unwrap().into()], sum_type).unwrap() | ||
} else { | ||
Value::sum(1, [super::INARROW_ERROR_VALUE.clone()], sum_type).unwrap() | ||
}; | ||
assert_fully_folded(&h, &expected); | ||
} | ||
|
||
|
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'm going to assume Alec has sufficiently reviewed the changes in this commit