public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug d/110516] New: core.volatile.volatileLoad is broken
@ 2023-07-01 22:41 witold.baryluk+gcc at gmail dot com
  2023-07-01 23:08 ` [Bug d/110516] " ibuclaw at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: witold.baryluk+gcc at gmail dot com @ 2023-07-01 22:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110516
           Summary: core.volatile.volatileLoad is broken
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: d
          Assignee: ibuclaw at gdcproject dot org
          Reporter: witold.baryluk+gcc at gmail dot com
  Target Milestone: ---

gcc 12.2.0 (from Debian stable) and gcc trunk 14.0.0 (in godbolt) tested.

core.volatile.volatileLoad simply does not work.

1) It merges loads.
2) It removes unused loads at -O1 and higher.

Example:

void actualRun(ubyte* ptr1) {
  import core.volatile : volatileLoad;
  volatileLoad(ptr1);
  volatileLoad(ptr1);
  volatileLoad(ptr1);
  volatileLoad(ptr1);
}


Without optimisations:

void example.actualRun(ubyte*):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        nop
        pop     rbp
        ret


Incorrect.



With optimisations:

void example.actualRun(ubyte*):
        ret

Incorrect.


Expected:

void example.actualRun(ubyte*):
        movzx   eax, byte ptr [rdi]
        movzx   eax, byte ptr [rdi]
        movzx   eax, byte ptr [rdi]
        movzx   eax, byte ptr [rdi]
        ret



dmd and ldc behave properly.


It looks like it never worked properly.

Would be good to have a test case for this, so it does not become a regression
later.


I did not test volatileStore, but I would not be surprised it is also broken.

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

* [Bug d/110516] core.volatile.volatileLoad is broken
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
@ 2023-07-01 23:08 ` ibuclaw at gcc dot gnu.org
  2023-07-02  0:21 ` ibuclaw at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ibuclaw at gcc dot gnu.org @ 2023-07-01 23:08 UTC (permalink / raw)
  To: gcc-bugs

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

ibuclaw at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw at gcc dot gnu.org

--- Comment #1 from ibuclaw at gcc dot gnu.org ---
(In reply to Witold Baryluk from comment #0)
> gcc 12.2.0 (from Debian stable) and gcc trunk 14.0.0 (in godbolt) tested.
> 
> core.volatile.volatileLoad simply does not work.
> 
> 1) It merges loads.
> 2) It removes unused loads at -O1 and higher.
> 
> Example:
> 
> void actualRun(ubyte* ptr1) {
>   import core.volatile : volatileLoad;
>   volatileLoad(ptr1);
>   volatileLoad(ptr1);
>   volatileLoad(ptr1);
>   volatileLoad(ptr1);
> }
Actually, doesn't seem to be doing either 1 or 2, it looks like the statement
is just dropped altogether as no obvious side-effect was picked up from the
expression to which the intrinsic was expanded into.

https://d.godbolt.org/z/361x4vKfn

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

* [Bug d/110516] core.volatile.volatileLoad is broken
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
  2023-07-01 23:08 ` [Bug d/110516] " ibuclaw at gcc dot gnu.org
@ 2023-07-02  0:21 ` ibuclaw at gcc dot gnu.org
  2023-07-02  1:37 ` [Bug d/110516] core.volatile.volatileLoad discarded if result is unused cvs-commit at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ibuclaw at gcc dot gnu.org @ 2023-07-02  0:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from ibuclaw at gcc dot gnu.org ---
