Skip to content

RyuJIT drops unused volatile reads. #6280

@pgavlin

Description

@pgavlin

If the following program is compiled with optimizations enabled (e.g. the /o+ flag is provided on the csc command line), RyuJIT will drop the volatile read of a:

using System.Runtime.CompilerServices; static class C { static volatile int a; static int b; [MethodImpl(MethodImplOptions.NoInlining)] static void LightweightStoreFence() { a = 1; var dummy = a; b = 2; } static void Main() { LightweightStoreFence(); } }� 

The IL as reported by a JIT dump is:

IL_0000 17 ldc.i4.1 IL_0001 fe 13 volatile. IL_0003 80 01 00 00 04 stsfld 0x4000001 IL_0008 fe 13 volatile. IL_000a 7e 01 00 00 04 ldsfld 0x4000001 IL_000f 26 pop IL_0010 18 ldc.i4.2 IL_0011 80 02 00 00 04 stsfld 0x4000002 IL_0016 2a ret � 

The generated assembly is:

G_M23768_IG01: ; func=00, offs=000000H, size=0000H, gcrefRegs=00000000 {}, byrefRegs=00000000 {}, byref, nogc <-- Prolog IG G_M23768_IG02: ; offs=000000H, size=0014H, gcrefRegs=00000000 {}, byrefRegs=00000000 {}, byref IN0001: 000000 mov dword ptr [reloc classVar[0xd2155a38]], 1 IN0002: 00000A mov dword ptr [reloc classVar[0xd2155a58]], 2 G_M23768_IG03: ; offs=000014H, size=0001H, epilog, nogc, emitadd IN0003: 000014 ret � 

From the dump, it appears that the importer drops the volatile ldsfld on the floor when it imports the pop instruction. This is not correct per the ECMA spec, which states that volatile operations may not be removed or coalesced.

Thanks to @omariom for reporting the original issue in #6257.

category:correctness
theme:volatile
skill-level:expert
cost:medium

Metadata

Metadata

Assignees

Labels

JitUntriagedCLR JIT issues needing additional triagearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbughelp wanted[up-for-grabs] Good issue for external contributorsmemory modelissues associated with memory model

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions