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

Overhaul of scala.scalanative.unsafe.{stackalloc,alloc} #3411

Merged

Conversation

WojciechMazur
Copy link
Contributor

Deprecation and further removal of stackalloc/alloc methods taking a primitive Int was a mistake - it made the Scala Native code less readable and did not improved typesafety in any way (-1.toUnsigned = 0xFFFFFFF...). In this PR we revert some of this changes, and provide new mechanisms for safer interop.

  • Restore stackalloc[T](Int) and alloc[T](Int) methods. The now have special handling for literals integers and detect zero and negative literals at compile time. Non-literal values now have a runtime check to ensure that provide integer is positive integer. Unsigned variants are still present and have the same sementics as before.
  • Intrinsic.stackallocsignature has changed, methods take now generic T type used to resolve size of required memory. The size: RawSize argument no longer describes size of the allocated memory, instead it describe number of elements of sizeOf[T] to allocate. This change allows to skip some arithmetic operations at runtime/optimizer and allows to use the nir.Type argument of nir.Op.Stackalloc (previously always Byte). This work is based on 2-parameter (num/size) stackalloc intrinsic overload #3260
  • 0-initialization of stackallocated memory is now moved to Lowering stage. Thanks to having a full information about allocated type, we can calculate exactly the required amount of memory at linktime after optimizer run, and allows to add optimizer runs designed to removal of zero-initialization of manually initialized memory.
  • All special intrinsic operations used as intermiediete step of compilation were moved to Intrinsics.internal object
  • Removed most of no longer required toUnsigned conversion

Copy link
Member

@ekrich ekrich left a comment

Choose a reason for hiding this comment

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

This looks like it will really help, especially with some math code I have.

@WojciechMazur WojciechMazur merged commit af36d31 into scala-native:main Aug 4, 2023
79 checks passed
@WojciechMazur WojciechMazur deleted the refactor/stackalloc-improvements branch August 4, 2023 11:01
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.

None yet

3 participants