Skip to content
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
13 changes: 13 additions & 0 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,19 @@ bool Compiler::optTryUnrollLoop(FlowGraphNaturalLoop* loop, bool* changedIR)
return false;
}

// The loop test must be both an exit and a backedge.
// FlowGraphNaturalLoop::AnalyzeIteration ensures it is an exit but we must
// make sure it is a backedge so that we can legally redirect it to the
// next iteration. If it isn't a backedge then redirecting it would skip
// all code between the loop test and the backedge.
assert(loop->ContainsBlock(iterInfo.TestBlock->GetTrueTarget()) !=
loop->ContainsBlock(iterInfo.TestBlock->GetFalseTarget()));
if (!iterInfo.TestBlock->TrueTargetIs(loop->GetHeader()) && !iterInfo.TestBlock->FalseTargetIs(loop->GetHeader()))
{
JITDUMP("Failed to unroll loop " FMT_LP ": test block is not a backedge\n", loop->GetIndex());
return false;
}

// Get the loop data:
// - initial constant
// - limit constant
Expand Down
33 changes: 33 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_97321/Runtime_97321.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Runtime.CompilerServices;
using Xunit;

public class Runtime_97321
{
[Fact]
public static int TestEntryPoint() => Foo(false);

[MethodImpl(MethodImplOptions.NoInlining)]
private static int Foo(bool b)
{
int sum = 1;
int i = 0;
goto Header;
Top:;
sum += 33;

Header:;
if (b)
{
goto Header;
}

Bottom:
i++;
if (i < 4)
{
goto Top;
}

return sum;
}
}
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>