public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* New option to turn off stack reuse for temporaries
@ 2012-06-20 23:44 Xinliang David Li
  2012-06-21  5:28 ` Jason Merrill
  2012-12-02 12:32 ` Olivier Ballereau
  0 siblings, 2 replies; 28+ messages in thread
From: Xinliang David Li @ 2012-06-20 23:44 UTC (permalink / raw)
  To: GCC Patches; +Cc: Jason Merrill, Richard Guenther

One of the most common runtime errors we have seen in gcc-4_7 is
caused by dangling references to temporaries whole life time have
ended

e.g,

 const A& a = foo();

or
foo (A());// where temp's address is saved and used after foo.

Of course this is user error according to the standard, triaging of
bugs like this is pretty time consuming to triage. This patch tries to
introduce an option to disable stack reuse for temporaries, which can
be used to debugging purpose.

Is this good for trunk?

thanks,

David

2012-06-20  Xinliang David Li  <davidxl@google.com>

        * common.opt: -ftemp-reuse-stack option.
        * gimplify.c (gimplify_target_expr): Check new flag.



Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi     (revision 188362)
+++ doc/invoke.texi     (working copy)
@@ -1003,6 +1003,7 @@ See S/390 and zSeries Options.
 -fstack-limit-register=@var{reg}  -fstack-limit-symbol=@var{sym} @gol
 -fno-stack-limit -fsplit-stack @gol
 -fleading-underscore  -ftls-model=@var{model} @gol
+-ftemp-stack-reuse @gol
 -ftrapv  -fwrapv  -fbounds-check @gol
 -fvisibility -fstrict-volatile-bitfields}
 @end table
@@ -19500,6 +19501,10 @@ indices used to access arrays are within
 currently only supported by the Java and Fortran front ends, where
 this option defaults to true and false respectively.

+@item -ftemp-stack-reuse
+@opindex ftemp_stack_reuse
+This option enables stack space reuse for temporaries. The default is on.
+
 @item -ftrapv
 @opindex ftrapv
 This option generates traps for signed overflow on addition, subtraction,
Index: gimplify.c
===================================================================
--- gimplify.c  (revision 188362)
+++ gimplify.c  (working copy)
@@ -5487,7 +5487,8 @@ gimplify_target_expr (tree *expr_p, gimp
       /* Add a clobber for the temporary going out of scope, like
         gimplify_bind_expr.  */
       if (gimplify_ctxp->in_cleanup_point_expr
-         && needs_to_live_in_memory (temp))
+         && needs_to_live_in_memory (temp)
+         && flag_temp_stack_reuse)
        {
          tree clobber = build_constructor (TREE_TYPE (temp), NULL);
          TREE_THIS_VOLATILE (clobber) = true;
Index: common.opt
===================================================================
--- common.opt  (revision 188362)
+++ common.opt  (working copy)
@@ -1322,6 +1322,10 @@ fif-conversion2
 Common Report Var(flag_if_conversion2) Optimization
 Perform conversion of conditional jumps to conditional execution

+ftemp-stack-reuse
+Common Report Var(flag_temp_stack_reuse) Init(1)
+Enable stack reuse for compiler generated temps
+
 ftree-loop-if-convert
 Common Report Var(flag_tree_loop_if_convert) Init(-1) Optimization
 Convert conditional jumps in innermost loops to branchless equivalents

^ permalink raw reply	[flat|nested] 28+ messages in thread
* Re: New option to turn off stack reuse for temporaries
@ 2012-06-22 21:09 Jason Merrill
  0 siblings, 0 replies; 28+ messages in thread
From: Jason Merrill @ 2012-06-22 21:09 UTC (permalink / raw)
  To: Richard Guenther; +Cc: Xinliang David Li, GCC Patches, Michael Matz

Yes.

-------- Original Message --------
 From: Richard Guenther <richard.guenther@gmail.com>
 Sent: Fri, Jun 22, 2012 02:39 AM
 To: Jason Merrill <jason@redhat.com>
 CC: Xinliang David Li <davidxl@google.com>; GCC Patches <gcc-patches@gcc.gnu.org>; Michael Matz <matz@suse.de>
 Subject: Re: New option to turn off stack reuse for temporaries

On Fri, Jun 22, 2012 at 11:29 AM, Jason Merrill <jason@redhat.com> wrote:
> On 06/22/2012 01:30 AM, Richard Guenther wrote:
>>>
>>> What other issues? It enables more potential code motion, but on the
>>> other hand, causes more conservative stack reuse. As far I can tell,
>>> the handling of temporaries is added independently after the clobber
>>> for scoped variables are introduced. This option can be used to
>>> restore the older behavior (in handling temps).
>>
>>
>> Well, it does not really restore the old behavior (if you mean before
>> adding
>> CLOBBERS, not before the single patch that might have used those for
>> gimplifying WITH_CLEANUP_EXPR).  You say it disables stack-slot sharing
>> for those decls but it also does other things via side-effects of no
>> longer
>> emitting the CLOBBER.  I say it's better to disable the stack-slot
>> sharing.
>
>
> The patch exactly restores the behavior of temporaries from before my change
> to add CLOBBERs for temporaries.  The primary effect of that change was to
> provide stack-slot sharing, but if there are other effects they are probably
> desirable as well, since the broken code depended on the old behavior.

So you see it as workaround option, like -fno-strict-aliasing, rather than
debugging aid?

Richard.

> Jason

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

end of thread, other threads:[~2012-12-03  1:03 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-20 23:44 New option to turn off stack reuse for temporaries Xinliang David Li
2012-06-21  5:28 ` Jason Merrill
2012-06-21  6:06   ` Xinliang David Li
2012-06-21  6:27     ` Jason Merrill
2012-06-21  9:32     ` Richard Guenther
2012-06-21 16:41       ` Michael Matz
2012-06-22  8:46         ` Richard Guenther
2012-06-21 18:19       ` Jason Merrill
2012-06-21 18:44       ` Xinliang David Li
2012-06-22  8:50         ` Richard Guenther
2012-06-22  9:39           ` Jason Merrill
2012-06-22  9:51             ` Richard Guenther
2012-06-22 16:09               ` Xinliang David Li
2012-06-25 16:29                 ` Xinliang David Li
2012-06-26  8:42                   ` Richard Guenther
2012-06-26 15:29                     ` Jason Merrill
2012-06-26 17:12                       ` Michael Matz
2012-06-26 17:19                         ` Jakub Jelinek
2012-06-26 20:12                         ` Mike Stump
2012-06-27  3:03                           ` Eric Botcazou
2012-06-29  8:18                     ` Xinliang David Li
2012-07-02 23:30                       ` Xinliang David Li
2012-07-04 15:01                         ` Xinliang David Li
2012-07-09 16:31                           ` Xinliang David Li
2012-07-09 22:53                         ` Jason Merrill
2012-12-02 12:32 ` Olivier Ballereau
2012-12-03  1:03   ` Xinliang David Li
2012-06-22 21:09 Jason Merrill

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