-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Add BorrowArray extension
#2395
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5365aa2
Add base extension
tatiana-s 1e96c16
Add take and put ops
tatiana-s a01ddd9
Add test (hanging)
tatiana-s 1fe5eb2
Fix
tatiana-s 405ac75
Generate
tatiana-s b718e6f
Renaming
tatiana-s ff34e1a
Address comments
tatiana-s d30d029
Add new empty array op
tatiana-s 930c56e
Merge remote-tracking branch 'origin/main' into ts/panic-array
tatiana-s 353c53b
Rename
tatiana-s 6e8e1ed
Merge remote-tracking branch 'origin/main' into ts/panic-array
tatiana-s 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,8 +120,10 @@ pub enum BArrayUnsafeOpDef { | |
| /// `return<size, elem_ty>: borrow_array<size, elem_ty>, index, elem_ty -> borrow_array<size, elem_ty>` | ||
| #[strum(serialize = "return")] | ||
| r#return, | ||
| /// `return<size, elem_ty>: borrow_array<size, elem_ty> -> ()` | ||
| /// `discard_empty_borrowed<size, elem_ty>: borrow_array<size, elem_ty> -> ()` | ||
| discard_empty_borrowed, | ||
| /// `new_empty_borrowed<size, elem_ty>: () -> borrow_array<size, elem_ty>` | ||
| new_empty_borrowed, | ||
|
||
| } | ||
|
|
||
| impl BArrayUnsafeOpDef { | ||
|
|
@@ -162,6 +164,9 @@ impl BArrayUnsafeOpDef { | |
| Self::discard_empty_borrowed => { | ||
| PolyFuncTypeRV::new(params, FuncValueType::new(vec![array_ty], type_row![])) | ||
| } | ||
| Self::new_empty_borrowed => { | ||
| PolyFuncTypeRV::new(params, FuncValueType::new(type_row![], vec![array_ty])) | ||
| } | ||
| } | ||
| .into() | ||
| } | ||
|
|
@@ -203,8 +208,9 @@ impl MakeOpDef for BArrayUnsafeOpDef { | |
| "Put an element into a borrow array (panicking if there is an element already)" | ||
| } | ||
| Self::discard_empty_borrowed => { | ||
| "Discard an array where all elements have been borrowed" | ||
| "Discard a borrow array where all elements have been borrowed" | ||
| } | ||
| Self::new_empty_borrowed => "Create a new borrow array that contains no elements", | ||
| } | ||
| .into() | ||
| } | ||
|
|
@@ -685,6 +691,25 @@ pub trait BArrayOpBuilder: GenericArrayOpBuilder { | |
| self.add_dataflow_op(op.to_extension_op().unwrap(), vec![input])?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Adds an operation to create a new empty borrowed array in the dataflow graph. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `elem_ty` - The type of the elements in the array. | ||
| /// * `size` - The size of the array. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if building the operation fails. | ||
| fn add_new_empty_borrowed(&mut self, elem_ty: Type, size: u64) -> Result<Wire, BuildError> { | ||
| let op = | ||
| BArrayUnsafeOpDef::new_empty_borrowed.instantiate(&[size.into(), elem_ty.into()])?; | ||
| let [arr] = self | ||
| .add_dataflow_op(op.to_extension_op().unwrap(), vec![])? | ||
| .outputs_arr(); | ||
| Ok(arr) | ||
| } | ||
| } | ||
|
|
||
| impl<D: Dataflow> BArrayOpBuilder for D {} | ||
|
|
@@ -695,7 +720,7 @@ mod test { | |
|
|
||
| use crate::{ | ||
| builder::{DFGBuilder, Dataflow, DataflowHugr as _}, | ||
| extension::prelude::{ConstUsize, qb_t}, | ||
| extension::prelude::{ConstUsize, qb_t, usize_t}, | ||
| ops::OpType, | ||
| std_extensions::collections::borrow_array::{ | ||
| BArrayOpBuilder, BArrayUnsafeOp, BArrayUnsafeOpDef, borrow_array_type, | ||
|
|
@@ -752,4 +777,24 @@ mod test { | |
| builder.finish_hugr_with_outputs([el]).unwrap() | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_new_empty_borrowed() { | ||
| let size = 5; | ||
| let elem_ty = usize_t(); | ||
| let arr_ty = borrow_array_type(size, elem_ty.clone()); | ||
| let _ = { | ||
| let mut builder = | ||
| DFGBuilder::new(Signature::new(vec![], vec![arr_ty.clone()])).unwrap(); | ||
| let arr = builder | ||
| .add_new_empty_borrowed(elem_ty.clone(), size) | ||
| .unwrap(); | ||
| let idx = builder.add_load_value(ConstUsize::new(3)); | ||
| let val = builder.add_load_value(ConstUsize::new(202)); | ||
| let arr_with_put = builder | ||
| .add_borrow_array_return(elem_ty, size, arr, idx, val) | ||
| .unwrap(); | ||
| builder.finish_hugr_with_outputs([arr_with_put]).unwrap() | ||
| }; | ||
| } | ||
| } | ||
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
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.
Maybe
discard_all_borrowedto avoid the confusion withdiscard_emptywhich does something different?