Skip to content

Commit

Permalink
Treat INDEX as a potentially throwing operation (#68381)
Browse files Browse the repository at this point in the history
* Treat INDEX as potentially throwing

Not doing so will lead to the side-effect extraction routine dropping it.

* Add a test
  • Loading branch information
SingleAccretion committed Apr 22, 2022
1 parent 10012c4 commit a1c61fd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
50 changes: 50 additions & 0 deletions src/tests/JIT/Methodical/Arrays/misc/IndexingSideEffects.cs
Original file line number Diff line number Diff line change
@@ -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() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit a1c61fd

Please sign in to comment.