Skip to content

JIT: fix retyping of BLK ops when parent local is retyped #116027

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

Merged
merged 1 commit into from
May 29, 2025
Merged
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
85 changes: 70 additions & 15 deletions src/coreclr/jit/objectalloc.cpp
Original file line number Diff line number Diff line change
@@ -2142,7 +2142,7 @@ void ObjectAllocator::UpdateAncestorTypes(
assert(parentType == TYP_BYREF);
parent->ChangeType(newType);

// Propgate that upwards.
// Propagate that upwards.
//
++parentIndex;
keepChecking = true;
@@ -2195,20 +2195,40 @@ void ObjectAllocator::UpdateAncestorTypes(
else
{
assert(tree == parent->AsIndir()->Data());
GenTree* const addr = parent->AsIndir()->Addr();

// If we are storing to a GC struct field, we may need to retype the store
//
if (parent->OperIs(GT_STOREIND) && varTypeIsGC(parent->TypeGet()))
if (varTypeIsGC(parent->TypeGet()))
{
parent->ChangeType(newType);
}

// If we are storing a struct, we may need to change the layout
//
if (retypeFields && parent->OperIs(GT_STORE_BLK))
else if (retypeFields && parent->OperIs(GT_STORE_BLK))
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Consider adding a more detailed comment above this branch to clarify the specific conditions for adjusting the block layout during BLK store operations, especially for the case when only a portion of the local is updated.

Copilot uses AI. Check for mistakes.

{
parent->AsBlk()->SetLayout(newLayout);
GenTreeBlk* const block = parent->AsBlk();
ClassLayout* const oldLayout = block->GetLayout();

if (oldLayout->HasGCPtr())
{
if (newLayout->GetSize() == oldLayout->GetSize())
{
block->SetLayout(newLayout);
}
else
{
// We must be storing just a portion of the original local
//
assert(newLayout->GetSize() > oldLayout->GetSize());

if (newLayout->HasGCPtr())
{
block->SetLayout(GetByrefLayout(oldLayout));
}
else
{
block->SetLayout(GetNonGCLayout(oldLayout));
}
}
}
}
}
break;
@@ -2219,19 +2239,54 @@ void ObjectAllocator::UpdateAncestorTypes(
{
// If we are loading from a GC struct field, we may need to retype the load
//
if (retypeFields && (varTypeIsGC(parent->TypeGet())))
if (retypeFields)
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

It might be helpful to document the purpose of the 'didRetype' flag and the rationale for resetting 'retypeFields' to false in this load operation branch, to improve future maintainability and clarity.

Copilot uses AI. Check for mistakes.

{
parent->ChangeType(newType);
bool didRetype = false;

if (parent->OperIs(GT_BLK))
if (varTypeIsGC(parent->TypeGet()))
{
parent->AsBlk()->SetLayout(newLayout);
parent->ChangeType(newType);
didRetype = true;
}
else if (parent->OperIs(GT_BLK))
{
GenTreeBlk* const block = parent->AsBlk();
ClassLayout* const oldLayout = block->GetLayout();

++parentIndex;
keepChecking = true;
retypeFields = false;
if (oldLayout->HasGCPtr())
{
if (newLayout->GetSize() == oldLayout->GetSize())
{
block->SetLayout(newLayout);
}
else
{
// We must be loading just a portion of the original local
//
assert(newLayout->GetSize() > oldLayout->GetSize());

if (newLayout->HasGCPtr())
{
block->SetLayout(GetByrefLayout(oldLayout));
}
else
{
block->SetLayout(GetNonGCLayout(oldLayout));
}
}

didRetype = true;
}
}

if (didRetype)
{
++parentIndex;
keepChecking = true;
retypeFields = false;
}
}

break;
}

68 changes: 68 additions & 0 deletions src/tests/JIT/opt/ObjectStackAllocation/Runtime_115979.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Generated by Fuzzlyn v3.0 on 2025-05-25 16:50:09
// Run on Arm64 MacOS
// Seed: 18178428402533635742-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256
// Reduced from 127.3 KiB to 0.6 KiB in 00:01:07
// Hits JIT assert in Release:
// Assertion failed 'm_blockLayout->CanAssignFrom(m_src->GetLayout(m_comp))' in 'Program:Main(Fuzzlyn.ExecutionServer.IRuntime)' during 'Morph - Global' (IL size 75; hash 0xade6b36b; FullOpts)
//
// File: /Users/runner/work/1/s/src/coreclr/jit/morphblock.cpp Line: 668
//

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

public class C1
{
}

public struct S1
{
public Vector64<short> F0;
public long F2;
public C1 F6;
public ulong F7;
}

public struct S3
{
public C1 F4;
public S1 F6;
public S3(C1 f4, S1 f6) : this()
{
F4 = f4;
F6 = f6;
}
}

public class Runtime_115979
{
[Fact]
public static int Test()
{
int result = -1;
try
{
Problem();
}
catch (NullReferenceException)
{
result = 100;
}
return result;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Problem()
{
S1[] vr0 = default(S1[]);
S3 vr1 = new S3(new C1(), new S1());
S3 vr2 = new S3(vr0[0].F6, vr1.F6);
System.Console.WriteLine(vr2.F6.F0);
}
}
9 changes: 9 additions & 0 deletions src/tests/JIT/opt/ObjectStackAllocation/Runtime_115979.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading
Oops, something went wrong.