From f653bf2f3b7def059aa975487b51a4e0212870b4 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Thu, 4 Nov 2021 23:40:06 +0300 Subject: [PATCH 1/4] fold "shift-by-zero" in lower --- src/coreclr/jit/lower.cpp | 12 +++++++- .../JitBlue/Runtime_61045/Runtime_61045.cs | 30 +++++++++++++++++++ .../Runtime_61045/Runtime_61045.csproj | 9 ++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.csproj diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index c525c59cfa062..6c5972b727b9a 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -5754,6 +5754,16 @@ void Lowering::LowerShift(GenTreeOp* shift) } ContainCheckShiftRotate(shift); + // Fold any kind of shift by zero, e.g. LSH(X, 0) => X + LIR::Use use; + if (shift->gtGetOp2()->IsIntegralConst(0) && BlockRange().TryGetUse(shift, &use)) + { + use.ReplaceWith(shift->gtGetOp1()); + BlockRange().Remove(shift->gtGetOp2()); + BlockRange().Remove(shift); + return; + } + #ifdef TARGET_ARM64 // Try to recognize ubfiz/sbfiz idiom in LSH(CAST(X), CNS) tree if (comp->opts.OptimizationEnabled() && shift->OperIs(GT_LSH) && shift->gtGetOp1()->OperIs(GT_CAST) && @@ -5773,7 +5783,7 @@ void Lowering::LowerShift(GenTreeOp* shift) assert(!cast->CastOp()->isContained()); // It has to be an upcast and CNS must be in [1..srcBits) range - if ((srcBits < dstBits) && ((UINT32)cns->IconValue() < srcBits)) + if ((srcBits < dstBits) && (cns->IconValue() > 0) && (cns->IconValue() < srcBits)) { JITDUMP("Recognized ubfix/sbfix pattern in LSH(CAST, CNS). Changing op to GT_BFIZ"); shift->ChangeOper(GT_BFIZ); diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs new file mode 100644 index 0000000000000..02e761a75d723 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Generated by Fuzzlyn v1.5 on 2021-11-04 18:29:11 +// Run on Arm64 Linux +// Seed: 1922924939431163374 +// Reduced from 261.1 KiB to 0.2 KiB in 00:05:39 +// Hits JIT assert in Release: +// Assertion failed 'isValidImmShift(lsb, size)' in 'Program:M4():int' during 'Generate code' (IL size 26) +// +// File: /__w/1/s/src/coreclr/jit/emitarm64.cpp Line: 7052 +// + +using System.Runtime.CompilerServices; + +public class Runtime_61045 +{ + public static byte[] s_1; + public static int Main() + { + Test(); + return 100; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static uint Test() + { + return (uint)((ushort)~s_1[0] << (0 >> s_1[0])); + } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.csproj new file mode 100644 index 0000000000000..6946bed81bfd5 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.csproj @@ -0,0 +1,9 @@ + + + Exe + True + + + + + From 2e8f67ae21f444e1de15272f720cab85996cabfd Mon Sep 17 00:00:00 2001 From: EgorBo Date: Thu, 4 Nov 2021 23:48:17 +0300 Subject: [PATCH 2/4] oh, the test actually throws NRE --- .../Regression/JitBlue/Runtime_61045/Runtime_61045.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs index 02e761a75d723..c37ffd5fd1b99 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs @@ -18,8 +18,15 @@ public class Runtime_61045 public static byte[] s_1; public static int Main() { - Test(); - return 100; + try + { + Test(); + } + catch (NullReferenceException) + { + return 100; + } + return 101; } [MethodImpl(MethodImplOptions.NoInlining)] From 2240c3a5b9edcbc6264b954fed8f9a7841e966fc Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Fri, 5 Nov 2021 00:42:58 +0300 Subject: [PATCH 3/4] Update Runtime_61045.cs --- src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs index c37ffd5fd1b99..b9c83b498d6cd 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_61045/Runtime_61045.cs @@ -11,6 +11,7 @@ // File: /__w/1/s/src/coreclr/jit/emitarm64.cpp Line: 7052 // +using System; using System.Runtime.CompilerServices; public class Runtime_61045 From d6f463d8474f1715b913d98e1963f7058359afee Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Fri, 5 Nov 2021 13:59:24 +0300 Subject: [PATCH 4/4] Update lower.cpp --- src/coreclr/jit/lower.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index 6c5972b727b9a..ea76536df60b3 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -5754,16 +5754,6 @@ void Lowering::LowerShift(GenTreeOp* shift) } ContainCheckShiftRotate(shift); - // Fold any kind of shift by zero, e.g. LSH(X, 0) => X - LIR::Use use; - if (shift->gtGetOp2()->IsIntegralConst(0) && BlockRange().TryGetUse(shift, &use)) - { - use.ReplaceWith(shift->gtGetOp1()); - BlockRange().Remove(shift->gtGetOp2()); - BlockRange().Remove(shift); - return; - } - #ifdef TARGET_ARM64 // Try to recognize ubfiz/sbfiz idiom in LSH(CAST(X), CNS) tree if (comp->opts.OptimizationEnabled() && shift->OperIs(GT_LSH) && shift->gtGetOp1()->OperIs(GT_CAST) &&