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

JIT: Fix invalid zero-init suppression for untracked variables #91580

Merged
merged 2 commits into from
Sep 9, 2023
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
16 changes: 8 additions & 8 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9029,9 +9029,9 @@ void Compiler::optRemoveRedundantZeroInits()
CompAllocator allocator(getAllocator(CMK_ZeroInit));
LclVarRefCounts refCounts(allocator);
BitVecTraits bitVecTraits(lvaCount, this);
BitVec zeroInitLocals = BitVecOps::MakeEmpty(&bitVecTraits);
bool hasGCSafePoint = false;
bool canThrow = false;
BitVec zeroInitLocals = BitVecOps::MakeEmpty(&bitVecTraits);
bool hasGCSafePoint = false;
bool hasImplicitControlFlow = false;

assert(fgNodeThreading == NodeThreading::AllTrees);

Expand All @@ -9042,6 +9042,8 @@ void Compiler::optRemoveRedundantZeroInits()
CompAllocator allocator(getAllocator(CMK_ZeroInit));
LclVarRefCounts defsInBlock(allocator);
bool removedTrackedDefs = false;
bool hasEHSuccs = block->HasPotentialEHSuccs(this);

for (Statement* stmt = block->FirstNonPhiDef(); stmt != nullptr;)
{
Statement* next = stmt->GetNextStmt();
Expand All @@ -9052,10 +9054,7 @@ void Compiler::optRemoveRedundantZeroInits()
hasGCSafePoint = true;
}

if ((tree->gtFlags & GTF_EXCEPT) != 0)
{
canThrow = true;
}
hasImplicitControlFlow |= hasEHSuccs && ((tree->gtFlags & GTF_EXCEPT) != 0);

switch (tree->gtOper)
{
Expand Down Expand Up @@ -9201,7 +9200,8 @@ void Compiler::optRemoveRedundantZeroInits()
}
}

if (!removedExplicitZeroInit && isEntire && (!canThrow || !lclDsc->lvLiveInOutOfHndlr))
if (!removedExplicitZeroInit && isEntire &&
(!hasImplicitControlFlow || (lclDsc->lvTracked && !lclDsc->lvLiveInOutOfHndlr)))
{
// If compMethodRequiresPInvokeFrame() returns true, lower may later
// insert a call to CORINFO_HELP_INIT_PINVOKE_FRAME which is a gc-safe point.
Expand Down
51 changes: 51 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.aa

// Generated by Fuzzlyn v1.6 on 2023-09-03 15:59:01
// Run on X64 Windows
// Seed: 11520325105937570553
// Reduced from 294.5 KiB to 0.7 KiB in 00:04:32
// Debug: Outputs False
// Release: Outputs True
using System;
using System.Runtime.CompilerServices;
using Xunit;

public class Runtime_91576
{
[Fact]
public static int TestEntryPoint()
{
Assert.Throws<NullReferenceException>(() =>
{
Run(new int[1]);
Run(null);
});

return s_result;
}

static int s_result;

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Run(int[] l)
{
bool b = false;
try
{
int result = l[0];
b = true;
}
finally
{
Check(ref b);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Check(ref bool b)
{
s_result = b ? 101 : 100;
}
}

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