public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/105051] New: consider not combining malloc + memset to calloc when inside calloc itself
@ 2022-03-25  4:01 godmar at gmail dot com
  2022-03-25  4:06 ` [Bug tree-optimization/105051] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: godmar at gmail dot com @ 2022-03-25  4:01 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105051
           Summary: consider not combining malloc + memset to calloc when
                    inside calloc itself
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: godmar at gmail dot com
  Target Milestone: ---

gcc 8.5.0 (and later) just broke our 18-year-old calloc implementation, which
goes like this:

void *
calloc (size_t a, size_t b)
{
  /* Calculate block size and make sure it fits in size_t. */
  size_t size = a * b;

  /* Allocate and zero memory. */
  void *p = malloc (size);
  if (p != NULL)
    memset (p, 0, size);

  return p;
}

by producing this ingenuous code sequence:

calloc:
    imulq   %rsi, %rdi
    movl    $1, %esi
    jmp calloc


I believe that this is due to an optimization whereby a sequence of malloc
followed by memset is transformed into a call to calloc.

My suggestion would be to suppress this optimization inside a function that is
itself called calloc as it leads to an infinite loop.

I'm also curious what the proper work-around is.  Is it `-fno-builtin-memset`?

I discovered this in 8.5.0 but Godbolt says it's present in the trunk as of
now:
https://godbolt.org/z/MKMT98qnr

This may be related to Bug #83022

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

* [Bug tree-optimization/105051] consider not combining malloc + memset to calloc when inside calloc itself
  2022-03-25  4:01 [Bug tree-optimization/105051] New: consider not combining malloc + memset to calloc when inside calloc itself godmar at gmail dot com
@ 2022-03-25  4:06 ` pinskia at gcc dot gnu.org
  2022-03-25  4:16 ` godmar at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-03-25  4:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 78074.

Also the code to do this has been there since 2014 (8 years ago) so your 18
year old calloc implementation has been broken for 8 years and it was 10 year
at the time it was broken.

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

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

* [Bug tree-optimization/105051] consider not combining malloc + memset to calloc when inside calloc itself
  2022-03-25  4:01 [Bug tree-optimization/105051] New: consider not combining malloc + memset to calloc when inside calloc itself godmar at gmail dot com
  2022-03-25  4:06 ` [Bug tree-optimization/105051] " pinskia at gcc dot gnu.org
@ 2022-03-25  4:16 ` godmar at gmail dot com
  2022-03-25  8:09 ` rguenth at gcc dot gnu.org
  2022-03-25 11:32 ` godmar at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: godmar at gmail dot com @ 2022-03-25  4:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Godmar Back <godmar at gmail dot com> ---
Thank you for your reply.  

To make sure I understand, the only work-around is to completely disable all
builtins (as in -fno-builtin), or is using `-fno-builtin-memset` as I proposed
sufficient?

I'm not sure why this bug is invalid, either. To me, applying this optimization
inside `calloc` seems invalid.

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

* [Bug tree-optimization/105051] consider not combining malloc + memset to calloc when inside calloc itself
  2022-03-25  4:01 [Bug tree-optimization/105051] New: consider not combining malloc + memset to calloc when inside calloc itself godmar at gmail dot com
  2022-03-25  4:06 ` [Bug tree-optimization/105051] " pinskia at gcc dot gnu.org
  2022-03-25  4:16 ` godmar at gmail dot com
@ 2022-03-25  8:09 ` rguenth at gcc dot gnu.org
  2022-03-25 11:32 ` godmar at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-03-25  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
It's more of a QOI issue, we have similar (old) bugs where we pattern-match a
memcpy loop to memcpy inside memcpy (PR56888).  The difficulty is to reliably
detect whether we're in a place where emitting a call to 'foo' will recurse. 
Unfortunately the people with knowledge in this area are too busy and given
workarounds exist this hasn't been top priority.

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

* [Bug tree-optimization/105051] consider not combining malloc + memset to calloc when inside calloc itself
  2022-03-25  4:01 [Bug tree-optimization/105051] New: consider not combining malloc + memset to calloc when inside calloc itself godmar at gmail dot com
                   ` (2 preceding siblings ...)
  2022-03-25  8:09 ` rguenth at gcc dot gnu.org
@ 2022-03-25 11:32 ` godmar at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: godmar at gmail dot com @ 2022-03-25 11:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Godmar Back <godmar at gmail dot com> ---
Don't let perfect be the enemy of good.  IMO, the detection doesn't need to be
perfect in the sense of not having false negatives.  All 3 bug reports of the
calloc implementations you broke called malloc + memset directly in calloc,
something that (I'd guess?) should be more readily detectable.

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

end of thread, other threads:[~2022-03-25 11:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-25  4:01 [Bug tree-optimization/105051] New: consider not combining malloc + memset to calloc when inside calloc itself godmar at gmail dot com
2022-03-25  4:06 ` [Bug tree-optimization/105051] " pinskia at gcc dot gnu.org
2022-03-25  4:16 ` godmar at gmail dot com
2022-03-25  8:09 ` rguenth at gcc dot gnu.org
2022-03-25 11:32 ` godmar 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).