public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value
@ 2024-03-22 22:05 xog4nar4@a-n.cc
  2024-03-22 22:08 ` [Bug middle-end/114437] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: xog4nar4@a-n.cc @ 2024-03-22 22:05 UTC (permalink / raw)
  To: gcc-bugs

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: xog4nar4@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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
@ 2024-03-22 22:08 ` pinskia at gcc dot gnu.org
  2024-03-22 22:17 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-22 22:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
"+m" does not do what you think it does ...

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
  2024-03-22 22:08 ` [Bug middle-end/114437] " pinskia at gcc dot gnu.org
@ 2024-03-22 22:17 ` pinskia at gcc dot gnu.org
  2024-03-22 22:39 ` xog4nar4@a-n.cc
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-22 22:17 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
These constraints don't do what think it does.

You can see the behavior by doing:
    asm volatile("#%0 %1" : "+m,r"(arg) : : "memory");


You get:
        #arg(%rip) .LC0(%rip)


Which is correct as `1` is stored in `.LC0(%rip)`.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
  2024-03-22 22:08 ` [Bug middle-end/114437] " pinskia at gcc dot gnu.org
  2024-03-22 22:17 ` pinskia at gcc dot gnu.org
@ 2024-03-22 22:39 ` xog4nar4@a-n.cc
  2024-03-22 22:51 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: xog4nar4@a-n.cc @ 2024-03-22 22:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from xog4nar4@a-n.cc ---
Just to confirm, is this an incorrect constraint for the intended goal of
convincing the compiler that the value is used and modified, without actually
doing either and preserving the value, or am I just using it wrong in this
particular example? I'm somewhat surprised since this inline asm snippet is
used in multiple well-known micro-benchmarking libraries.


Is the following version correct?
```
asm volatile("" : "=m,r"(arg) : "0,0"(arg) : "memory");
```

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
                   ` (2 preceding siblings ...)
  2024-03-22 22:39 ` xog4nar4@a-n.cc
@ 2024-03-22 22:51 ` pinskia at gcc dot gnu.org
  2024-03-23  0:08 ` xog4nar4@a-n.cc
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-22 22:51 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to xog4nar4 from comment #3)
> Just to confirm, is this an incorrect constraint for the intended goal of
> convincing the compiler that the value is used and modified, without
> actually doing either and preserving the value, or am I just using it wrong
> in this particular example? I'm somewhat surprised since this inline asm
> snippet is used in multiple well-known micro-benchmarking libraries.
> 
> 
> Is the following version correct?
> ```
> asm volatile("" : "=m,r"(arg) : "0,0"(arg) : "memory");
> ```

The correct way of implementing this inline-asm is don't. Oh and the above does
not work either because a constraint of `"=m":"0"` is also not going to work.
Inline-asm is really NOT designed for this purpose at all and abusing it this
way is just going to run into these issues.

*** This bug has been marked as a duplicate of bug 92597 ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
                   ` (3 preceding siblings ...)
  2024-03-22 22:51 ` pinskia at gcc dot gnu.org
@ 2024-03-23  0:08 ` xog4nar4@a-n.cc
  2024-03-23  0:10 ` pinskia at gcc dot gnu.org
  2024-03-23  0:10 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: xog4nar4@a-n.cc @ 2024-03-23  0:08 UTC (permalink / raw)
  To: gcc-bugs

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

xog4nar4@a-n.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|DUPLICATE                   |INVALID

--- Comment #5 from xog4nar4@a-n.cc ---
I would prefer to use something else, but I'm not aware of any less hacky way
to ensure that the compiler does not interfere with a benchmark. If there's any
explicitly supported way to achieve that in GCC, I'm all ears, but I don't
assume so given that each and every C++ benchmarking library I know uses inline
asm.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
                   ` (4 preceding siblings ...)
  2024-03-23  0:08 ` xog4nar4@a-n.cc
@ 2024-03-23  0:10 ` pinskia at gcc dot gnu.org
  2024-03-23  0:10 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-23  0:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You can juse use "+mr" without the comma. There is no way to use alterantives
in inline-asm.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/114437] Inline asm with "+m,r" operand ignores input value
  2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
                   ` (5 preceding siblings ...)
  2024-03-23  0:10 ` pinskia at gcc dot gnu.org
@ 2024-03-23  0:10 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-23  0:10 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Still a dup.

*** This bug has been marked as a duplicate of bug 92597 ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-03-23  0:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-22 22:05 [Bug c/114437] New: Inline asm with "+m,r" operand ignores input value xog4nar4@a-n.cc
2024-03-22 22:08 ` [Bug middle-end/114437] " pinskia at gcc dot gnu.org
2024-03-22 22:17 ` pinskia at gcc dot gnu.org
2024-03-22 22:39 ` xog4nar4@a-n.cc
2024-03-22 22:51 ` pinskia at gcc dot gnu.org
2024-03-23  0:08 ` xog4nar4@a-n.cc
2024-03-23  0:10 ` pinskia at gcc dot gnu.org
2024-03-23  0:10 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).