-
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
Merged
Changes from all commits
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
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 |
---|---|---|
|
@@ -16,6 +16,16 @@ use crate::{ | |
|
||
use super::IntOpDef; | ||
|
||
use lazy_static::lazy_static; | ||
|
||
lazy_static! { | ||
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. I'm going to assume Alec has sufficiently reviewed the changes in this commit |
||
static ref INARROW_ERROR_VALUE: Value = ConstError { | ||
signal: 0, | ||
message: "Integer too large to narrow".to_string(), | ||
} | ||
.into(); | ||
} | ||
|
||
fn bitmask_from_width(width: u64) -> u64 { | ||
debug_assert!(width <= 64); | ||
if width == 64 { | ||
|
@@ -111,28 +121,22 @@ pub(super) fn set_fold(op: &IntOpDef, def: &mut OpDef) { | |
let logwidth0: u8 = get_log_width(arg0).ok()?; | ||
let logwidth1: u8 = get_log_width(arg1).ok()?; | ||
let n0: &ConstInt = get_single_input_value(consts)?; | ||
(logwidth0 >= logwidth1 && n0.log_width() == logwidth0).then_some(())?; | ||
|
||
let int_out_type = INT_TYPES[logwidth1 as usize].to_owned(); | ||
let sum_type = sum_with_error(int_out_type.clone()); | ||
let err_value = || { | ||
let err_val = ConstError { | ||
signal: 0, | ||
message: "Integer too large to narrow".to_string(), | ||
}; | ||
Value::sum(1, [err_val.into()], sum_type.clone()) | ||
|
||
let mk_out_const = |i, mb_v: Result<Value, _>| { | ||
mb_v.and_then(|v| Value::sum(i, [v], sum_type)) | ||
.unwrap_or_else(|e| panic!("Invalid computed sum, {}", e)) | ||
}; | ||
let n0val: u64 = n0.value_u(); | ||
let out_const: Value = if n0val >> (1 << logwidth1) != 0 { | ||
err_value() | ||
mk_out_const(1, Ok(INARROW_ERROR_VALUE.clone())) | ||
} else { | ||
Value::extension(ConstInt::new_u(logwidth1, n0val).unwrap()) | ||
mk_out_const(0, ConstInt::new_u(logwidth1, n0val).map(Into::into)) | ||
}; | ||
if logwidth0 < logwidth1 || n0.log_width() != logwidth0 { | ||
None | ||
} else { | ||
Some(vec![(0.into(), out_const)]) | ||
} | ||
Some(vec![(0.into(), out_const)]) | ||
}, | ||
), | ||
}, | ||
|
@@ -145,29 +149,22 @@ pub(super) fn set_fold(op: &IntOpDef, def: &mut OpDef) { | |
let logwidth0: u8 = get_log_width(arg0).ok()?; | ||
let logwidth1: u8 = get_log_width(arg1).ok()?; | ||
let n0: &ConstInt = get_single_input_value(consts)?; | ||
(logwidth0 >= logwidth1 && n0.log_width() == logwidth0).then_some(())?; | ||
|
||
let int_out_type = INT_TYPES[logwidth1 as usize].to_owned(); | ||
let sum_type = sum_with_error(int_out_type.clone()); | ||
let err_value = || { | ||
let err_val = ConstError { | ||
signal: 0, | ||
message: "Integer too large to narrow".to_string(), | ||
}; | ||
Value::sum(1, [err_val.into()], sum_type.clone()) | ||
let mk_out_const = |i, mb_v: Result<Value, _>| { | ||
mb_v.and_then(|v| Value::sum(i, [v], sum_type)) | ||
.unwrap_or_else(|e| panic!("Invalid computed sum, {}", e)) | ||
}; | ||
let n0val: i64 = n0.value_s(); | ||
let ub = 1i64 << ((1 << logwidth1) - 1); | ||
let out_const: Value = if n0val >= ub || n0val < -ub { | ||
err_value() | ||
mk_out_const(1, Ok(INARROW_ERROR_VALUE.clone())) | ||
} else { | ||
Value::extension(ConstInt::new_s(logwidth1, n0val).unwrap()) | ||
mk_out_const(0, ConstInt::new_s(logwidth1, n0val).map(Into::into)) | ||
}; | ||
if logwidth0 < logwidth1 || n0.log_width() != logwidth0 { | ||
None | ||
} else { | ||
Some(vec![(0.into(), out_const)]) | ||
} | ||
Some(vec![(0.into(), out_const)]) | ||
}, | ||
), | ||
}, | ||
|
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.
Is it worth also including the test from #996 ?
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.
Yes, will do.