public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting
@ 2022-09-02  6:49 rguenth at gcc dot gnu.org
  2022-11-02  9:59 ` [Bug middle-end/106811] " nunoplopes at sapo dot pt
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-09-02  6:49 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106811
           Summary: GENERIC and GIMPLE IL undefined behavior needs
                    documenting
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rguenth at gcc dot gnu.org
  Target Milestone: ---

Undefined behavior triggered by the GENERIC operations from tree.def (also used
in GIMPLE) need to be documented.  In general we follow C here, including GCCs
implementation defined behavior here, but not all C undefined behavior cases
are exploited by the middle-end so the state of those cases is not clear.  We
need

 - a list of operations with undefined behavior cases that are currently
 exploited or seen as OK to exploit
 - a list of operations where C documents undefined behavior cases but the
 middle-end conciously defines some implementation defined behavior (which
 should not be target specific)
 - middle-end predicates that can be used to check
 - variants of the operations with (implementation) defined behavior

probably best in generic.texi with cross reference from gimple.texi

Note for RTL operations doing the same is necessary.

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

* [Bug middle-end/106811] GENERIC and GIMPLE IL undefined behavior needs documenting
  2022-09-02  6:49 [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting rguenth at gcc dot gnu.org
@ 2022-11-02  9:59 ` nunoplopes at sapo dot pt
  2022-11-05 14:22 ` rguenther at suse dot de
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: nunoplopes at sapo dot pt @ 2022-11-02  9:59 UTC (permalink / raw)
  To: gcc-bugs

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

Nuno Lopes <nunoplopes at sapo dot pt> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nunoplopes at sapo dot pt

--- Comment #1 from Nuno Lopes <nunoplopes at sapo dot pt> ---
I suggest adopting the concept of poison that LLVM has. It allows operations to
have undefined behavior, while still allow them to be moved freely.
I have some slides that may serve as an introduction to the topic:
https://web.ist.utl.pt/nuno.lopes/pres/ub-vmcai19.pdf

Happy to discuss further.

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

* [Bug middle-end/106811] GENERIC and GIMPLE IL undefined behavior needs documenting
  2022-09-02  6:49 [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting rguenth at gcc dot gnu.org
  2022-11-02  9:59 ` [Bug middle-end/106811] " nunoplopes at sapo dot pt
@ 2022-11-05 14:22 ` rguenther at suse dot de
  2022-11-07 18:07 ` nunoplopes at sapo dot pt
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenther at suse dot de @ 2022-11-05 14:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from rguenther at suse dot de <rguenther at suse dot de> ---
On Wed, 2 Nov 2022, nunoplopes at sapo dot pt wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106811
> 
> Nuno Lopes <nunoplopes at sapo dot pt> changed:
> 
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                  CC|                            |nunoplopes at sapo dot pt
> 
> --- Comment #1 from Nuno Lopes <nunoplopes at sapo dot pt> ---
> I suggest adopting the concept of poison that LLVM has. It allows operations to
> have undefined behavior, while still allow them to be moved freely.
> I have some slides that may serve as an introduction to the topic:
> https://web.ist.utl.pt/nuno.lopes/pres/ub-vmcai19.pdf
> 
> Happy to discuss further.

I've seen this before but I don't really see how that manages to avoid
issues when making conditional undefined behavior invoking stmts
unconditional.

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

