Skip to content

Commit

Permalink
Do not change gtRetClsHnd in impNormStructVal (#70699)
Browse files Browse the repository at this point in the history
* Do not change "gtRetClsHdl" in "impNormStructVal"

Doing so leads breaking the proper ABI handling for the call.

* Add a test
  • Loading branch information
SingleAccretion authored Jun 15, 2022
1 parent 42d76d3 commit 4501e39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1723,21 +1723,14 @@ GenTree* Compiler::impNormStructVal(GenTree* structVal,
genTreeOps oper = structVal->OperGet();
switch (oper)
{
// GT_RETURN and GT_MKREFANY don't capture the handle.
case GT_RETURN:
break;
// GT_MKREFANY is supported directly by args morphing.
case GT_MKREFANY:
alreadyNormalized = true;
break;

case GT_CALL:
structVal->AsCall()->gtRetClsHnd = structHnd;
makeTemp = true;
break;

case GT_RET_EXPR:
structVal->AsRetExpr()->gtRetClsHnd = structHnd;
makeTemp = true;
makeTemp = true;
break;

case GT_FIELD:
Expand All @@ -1758,7 +1751,6 @@ GenTree* Compiler::impNormStructVal(GenTree* structVal,

case GT_OBJ:
case GT_BLK:
case GT_ASG:
// These should already have the appropriate type.
assert(structVal->gtType == structType);
alreadyNormalized = true;
Expand All @@ -1771,10 +1763,8 @@ GenTree* Compiler::impNormStructVal(GenTree* structVal,
break;

case GT_CNS_VEC:
{
assert(varTypeIsSIMD(structVal) && (structVal->gtType == structType));
break;
}

#ifdef FEATURE_SIMD
case GT_SIMD:
Expand Down
31 changes: 31 additions & 0 deletions src/tests/JIT/Directed/StructABI/TypeMismatchedArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

Expand All @@ -10,6 +12,7 @@ public unsafe class TypeMismatchedArgs
private static readonly HfaUnion s_hfaDblFlt = new HfaUnion { DblHfa = { FirstDblValue = 1.0, SecondDblValue = 2.0 } };
private static readonly HfaDblLngUnion s_dblLngHfa = new HfaDblLngUnion { DblLng = { FirstLngValue = 10, SecondLngValue = 20 } };
private static readonly FourDblLngUnion s_fourDblLngHfa = new FourDblLngUnion { Lngs = { LongOne = 30 } };
private static readonly Vtor128Union s_vtor128 = new Vtor128Union { Vtor4 = new Vector4(4, 3, 2, 1) };

public static int Main()
{
Expand All @@ -33,6 +36,11 @@ public static int Main()
return 104;
}

if (ProblemWithVectorCallArg())
{
return 105;
}

return 100;
}

Expand Down Expand Up @@ -70,13 +78,36 @@ private static bool ProblemWithSplitStructHfaMismatch(FourDoublesHfaStruct fourD
return result != s_fourDblLngHfa.Lngs.LongOne;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool ProblemWithVectorCallArg()
{
var result = CallForVector4(GetVector128().AsVector4());

return result != s_vtor128.Vtor4.X;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static float CallForVector4(Vector4 value) => value.X;

[MethodImpl(MethodImplOptions.NoInlining)]
private static Vector128<float> GetVector128() => s_vtor128.Vtor128;

[MethodImpl(MethodImplOptions.NoInlining)]
private static double CallForHfaDblStruct(HfaDblStruct value) => value.FirstDblValue;

[MethodImpl(MethodImplOptions.NoInlining)]
private static long CallForSplitStructWithFourLongs(int arg0, int arg1, StructWithFourLongs splitArg) => splitArg.LongOne;
}

[StructLayout(LayoutKind.Explicit)]
struct Vtor128Union
{
[FieldOffset(0)]
public Vector4 Vtor4;
[FieldOffset(0)]
public Vector128<float> Vtor128;
}

[StructLayout(LayoutKind.Explicit)]
struct HfaDblLngUnion
{
Expand Down

0 comments on commit 4501e39

Please sign in to comment.