(In reply to Witold Baryluk from comment #0)
> I did not test volatileStore, but I would not be surprised it is also broken.
volatileStore is fine, because that expands to an assignment, which is assumed
to have a side effect.

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
  2023-07-01 23:08 ` [Bug d/110516] " ibuclaw at gcc dot gnu.org
  2023-07-02  0:21 ` ibuclaw at gcc dot gnu.org
@ 2023-07-02  1:37 ` cvs-commit at gcc dot gnu.org
  2023-07-02  1:38 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-02  1:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:80ae426a195a0d035640a6301da833564deade52

commit r14-2236-g80ae426a195a0d035640a6301da833564deade52
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Jul 2 03:24:53 2023 +0200

    d: Fix core.volatile.volatileLoad discarded if result is unused

    The first pass of code generation in the D front-end splits up all
    compound expressions and discards expressions that have no side effects.
    This included calls to the `volatileLoad' intrinsic if its result was
    not used, causing such calls to be eliminated from the program.

    We already set TREE_THIS_VOLATILE on the expression, however the
    tree documentation says if this bit is set in an expression, so is
    TREE_SIDE_EFFECTS.  So set TREE_SIDE_EFFECTS on the expression too.
    This prevents any early discarding from occuring.

            PR d/110516

    gcc/d/ChangeLog:

            * intrinsics.cc (expand_volatile_load): Set TREE_SIDE_EFFECTS on
the
            expanded expression.
            (expand_volatile_store): Likewise.

    gcc/testsuite/ChangeLog:

            * gdc.dg/torture/pr110516a.d: New test.
            * gdc.dg/torture/pr110516b.d: New test.

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
                   ` (2 preceding siblings ...)
  2023-07-02  1:37 ` [Bug d/110516] core.volatile.volatileLoad discarded if result is unused cvs-commit at gcc dot gnu.org
@ 2023-07-02  1:38 ` cvs-commit at gcc dot gnu.org
  2023-07-02  1:40 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-02  1:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Iain Buclaw
<ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:78e9bd776d559b4a2193d61dd074ee0880d43ec5

commit r13-7521-g78e9bd776d559b4a2193d61dd074ee0880d43ec5
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Jul 2 03:24:53 2023 +0200

    d: Fix core.volatile.volatileLoad discarded if result is unused

    The first pass of code generation in the D front-end splits up all
    compound expressions and discards expressions that have no side effects.
    This included calls to the `volatileLoad' intrinsic if its result was
    not used, causing such calls to be eliminated from the program.

    We already set TREE_THIS_VOLATILE on the expression, however the
    tree documentation says if this bit is set in an expression, so is
    TREE_SIDE_EFFECTS.  So set TREE_SIDE_EFFECTS on the expression too.
    This prevents any early discarding from occuring.

            PR d/110516

    gcc/d/ChangeLog:

            * intrinsics.cc (expand_volatile_load): Set TREE_SIDE_EFFECTS on
the
            expanded expression.
            (expand_volatile_store): Likewise.

    gcc/testsuite/ChangeLog:

            * gdc.dg/torture/pr110516a.d: New test.
            * gdc.dg/torture/pr110516b.d: New test.

    (cherry picked from commit 80ae426a195a0d035640a6301da833564deade52)

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
                   ` (3 preceding siblings ...)
  2023-07-02  1:38 ` cvs-commit at gcc dot gnu.org
@ 2023-07-02  1:40 ` cvs-commit at gcc dot gnu.org
  2023-07-02  1:42 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-02  1:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Iain Buclaw
<ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:b224692084e40aabcba1e73166eedf55dfd7f563

commit r12-9749-gb224692084e40aabcba1e73166eedf55dfd7f563
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Jul 2 03:24:53 2023 +0200

    d: Fix core.volatile.volatileLoad discarded if result is unused

    The first pass of code generation in the D front-end splits up all
    compound expressions and discards expressions that have no side effects.
    This included calls to the `volatileLoad' intrinsic if its result was
    not used, causing such calls to be eliminated from the program.

    We already set TREE_THIS_VOLATILE on the expression, however the
    tree documentation says if this bit is set in an expression, so is
    TREE_SIDE_EFFECTS.  So set TREE_SIDE_EFFECTS on the expression too.
    This prevents any early discarding from occuring.

            PR d/110516

    gcc/d/ChangeLog:

            * intrinsics.cc (expand_volatile_load): Set TREE_SIDE_EFFECTS on
the
            expanded expression.
            (expand_volatile_store): Likewise.

    gcc/testsuite/ChangeLog:

            * gdc.dg/torture/pr110516a.d: New test.
            * gdc.dg/torture/pr110516b.d: New test.

    (cherry picked from commit 80ae426a195a0d035640a6301da833564deade52)

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
                   ` (4 preceding siblings ...)
  2023-07-02  1:40 ` cvs-commit at gcc dot gnu.org
@ 2023-07-02  1:42 ` cvs-commit at gcc dot gnu.org
  2023-07-02  1:44 ` ibuclaw at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-02  1:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Iain Buclaw
<ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:da7a10ea9245d7461253495698873891ffc20666

commit r11-10890-gda7a10ea9245d7461253495698873891ffc20666
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Jul 2 03:24:53 2023 +0200

    d: Fix core.volatile.volatileLoad discarded if result is unused

    The first pass of code generation in the D front-end splits up all
    compound expressions and discards expressions that have no side effects.
    This included calls to the `volatileLoad' intrinsic if its result was
    not used, causing such calls to be eliminated from the program.

    We already set TREE_THIS_VOLATILE on the expression, however the
    tree documentation says if this bit is set in an expression, so is
    TREE_SIDE_EFFECTS.  So set TREE_SIDE_EFFECTS on the expression too.
    This prevents any early discarding from occuring.

            PR d/110516

    gcc/d/ChangeLog:

            * intrinsics.cc (expand_volatile_load): Set TREE_SIDE_EFFECTS on
the
            expanded expression.
            (expand_volatile_store): Likewise.

    gcc/testsuite/ChangeLog:

            * gdc.dg/torture/pr110516a.d: New test.
            * gdc.dg/torture/pr110516b.d: New test.

    (cherry picked from commit 80ae426a195a0d035640a6301da833564deade52)

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
                   ` (5 preceding siblings ...)
  2023-07-02  1:42 ` cvs-commit at gcc dot gnu.org
@ 2023-07-02  1:44 ` ibuclaw at gcc dot gnu.org
  2023-07-02  2:22 ` witold.baryluk+gcc at gmail dot com
  2023-07-02  2:23 ` witold.baryluk+gcc at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: ibuclaw at gcc dot gnu.org @ 2023-07-02  1:44 UTC (permalink / raw)
  To: gcc-bugs

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

ibuclaw at gcc dot gnu.org changed:

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

--- Comment #7 from ibuclaw at gcc dot gnu.org ---
Fix committed and backported.

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
                   ` (6 preceding siblings ...)
  2023-07-02  1:44 ` ibuclaw at gcc dot gnu.org
@ 2023-07-02  2:22 ` witold.baryluk+gcc at gmail dot com
  2023-07-02  2:23 ` witold.baryluk+gcc at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: witold.baryluk+gcc at gmail dot com @ 2023-07-02  2:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Witold Baryluk <witold.baryluk+gcc at gmail dot com> ---
I see.

Point 1 is definitively incorrect. I interpreted asembler wrong:

void example.actualRun(ubyte*):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        nop
        pop     rbp
        ret


The move there, is just some stack manipulation, it has nothing to do with
volatileLoad.



You are right about the side effect visibility and volatileStore.

Still, there should be a way to express real memory read, with result not
stored anywhere in program (just written to register, then discarded).

This has some (not very common) uses in memmory-mapped IO, i.e. in drivers for
devices where the read itself could indicate something (this of course usually
also require setting proper page table attributes to disable caching or other
optimizations, etc, not just volatile load in machine code). I do not have
specific examples at hand, but afaik I saw some examples in the past (mostly on
older architectures), as well some watchdog chips that reset timer on read.

Another use is for doing memory and cache read benchmarks and profiling. We
want to invoke read (to register) from some memory location, but we do not need
the value for anything else.

And more esoteric use might be memory probing. On some level systems, kernel or
bootloader, might not know the memory layout, and resort to just doing reads,
and relaying on CPU fault handlers to report invalid reads.

And some people might use load without destination, as a prefetch hint, or to
prefault some memory pages.

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

* [Bug d/110516] core.volatile.volatileLoad discarded if result is unused
  2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
                   ` (7 preceding siblings ...)
  2023-07-02  2:22 ` witold.baryluk+gcc at gmail dot com
@ 2023-07-02  2:23 ` witold.baryluk+gcc at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: witold.baryluk+gcc at gmail dot com @ 2023-07-02  2:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Witold Baryluk <witold.baryluk+gcc at gmail dot com> ---
Thank you for a quick fix Iain!

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

end of thread, other threads:[~2023-07-02  2:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-01 22:41 [Bug d/110516] New: core.volatile.volatileLoad is broken witold.baryluk+gcc at gmail dot com
2023-07-01 23:08 ` [Bug d/110516] " ibuclaw at gcc dot gnu.org
2023-07-02  0:21 ` ibuclaw at gcc dot gnu.org
2023-07-02  1:37 ` [Bug d/110516] core.volatile.volatileLoad discarded if result is unused cvs-commit at gcc dot gnu.org
2023-07-02  1:38 ` cvs-commit at gcc dot gnu.org
2023-07-02  1:40 ` cvs-commit at gcc dot gnu.org
2023-07-02  1:42 ` cvs-commit at gcc dot gnu.org
2023-07-02  1:44 ` ibuclaw at gcc dot gnu.org
2023-07-02  2:22 ` witold.baryluk+gcc at gmail dot com
2023-07-02  2:23 ` witold.baryluk+gcc at gmail dot com

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).