From 6413f4d162bc7e8c2f3911e1d5d0892367195b92 Mon Sep 17 00:00:00 2001 From: Sergey Andreenko Date: Tue, 25 Oct 2016 14:54:52 -0700 Subject: [PATCH] fix VSO 278365 don't convert (int) ((double)Round(double)) to (int)Round(double) on RyuJit/x86 --- src/jit/morph.cpp | 7 +++- src/jit/rationalize.cpp | 9 +++- .../JitBlue/DevDiv_278365/DevDiv_278365.cs | 49 ++++++++++++++++++++++ .../JitBlue/DevDiv_278365/DevDiv_278365.csproj | 46 ++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.cs create mode 100644 tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.csproj diff --git a/src/jit/morph.cpp b/src/jit/morph.cpp index b55c24a..fbe310c 100644 --- a/src/jit/morph.cpp +++ b/src/jit/morph.cpp @@ -204,6 +204,9 @@ GenTreePtr Compiler::fgMorphCast(GenTreePtr tree) { case TYP_INT: #ifdef _TARGET_X86_ // there is no rounding convert to integer instruction on ARM or x64 so skip this +#ifdef LEGACY_BACKEND + // the RyuJIT backend does not use the x87 FPU and therefore + // does not support folding the cast conv.i4(round.d(d)) if ((oper->gtOper == GT_INTRINSIC) && (oper->gtIntrinsic.gtIntrinsicId == CORINFO_INTRINSIC_Round)) { @@ -212,7 +215,9 @@ GenTreePtr Compiler::fgMorphCast(GenTreePtr tree) return fgMorphTree(oper); } // if SSE2 is not enabled, we need the helper - else if (!opts.compCanUseSSE2) + else +#endif // LEGACY_BACKEND + if (!opts.compCanUseSSE2) { return fgMorphCastIntoHelper(tree, CORINFO_HELP_DBL2INT, oper); } diff --git a/src/jit/rationalize.cpp b/src/jit/rationalize.cpp index 42c2cf6..ad90196 100644 --- a/src/jit/rationalize.cpp +++ b/src/jit/rationalize.cpp @@ -156,7 +156,14 @@ void Rationalizer::RewriteNodeAsCall(GenTree** use, // Create the call node GenTreeCall* call = comp->gtNewCallNode(CT_USER_FUNC, callHnd, tree->gtType, args); - call = comp->fgMorphArgs(call); + +#if DEBUG + CORINFO_SIG_INFO sig; + comp->eeGetMethodSig(callHnd, &sig); + assert(JITtype2varType(sig.retType) == tree->gtType); +#endif // DEBUG + + call = comp->fgMorphArgs(call); // Determine if this call has changed any codegen requirements. comp->fgCheckArgCnt(); diff --git a/tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.cs b/tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.cs new file mode 100644 index 0000000..631175a --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.cs @@ -0,0 +1,49 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + + +using System; +using System.Runtime.CompilerServices; + +// This test is a reduced repro case for DevDiv VSO bug 278365. +// The failure mode is that the RyuJIT/x86 backend changed call to ROUND intrinsic +// with double return type to ROUND intrinsic with int return type, that is not supported. + +internal class Program +{ + [MethodImpl(MethodImplOptions.NoInlining)] + public static int Bar() + { + int sum = 0; + for (int i = 0; i < 100; ++i) + { + int v = (int)Math.Round(4.4 + i); + sum += v; + } + sum -= 4 * 100; + if (sum != 100 * 99 / 2) + { + return 0; + } + else + { + return 100; + } + } + + private static int Main(string[] args) + { + try + { + if (Bar() != 100) + return 0; + } + catch (Exception) + { + } + + Console.WriteLine("Pass"); + return 100; + } +} diff --git a/tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.csproj b/tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.csproj new file mode 100644 index 0000000..0abcfd8 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/DevDiv_278365/DevDiv_278365.csproj @@ -0,0 +1,46 @@ + + + + + Debug + AnyCPU + $(MSBuildProjectName) + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + + 7a9bfb7d + + + + + + + + + False + + + + + True + + + + + + + + + $(JitPackagesConfigFileDirectory)minimal\project.json + $(JitPackagesConfigFileDirectory)minimal\project.lock.json + + + + + -- 2.7.4