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: Give up on the tail call if there are unexpected blocks after it #50806

Merged
merged 7 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 25 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7733,7 +7733,31 @@ GenTree* Compiler::fgMorphPotentialTailCall(GenTreeCall* call)
//
if (nextBlock->bbJumpKind != BBJ_RETURN)
{
BasicBlock* const nextNextBlock = nextBlock->GetUniqueSucc();
BasicBlock* nextNextBlock = nextBlock->GetUniqueSucc();

// Check if we have a sequence of GT_ASG blocks where the same variable is assigned to temps over
// and over (can happen after multiple GDVs of potential tall calls).
// TODO-CQ: Don't introduce new temps when we replace GT_RET_EXPR which is already assigned to a
// local.
if (nextNextBlock->bbJumpKind != BBJ_RETURN)
{
assert(nextBlock->firstStmt() == nextBlock->lastStmt());
GenTree* asgNode = nextBlock->firstStmt()->GetRootNode();
assert(asgNode->OperIs(GT_ASG));

unsigned lcl = asgNode->gtGetOp1()->AsLclVarCommon()->GetLclNum();

while (nextNextBlock->bbJumpKind != BBJ_RETURN)
{
assert(nextNextBlock->firstStmt() == nextNextBlock->lastStmt());
asgNode = nextNextBlock->firstStmt()->GetRootNode();
assert(asgNode->OperIs(GT_ASG));
assert(lcl == asgNode->gtGetOp2()->AsLclVarCommon()->GetLclNum());
lcl = asgNode->gtGetOp1()->AsLclVarCommon()->GetLclNum();
nextNextBlock = nextNextBlock->GetUniqueSucc();
}
}

assert(nextNextBlock->bbJumpKind == BBJ_RETURN);

if (nextNextBlock->hasProfileWeight())
Expand Down
42 changes: 42 additions & 0 deletions src/tests/JIT/opt/Devirtualization/GitHub_50492.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Threading;

class Base
{
public virtual int Test(Base b1, Base b2, bool p) => 0;

public virtual int Foo() => 1;
}

class ClassA : Base
{
public override int Test(Base b1, Base b2, bool p) => p ? b2.Foo() : 42;
}

class ClassB : ClassA
{
public override int Test(Base b1, Base b2, bool p) => b1.Test(b1, b2, p);
}

class Program
{
public static int Main()
{
for (int i = 0; i < 100; i++)
{
// Make sure it doesn't assert, see https://github.com/dotnet/runtime/issues/50492
Test(new ClassB(), new ClassA(), new Base(), true);
Thread.Sleep(15);
}
return 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static int Test(Base b0, Base b1, Base b2, bool p)
{
return b0.Test(b1, b2, p);
}
}
9 changes: 9 additions & 0 deletions src/tests/JIT/opt/Devirtualization/GitHub_50492.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="GitHub_50492.cs" />
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>