-
Notifications
You must be signed in to change notification settings - Fork 880
[i64-to-i32-lowering] Lower nontrapping float-to-int conversions #9017
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
+416
−4
Merged
Changes from all commits
Commits
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
Oops, something went wrong.
Oops, something went wrong.
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.
Surely there are differences in semantics that we would have to handle here?
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.
I would have thought so too.. maybe the existing implementation (outlined above) already happens to do saturation rather than trapping?
Uh oh!
There was an error while loading. Please reload this page.
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.
OK some more details here, aided by AI:
The existing implementation in I64ToI32Lowering.cpp was already compatible with saturating conversions for several key reasons:
1.
wasm2jsSemantics Already Do Not TrapI64ToI32Loweringis primarily designed for the
wasm2jspipeline. Inwasm2js, standardtrapping truncation (
i32.trunc_*/i64.trunc_*) is lowered to JavaScriptbitwise and floating-point operations (e.g.,
~~expr/(~~expr) >>> 0inwasm2js.h),which never trap on out-of-range values, infinities, or
NaN. Because trappingsemantics were already not strictly emulated, standard
truncand saturatingtrunc_satconverge on the same JS-level lowering.2. The Arithmetic Naturally Handles Saturating Edge Cases
3. Signed and Unsigned Already Shared the Same Logic
Even for non-saturating conversions (
TruncSFloat*ToInt64andTruncUFloat*ToInt64),lowerTruncFloatToIntalready routed both signed and unsigned opcodes to the exact same logic
because the two's complement bit decomposition works identically for positive
and negative values. The saturating variants (
TruncSatS*andTruncSatU*)fit directly into the same pattern.
4. Consistency with 32-bit
trunc_satLoweringIn
wasm2js.h,32-bit saturating operations (
TruncSatSFloat32ToInt32,TruncSatUFloat32ToInt32, etc.) already fall through to the exact samecodegen as the trapping 32-bit conversions (
~~exprand(~~expr) >>> 0).Reusing
lowerTruncFloatToIntfor 64-bit
trunc_satmaintains parity between 32-bit and 64-bit lowering.Uh oh!
There was an error while loading. Please reload this page.
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.
(raced with the previous comment)
It's basically impossible to tell because there are no comments 🥲 I see the output uses a trapping 32-bit truncation, so it seems possible that it traps.
Do you know how this is tested beyond that single lit test? Are we running the spec tests through wasm2js, perhaps?
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.
Ok, I buy that wasm2js doesn't care about preserving these traps. but is there precedent in i64-to-i32-lowering for changing trap behavior?
If not, maybe this is still ok, but we would want to document that at the top of the file, at least.
Uh oh!
There was an error while loading. Please reload this page.
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.
I believe the existing conversions are the ones that are non-confirming in that they do not trap when they should.
These new ones I'm adding are more conforming than the existing ones because they do not expect trapping behavior, right? They expect the saturating behaviour.
So, yes, I believe there is precedent in i64-to-i32-lowering for changing trap behavior.. but that is not relevant to this change IIUC.