From a1c61fd5d69a058966e3817c6c7884fabbea2fa4 Mon Sep 17 00:00:00 2001 From: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com> Date: Sat, 23 Apr 2022 01:38:19 +0300 Subject: [PATCH] Treat `INDEX` as a potentially throwing operation (#68381) * Treat INDEX as potentially throwing Not doing so will lead to the side-effect extraction routine dropping it. * Add a test --- src/coreclr/jit/gentree.cpp | 1 + .../Arrays/misc/IndexingSideEffects.cs | 50 +++++++++++++++++++ .../Arrays/misc/IndexingSideEffects.csproj | 9 ++++ 3 files changed, 60 insertions(+) create mode 100644 src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.cs create mode 100644 src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.csproj diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index e420c710fcdfe..f4bcbb5157bfc 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -6464,6 +6464,7 @@ bool GenTree::OperMayThrow(Compiler* comp) case GT_ARR_OFFSET: case GT_LCLHEAP: case GT_CKFINITE: + case GT_INDEX: case GT_INDEX_ADDR: return true; diff --git a/src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.cs b/src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.cs new file mode 100644 index 0000000000000..db4f5191be29b --- /dev/null +++ b/src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.cs @@ -0,0 +1,50 @@ +// 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.Runtime.CompilerServices; +using Xunit; + +public class IndexingSideEffects +{ + [Fact] + public static int TestEntryPoint() + { + if (!Problem()) + { + return 101; + } + + return 100; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool Problem() + { + bool result = false; + + try + { + TryIndexing(new int[0]); + } + catch (IndexOutOfRangeException) + { + result = true; + } + + return result; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void TryIndexing(int[] a) + { + // Make sure that early flowgraph simplification does not remove the side effect of indexing + // when deleting the relop. + if (a[int.MaxValue] == 0) + { + NopInlinedCall(); + } + } + + private static void NopInlinedCall() { } +} diff --git a/src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.csproj b/src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.csproj new file mode 100644 index 0000000000000..501217e4d8689 --- /dev/null +++ b/src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.csproj @@ -0,0 +1,9 @@ + + + None + True + + + + +