Skip to content

JIT: rely on DFS for cycle entry detection in optRemoveRedundantZeroInits #103788

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

Merged
merged 3 commits into from
Jun 21, 2024
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
39 changes: 29 additions & 10 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5918,10 +5918,35 @@ void Compiler::optRemoveRedundantZeroInits()

assert(fgNodeThreading == NodeThreading::AllTrees);

for (BasicBlock* block = fgFirstBB; (block != nullptr) && !block->HasFlag(BBF_MARKED);
block = block->GetUniqueSucc())
for (BasicBlock* block = fgFirstBB; block != nullptr; block = block->GetUniqueSucc())
{
block->SetFlags(BBF_MARKED);
if (m_dfsTree->HasCycle())
{
// See if this block is a cycle entry
//
bool stop = false;
for (FlowEdge* predEdge = BlockPredsWithEH(block); predEdge != nullptr;
predEdge = predEdge->getNextPredEdge())
{
BasicBlock* const predBlock = predEdge->getSourceBlock();
if (m_dfsTree->IsAncestor(block, predBlock))
{
JITDUMP(FMT_BB " is part of a cycle, stopping the block scan\n", block->bbNum);
stop = true;
break;
}
}

// If so, stop looking for redundant zero inits
//
if (stop)
{
break;
}
}

JITDUMP("Analyzing " FMT_BB "\n", block->bbNum);

CompAllocator allocator(getAllocator(CMK_ZeroInit));
LclVarRefCounts defsInBlock(allocator);
bool removedTrackedDefs = false;
Expand Down Expand Up @@ -6038,7 +6063,7 @@ void Compiler::optRemoveRedundantZeroInits()

if (tree->Data()->IsIntegralConst(0))
{
bool bbInALoop = block->HasFlag(BBF_BACKWARD_JUMP);
bool bbInALoop = false;
bool bbIsReturn = block->KindIs(BBJ_RETURN);

if (!bbInALoop || bbIsReturn)
Expand Down Expand Up @@ -6116,12 +6141,6 @@ void Compiler::optRemoveRedundantZeroInits()
}
}
}

for (BasicBlock* block = fgFirstBB; (block != nullptr) && block->HasFlag(BBF_MARKED);
block = block->GetUniqueSucc())
{
block->RemoveFlags(BBF_MARKED);
}
}

//------------------------------------------------------------------------
Expand Down
71 changes: 71 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_103477/Runtime_103477.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 Runtime_103477
{
static int s_count;

[Fact]
public static int Test()
{
int result = -1;
try
{
Problem();
result = 100;
}
catch (Exception)
{
}
return result;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Problem()
{
string s = "12151";
int i = 0;

char? a = null;
int count = 0;
while (true)
{
string? res = get(s, ref i, ref a);
if (res != null)
{
Count(res);
a = null; // !!! this line is removed from the published version
continue;
}

if (i >= s.Length)
break;

a = '.';
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Count(string s)
{
s_count++;
if (s_count > 5) throw new Exception();
}

[MethodImpl(MethodImplOptions.NoInlining)]
static string? get(string s, ref int index, ref char? a)
{
if (index >= s.Length)
return null;

if (a == '.')
return ".";

a ??= s[index++];
return (a == '1') ? "1" : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading