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
WojciechMazur
merged 13 commits into
scala-native:main
from
WojciechMazur:refactor/stackalloc-improvements
Aug 4, 2023
Merged
Overhaul of scala.scalanative.unsafe.{stackalloc,alloc}
#3411
WojciechMazur
merged 13 commits into
scala-native:main
from
WojciechMazur:refactor/stackalloc-improvements
Aug 4, 2023
Conversation
This file contains 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
stackalloc(num, size) allocates num * size bytes as memory.
…ntitializaiton to backend
ekrich
suggested changes
Jul 25, 2023
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.
This looks like it will really help, especially with some math code I have.
nativelib/src/main/scala-2/scala/scalanative/unsafe/UnsafePackageCompat.scala
Outdated
Show resolved
Hide resolved
nativelib/src/main/scala-3/scala/scalanative/unsafe/UnsafePackageCompat.scala
Outdated
Show resolved
Hide resolved
nscplugin/src/test/scala/scala/scalanative/unsafe/StackallocTest.scala
Outdated
Show resolved
Hide resolved
nscplugin/src/test/scala/scala/scalanative/unsafe/StackallocTest.scala
Outdated
Show resolved
Hide resolved
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.
stackalloc[T](Int)
andalloc[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.stackalloc
signature has changed, methods take now genericT
type used to resolve size of required memory. Thesize: 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 thenir.Type
argument ofnir.Op.Stackalloc
(previously always Byte). This work is based on 2-parameter (num/size)stackalloc
intrinsic overload #3260Lowering
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.Intrinsics.internal
objecttoUnsigned
conversion