-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly type morphed NEG nodes (dotnet/coreclr#18837)
* Properly type optimized NEG nodes When the JIT was morphing trees like '-1 * expr', it would turn the multiplication into a NEG node with the same type as its right operand. This is a problem when the right operand was a small type like TYP_UBYTE because the NEG node always produces a widened result. This could cause problems when the negation was fed into something that depended on the type, such as a cast to TYP_UBYTE: here the JIT would conclude that the cast could be dropped, and end up producing a widened result. The solution is to give the tree the actual type of the NEG node. Also add a test for this case and for a similar case of '0 - expr', which already had a fix. Fix dotnet/coreclr#18780 * Address PR feedback * Clarify comment Commit migrated from dotnet/coreclr@c06c61a
- Loading branch information
1 parent
062b600
commit 96a0e22
Showing
5 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_18780/GitHub_18780.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
public class GitHub_18780 | ||
{ | ||
public static int Main() | ||
{ | ||
bool ok = true; | ||
ok &= M1(0); | ||
ok &= M2(); | ||
ok &= M3(); | ||
return ok ? 100 : -1; | ||
} | ||
|
||
// The multiplication by uint.MaxValue was optimized to a NEG | ||
// which was typed as the second operand, giving a byte NEG node. | ||
// With x86/ARM32 RyuJIT the cast to ulong would then treat vr13 | ||
// as a byte instead of a uint and zero extend from 8 bits. | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static bool M1(byte arg2) | ||
{ | ||
byte vr23 = arg2++; | ||
return (ulong)(uint.MaxValue * arg2) == uint.MaxValue; | ||
} | ||
|
||
// Like above, the -1 multiplication was turned into a byte NEG node. | ||
// The byte cast was then removed, but since the NEG byte node still | ||
// produces a wide result this meant (byte)val was -1. | ||
static byte s_1 = 1; | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static bool M2() | ||
{ | ||
return (byte)(-1 * s_1) == 255; | ||
} | ||
|
||
// Exactly the same as above, but this tests the optimization for | ||
// transforming (0 - expr) into -expr. | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static bool M3() | ||
{ | ||
return (byte)(0 - s_1) == 255; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_18780/GitHub_18780.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<AssemblyName>$(MSBuildProjectName)</AssemblyName> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> | ||
</PropertyGroup> | ||
<!-- Default configurations to help VS understand the configurations --> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup> | ||
<ItemGroup> | ||
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> | ||
<Visible>False</Visible> | ||
</CodeAnalysisDependentAssemblyPaths> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<DebugType></DebugType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> | ||
</ItemGroup> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> | ||
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup> | ||
</Project> |