Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 27 additions & 10 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5918,10 +5918,33 @@ 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 (BasicBlock* const pred : block->PredBlocks())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use BlockPredsWithEH... It probably doesn't actually matter since we walk forwards with block->GetUniqueSucc(), so won't ever end up at a handler, but it would be more consistent with the graph that the DFS tree is constructed over.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done... no change in SPMI diffs.

{
if (m_dfsTree->IsAncestor(block, pred))
{
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 +6061,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 +6139,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>