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

Add intrinsics for operating on raw_ptrs #3157

Merged
merged 6 commits into from
Nov 2, 2022
Merged

Conversation

AlicanC
Copy link
Contributor

@AlicanC AlicanC commented Oct 26, 2022

This PR:

  • Adds __ptr_add<T>(ptr: raw_ptr, count: u64) (which basically does ptr + __size_of::<T>() * count) and its sub counterpart to be used in pointer offset calculations.
  • Makes std::alloc::* and raw_ptr::* have type arguments and work in increments of size_of::<T>() instead of bytes.
  • Removes test fixing_generic_type which was broken instead of maintaining it. This test was created to reproduce an issue I was having a few months ago. Fixing this allowed other things to be built so there are other tests that touch this case.

This abstracts away more hacky stuff from the Sway side, and also significantly reduces bytecode size in some tests, especially in Vec stuff:

Test Before After Change
should_pass/stdlib/alloc 684 948 27.85%
should_fail/vec_swap_param2_out_of_bounds 1028 1132 9.19%
should_fail/vec_swap_param1_out_of_bounds 1028 1132 9.19%
should_pass/stdlib/raw_ptr 1964 2020 2.77%
should_pass/stdlib/vec_swap 32764 33460 2.08%
should_pass/stdlib/vec 175952 178992 1.70%

Related: #3112

@AlicanC AlicanC added the compiler: ir IRgen and sway-ir including optimization passes label Oct 26, 2022
@AlicanC AlicanC self-assigned this Oct 26, 2022
@mohammadfawaz
Copy link
Contributor

That's amazing 🚀 . Do you mind checking the impact of this on the sway-applications repo?

@AlicanC
Copy link
Contributor Author

AlicanC commented Oct 27, 2022

Do you mind checking the impact of this on the sway-applications repo?

I was surprised at first to see not even a single app's size change, but then realized we use Vec in only one app and that's just two method calls or something.

So 0% change on sway-applications.

@mohammadfawaz mohammadfawaz added lib: std Standard library compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen and removed compiler: ir IRgen and sway-ir including optimization passes labels Oct 27, 2022
@mohammadfawaz
Copy link
Contributor

Ok so one more comment. Otherwise, is this ready for review? You also seem to have a merge conflict now :/

@AlicanC
Copy link
Contributor Author

AlicanC commented Oct 31, 2022

Otherwise, is this ready for review?

Well IDK really.

This only improves Vec and if we are to improve Vec it would be better to have separate intrinsics like __ptr_add(ptr: raw_ptr, len: u64, count: u64) where we do raw_ptr += len * count which is what Vec really needs.

If we had typed pointers, then the signature would be __ptr_add<T>(ptr: ptr<T>, count: u64) where we get len from __size_of::<T>.

@mohammadfawaz
Copy link
Contributor

Otherwise, is this ready for review?

Well IDK really.

This only improves Vec and if we are to improve Vec it would be better to have separate intrinsics like __ptr_add(ptr: raw_ptr, len: u64, count: u64) where we do raw_ptr += len * count which is what Vec really needs.

If we had typed pointers, then the signature would be __ptr_add<T>(ptr: ptr<T>, count: u64) where we get len from __size_of::<T>.

You may be right in the sense that it's odd to reuse the binary intrinsics __add and __sub and hacking them to allow accepting two values of different types (raw_ptr and u64). Besides, we also have to disallow __mul and __div for raw_ptr. @otrho what do you think?

@otrho
Copy link
Contributor

otrho commented Oct 31, 2022

Yeah, you're (both) right. __ptr_add() sounds like a way better solution. Doing arithmetic on pointers should obviously be discouraged and constrained.

@AlicanC
Copy link
Contributor Author

AlicanC commented Nov 1, 2022

Ok, so this version adds __ptr_add<T>(ptr: raw_ptr, count: u64) and its sub.

I've also made std::alloc::* and raw_ptr::* have type arguments and work in increments of size_of::<T>() instead of bytes. This introduces a constraint since T has to be multiples of 8 in Sway. (You can't alloc 1 byte, 7 bytes, etc. anymore, only multiples of 8.) I think this is a nice constraint to have since Sway is word-sized and not byte-sized.

I've also removed a test that was broken. It was a test I was familiar with, and what's being tested there should have no way of not being caught by other tests since we started making heavy usage of it after it was fixed.

@AlicanC AlicanC marked this pull request as ready for review November 1, 2022 12:17
@AlicanC AlicanC requested a review from otrho as a code owner November 1, 2022 12:17
Copy link
Contributor

@mohammadfawaz mohammadfawaz left a comment

Choose a reason for hiding this comment

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

This seems more reasonable and safer. Thanks!

  • Just one small question in one of the tests
  • Also, why are we deleting the test fixing_generic_type?
  • Finally, will you please update the PR description to reflect the new approach?

mohammadfawaz
mohammadfawaz previously approved these changes Nov 1, 2022
@mohammadfawaz mohammadfawaz requested a review from a team November 1, 2022 14:12
@AlicanC AlicanC changed the title Overload intrinsics to support operating on raw_ptrs Add intrinsics for operating on raw_ptrs Nov 1, 2022
@AlicanC
Copy link
Contributor Author

AlicanC commented Nov 1, 2022

@mohammadfawaz updated the PR desc!

@AlicanC AlicanC enabled auto-merge (squash) November 1, 2022 15:53
mohammadfawaz
mohammadfawaz previously approved these changes Nov 2, 2022
@AlicanC AlicanC requested a review from a team November 2, 2022 13:55
@mohammadfawaz mohammadfawaz added the breaking May cause existing user code to break. Requires a minor or major release. label Nov 2, 2022
@AlicanC AlicanC merged commit 81f7f57 into master Nov 2, 2022
@AlicanC AlicanC deleted the jc/intrinsic-raw-ptr branch November 2, 2022 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release. compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen lib: std Standard library
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants