Don't create mismatched block copies in `cpblk` import (#71160)
authorSingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Wed, 27 Jul 2022 09:27:06 +0000 (12:27 +0300)
committerGitHub <noreply@github.com>
Wed, 27 Jul 2022 09:27:06 +0000 (11:27 +0200)
* Don't create questionable IR in "cpblk" import

* Add a test

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

index dd7b9c7..61d3977 100644 (file)
@@ -17330,25 +17330,18 @@ void Compiler::impImportBlockCode(BasicBlock* block)
                 op2 = impPopStack().val; // Src addr
                 op1 = impPopStack().val; // Dst addr
 
-                if (op2->OperGet() == GT_ADDR)
-                {
-                    op2 = op2->AsOp()->gtOp1;
-                }
-                else
-                {
-                    op2 = gtNewOperNode(GT_IND, TYP_STRUCT, op2);
-                }
-
                 if (op3->IsCnsIntOrI())
                 {
-                    size = (unsigned)op3->AsIntConCommon()->IconValue();
-                    op1  = new (this, GT_BLK) GenTreeBlk(GT_BLK, TYP_STRUCT, op1, typGetBlkLayout(size));
-                    op1  = gtNewBlkOpNode(op1, op2, (prefixFlags & PREFIX_VOLATILE) != 0, true);
+                    size = static_cast<unsigned>(op3->AsIntConCommon()->IconValue());
+
+                    op1 = gtNewBlockVal(op1, size);
+                    op2 = gtNewBlockVal(op2, size);
+                    op1 = gtNewBlkOpNode(op1, op2, (prefixFlags & PREFIX_VOLATILE) != 0, /* isCopyBlock */ true);
                 }
                 else
                 {
-                    op1  = new (this, GT_STORE_DYN_BLK) GenTreeStoreDynBlk(op1, op2, op3);
-                    size = 0;
+                    op2 = gtNewOperNode(GT_IND, TYP_STRUCT, op2);
+                    op1 = new (this, GT_STORE_DYN_BLK) GenTreeStoreDynBlk(op1, op2, op3);
 
                     if ((prefixFlags & PREFIX_VOLATILE) != 0)
                     {
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_71156/Runtime_71156.cs b/src/tests/JIT/Regression/JitBlue/Runtime_71156/Runtime_71156.cs
new file mode 100644 (file)
index 0000000..6debf2b
--- /dev/null
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Runtime.CompilerServices;
+
+public unsafe class Runtime_71156
+{
+    public static int Main()
+    {
+        return Problem() ? 101 : 100;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    private static bool Problem()
+    {
+        float a;
+        var b = 0xFF000000;
+        Unsafe.CopyBlock(&a, &b, 4);
+
+        return *(uint*)&a != 0xFF000000u;
+    }
+}
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_71156/Runtime_71156.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_71156/Runtime_71156.csproj
new file mode 100644 (file)
index 0000000..cf94135
--- /dev/null
@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <Optimize>True</Optimize>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+</Project>
\ No newline at end of file