-
Notifications
You must be signed in to change notification settings - Fork 406
feat(SSA): always simplify AsSlice intrinsic in ACIR functions #10279
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
Open
asterite
wants to merge
13
commits into
master
Choose a base branch
from
ab/always-simplify-as-slice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d734b3b
feat(SSA): always simplify AsSlice intrinsic
asterite e05c9da
Only optimize for ACIR
asterite 6dcd97b
No need to handle AsSlice in ACIR-gen
asterite 916b283
Remove test
asterite 65f90bc
Remove test
asterite ac23f43
Write test in another way
asterite 5872785
Revert "Remove test"
asterite 42e00c3
Fix incorrect index
asterite 87176a2
Merge branch 'master' into ab/always-simplify-as-slice
asterite 98fdc66
Merge branch 'master' into ab/always-simplify-as-slice
asterite a3631af
Use early return
asterite cce2476
Use make_array helper
asterite 6ad41b1
Merge branch 'master' into ab/always-simplify-as-slice
TomAFrench 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -106,26 +106,59 @@ pub(super) fn simplify_call( | |
| // Strings are already arrays of bytes in SSA | ||
| Intrinsic::ArrayAsStrUnchecked => SimplifyResult::SimplifiedTo(arguments[0]), | ||
| Intrinsic::AsSlice => { | ||
| let array_type = dfg.type_of_value(arguments[0]); | ||
| let Type::Array(element_types, length) = array_type else { | ||
| panic!("Expected as_slice input to be an array") | ||
| }; | ||
|
|
||
| let array = dfg.get_array_constant(arguments[0]); | ||
| if let Some((array, array_type)) = array { | ||
| // Compute the resulting slice length by dividing the flattened | ||
| // array length by the size of each array element | ||
| let elements_size = array_type.element_size(); | ||
| let inner_element_types = array_type.element_types(); | ||
| assert_eq!( | ||
| 0, | ||
| array.len() % elements_size, | ||
| "expected array length to be multiple of its elements size" | ||
| ); | ||
| let slice_length_value = array.len() / elements_size; | ||
| let slice_length = | ||
| dfg.make_constant(slice_length_value.into(), NumericType::length_type()); | ||
| if let Some((array, _array_type)) = array { | ||
| let slice_length = dfg.make_constant(length.into(), NumericType::length_type()); | ||
| let new_slice = | ||
| make_array(dfg, array, Type::Slice(inner_element_types), block, call_stack); | ||
| SimplifyResult::SimplifiedToMultiple(vec![slice_length, new_slice]) | ||
| } else { | ||
| SimplifyResult::None | ||
| make_array(dfg, array, Type::Slice(element_types), block, call_stack); | ||
| return SimplifyResult::SimplifiedToMultiple(vec![slice_length, new_slice]); | ||
| } | ||
|
|
||
| // In ACIR we can simplify `as_slice(array)`, to: | ||
| // | ||
| // ``` | ||
| // v0 = array_get array, index u32 0 -> T | ||
| // v1 = array_get array, index u32 1 -> T | ||
| // ... | ||
| // vN = make_array [v0, v1, ...] | ||
| // ``` | ||
| // | ||
| // We don't do this for Brillig because it sometimes leads to more opcodes. | ||
| if dfg.runtime().is_acir() { | ||
|
Contributor
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. Could we reduce nesting here with an inverted condition and early return |
||
| let mut elements = im::Vector::default(); | ||
| let mut index: u32 = 0; | ||
| for _ in 0..length { | ||
| for element_type in element_types.iter() { | ||
| let index_value = | ||
| dfg.make_constant(index.into(), NumericType::length_type()); | ||
| let array_get = | ||
| Instruction::ArrayGet { array: arguments[0], index: index_value }; | ||
| let element = dfg | ||
| .insert_instruction_and_results( | ||
| array_get, | ||
| block, | ||
| Some(vec![element_type.clone()]), | ||
| call_stack, | ||
| ) | ||
| .first(); | ||
| elements.push_back(element); | ||
| index += 1; | ||
| } | ||
| } | ||
| let make_array = | ||
| Instruction::MakeArray { elements, typ: Type::Slice(element_types.clone()) }; | ||
| let slice_length = dfg.make_constant(length.into(), NumericType::length_type()); | ||
| let new_slice = | ||
| dfg.insert_instruction_and_results(make_array, block, None, call_stack).first(); | ||
|
Contributor
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. There is a |
||
| return SimplifyResult::SimplifiedToMultiple(vec![slice_length, new_slice]); | ||
| } | ||
|
|
||
| return SimplifyResult::None; | ||
| } | ||
| Intrinsic::SlicePushBack => { | ||
| let slice = dfg.get_array_constant(arguments[1]); | ||
|
|
@@ -921,4 +954,69 @@ mod tests { | |
| "#; | ||
| let _ = Ssa::from_str_simplifying(src).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn simplifies_as_slice_for_known_array() { | ||
| let src = r#" | ||
| acir(inline) fn main func { | ||
| b0(): | ||
| v0 = make_array [Field 1, Field 2, Field 3] : [Field; 3] | ||
| v1, v2 = call as_slice(v0) -> (u32, [Field]) | ||
| return v1, v2 | ||
| } | ||
| "#; | ||
| let ssa = Ssa::from_str_simplifying(src).unwrap(); | ||
|
|
||
| assert_ssa_snapshot!(ssa, @r" | ||
| acir(inline) fn main f0 { | ||
| b0(): | ||
| v3 = make_array [Field 1, Field 2, Field 3] : [Field; 3] | ||
| v4 = make_array [Field 1, Field 2, Field 3] : [Field] | ||
| return u32 3, v4 | ||
| } | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn simplifies_as_slice_for_unknown_array_in_acir() { | ||
| let src = r#" | ||
| acir(inline) fn main func { | ||
| b0(v0: [Field; 3]): | ||
| v1, v2 = call as_slice(v0) -> (u32, [Field]) | ||
| return v1, v2 | ||
| } | ||
| "#; | ||
| let ssa = Ssa::from_str_simplifying(src).unwrap(); | ||
|
|
||
| assert_ssa_snapshot!(ssa, @r" | ||
| acir(inline) fn main f0 { | ||
| b0(v0: [Field; 3]): | ||
| v2 = array_get v0, index u32 0 -> Field | ||
|
vezenovm marked this conversation as resolved.
|
||
| v4 = array_get v0, index u32 1 -> Field | ||
| v6 = array_get v0, index u32 2 -> Field | ||
| v7 = make_array [v2, v4, v6] : [Field] | ||
| return u32 3, v7 | ||
| } | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn does_not_simplify_as_slice_for_unknown_array_in_brillig() { | ||
| let src = r#" | ||
| brillig(inline) fn main func { | ||
| b0(v0: [Field; 3]): | ||
| v1, v2 = call as_slice(v0) -> (u32, [Field]) | ||
| return v1, v2 | ||
| } | ||
| "#; | ||
| let ssa = Ssa::from_str_simplifying(src).unwrap(); | ||
|
|
||
| assert_ssa_snapshot!(ssa, @r" | ||
| brillig(inline) fn main f0 { | ||
| b0(v0: [Field; 3]): | ||
| v2, v3 = call as_slice(v0) -> (u32, [Field]) | ||
| return v2, v3 | ||
| } | ||
| "); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.