Fixes 55107 (#56259)
authorBrian Sullivan <briansul@microsoft.com>
Mon, 2 Aug 2021 17:40:25 +0000 (10:40 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Aug 2021 17:40:25 +0000 (10:40 -0700)
* Fixes 55107
Add support for the very rare case of a constant byref to Compiler::optAssertionPropGlobal_RelOp
Added test case

* Changed casts as suggested by Jakob

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

index 410c572..917aa94 100644 (file)
@@ -3394,6 +3394,10 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions, Gen
                 assert(vnStore->ConstantValue<size_t>(vnCns) == 0);
                 printf("null\n");
             }
+            else if (op1->TypeGet() == TYP_BYREF)
+            {
+                printf("%d (byref)\n", static_cast<target_ssize_t>(vnStore->ConstantValue<size_t>(vnCns)));
+            }
             else
             {
                 printf("??unknown\n");
@@ -3449,6 +3453,11 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions, Gen
             noway_assert(vnStore->ConstantValue<size_t>(vnCns) == 0);
             op1->AsIntCon()->gtIconVal = 0;
         }
+        else if (op1->TypeGet() == TYP_BYREF)
+        {
+            op1->ChangeOperConst(GT_CNS_INT);
+            op1->AsIntCon()->gtIconVal = static_cast<target_ssize_t>(vnStore->ConstantValue<size_t>(vnCns));
+        }
         else
         {
             noway_assert(!"unknown type in Global_RelOp");
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_55107/Runtime_55107.cs b/src/tests/JIT/Regression/JitBlue/Runtime_55107/Runtime_55107.cs
new file mode 100644 (file)
index 0000000..366bd50
--- /dev/null
@@ -0,0 +1,40 @@
+
+// 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;
+
+namespace Runtime_55107
+{
+    class Program
+    {
+        class G
+        {
+        }
+
+        static int Main(string[] args)
+        {
+            G g = new G();
+
+            ref G iprnull = ref Unsafe.NullRef<G>();
+            ref G ipr1 = ref g;
+
+            if(Unsafe.AreSame(ref ipr1, ref iprnull))
+            {
+                // Failure case 1
+                return -101;
+            }
+            else if(Unsafe.AreSame(ref ipr1, ref Unsafe.NullRef<G>()))
+            {
+                // Failure case 2
+                return -102;
+            }
+            else
+            {
+               // Successful exit
+               return 100;
+            }
+        }
+    }
+}
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_55107/Runtime_55107.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_55107/Runtime_55107.csproj
new file mode 100644 (file)
index 0000000..dad19c4
--- /dev/null
@@ -0,0 +1,22 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+  </PropertyGroup>
+  <PropertyGroup>
+    <DebugType />
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <PropertyGroup>
+    <CLRTestBatchPreCommands><![CDATA[
+$(CLRTestBatchPreCommands)
+set COMPlus_JitDoRedundantBranchOpts=0
+]]></CLRTestBatchPreCommands>
+    <BashCLRTestPreCommands><![CDATA[
+$(BashCLRTestPreCommands)
+export COMPlus_JitDoRedundantBranchOpts=0
+]]></BashCLRTestPreCommands>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+</Project>