Skip to content

Commit 671d42c

Browse files
authored
JIT: Fix invalid zero-init suppression for untracked variables (#91580)
optRemoveRedundantZeroInits has logic to remove unnecessary zero inits if we can determine that the local will be zeroed in the prolog. In addition, it also has logic to suppress the prolog zero init if there is a dominating initialization already. The latter logic was trying to reason about liveness for untracked locals, which does not make sense. Fix #91576
1 parent 9f138e0 commit 671d42c

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

src/coreclr/jit/optimizer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9044,9 +9044,9 @@ void Compiler::optRemoveRedundantZeroInits()
90449044
CompAllocator allocator(getAllocator(CMK_ZeroInit));
90459045
LclVarRefCounts refCounts(allocator);
90469046
BitVecTraits bitVecTraits(lvaCount, this);
9047-
BitVec zeroInitLocals = BitVecOps::MakeEmpty(&bitVecTraits);
9048-
bool hasGCSafePoint = false;
9049-
bool canThrow = false;
9047+
BitVec zeroInitLocals = BitVecOps::MakeEmpty(&bitVecTraits);
9048+
bool hasGCSafePoint = false;
9049+
bool hasImplicitControlFlow = false;
90509050

90519051
assert(fgNodeThreading == NodeThreading::AllTrees);
90529052

@@ -9057,6 +9057,8 @@ void Compiler::optRemoveRedundantZeroInits()
90579057
CompAllocator allocator(getAllocator(CMK_ZeroInit));
90589058
LclVarRefCounts defsInBlock(allocator);
90599059
bool removedTrackedDefs = false;
9060+
bool hasEHSuccs = block->HasPotentialEHSuccs(this);
9061+
90609062
for (Statement* stmt = block->FirstNonPhiDef(); stmt != nullptr;)
90619063
{
90629064
Statement* next = stmt->GetNextStmt();
@@ -9067,10 +9069,7 @@ void Compiler::optRemoveRedundantZeroInits()
90679069
hasGCSafePoint = true;
90689070
}
90699071

9070-
if ((tree->gtFlags & GTF_EXCEPT) != 0)
9071-
{
9072-
canThrow = true;
9073-
}
9072+
hasImplicitControlFlow |= hasEHSuccs && ((tree->gtFlags & GTF_EXCEPT) != 0);
90749073

90759074
switch (tree->gtOper)
90769075
{
@@ -9216,7 +9215,8 @@ void Compiler::optRemoveRedundantZeroInits()
92169215
}
92179216
}
92189217

9219-
if (!removedExplicitZeroInit && isEntire && (!canThrow || !lclDsc->lvLiveInOutOfHndlr))
9218+
if (!removedExplicitZeroInit && isEntire &&
9219+
(!hasImplicitControlFlow || (lclDsc->lvTracked && !lclDsc->lvLiveInOutOfHndlr)))
92209220
{
92219221
// If compMethodRequiresPInvokeFrame() returns true, lower may later
92229222
// insert a call to CORINFO_HELP_INIT_PINVOKE_FRAME which is a gc-safe point.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.aa
3+
4+
// Generated by Fuzzlyn v1.6 on 2023-09-03 15:59:01
5+
// Run on X64 Windows
6+
// Seed: 11520325105937570553
7+
// Reduced from 294.5 KiB to 0.7 KiB in 00:04:32
8+
// Debug: Outputs False
9+
// Release: Outputs True
10+
using System;
11+
using System.Runtime.CompilerServices;
12+
using Xunit;
13+
14+
public class Runtime_91576
15+
{
16+
[Fact]
17+
public static int TestEntryPoint()
18+
{
19+
Assert.Throws<NullReferenceException>(() =>
20+
{
21+
Run(new int[1]);
22+
Run(null);
23+
});
24+
25+
return s_result;
26+
}
27+
28+
static int s_result;
29+
30+
[MethodImpl(MethodImplOptions.NoInlining)]
31+
private static void Run(int[] l)
32+
{
33+
bool b = false;
34+
try
35+
{
36+
int result = l[0];
37+
b = true;
38+
}
39+
finally
40+
{
41+
Check(ref b);
42+
}
43+
}
44+
45+
[MethodImpl(MethodImplOptions.NoInlining)]
46+
private static void Check(ref bool b)
47+
{
48+
s_result = b ? 101 : 100;
49+
}
50+
}
51+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)