https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114437

            Bug ID: 114437
           Summary: Inline asm with "+m,r" operand ignores input value
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xog4n...@a-n.cc
  Target Milestone: ---

In the following snippet, GCC seems to assume that `arg` is not read by the asm
statement and optimizes out previous stores when compiling on -O3:
```
extern int external_fn(int);
extern int arg;

int fn() {
    arg = 123;
    asm volatile("" : "+m,r"(arg) : : "memory");
    return external_fn(arg);
}
```

Resulting asm (123 is not stored, leaving `arg` uninitialized):
```
0000000000000000 <fn>:
   0:   f3 0f 1e fa             endbr64
   4:   8b 3d 00 00 00 00       mov    edi,DWORD PTR [rip+0x0]        # a
<fn+0xa>
   a:   e9 00 00 00 00          jmp    f <fn+0xf>
```

The asm snippet comes from the `DoNotOptimize` function in Google Benchmark,
where it is intended to prevent the compiler from optimizing away parts of a
micro-benchmark:
https://github.com/google/benchmark/blob/main/include/benchmark/benchmark.h#L518


Godbolt reproduction: https://godbolt.org/z/3W97dM9eW

Reply via email to