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: Fix illegal IR created by GetElement/ToScalar lowering #91272

Merged
merged 2 commits into from
Aug 30, 2023
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
24 changes: 12 additions & 12 deletions src/coreclr/jit/lowerxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3754,17 +3754,14 @@ GenTree* Lowering::LowerHWIntrinsicGetElement(GenTreeHWIntrinsic* node)
// We want to optimize GetElement down to an Indir where possible as
// this unlocks additional containment opportunities for various nodes

var_types newType;
GenTree* newBase;
GenTree* newIndex;
uint32_t newScale;
int32_t newOffset;
GenTree* newBase;
GenTree* newIndex;
uint32_t newScale;
int32_t newOffset;

GenTreeIndir* indir = op1->AsIndir();
GenTree* addr = indir->Addr();

newType = simdBaseType;

if (addr->OperIsAddrMode())
{
// We have an existing addressing mode, so we want to try and
Expand Down Expand Up @@ -3860,7 +3857,8 @@ GenTree* Lowering::LowerHWIntrinsicGetElement(GenTreeHWIntrinsic* node)
new (comp, GT_LEA) GenTreeAddrMode(addr->TypeGet(), newBase, newIndex, newScale, newOffset);
BlockRange().InsertBefore(node, newAddr);

GenTreeIndir* newIndir = comp->gtNewIndir(newType, newAddr, (indir->gtFlags & GTF_IND_FLAGS));
GenTreeIndir* newIndir =
Copy link
Member

Choose a reason for hiding this comment

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

Would it be good to cache the JITtype2varType(simdBaseJitType) somewhere, rather than duplicating it in 3 places?

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 preferred the minor duplication here since we don't expect these transformations to kick in that often, so we would usually end up not using the computed value if we did it before.

comp->gtNewIndir(JITtype2varType(simdBaseJitType), newAddr, (indir->gtFlags & GTF_IND_FLAGS));
BlockRange().InsertBefore(node, newIndir);

LIR::Use use;
Expand Down Expand Up @@ -3911,8 +3909,8 @@ GenTree* Lowering::LowerHWIntrinsicGetElement(GenTreeHWIntrinsic* node)

if (lclDsc->lvDoNotEnregister && (lclOffs <= 0xFFFF) && ((lclOffs + elemSize) <= lclDsc->lvExactSize()))
{
GenTree* lclFld =
comp->gtNewLclFldNode(lclVar->GetLclNum(), simdBaseType, static_cast<uint16_t>(lclOffs));
GenTree* lclFld = comp->gtNewLclFldNode(lclVar->GetLclNum(), JITtype2varType(simdBaseJitType),
static_cast<uint16_t>(lclOffs));
BlockRange().InsertBefore(node, lclFld);

LIR::Use use;
Expand Down Expand Up @@ -5327,7 +5325,8 @@ GenTree* Lowering::LowerHWIntrinsicToScalar(GenTreeHWIntrinsic* node)

GenTreeIndir* indir = op1->AsIndir();

GenTreeIndir* newIndir = comp->gtNewIndir(simdBaseType, indir->Addr(), (indir->gtFlags & GTF_IND_FLAGS));
GenTreeIndir* newIndir =
comp->gtNewIndir(JITtype2varType(simdBaseJitType), indir->Addr(), (indir->gtFlags & GTF_IND_FLAGS));
BlockRange().InsertBefore(node, newIndir);

LIR::Use use;
Expand Down Expand Up @@ -5359,7 +5358,8 @@ GenTree* Lowering::LowerHWIntrinsicToScalar(GenTreeHWIntrinsic* node)

if (lclDsc->lvDoNotEnregister && (lclOffs <= 0xFFFF) && ((lclOffs + elemSize) <= lclDsc->lvExactSize()))
{
GenTree* lclFld = comp->gtNewLclFldNode(lclVar->GetLclNum(), simdBaseType, lclVar->GetLclOffs());
GenTree* lclFld =
comp->gtNewLclFldNode(lclVar->GetLclNum(), JITtype2varType(simdBaseJitType), lclVar->GetLclOffs());
BlockRange().InsertBefore(node, lclFld);

LIR::Use use;
Expand Down
30 changes: 30 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_91174/Runtime_91174.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.aa

using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using Xunit;

public class Runtime_91174
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static int Foo(ref Vector256<uint> v1, ref Vector256<uint> v2)
{
if (Vector256.ToScalar(v1) < Vector256.ToScalar(v2))
{
Console.WriteLine("FAIL");
return 101;
}

return 100;
}

[Fact]
public static int TestEntryPoint()
{
Vector256<uint> v1 = Vector256.Create<uint>(20);
Vector256<uint> v2 = Vector256.Create<uint>(10);
return Foo(ref v1, ref v2);
}
}
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>