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] LclMorph GT_IND(GT_LCL_VAR_ADDR) => GT_CAST(GT_LCL_VAR) narrow-cast only #81454

Merged
merged 30 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e05deac
Experiment with folding IND(GT_LCL_VAR_ADDR)
TIHan Feb 1, 2023
afdf056
Fix assertion
TIHan Feb 1, 2023
19f19ef
Remove duplicate code
TIHan Feb 1, 2023
c17e46c
Handling long types
TIHan Feb 1, 2023
343d827
Handling long types
TIHan Feb 1, 2023
3a141c0
Handling long types
TIHan Feb 1, 2023
af037ec
Handling long types
TIHan Feb 1, 2023
936dbae
Trying to fix build
TIHan Feb 1, 2023
69ff4b2
Trying to fix build
TIHan Feb 1, 2023
be3674c
Trying to fix builds
TIHan Feb 3, 2023
56dd025
Merge remote-tracking branch 'upstream/main' into fold-gt-lcl-var-addr
TIHan Feb 3, 2023
832666e
Added IndirTransform::CastOfLclVar
TIHan Feb 4, 2023
579b415
Fixing build
TIHan Feb 4, 2023
054c955
Some formatting
TIHan Feb 4, 2023
04429cb
Skip isDef
TIHan Feb 4, 2023
3a96f83
Formatting
TIHan Feb 4, 2023
9e12992
Fixed AV
TIHan Feb 4, 2023
e40b1b7
Enable long
TIHan Feb 6, 2023
e81aad1
Renamed CastOfLclVar to NarrowCastOfLclVar
TIHan Feb 7, 2023
a4b8bd8
Trying out wide-cast ind(lcl_var_addr) => cast(lcl_var)
TIHan Feb 7, 2023
8ebd1ef
Formatting
TIHan Feb 7, 2023
73ffc26
Remove widening
TIHan Feb 7, 2023
aa91384
Merge remote-tracking branch 'upstream/main' into fold-gt-lcl-var-addr
TIHan Feb 14, 2023
97b7bf1
Added a comment. Added disasm tests.
TIHan Feb 14, 2023
d48d744
Fixing tests
TIHan Feb 14, 2023
ccc56b1
Rename NarrowCastOfLclVar to NarrowCast
TIHan Feb 14, 2023
4ac5f4c
Feedback
TIHan Feb 15, 2023
7e695f3
Feedback
TIHan Feb 15, 2023
a3c0ab8
Merge remote-tracking branch 'upstream/main' into fold-gt-lcl-var-addr
TIHan Feb 15, 2023
93d81b6
Update src/coreclr/jit/lclmorph.cpp
TIHan Feb 15, 2023
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
21 changes: 20 additions & 1 deletion src/coreclr/jit/lclmorph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
None,
Nop,
BitCast,
NarrowCast,
#ifdef FEATURE_HW_INTRINSICS
GetElement,
WithElement,
Expand Down Expand Up @@ -1138,6 +1139,7 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
unsigned lclNum = val.LclNum();
LclVarDsc* varDsc = m_compiler->lvaGetDesc(lclNum);
GenTreeLclVarCommon* lclNode = nullptr;
bool isDef = (user != nullptr) && user->OperIs(GT_ASG) && (user->AsOp()->gtGetOp1() == indir);

switch (transform)
{
Expand Down Expand Up @@ -1244,6 +1246,17 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
lclNode = indir->AsLclVarCommon();
break;

case IndirTransform::NarrowCast:
assert(varTypeIsIntegral(indir));
assert(varTypeIsIntegral(varDsc));
assert(genTypeSize(varDsc) >= genTypeSize(indir));
assert(!isDef);

lclNode = BashToLclVar(indir->gtGetOp1(), lclNum);
*val.Use() = m_compiler->gtNewCastNode(genActualType(indir), lclNode, false, indir->TypeGet());

TIHan marked this conversation as resolved.
Show resolved Hide resolved
break;

case IndirTransform::LclFld:
indir->ChangeOper(GT_LCL_FLD);
indir->AsLclFld()->SetLclNum(lclNum);
Expand All @@ -1266,7 +1279,7 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>

GenTreeFlags lclNodeFlags = GTF_EMPTY;

if (user->OperIs(GT_ASG) && (user->AsOp()->gtGetOp1() == indir))
if (isDef)
{
lclNodeFlags |= (GTF_VAR_DEF | GTF_DONT_CSE);

Expand Down Expand Up @@ -1365,6 +1378,12 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
}
#endif // FEATURE_HW_INTRINSICS

// Turn this into a narrow-cast if we can.
if (!isDef && varTypeIsIntegral(indir) && varTypeIsIntegral(varDsc))
{
return IndirTransform::NarrowCast;
}

// Turn this into a bitcast if we can.
if ((genTypeSize(indir) == genTypeSize(varDsc)) && (varTypeIsFloating(indir) || varTypeIsFloating(varDsc)))
{
Expand Down
46 changes: 46 additions & 0 deletions src/tests/JIT/opt/Unsafe/Unsafe.cs
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.

using System;
using System.Runtime.CompilerServices;

namespace CodeGenTests
{
class UnsafeTests
{
[MethodImpl(MethodImplOptions.NoInlining)]
static byte UnsafeAsNarrowCast_Short(short value)
{
// X64-NOT: dword ptr
return Unsafe.As<short, byte>(ref value);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static byte UnsafeAsNarrowCast_Int(int value)
{
// X64-NOT: dword ptr
return Unsafe.As<int, byte>(ref value);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static byte UnsafeAsNarrowCast_Long(long value)
{
// X64-NOT: qword ptr
return Unsafe.As<long, byte>(ref value);
}

static int Main()
{
if (UnsafeAsNarrowCast_Short(255) != 255)
return 0;

if (UnsafeAsNarrowCast_Int(255) != 255)
return 0;

if (UnsafeAsNarrowCast_Long(255) != 255)
return 0;

return 100;
}
}
}
17 changes: 17 additions & 0 deletions src/tests/JIT/opt/Unsafe/Unsafe.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs">
<HasDisasmCheck>true</HasDisasmCheck>
</Compile>

<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" />
<CLRTestEnvironmentVariable Include="DOTNET_JITMinOpts" Value="0" />
</ItemGroup>
</Project>