From 4354abd606119bd9fb0fdefa9f3c0f58f414bfa9 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 1 Feb 2023 21:46:53 -0800 Subject: [PATCH] Ensure that we don't try to constant fold BSF(0) or BSR(0) (#81516) * Ensure that we don't try to constant fold BSF(0) or BSR(0) * Ensure the test returns 100 and has correct usings * Apply formatting patch --- src/coreclr/jit/valuenum.cpp | 36 +++++++++++++++++----- .../JitBlue/Runtime_81460/Runtime_81460.cs | 25 +++++++++++++++ .../JitBlue/Runtime_81460/Runtime_81460.csproj | 9 ++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj diff --git a/src/coreclr/jit/valuenum.cpp b/src/coreclr/jit/valuenum.cpp index 8995eaa..cd91c27 100644 --- a/src/coreclr/jit/valuenum.cpp +++ b/src/coreclr/jit/valuenum.cpp @@ -6121,40 +6121,60 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunUnary( case NI_X86Base_BitScanForward: { assert(!varTypeIsSmall(type) && !varTypeIsLong(type)); + int32_t value = GetConstantInt32(arg0VN); - int32_t value = GetConstantInt32(arg0VN); - uint32_t result = BitOperations::BitScanForward(static_cast(value)); + if (value == 0) + { + // bsf is undefined for 0 + break; + } + uint32_t result = BitOperations::BitScanForward(static_cast(value)); return VNForIntCon(static_cast(result)); } case NI_X86Base_X64_BitScanForward: { assert(varTypeIsLong(type)); + int64_t value = GetConstantInt64(arg0VN); - int64_t value = GetConstantInt64(arg0VN); - uint32_t result = BitOperations::BitScanForward(static_cast(value)); + if (value == 0) + { + // bsf is undefined for 0 + break; + } + uint32_t result = BitOperations::BitScanForward(static_cast(value)); return VNForLongCon(static_cast(result)); } case NI_X86Base_BitScanReverse: { assert(!varTypeIsSmall(type) && !varTypeIsLong(type)); + int32_t value = GetConstantInt32(arg0VN); - int32_t value = GetConstantInt32(arg0VN); - uint32_t result = BitOperations::BitScanReverse(static_cast(value)); + if (value == 0) + { + // bsr is undefined for 0 + break; + } + uint32_t result = BitOperations::BitScanReverse(static_cast(value)); return VNForIntCon(static_cast(result)); } case NI_X86Base_X64_BitScanReverse: { assert(varTypeIsLong(type)); + int64_t value = GetConstantInt64(arg0VN); - int64_t value = GetConstantInt64(arg0VN); - uint32_t result = BitOperations::BitScanReverse(static_cast(value)); + if (value == 0) + { + // bsr is undefined for 0 + break; + } + uint32_t result = BitOperations::BitScanReverse(static_cast(value)); return VNForLongCon(static_cast(result)); } #endif // TARGET_XARCH diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs b/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs new file mode 100644 index 0000000..f3a52db --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Numerics; +using System.Runtime.CompilerServices; + +public static class FloatingPointHelper + where TSelf : IFloatingPoint +{ + public static int GetExponentShortestBitLength(TSelf value) + => value.GetExponentShortestBitLength(); +} + +class Runtime_81460 +{ + static int Main() + { + return (Test() == 0) ? 100 : 0; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static int Test() => + FloatingPointHelper.GetExponentShortestBitLength(1.0); +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj new file mode 100644 index 0000000..75e7d24 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj @@ -0,0 +1,9 @@ + + + Exe + True + + + + + -- 2.7.4