* [Bug middle-end/106811] GENERIC and GIMPLE IL undefined behavior needs documenting
  2022-09-02  6:49 [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting rguenth at gcc dot gnu.org
  2022-11-02  9:59 ` [Bug middle-end/106811] " nunoplopes at sapo dot pt
  2022-11-05 14:22 ` rguenther at suse dot de
@ 2022-11-07 18:07 ` nunoplopes at sapo dot pt
  2022-11-07 18:17 ` rguenther at suse dot de
  2023-09-15 10:22 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: nunoplopes at sapo dot pt @ 2022-11-07 18:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Nuno Lopes <nunoplopes at sapo dot pt> ---
> > I suggest adopting the concept of poison that LLVM has. It allows operations to
> > have undefined behavior, while still allow them to be moved freely.
> > I have some slides that may serve as an introduction to the topic:
> > https://web.ist.utl.pt/nuno.lopes/pres/ub-vmcai19.pdf
> > 
> > Happy to discuss further.
> 
> I've seen this before but I don't really see how that manages to avoid
> issues when making conditional undefined behavior invoking stmts
> unconditional.

It's like a NaN in the sense that you can invoke e.g. arithmetic operations.
You can do 2 + poison. That's poison.

But you can't do 2 / poison. That's undefined behavior (UB), since poison could
be a zero at run time.

So poison enables free movement of instructions that don't trigger UB. For
instructions that may trigger UB, like division, memory accesses, function
calls, etc, you still need to be careful as usual when moving those around.

But the point is that poison explains the correctness of the optimizations that
gcc is already doing today, like hoisting arithmetic operations. You can't
their semantics be full UB on e.g. signed overflow as otherwise you couldn't
hoist them without proving that overflows can't happen.

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

* [Bug middle-end/106811] GENERIC and GIMPLE IL undefined behavior needs documenting
  2022-09-02  6:49 [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting rguenth at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-11-07 18:07 ` nunoplopes at sapo dot pt
@ 2022-11-07 18:17 ` rguenther at suse dot de
  2023-09-15 10:22 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenther at suse dot de @ 2022-11-07 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from rguenther at suse dot de <rguenther at suse dot de> ---
> Am 07.11.2022 um 19:07 schrieb nunoplopes at sapo dot pt <gcc-bugzilla@gcc.gnu.org>:
> 
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106811
> 
> --- Comment #3 from Nuno Lopes <nunoplopes at sapo dot pt> ---
>>> I suggest adopting the concept of poison that LLVM has. It allows operations to
>>> have undefined behavior, while still allow them to be moved freely.
>>> I have some slides that may serve as an introduction to the topic:
>>> https://web.ist.utl.pt/nuno.lopes/pres/ub-vmcai19.pdf
>>> 
>>> Happy to discuss further.
>> 
>> I've seen this before but I don't really see how that manages to avoid
>> issues when making conditional undefined behavior invoking stmts
>> unconditional.
> 
> It's like a NaN in the sense that you can invoke e.g. arithmetic operations.
> You can do 2 + poison. That's poison.
> 
> But you can't do 2 / poison. That's undefined behavior (UB), since poison could
> be a zero at run time.
> 
> So poison enables free movement of instructions that don't trigger UB. For
> instructions that may trigger UB, like division, memory accesses, function
> calls, etc, you still need to be careful as usual when moving those around.
> 
> But the point is that poison explains the correctness of the optimizations that
> gcc is already doing today, like hoisting arithmetic operations. You can't
> their semantics be full UB on e.g. signed overflow as otherwise you couldn't
> hoist them without proving that overflows can't happen.

The point is they are and we are rewriting them to defined overflow when
hoisting.  Or in case of division avoid hoisting (because the way to make them
defined is to make them unsigned)

> 
> -- 
> You are receiving this mail because:
> You reported the bug.

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

* [Bug middle-end/106811] GENERIC and GIMPLE IL undefined behavior needs documenting
  2022-09-02  6:49 [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting rguenth at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-11-07 18:17 ` rguenther at suse dot de
@ 2023-09-15 10:22 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-09-15 10:22 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |documentation
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2023-09-15
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
Mine.

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

end of thread, other threads:[~2023-09-15 10:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-02  6:49 [Bug middle-end/106811] New: GENERIC and GIMPLE IL undefined behavior needs documenting rguenth at gcc dot gnu.org
2022-11-02  9:59 ` [Bug middle-end/106811] " nunoplopes at sapo dot pt
2022-11-05 14:22 ` rguenther at suse dot de
2022-11-07 18:07 ` nunoplopes at sapo dot pt
2022-11-07 18:17 ` rguenther at suse dot de
2023-09-15 10:22 ` rguenth 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).