Skip to content

JIT compiler bug: Incorrect caching of loop variable #13328

@Grimfane

Description

@Grimfane

Environment:

•Visual Studio 2019 version 16.2.3
•.Net 4.8
•Console application
•x64 build
•Release build

Code to reproduce:

using System;

namespace Demo
    {
        class Program
        {
            static int _a;

            static void Main()
            {
                _a = 1;

                while (_a == 1)
                {
                    Console.WriteLine(_a);
                    _a = 0;
                }
            }
        }
    }

Expected Behavior:
"1" is printed once and the program terminates.

Actual Behavior:
"0" is repeatedly printed; the program does not terminate.

Notes
If you declare _a as volatile, it works OK.
If you initialise _a as static int _a = 0; it works.

This was reported on StackOverflow here: https://stackoverflow.com/questions/57693386/vs-2019-optimize-code-in-release-mode-broken

category:correctness
theme:optimization
skill-level:expert
cost:large

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions