Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix impBoxPatternMatch #68382

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6983,9 +6983,14 @@ int Compiler::impBoxPatternMatch(CORINFO_RESOLVED_TOKEN* pResolvedToken,
if (((treeToBox->gtFlags & GTF_SIDE_EFFECT) == GTF_EXCEPT) &&
treeToBox->OperIs(GT_OBJ, GT_BLK, GT_IND))
{
// Yes, we just need to perform a null check if needed.
// If the only side effect comes from the dereference itself, yes.
GenTree* const addr = treeToBox->AsOp()->gtGetOp1();
if (fgAddrCouldBeNull(addr))

if ((addr->gtFlags & GTF_SIDE_EFFECT) != 0)
{
canOptimize = false;
}
else if (fgAddrCouldBeNull(addr))
{
treeToNullcheck = addr;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 BoxPatternMatchAndSideEffects
{
[Fact]
public static int TestEntryPoint()
{
if (!Problem(new Struct[0]))
{
return 101;
}

return 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool Problem(Struct[] a)
{
bool result = false;

try
{
// Make sure the "box(x) != null" optimization does not drop the side-effect of indexing into the array.
if ((object)a[int.MaxValue] != null)
{
result = false;
}
}
catch (IndexOutOfRangeException)
{
result = true;
}

return result;
}

struct Struct { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugType>PdbOnly</DebugType>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>