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 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
35 changes: 27 additions & 8 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11141,6 +11141,7 @@ void Compiler::fgValueNumberPhiDef(GenTreeLclVar* newSsaDef, BasicBlock* blk, bo

GenTreePhi* phiNode = newSsaDef->AsLclVar()->Data()->AsPhi();
ValueNumPair sameVNP;
VNSet loopInvariantCache(getAllocator(CMK_ValueNumber));

for (GenTreePhi::Use& use : phiNode->Uses())
{
Expand All @@ -11161,16 +11162,34 @@ 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");
}
FlowGraphNaturalLoop* const blockLoop = m_loops->GetLoopByHeader(blk);
bool const canUseNewVN = optVNIsLoopInvariant(phiArgVNP.GetConservative(), blockLoop, &loopInvariantCache);

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
{
JITDUMP("Can't update phi arg [%06u] with " FMT_VN " -- varies in " FMT_LP "\n", dspTreeID(phiArg),
phiArgVNP.GetConservative(), blockLoop->GetIndex());

// Code below uses phiArgVNP, reset to the old value
//
phiArgVNP = phiArg->gtVNPair;
}
}

phiArg->gtVNPair = phiArgVNP;

Expand Down
47 changes: 47 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,47 @@
// 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 Problem1(int x)
{
int y = 0;
while (x != 0)
{
if (y == x) return -1;
y = x;
Update(ref x);
}
return x;
}

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

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

[Fact]
public static int Test1() => Problem1(10) + 100;

[Fact]
public static int Test2() => Problem2(10) + 100;
}
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>
Loading