JIT: Avoid omitting copies for struct fields passed implicitly byrefs (#81787)
authorJakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
Wed, 8 Feb 2023 15:38:24 +0000 (16:38 +0100)
committerGitHub <noreply@github.com>
Wed, 8 Feb 2023 15:38:24 +0000 (16:38 +0100)
The code does not properly mark the parent struct as address exposed,
but also doing so would completely disable any tracking/optimization for
these locals which we do not want to do.

Fix #81739

src/coreclr/jit/lclvars.cpp
src/coreclr/jit/morph.cpp
src/tests/JIT/Regression/JitBlue/Runtime_81739/Runtime_81739.cs [new file with mode: 0644]
src/tests/JIT/Regression/JitBlue/Runtime_81739/Runtime_81739.csproj [new file with mode: 0644]

index eec8e0d..7cd22c1 100644 (file)
@@ -2575,6 +2575,7 @@ unsigned Compiler::lvaGetFieldLocal(const LclVarDsc* varDsc, unsigned int fldOff
 void Compiler::lvaSetVarAddrExposed(unsigned varNum DEBUGARG(AddressExposedReason reason))
 {
     LclVarDsc* varDsc = lvaGetDesc(varNum);
+    assert(!varDsc->lvIsStructField);
 
     varDsc->SetAddressExposed(true DEBUGARG(reason));
 
index 4d20705..7216b77 100644 (file)
@@ -3976,7 +3976,7 @@ void Compiler::fgMakeOutgoingStructArgCopy(GenTreeCall* call, CallArg* arg)
 
             if (!omitCopy && fgGlobalMorph)
             {
-                omitCopy = !varDsc->lvPromoted && ((lcl->gtFlags & GTF_VAR_DEATH) != 0);
+                omitCopy = !varDsc->lvPromoted && !varDsc->lvIsStructField && ((lcl->gtFlags & GTF_VAR_DEATH) != 0);
             }
 
             if (!omitCopy && (totalAppearances == 1))
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_81739/Runtime_81739.cs b/src/tests/JIT/Regression/JitBlue/Runtime_81739/Runtime_81739.cs
new file mode 100644 (file)
index 0000000..9b49d8c
--- /dev/null
@@ -0,0 +1,21 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Numerics;
+using System.Runtime.CompilerServices;
+
+public class Runtime_81739
+{
+    public static int Main()
+    {
+        Plane p;
+        p.Normal = default;
+        Consume(p.Normal);
+        return 100;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    private static void Consume<T>(T v)
+    {
+    }
+}
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_81739/Runtime_81739.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_81739/Runtime_81739.csproj
new file mode 100644 (file)
index 0000000..c0a5f3d
--- /dev/null
@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+</Project>
\ No newline at end of file