From f5acabe8f4b8528b7db0641d8ab505fcdf31da38 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Tue, 17 Sep 2024 09:18:13 -0700 Subject: [PATCH] JIT: use explicit cast away GC when passing byref to unmanaged callees (#107811) Revise the fix done in #39105 to make the cast from byref to native int via an explicit cast, rather than retyping the node. Fixes #107045. --- src/coreclr/jit/importercalls.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index c1f01983993f2..05ec46c51d199 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -2046,9 +2046,7 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, CORINFO_SIG_INFO* s // call - The call // // Remarks: -// This makes use of the fact that TYP_I_IMPL <-> TYP_BYREF casts are -// implicit in JIT IR, allowing us to change the types directly without -// inserting a cast node. +// Make the "casting away" of GC explicit here instead of retyping. // void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call) { @@ -2059,7 +2057,7 @@ void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call) // We should not be passing gc typed args to an unmanaged call. if (varTypeIsGC(argNode->TypeGet())) { - // Tolerate byrefs by retyping to native int. + // Tolerate byrefs by casting to native int. // // This is needed or we'll generate inconsistent GC info // for this arg at the call site (gc info says byref, @@ -2067,7 +2065,8 @@ void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call) // if (argNode->TypeGet() == TYP_BYREF) { - argNode->ChangeType(TYP_I_IMPL); + GenTree* cast = gtNewCastNode(TYP_I_IMPL, argNode, false, TYP_I_IMPL); + arg.SetEarlyNode(cast); } else {