Skip to content

Commit f5acabe

Browse files
authored
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.
1 parent 3992788 commit f5acabe

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/coreclr/jit/importercalls.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,9 +2046,7 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, CORINFO_SIG_INFO* s
20462046
// call - The call
20472047
//
20482048
// Remarks:
2049-
// This makes use of the fact that TYP_I_IMPL <-> TYP_BYREF casts are
2050-
// implicit in JIT IR, allowing us to change the types directly without
2051-
// inserting a cast node.
2049+
// Make the "casting away" of GC explicit here instead of retyping.
20522050
//
20532051
void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call)
20542052
{
@@ -2059,15 +2057,16 @@ void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call)
20592057
// We should not be passing gc typed args to an unmanaged call.
20602058
if (varTypeIsGC(argNode->TypeGet()))
20612059
{
2062-
// Tolerate byrefs by retyping to native int.
2060+
// Tolerate byrefs by casting to native int.
20632061
//
20642062
// This is needed or we'll generate inconsistent GC info
20652063
// for this arg at the call site (gc info says byref,
20662064
// pinvoke sig says native int).
20672065
//
20682066
if (argNode->TypeGet() == TYP_BYREF)
20692067
{
2070-
argNode->ChangeType(TYP_I_IMPL);
2068+
GenTree* cast = gtNewCastNode(TYP_I_IMPL, argNode, false, TYP_I_IMPL);
2069+
arg.SetEarlyNode(cast);
20712070
}
20722071
else
20732072
{

0 commit comments

Comments
 (0)