Skip to content

Commit edad3b9

Browse files
committed
JIT: add missing xarch RMW case
Handle the case where we're indirectly updating a local with a value that is not a constant. Fixes dotnet#92218.
1 parent 1f9764f commit edad3b9

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/coreclr/jit/emitxarch.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -5485,6 +5485,13 @@ void emitter::emitInsRMW(instruction ins, emitAttr attr, GenTreeStoreInd* storeI
54855485
{
54865486
assert(!src->isContained()); // there must be one non-contained src
54875487

5488+
if (addr->isContained() && addr->OperIs(GT_LCL_ADDR))
5489+
{
5490+
GenTreeLclVarCommon* lclVar = addr->AsLclVarCommon();
5491+
emitIns_S_R(ins, attr, src->GetRegNum(), lclVar->GetLclNum(), lclVar->GetLclOffs());
5492+
return;
5493+
}
5494+
54885495
// ind, reg
54895496
id = emitNewInstrAmd(attr, offset);
54905497
emitHandleMemOp(storeInd, id, emitInsModeFormat(ins, IF_ARD_RRD), ins);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Threading;
7+
using Xunit;
8+
9+
public struct MutableStruct
10+
{
11+
private long _internalValue;
12+
13+
public long InternalValue
14+
{
15+
get => Volatile.Read(ref _internalValue);
16+
private set => Volatile.Write(ref _internalValue, value);
17+
}
18+
19+
public void Add(long value) => AddInternal(value);
20+
private void AddInternal(long value) => InternalValue += value;
21+
public MutableStruct(long value) => InternalValue = value;
22+
}
23+
24+
public static class Runtime_92218
25+
{
26+
[Fact]
27+
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
28+
public static void Problem()
29+
{
30+
var test = new MutableStruct(420);
31+
var from = new MutableStruct(42);
32+
33+
var wrapper = -new TimeSpan(3);
34+
35+
while (test.InternalValue >= from.InternalValue)
36+
{
37+
test.Add(wrapper.Ticks);
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)