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

Ensure that constant folding for long->float is handled correctly #90325

Merged
merged 6 commits into from
Aug 14, 2023

Conversation

tannergooding
Copy link
Member

@tannergooding tannergooding commented Aug 10, 2023

This resolves #90323 and was raised by an internal partner team.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Aug 10, 2023
@ghost ghost assigned tannergooding Aug 10, 2023
@ghost
Copy link

ghost commented Aug 10, 2023

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

This resolves #90323

Author: tannergooding
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@JulieLeeMSFT JulieLeeMSFT added this to the 8.0.0 milestone Aug 10, 2023
@tannergooding tannergooding marked this pull request as ready for review August 11, 2023 17:35
@JulieLeeMSFT
Copy link
Member

@BruceForstall Please review. CC @dotnet/jit-contrib

</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="1" />
Copy link
Member

Choose a reason for hiding this comment

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

Tests using this also require (something like):

<PropertyGroup>
  <!-- Needed for CLRTestEnvironmentVariable -->
  <RequiresProcessIsolation>true</RequiresProcessIsolation>
</PropertyGroup>

[MethodImpl(MethodImplOptions.NoInlining)]
private static float ConvertToSingle(long value) => (float)value;

// 32-bit currently performs a 2-step conversion which causes a different result to be produced
Copy link
Member

Choose a reason for hiding this comment

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

"currently" is a problematic word for a long-lived test.

Copy link
Member Author

Choose a reason for hiding this comment

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

The current plan of record is to fix the places that are doing incorrect conversions early in .NET 9.

I can certainly remove "currently" in the interim if that helps?

#if defined(TARGET_64BIT)
if (tree->IsUnsigned())
{
f1 = (float)UINT32(i1);
Copy link
Member

Choose a reason for hiding this comment

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

Is the problem we were converting UINT32 to double when calling forceCastToFloat, before converting to float in that function?

Seems like we should get rid of forceCastToFloat and forceCastToUInt32 (their comments refer to ancient build compilers). But maybe all casts to/from floats should be explicit function calls to FloatingPointUtils functions. E.g., here maybe add FloatingPointUtils::convertUInt32ToFloat()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, the problem was that it was doing uint->double->float which can produce a different result from uint->float in some edge cases.

64-bit currently does uint->long->float; where-as 32-bit currently does uint->long->double->float, so this ensures the constant folding matches that behavior.

Longer term it would be beneficial to use helpers for these conversions, but that is a much bigger and more involved change. I tried to keep this as minimal as possible for .NET 8 to avoid churn/risk.

Copy link
Member

@BruceForstall BruceForstall left a comment

Choose a reason for hiding this comment

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

Can you make sure there's a .NET 9 GitHub issue detailing the follow-up work (if there isn't one already)?

@tannergooding
Copy link
Member Author

Can you make sure there's a .NET 9 GitHub issue detailing the follow-up work (if there isn't one already)?

It's tracked by #61885, which I've explicitly moved to the 9 milestone

@tannergooding tannergooding merged commit 5d8dc41 into dotnet:main Aug 14, 2023
137 checks passed
@tannergooding tannergooding deleted the flt-cns-fold-bug branch August 14, 2023 15:18
@ghost ghost locked as resolved and limited conversation to collaborators Sep 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Constant folding for long->float produces an incorrect result.
3 participants