Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
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;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types by Copilot · Pull Request #128604 · dotnet/runtime · GitHub
Skip to content

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types - #128604

Merged
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768
Jul 16, 2026
Merged

JIT, AOT preinit, and CoreLib: Saturate float/double conversions to small integral types#128604
tannergooding merged 33 commits into
mainfrom
copilot/fix-invalid-result-double-32768

Conversation

CopilotAI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Unchecked float/double → small integral type (sbyte, byte, short, ushort, char) conversions did not saturate at the small-type boundary. For example, (short)(double)32768.000000000007 produced -32768 instead of 32767, diverging from the saturating R → int contract introduced in .NET 9.

The JIT expands R → smallT as R → int → smallT. The inner R → int saturates, but the outer int → smallT only truncates low bits. The CoreCLR interpreter VM helper and the ILC type preinitializer had analogous bugs and were updated for parity with runtime behavior. In addition, CoreLib TryConvert paths for float/double to small integral types were updated to use direct casts on CoreCLR (while preserving existing MONO behavior under #if MONO).

Summary

Fixes saturating semantics for unchecked float/double → small integral type casts across all supported JIT targets (xarch, x86, arm64, RISC-V64, LoongArch64, ARM32, and WASM), the CoreCLR interpreter VM helper, and the NativeAOT type preinitializer, and applies corresponding CoreCLR-side TryConvert simplifications that rely on the corrected cast semantics. Mono runtime backends still need analogous updates and are tracked separately; the JIT regression test is [SkipOnMono] in the meantime.

Changes Made

  • src/coreclr/jit/morph.cppfgMorphExpandCast: Added target-specific handling so R -> small saturates at destination bounds across supported architectures:
    • FEATURE_HW_INTRINSICS targets use float-domain min/max clamping before existing cast expansion.
    • WASM lowers min/max via GT_INTRINSIC.
    • ARM32, RISC-V64, and LoongArch64 use integer-domain saturation (R -> int32 followed by NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16}), preserving NaN→0 semantics from the saturating R → int cast.
  • JIT plumbing for the new NI_PRIMITIVE_SaturateTo{Int8,Int16,UInt8,UInt16} intrinsics and the WASM binary GT_INTRINSIC(MaxNative/MinNative) produced by morph:
    • gentree.cpp: added the four SaturateTo* cases to the unary GT_INTRINSIC cost switch in gtSetEvalOrder, added a TARGET_WASM block to the binary GT_INTRINSIC cost switch for MaxNative/MinNative, and added dump labels for the four SaturateTo* IDs (fixes the 'Unknown binary GT_INTRINSIC operator' assert seen on e.g. System.Half:op_Explicit(System.Half):byte).
    • importercalls.cppIsTargetIntrinsic: added the four SaturateTo* IDs to the ARM, RISCV64, and LoongArch64 switches so rationalize.cpp's IsTargetIntrinsic assertion is satisfied.
    • importercalls.cppimpPrimitiveNamedIntrinsic: split the small-target-type path so NI_PRIMITIVE_ConvertToInteger emits a single saturating R -> small cast while NI_PRIMITIVE_ConvertToIntegerNative keeps the original two-cast (saturating R -> int followed by truncating int -> small) pattern that preserves native truncating semantics.
    • valuenum.cppfgValueNumberIntrinsic: added an explicit branch for the SaturateTo* IDs that gives the node an opaque unary VN, backed by new VNF_SaturateToInt8/Int16/UInt8/UInt16 entries in valuenumfuncs.h, so VN no longer hits the assert(NI_System_Object_GetType) for these intrinsics.
    • valuenum.cppEvalMathFuncBinary: extended the MaxNative/MinNative constant-folding (TYP_DOUBLE / TYP_FLOAT) and VNFunc selection (VNF_MaxNumber / VNF_MinNumber) cases to also apply on WASM, not just RISC-V64, so the binary GT_INTRINSIC produced by morph on WASM no longer hits unreached() here.
    • assertionprop.cppIntegralRange::ForNode: taught range analysis the exact result range for each SaturateTo* intrinsic ([ByteMin, ByteMax], [ShortMin, ShortMax], [0, UByteMax], [0, UShortMax]).
  • ARM32 codegen for SaturateTo*: new INS_ssat/INS_usat Thumb-2 instructions in instrsarm.h, encoding/disassembly support in emitarm.cpp, LSRA/codegen wiring in lsraarm.cpp and codegenarmarch.cpp.
  • RISC-V64 / LoongArch64 codegen for SaturateTo*: branch-based clamp in codegenriscv64.cpp / codegenloongarch64.cpp, with LSRA support in lsrariscv64.cpp / lsraloongarch64.cpp.
  • src/coreclr/vm/interpexec.cppConvFpHelper: clamps to numeric_limits<TResult> (the destination type) instead of TIntermediate, so floating-point → small-integral conversions now saturate at the destination type's range.
  • src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs: float-domain conv_i1/conv_i2/conv_u1/conv_u2 cases now explicitly saturate via Math.Clamp (NaN → 0), so AOT-baked statics match runtime behavior regardless of which host JIT runs ILC. Original simple-cast version is kept commented out with a TODO to restore once the fix has propagated through the toolchain.
  • CoreLib TryConvert simplifications (Double.cs, Single.cs, SByte.cs, Int16.cs): float/double → small-integral TryConvertTo* / TryConvertFrom* paths now use a direct cast on CoreCLR, with the existing manual clamp preserved under #if MONO.
  • Tests:
    • Re-enabled previously-disabled DoubleTests.GenericMath.ConvertToIntegerTest / SingleTests.GenericMath.ConvertToIntegerTest (issue (short)(double)32768.000000000007 produces invalid result #116823 suppressions removed).
    • Added a JitBlue regression test for the float/double → small-integral saturation contract (marked [SkipOnMono] pending Mono backend updates).
    • Trimmed DoubleTests.GenericMath.ConvertToIntegerNativeTest / SingleTests.GenericMath.ConvertToIntegerNativeTest to validate only in-range inputs and removed the [ActiveIssue("#124344", ... IsAppleMobile && IsX64Process && IsCoreCLR)] suppression, since ConvertToIntegerNative is explicitly allowed to use platform-native behavior for out-of-range inputs (which can also evolve over time, e.g. as AVX10.2 saturating conversions get exposed on xarch).
    • Updated the interpreter test comment/coverage.

Note: the previously included change to src/coreclr/interpreter/compiler.cpp (adding a two-step expansion for NI_PRIMITIVE_ConvertToIntegerNative on small target types) was reverted. The saturating behavior is allowed as the native interpreter behavior, and when R2R is present the R2R version of the intrinsic is used, so consistency with R2R is achieved that way.

CopilotAI review requested due to automatic review settings May 26, 2026 20:10
CopilotAI removed the request for review from CopilotMay 26, 2026 20:10
CopilotAI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:30
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

…ll casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
CopilotAI requested review from Copilot and removed request for CopilotMay 26, 2026 20:49
CopilotAI changed the title [WIP] Fix invalid result for short double 32768JIT: Saturate float/double conversions to small integral typesMay 26, 2026
CopilotAI requested a review from tannergoodingMay 26, 2026 20:53
Comment threadsrc/coreclr/jit/morph.cpp Outdated
…ing casts
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Comment threadsrc/tests/JIT/interpreter/Interpreter.cs

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Comment threadsrc/coreclr/jit/valuenum.cpp
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0 new

Comment threadsrc/tests/JIT/Regression/JitBlue/Runtime_116823/Runtime_116823.cs Outdated
Comment threadsrc/coreclr/jit/morph.cpp
@jkotas

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.

@tannergooding

Copy link
Copy Markdown
Member

Anything left here? It looks good to me overall, the JIT changes can use a review from somebody on codegen.

It should be ready and was just pending review.

CC. @dotnet/jit-contrib for review

Comment threadsrc/coreclr/jit/gentree.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/importercalls.cpp
…oongArch64
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 3

Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/emitarm.cpp
Comment threadsrc/coreclr/jit/instrsarm.h
…E_HW_INTRINSICS
gtNewSimdMinMaxNativeNode is NYI for WASM, so route the float-domain clamp through the scalar GT_INTRINSIC MaxNative/MinNative nodes (which have real codegen) instead of asserting during Morph.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 1

Comment threadsrc/coreclr/jit/codegenloongarch64.cpp
@tannergooding

Copy link
Copy Markdown
Member

CI is passing, so this is just pending sign-off to be merged now.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(short)(double)32768.000000000007 produces invalid result

7 participants

@jkotas@tannergooding@am11@MichalPetryka@JulieLeeMSFT