From 8f59db60a43d2c5d62490e36829c6bb5b1c46969 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Mon, 20 Apr 2026 16:51:10 +0200 Subject: [PATCH] [browser][interp] Fix ConvertToInteger intrinsic on wasm Set InterpMode to 3 on wasm so the interpreter expands all intrinsics, including ConvertToInteger. Without this, the managed fallback (CreateSaturating) produces incorrect truncation results for small integer types. Register ConvertToInteger as a named intrinsic alongside the existing ConvertToIntegerNative, and handle both in the compiler's opcode emission. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/coreclr/interpreter/compiler.cpp | 1 + src/coreclr/interpreter/interpconfig.cpp | 7 +++++++ src/coreclr/interpreter/intrinsics.cpp | 2 ++ .../System.Runtime.Tests/System/DoubleTests.GenericMath.cs | 1 - .../System.Runtime.Tests/System/SingleTests.GenericMath.cs | 1 - 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/coreclr/interpreter/compiler.cpp b/src/coreclr/interpreter/compiler.cpp index 045839aa29504a..c95822b2562078 100644 --- a/src/coreclr/interpreter/compiler.cpp +++ b/src/coreclr/interpreter/compiler.cpp @@ -3474,6 +3474,7 @@ bool InterpCompiler::EmitNamedIntrinsicCall(NamedIntrinsic ni, bool nonVirtualCa m_pLastNewIns->SetDVar(m_pStackPointer[-1].var); return true; + case NI_PRIMITIVE_ConvertToInteger: case NI_PRIMITIVE_ConvertToIntegerNative: { CHECK_STACK(1); diff --git a/src/coreclr/interpreter/interpconfig.cpp b/src/coreclr/interpreter/interpconfig.cpp index 7783a54c0c7c0c..a7d0807cb01e1a 100644 --- a/src/coreclr/interpreter/interpconfig.cpp +++ b/src/coreclr/interpreter/interpconfig.cpp @@ -14,6 +14,13 @@ void InterpConfigValues::Initialize(ICorJitHost* host) #define RELEASE_CONFIG_INTEGER(name, key, defaultValue) m_##name = host->getIntConfigValue(key, defaultValue); #include "interpconfigvalues.h" +#ifdef TARGET_WASM + // WASM-TODO: update when R2R is enabled + // Default to InterpMode 3 unless explicitly overridden via DOTNET_InterpMode. + if (m_InterpMode == 0) + m_InterpMode = 3; +#endif + m_isInitialized = true; } diff --git a/src/coreclr/interpreter/intrinsics.cpp b/src/coreclr/interpreter/intrinsics.cpp index 0bdcb718d5b6e7..f3cf4f176d5ef9 100644 --- a/src/coreclr/interpreter/intrinsics.cpp +++ b/src/coreclr/interpreter/intrinsics.cpp @@ -27,6 +27,8 @@ NamedIntrinsic GetNamedIntrinsic(COMP_HANDLE compHnd, CORINFO_METHOD_HANDLE comp { if (!strcmp(methodName, "ConvertToIntegerNative")) return NI_PRIMITIVE_ConvertToIntegerNative; + else if (!strcmp(methodName, "ConvertToInteger")) + return NI_PRIMITIVE_ConvertToInteger; else if (!strcmp(methodName, "MultiplyAddEstimate")) return NI_System_Math_MultiplyAddEstimate; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DoubleTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DoubleTests.GenericMath.cs index a0b3afb2382c80..df3180e4390581 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DoubleTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DoubleTests.GenericMath.cs @@ -346,7 +346,6 @@ public static void op_InequalityTest() [Fact] [SkipOnMono("https://github.com/dotnet/runtime/issues/100368")] - [ActiveIssue("https://github.com/dotnet/runtime/issues/123011", typeof(PlatformDetection), nameof(PlatformDetection.IsBrowser), nameof(PlatformDetection.IsCoreCLR))] public static void ConvertToIntegerTest() { // Signed Values diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SingleTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SingleTests.GenericMath.cs index 75f6ba30363b36..edb6be3848f1cc 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SingleTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SingleTests.GenericMath.cs @@ -346,7 +346,6 @@ public static void op_InequalityTest() [Fact] [SkipOnMono("https://github.com/dotnet/runtime/issues/100368")] - [ActiveIssue("https://github.com/dotnet/runtime/issues/123011", typeof(PlatformDetection), nameof(PlatformDetection.IsBrowser), nameof(PlatformDetection.IsCoreCLR))] public static void ConvertToIntegerTest() { // Signed Values