Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -9059,12 +9059,18 @@ GenTreeQmark* Compiler::gtNewQmarkNode(var_types type, GenTree* cond, GenTreeCol
GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
// For TYP_INT-sized constants the value must fit in int32_t; otherwise the
// node is in an invalid state and downstream SetIconValue / BashToConst
// will assert when the constant is updated. Wider values should use
// gtNewLconNode (TYP_LONG) or TYP_I_IMPL on 64-bit targets.
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
return new (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

GenTreeIntCon* Compiler::gtNewIconNodeWithVN(Compiler* comp, ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
GenTreeIntCon* cns = new (this, GT_CNS_INT) GenTreeIntCon(type, value);
comp->fgUpdateConstTreeValueNumber(cns);
return cns;
Expand DownExpand Up@@ -25492,7 +25498,7 @@ GenTree* Compiler::gtNewSimdIsNegativeInfinityNode(var_types type,
if (simdBaseType == TYP_FLOAT)
{
simdBaseType = TYP_UINT;
cnsNode = gtNewIconNode(0xFF800000);
cnsNode = gtNewIconNode(static_cast<int32_t>(0xFF800000));
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6408,7 +6408,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateLeft(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateLeft(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateLeft(cns1, cns2)), baseType);
Comment on lines +6416 to +6417

Copy link
Copy Markdown
Member

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 to TYP_INT and so that scenario should really have an assert(FitsIn<int32_t>(ssize_t)) or insert the static_cast<int32_t>(value) itself, since anything else is just "incorrect IR"

Anything that doesn't fit rather should be TYP_LONG and should've gone through gtNewLconNode instead (or possibly TYP_BYREF on 64-bit for the few cases that have it).

I wonder if even the general signature of gtNewIconNode is "incorrect" and if it rather should be int32_t instead, to help enforce correctness here; particularly since any larger value may need to be LconNode to work on 32-bit.

Copy link
Copy Markdown
MemberAuthor

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 gtNewIconNode itself:

GenTreeIntCon* Compiler::gtNewIconNode(ssize_t value, var_types type)
{
assert(genActualType(type) == type);
assert(genTypeSize(type) > genTypeSize(TYP_INT) || FitsIn<int32_t>(value));
returnnew (this, GT_CNS_INT) GenTreeIntCon(type, value);
}

This catches the bug class at construction time rather than per-call-site. Smoke-verified locally:

  • All built Regression_ro_* JIT regression tests still pass
  • A 120-trial ReifyCs sweep produces no firings of the new assert
  • The fix-129099 regression test continues to fail without the call-site fix (the assert at gtNewIconNode would also fire) and pass with it

On the deeper signature question (int32_t vs ssize_t): a much larger refactor that touches 455 call sites — many of which legitimately want ssize_t for TYP_I_IMPL/TYP_BYREF/TYP_LONG use. 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.

Copy link
Copy Markdown
MemberAuthor

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_t is 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.

Copy link
Copy Markdown
MemberAuthor

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.

Copy link
Copy Markdown
Member

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.

}
break;
}
Expand DownExpand Up@@ -6457,7 +6463,13 @@ GenTree* Compiler::impPrimitiveNamedIntrinsic(NamedIntrinsic intrinsic,
else
{
uint32_t cns1 = static_cast<uint32_t>(op1->AsIntConCommon()->IconValue());
result = gtNewIconNode(BitOperations::RotateRight(cns1, cns2), baseType);
// Sign-extend the unsigned fold result to int32_t so that downstream
// SetIconValue / BashToConst calls (which assert FitsIn<int32_t> for
// TYP_INT-sized constants) don't trip on the high-bit-set case
// (e.g. RotateRight(0xFFFFFFFFu, k) -> 0xFFFFFFFF zero-extended to a
// positive ssize_t that doesn't fit in int32_t).
result = gtNewIconNode(
static_cast<int32_t>(BitOperations::RotateRight(cns1, cns2)), baseType);
}
break;
}
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8350,7 +8350,16 @@ bool Lowering::TryLowerConstIntUDivOrUMod(GenTreeOp* divMod)
if (!isDiv)
{
// divisor UMOD dividend = dividend SUB (div MUL divisor)
GenTree* divisor = m_compiler->gtNewIconNode(divisorValue, type);
// For TYP_INT, divisorValue was masked to UINT32_MAX above; if the
// original divisor has bit 31 set then divisorValue does not fit in
// int32_t. Sign-extend via int32_t so gtNewIconNode's FitsIn<int32_t>
// invariant holds and the constant round-trips correctly (analogous
// to the importercalls.cpp fix in #129136 for the Rotate{Left,Right}
// const-fold path).
ssize_t newDivisorValue = (type == TYP_INT)
? static_cast<ssize_t>(static_cast<int32_t>(divisorValue))
: static_cast<ssize_t>(divisorValue);
GenTree* divisor = m_compiler->gtNewIconNode(newDivisorValue, type);
GenTree* mul = m_compiler->gtNewOperNode(GT_MUL, type, mulhi, divisor);
dividend = m_compiler->gtNewLclvNode(dividend->AsLclVar()->GetLclNum(), dividend->TypeGet());

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,7 @@ void Lowering::LowerCast(GenTree* tree)
convertIntrinsic = TargetArchitecture::Is64Bit
? NI_X86Base_X64_ConvertToInt64WithTruncation
: NI_X86Base_ConvertToVector128Int32WithTruncation;
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<ssize_t>(UINT32_MAX));
maxIntegralValue = m_compiler->gtNewIconNode(static_cast<int32_t>(UINT32_MAX));
minFloatOverflow = 4294967296.0; // 2^32;
break;
}
Expand Down
Original file line numberDiff line numberDiff 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;
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,7 @@
<Compile Include="JitBlue\Runtime_128631\Runtime_128631.cs" />
<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129099\Runtime_129099.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
</Project>