Skip to content
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: Lower collections extension #1720

Merged
merged 11 commits into from
Nov 27, 2024
Merged

feat: Lower collections extension #1720

merged 11 commits into from
Nov 27, 2024

Conversation

mark-koch
Copy link
Contributor

Closes #1701 and fixes #1645.

Note that destructors and ref-counting hooks are still missing. These will follow in a future PR once they are available in hugr-llvm.

@mark-koch mark-koch requested a review from doug-q November 26, 2024 17:34
@mark-koch mark-koch requested a review from a team as a code owner November 26, 2024 17:34
Copy link

codecov bot commented Nov 26, 2024

Codecov Report

Attention: Patch coverage is 82.88591% with 51 lines in your changes missing coverage. Please review.

Project coverage is 86.35%. Comparing base (fc609a2) to head (31b06a7).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
hugr-llvm/src/extension/collections.rs 83.65% 4 Missing and 39 partials ⚠️
hugr-llvm/src/emit/func.rs 71.42% 0 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1720      +/-   ##
==========================================
- Coverage   86.38%   86.35%   -0.03%     
==========================================
  Files         166      167       +1     
  Lines       30063    30361     +298     
  Branches    26975    27273     +298     
==========================================
+ Hits        25969    26219     +250     
- Misses       2558     2559       +1     
- Partials     1536     1583      +47     
Flag Coverage Δ
python 92.42% <ø> (ø)
rust 85.67% <82.88%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Some nits, see what you think. I understand execution tests would be hard, could you add an issue for those?

@@ -59,6 +59,11 @@ impl ListValue {
pub fn custom_type(&self) -> CustomType {
list_custom_type(self.1.clone())
}

/// Returns the values contained inside the `[ListValue]`.
pub fn get_contents(&self) -> &Vec<Value> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more idomatic returning &[Value]. Not too bothered.

/// delegates everything to those default implementations.
pub trait CollectionsCodegen: Clone {
/// Return the llvm type of [hugr_core::std_extensions::collections::LIST_TYPENAME].
fn list_type<'c>(&self, session: &TypingSession<'c, '_>) -> BasicTypeEnum<'c> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn list_type<'c>(&self, session: &TypingSession<'c, '_>) -> BasicTypeEnum<'c> {
fn list_type<'c>(&self, session: TypingSession<'c, '_>) -> BasicTypeEnum<'c> {

}

/// Helper function to compute the signature of runtime functions.
fn rt_func_sig<'c>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like these bools, I would prefer something like pub enum CollectionRuntimeFunc { ... }. If you do keep the bools please elaborate in the comment what they mean.

],
false,
);
let func = ctx.get_extern_func("__rt__list__new", func_ty)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this is not the worst. Users with a different function name could reimplement this. I would much prefer self.runtime_func_name(CollectionsRuntimeFunc::new) though.

}
}

fn build_option<'c, H: HugrView>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as a free function in crate::emit::func, there are many other places that would like it.

Ok(option)
}

fn build_ok_or_else<'c, H: HugrView>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as for build_option

where
Self: 'a,
{
add_collections_extensions(builder, self.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My current thinking is that the free add_collections_extension is unnecessary, we should just put it's implementation here. What do you think?

Comment on lines 445 to 446
_ => {
return Err(anyhow!("Collections: invalid type args for list op"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => {
return Err(anyhow!("Collections: invalid type args for list op"));
args => bail!("Collections: invalid type args for list op: {args:?}"));

@mark-koch
Copy link
Contributor Author

I added the runtime function enum and removed most of the functions in CollectionsCodegen. Agreed that this is a lot nicer 👍

Also created issue #1722 for execution tests

@mark-koch mark-koch requested a review from doug-q November 27, 2024 14:59
Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you Mark.

};

/// Runtime functions that implement operations on lists.
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
#[non_exhaustive]

would mean adding enums isn't a breaking change

@mark-koch mark-koch enabled auto-merge November 27, 2024 15:19
@mark-koch mark-koch added this pull request to the merge queue Nov 27, 2024
Merged via the queue into main with commit c87d4f3 Nov 27, 2024
24 checks passed
@mark-koch mark-koch deleted the mk/collections branch November 27, 2024 15:25
@hugrbot hugrbot mentioned this pull request Nov 27, 2024
This was referenced Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add lowering for collections extension There is no way to access the contents of a ListValue
2 participants