Skip to content

Commit

Permalink
Fixes #56930 (#57000)
Browse files Browse the repository at this point in the history
* Don't eliminate store for the following sequence of instructions
```
ldr wzr, [addrReg, #immOff]
str wzr, [addrReg, #immOff]
```

* Add regression test for #56930
  • Loading branch information
echesakov committed Aug 6, 2021
1 parent 22fa9e8 commit 3caf2f0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/coreclr/jit/emitarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15697,7 +15697,11 @@ bool emitter::IsRedundantLdStr(
// Make sure src and dst registers are not same.
// ldr x0, [x0, #4]
// str x0, [x0, #4] <-- can't eliminate because [x0+3] is not same destination as previous source.
if ((reg1 != reg2) && (prevReg1 == reg1) && (prevReg2 == reg2) && (imm == prevImm))
// Note, however, that we can not eliminate store in the following sequence
// ldr wzr, [x0, #4]
// str wzr, [x0, #4]
// since load operation doesn't (and can't) change the value of its destination register.
if ((reg1 != reg2) && (prevReg1 == reg1) && (prevReg2 == reg2) && (imm == prevImm) && (reg1 != REG_ZR))
{
JITDUMP("\n -- suppressing 'str reg%u [reg%u, #%u]' as previous 'ldr reg%u [reg%u, #%u]' was from same "
"location.\n",
Expand Down
39 changes: 39 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_56930/Runtime_56930.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Runtime_56930
{
class C0
{
public int F1;
public C0(int f1)
{
F1 = f1;
}
}

class Program
{
static C0 s_2 = new C0(1);

public static int Main()
{
// Since the following statement is a dead store the next two statements will become
// a NULLCHECK(s_2) followed by STOREIND(&s_2[8], 0)
int val = s_2.F1;
s_2.F1 = 0;

// On Arm64 these will be compiled to
//
// ldr wzr, [x0, #8]
// str wzr, [x0, #8]
//
// The issue was that the JIT falsely assumes the store to be redundant and eliminated
// the instruction during pipehole optimizations.

return s_2.F1 + 100;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit 3caf2f0

Please sign in to comment.