From f803650b7e06e2b9089d73094edb212580748ab5 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Tue, 5 Sep 2023 12:43:07 +0200 Subject: [PATCH 1/2] JIT: Fix invalid zero-init supression for untracked variables 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 --- src/coreclr/jit/optimizer.cpp | 3 +- .../JitBlue/Runtime_91576/Runtime_91576.cs | 51 +++++++++++++++++++ .../Runtime_91576/Runtime_91576.csproj | 8 +++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.csproj diff --git a/src/coreclr/jit/optimizer.cpp b/src/coreclr/jit/optimizer.cpp index 1df34104d4238..e3bd7cb397607 100644 --- a/src/coreclr/jit/optimizer.cpp +++ b/src/coreclr/jit/optimizer.cpp @@ -9201,7 +9201,8 @@ void Compiler::optRemoveRedundantZeroInits() } } - if (!removedExplicitZeroInit && isEntire && (!canThrow || !lclDsc->lvLiveInOutOfHndlr)) + if (!removedExplicitZeroInit && isEntire && + (!canThrow || (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. diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.cs b/src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.cs new file mode 100644 index 0000000000000..4df185d99ab24 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.cs @@ -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(() => + { + 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; + } +} + diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.csproj new file mode 100644 index 0000000000000..de6d5e08882e8 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_91576/Runtime_91576.csproj @@ -0,0 +1,8 @@ + + + True + + + + + From ea2af399ab529150e3172f9df1a919a41cbe2857 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Tue, 5 Sep 2023 16:17:19 +0200 Subject: [PATCH 2/2] Make EH succ logic less conservative --- src/coreclr/jit/optimizer.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/coreclr/jit/optimizer.cpp b/src/coreclr/jit/optimizer.cpp index e3bd7cb397607..899a76a49cfd5 100644 --- a/src/coreclr/jit/optimizer.cpp +++ b/src/coreclr/jit/optimizer.cpp @@ -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); @@ -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(); @@ -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) { @@ -9202,7 +9201,7 @@ void Compiler::optRemoveRedundantZeroInits() } if (!removedExplicitZeroInit && isEntire && - (!canThrow || (lclDsc->lvTracked && !lclDsc->lvLiveInOutOfHndlr))) + (!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.