From 2b863217d84e6e0b02d60bc50e6624fd1d0f25ae Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Fri, 27 May 2022 14:55:50 -0700 Subject: [PATCH] JIT: allow pinvoke calli helper in TreatAsShouldHaveRetBufArg If we have multiple cold pinvokes in return positions that return structs by value and we are merging returns, we will end up querying these calls post-morph, and may see them using CORINFO_HELP_PINVOKE_CALLI. Fixes #69612. --- src/coreclr/jit/gentree.cpp | 12 ++-- .../JitBlue/Runtime_69612/Runtime_69612.cs | 64 +++++++++++++++++++ .../Runtime_69612/Runtime_69612.csproj | 26 ++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.csproj diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 526e1da038475a..ea690b31eef2ce 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -2097,14 +2097,14 @@ bool GenTreeCall::TreatAsShouldHaveRetBufArg(Compiler* compiler) const return true; } - // If we see a Jit helper call that returns a TYP_STRUCT we will + // If we see a Jit helper call that returns a TYP_STRUCT we may // transform it as if it has a Return Buffer Argument // if (IsHelperCall() && (gtReturnType == TYP_STRUCT)) { - // There are two possible helper calls that use this path: - // CORINFO_HELP_GETFIELDSTRUCT and CORINFO_HELP_UNBOX_NULLABLE - // + // There are three possible helper calls that use this path: + // CORINFO_HELP_GETFIELDSTRUCT, CORINFO_HELP_UNBOX_NULLABLE + // CORINFO_HELP_PINVOKE_CALLI CorInfoHelpFunc helpFunc = compiler->eeGetHelperNum(gtCallMethHnd); if (helpFunc == CORINFO_HELP_GETFIELDSTRUCT) @@ -2115,6 +2115,10 @@ bool GenTreeCall::TreatAsShouldHaveRetBufArg(Compiler* compiler) const { return true; } + else if (helpFunc == CORINFO_HELP_PINVOKE_CALLI) + { + return false; + } else { assert(!"Unexpected JIT helper in TreatAsShouldHaveRetBufArg"); diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.cs b/src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.cs new file mode 100644 index 00000000000000..a8859d53963e1b --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.cs @@ -0,0 +1,64 @@ +// 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.Threading; + +ref struct NewReference +{ + public IntPtr pointer; +} + +static unsafe class Delegates +{ + static Delegates() + { + PyLong_FromLongLong = (delegate* unmanaged[Cdecl])0xbadcab; + z = 100; + } + + public static delegate* unmanaged[Cdecl] PyLong_FromLongLong { get; } + public static long z; +} + +class Runtime_69612 +{ + static NewReference ToPython(object value, Type type) + { + TypeCode tc = Type.GetTypeCode(type); + + switch (tc) + { + case TypeCode.Byte: + return PyInt_FromInt32((byte)value); + case TypeCode.Int16: + return PyInt_FromInt32((short)value); + case TypeCode.UInt16: + return PyInt_FromInt32((ushort)value); + case TypeCode.Int32: + return PyInt_FromInt32((int)value); + default: + return new NewReference(); + } + } + + static NewReference PyInt_FromInt32(int value) => PyLong_FromLongLong(value); + + unsafe static NewReference PyLong_FromLongLong(long value) => Delegates.PyLong_FromLongLong(value); + + [MethodImpl(MethodImplOptions.NoOptimization)] + static int Main() + { + for (int i = 0; i < 100; i++) + { + _ = ToPython(Delegates.z, typeof(long)); + Thread.Sleep(15); + } + + Thread.Sleep(50); + _ = ToPython(Delegates.z, typeof(long)); + return 100; + + } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.csproj new file mode 100644 index 00000000000000..8015778d2fc9b3 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_69612/Runtime_69612.csproj @@ -0,0 +1,26 @@ + + + Exe + True + true + + + + + + + + + \ No newline at end of file