Skip to content

Commit

Permalink
[release/8.0] JIT: Fix illegal IR created by GetElement/ToScalar lowe…
Browse files Browse the repository at this point in the history
…ring (#91874)

* JIT: Fix illegal IR created by GetElement/ToScalar lowering

These could create TYP_ULONG/TYP_UINT indirs/LCL_FLD nodes, which are
illegal.

Fix #91174

* Add test

* Fix license header in new test

---------

Co-authored-by: Jakob Botsch Nielsen <jakob.botsch.nielsen@gmail.com>
  • Loading branch information
github-actions[bot] and jakobbotsch authored Sep 12, 2023
1 parent 52ff583 commit ac388a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
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 =
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.

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>

0 comments on commit ac388a1

Please sign in to comment.