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: limit phi-refinement to loop-invariant VNs #106229

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 47 additions & 8 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11161,16 +11161,55 @@ void Compiler::fgValueNumberPhiDef(GenTreeLclVar* newSsaDef, BasicBlock* blk, bo

ValueNumPair phiArgVNP = lvaGetDesc(phiArg)->GetPerSsaData(phiArg->GetSsaNum())->m_vnPair;

#ifdef DEBUG
if (verbose && isUpdate && (phiArgVNP != phiArg->gtVNPair))
if (isUpdate && (phiArgVNP != phiArg->gtVNPair))
{
printf("Updating phi arg [%06u] VN from ", dspTreeID(phiArg));
vnpPrint(phiArg->gtVNPair, 0);
printf(" to ");
vnpPrint(phiArgVNP, 0);
printf("\n");
}
bool canUseNewVN = false;

// We can potentially refine this phi arg.
// Make sure the new phi arg VN is loop invariant.
//
FlowGraphNaturalLoop* const vnLoop = vnStore->LoopOfVN(phiArgVNP.GetConservative());
Copy link
Member

Choose a reason for hiding this comment

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

Is this sufficient? Doesn't it need to use the same logic as optVNIsLoopInvariant?

Copy link
Member

Choose a reason for hiding this comment

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

E.g. for

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Problem(int x)
    {
        int y = 0;
        while (x != 0)
        {
            if (y == x + 1) return -1;
            y = x + 1;
            Update(ref x);
        }
        return x;
    }

the VN will be VNF_ADD and LoopOfVN will return nullptr

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right.

We can directly use optVNIsLoopInvariant since we only ever try updating phis for loops.


if (vnLoop != nullptr)
{
FlowGraphNaturalLoop* const blockLoop = m_loops->GetLoopByHeader(blk);
assert(blockLoop != nullptr);
canUseNewVN = !blockLoop->ContainsLoop(vnLoop);

if (!canUseNewVN)
{
JITDUMP("Can't refine [%06u] with " FMT_VN " -- varies in " FMT_LP ", contained in " FMT_LP "\n",
dspTreeID(phiArg), phiArgVNP.GetConservative(), vnLoop->GetIndex(), blockLoop->GetIndex());
}
}
else
{
// phiArgVNP is invariant in all loops
//
canUseNewVN = true;
}

if (canUseNewVN)
{

#ifdef DEBUG
if (verbose)
{
printf("Updating phi arg [%06u] VN from ", dspTreeID(phiArg));
vnpPrint(phiArg->gtVNPair, 0);
printf(" to ");
vnpPrint(phiArgVNP, 0);
printf("\n");
}
#endif
}
else
{
// Code below uses phiArgVNP, reset to the old value
//
phiArgVNP = phiArg->gtVNPair;
}
}

phiArg->gtVNPair = phiArgVNP;

Expand Down
31 changes: 31 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_105792/Runtime_105792.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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_105792
{
[MethodImpl(MethodImplOptions.NoInlining)]
static int Problem(int x)
{
int y = 0;
while (x != 0)
{
if (y == x) return -1;
y = x;
Update(ref x);
}
return x;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Update(ref int x)
{
x = x - 1;
}

[Fact]
public static int Test() => Problem(10) + 100;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<ItemGroup>
<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" />
</ItemGroup>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<ItemGroup>
<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" />
</ItemGroup>

(It won't have any effect without RequiresProcessIsolation anyway)

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we get enough TC=0 coverage with external config settings.

</Project>
Loading