-
Notifications
You must be signed in to change notification settings - Fork 5.5k
JIT: fix FitsIn<int32_t> assert in BitOperations.Rotate{Left,Right} const-fold #129136
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
Open
AndyAyersMS
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
AndyAyersMS:fix-129099-rotate-fold
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
67 changes: 67 additions & 0 deletions
67
src/tests/JIT/Regression/JitBlue/Runtime_129099/Runtime_129099.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // NI_PRIMITIVE_RotateLeft/Right's const-fold path stored the unsigned | ||
| // fold result into a TYP_INT/TYP_UINT GenTreeIntCon via gtNewIconNode. | ||
| // For uint operands with the high bit set (e.g. RotateRight(0xFFFFFFFFu, k)) | ||
| // the zero-extended ssize_t value (0xFFFFFFFF = 4294967295) does not fit | ||
| // in int32_t, tripping a downstream FitsIn<int32_t> assert during | ||
| // 'Morph - Global' when the constant was bashed/updated. | ||
| // | ||
| // The volatile Sink is required so the fold result is materialized as a | ||
| // store (rather than dropped or inlined into the return path) -- that | ||
| // store is what hits the wide-value assert. | ||
|
|
||
| namespace Runtime_129099; | ||
|
|
||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using Xunit; | ||
|
|
||
| public static class Runtime_129099 | ||
| { | ||
| private static volatile uint SinkU32; | ||
| private static volatile int SinkI32; | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static uint FoldRotateRightUInt() | ||
| { | ||
| uint v = BitOperations.RotateRight(0xFFFFFFFFu, 1); | ||
| SinkU32 = v; | ||
| return v; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static uint FoldRotateLeftUInt() | ||
| { | ||
| uint v = BitOperations.RotateLeft(0xFFFFFFFFu, 1); | ||
| SinkU32 = v; | ||
| return v; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static uint FoldRotateRightHighBit() | ||
| { | ||
| uint v = BitOperations.RotateRight(0x80000000u, 3); | ||
| SinkU32 = v; | ||
| return v; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static int FoldIntRotateRightMinusOne() | ||
| { | ||
| int v = int.RotateRight(-1, 3); | ||
| SinkI32 = v; | ||
| return v; | ||
| } | ||
|
|
||
| [Fact] | ||
| public static int TestEntryPoint() | ||
| { | ||
| if (FoldRotateRightUInt() != 0xFFFFFFFFu) return 101; | ||
| if (FoldRotateLeftUInt() != 0xFFFFFFFFu) return 102; | ||
| if (FoldRotateRightHighBit() != 0x10000000u) return 103; | ||
| if (FoldIntRotateRightMinusOne() != -1) return 104; | ||
| return 100; | ||
| } | ||
| } |
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.
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.
Is this the best/correct way to do it?
That is, this seems like a general issue with
gtNewIconNode(ssize_t)since we default toTYP_INTand so that scenario should really have anassert(FitsIn<int32_t>(ssize_t))or insert thestatic_cast<int32_t>(value)itself, since anything else is just "incorrect IR"Anything that doesn't fit rather should be
TYP_LONGand should've gone throughgtNewLconNodeinstead (or possiblyTYP_BYREFon 64-bit for the few cases that have it).I wonder if even the general signature of
gtNewIconNodeis "incorrect" and if it rather should beint32_tinstead, to help enforce correctness here; particularly since any larger value may need to beLconNodeto work on 32-bit.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.
Note
Reply is AI-generated (GitHub Copilot CLI).
Agreed — the call-site cast is a workaround for a missing API contract. I've pushed an update that also adds the invariant at
gtNewIconNodeitself:This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:
Regression_ro_*JIT regression tests still passgtNewIconNodewould also fire) and pass with itOn the deeper signature question (
int32_tvsssize_t): a much larger refactor that touches 455 call sites — many of which legitimately wantssize_tforTYP_I_IMPL/TYP_BYREF/TYP_LONGuse. The assert above gives us the safety net without the API churn. I'd suggest filing a separate issue for the signature change if you want to pursue it.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.
Good question. I don't know if
int32_tis the right direction but will dig into it some.There's something unnecessarily clunky about the icon nodes in general. Not sure I want to revisit that right now though.
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.
Interesting... AI is getting ahead of itself here and just replying on its own. Let me reign it in a bit.
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.
Yeah, a bit unsure as to the direction myself, but I think assert is a good starting point and will help catch any other issues longer term.