Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5376,6 +5376,17 @@ void Compiler::optComputeLoopSideEffectsOfBlock(BasicBlock* blk, FlowGraphNatura
}
break;

case GT_IND:
case GT_BLK:
{
if (tree->AsIndir()->IsVolatile())
{
// On a loop backedge, memory operations in a subsequent iteration may follow this acquire.
memoryHavoc |= memoryKindSet(GcHeap, ByrefExposed);
}
}
break;

case GT_STOREIND:
case GT_STORE_BLK:
{
Expand Down
60 changes: 60 additions & 0 deletions src/tests/JIT/Regression_o_2/Runtime_133579.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Runtime_133579;

using System.Runtime.CompilerServices;
using System.Threading;
using TestLibrary;
using Xunit;

public class Runtime_133579
{
private sealed class Box
{
public int Value;
public int Ready;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static int ReadAfterAcquire(Box box, ManualResetEventSlim readerStarted)
{
readerStarted.Set();
Comment thread
AndyAyersMS marked this conversation as resolved.
Resolved

int value = 0;
int seen = 0;
while (seen < 2)
{
value = box.Value;
if (Volatile.Read(ref box.Ready) != 0)
{
seen++;
}
}

return value;
}

[OuterLoop]
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/41472", typeof(PlatformDetection), nameof(PlatformDetection.IsNotMultithreadingSupported))]
public static void OrdinaryLoadIsNotHoistedAcrossVolatileRead()
{
Box box = new();
using ManualResetEventSlim readerStarted = new();
int readerResult = -1;
Thread reader = new(() =>
{
readerResult = ReadAfterAcquire(box, readerStarted);
});
reader.Start();

readerStarted.Wait();
Thread.Sleep(50);
box.Value = 42;
Volatile.Write(ref box.Ready, 1);
reader.Join();

Assert.Equal(42, readerResult);
}
}
Loading