Skip to content
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

JIT: avoid cloning mid-entry loops with multiple non-loop entry preds #70959

Merged
merged 1 commit into from
Jun 21, 2022
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
19 changes: 19 additions & 0 deletions src/coreclr/jit/loopcloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,25 @@ bool Compiler::optIsLoopClonable(unsigned loopInd)
return false;
}

// Reject cloning if this is a mid-entry loop and the entry has non-loop predecessors other than its head.
// This loop may be part of a larger looping construct that we didn't recognize.
//
// We should really fix this in optCanonicalizeLoop.
//
if (!loop.lpIsTopEntry())
{
for (BasicBlock* const entryPred : loop.lpEntry->PredBlocks())
{
if ((entryPred != loop.lpHead) && !loop.lpContains(entryPred))
{
JITDUMP("Loop cloning: rejecting loop " FMT_LP
". Is not top entry, and entry has multiple non-loop preds.\n",
loopInd);
return false;
}
}
}

// We've previously made a decision whether to have separate return epilogs, or branch to one.
// There's a GCInfo limitation in the x86 case, so that there can be no more than SET_EPILOGCNT_MAX separate
// epilogs. Other architectures have a limit of 4 here for "historical reasons", but this should be revisited
Expand Down
64 changes: 64 additions & 0 deletions src/tests/JIT/opt/Cloning/Runtime_70802.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 System.Threading;

public class B
{
public virtual int V() => 33;
}

public class D : B
{
public override int V() => 44;
}

class Runtime_70802
{
[MethodImpl(MethodImplOptions.NoInlining)]
static void G() {}

[MethodImpl(MethodImplOptions.NoInlining)]
static int F(B b, int n = 10, int m = 10)
{
int i = 0;
int j = 0;
int r = 0;
goto mid;
top:
G();
mid:
r += b.V();
i++;
if (i < n) goto top;
j++;
if (i < m) goto mid;
return r;
}

public static int Main()
{
D d = new D();

for (int i = 0; i < 100; i++)
{
_ = F(d);
Thread.Sleep(15);
}

Thread.Sleep(50);

int r = 0;

for (int i = 0; i < 100; i++)
{
r += F(d);
}

Console.WriteLine($"result is {r} (expected 44000)");

return r / 440;
}
}
19 changes: 19 additions & 0 deletions src/tests/JIT/opt/Cloning/Runtime_70802.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<PropertyGroup>
<CLRTestBatchPreCommands><![CDATA[
$(CLRTestBatchPreCommands)
set COMPlus_TieredPGO=1
]]></CLRTestBatchPreCommands>
<BashCLRTestPreCommands><![CDATA[
$(BashCLRTestPreCommands)
export COMPlus_TieredPGO=1
]]></BashCLRTestPreCommands>
</PropertyGroup>
</Project>