diff mbox series

[testsuite/guality] Don't use attribute used in pr45882.c for -O0

Message ID 20180628230025.jxhojynvjhshu7t3@delia
State New
Headers show
Series [testsuite/guality] Don't use attribute used in pr45882.c for -O0 | expand

Commit Message

Tom de Vries June 28, 2018, 11 p.m. UTC
[ was: Re: [testsuite] Fix guality/pr45882.c for flto ]

On Thu, Jun 21, 2018 at 02:52:31PM +0200, Richard Biener wrote:
> On Thu, 21 Jun 2018, Tom de Vries wrote:
> 
> > Hi,
> > 
> > Atm this test in pr45882.c:
> > ...
> >   int d = a[i];  /* { dg-final { gdb-test 16 "d" "112" } } */
> > ...
> > fails as follows with -flto:
> > ...
> > FAIL: gcc.dg/guality/pr45882.c   -O2 -flto -fuse-linker-plugin \
> >       -fno-fat-lto-objects  line 16 d == 112
> > ...
> > 
> > In more detail, gdb fails to print the value of d:
> > ...
> > Breakpoint 1, foo (i=i@entry=7, j=j@entry=7) at pr45882.c:16
> > 16        ++v;
> > $1 = <optimized out>
> > $2 = 112
> > <optimized out> != 112
> > ...
> > 
> > Variable d is a local variable in function foo, initialized from global array a.
> > When compiling, first cddce1 removes the initialization of d in foo, given
> > that d is not used afterwards.  Then ipa marks array a as write-only, and
> > removes the stores to array a in main.  This invalidates the location
> > expression for d, which points to a[i], so it is removed, which is why gdb
> > ends up printing <optimized out> for d.
> > 
> > This patches fixes the fail by adding attribute used to array a, preventing
> > array a from being marked as write-only.
> > 
> > Tested on x86_64.
> > 
> > OK for trunk?
> 
> OK.
> 

I committed this patch, but thought about it some more, and realized that this
is the right solution for optimized code, but not for non-optimized code.

For optimized code, we generate debug info, but as best effort, and we might
need to inhibit the optimizers here and there by adding attribute used and
volatile etc in testcases, in order to allow sufficient debug information to
be generated.

But for non-optimized code (-O0), we shouldn't need such additions in the
testcases, since we're not supposed to optimize in the first place.

So, this patch conditionalizes attribute used in pr45882.c, depending on
whether we're optimizing or not.

This patch as is makes the test-case fail due to adding lines in front of
gdb-test, and gdb-test using absolute line numbers.  So, we either need
"[testsuite/guality] Use line number vars in gdb-test" (
https://gcc.gnu.org/ml/gcc-patches/2018-06/msg01825.html ), or the absolute
line numbers need to be updated.

OK for trunk if bootstrap and reg-test succeeds?

Thanks,
- Tom

[testsuite/guality] Don't use attribute used in pr45882.c for -O0

2018-06-28  Tom de Vries  <tdevries@suse.de>

	* gcc.dg/guality/guality.exp (guality_transform_options): New proc.
	(toplevel): Apply guality_transform_options on DG_TORTURE_OPTIONS and
	LTO_TORTURE_OPTIONS.
	* gcc.dg/guality/prevent-optimization.h: New file.
	* gcc.dg/guality/pr45882.c: Include prevent-optimization.h.
	(a): Replace __attribute__((used)) with ATTRIBUTE_USED.

---
 gcc/testsuite/gcc.dg/guality/guality.exp           | 23 ++++++++++++++++++
 gcc/testsuite/gcc.dg/guality/pr45882.c             |  4 +++-
 .../gcc.dg/guality/prevent-optimization.h          | 28 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)

Comments

Jeff Law June 29, 2018, 12:04 a.m. UTC | #1
On 06/28/2018 05:00 PM, Tom de Vries wrote:
> [ was: Re: [testsuite] Fix guality/pr45882.c for flto ]
> 
> On Thu, Jun 21, 2018 at 02:52:31PM +0200, Richard Biener wrote:
>> On Thu, 21 Jun 2018, Tom de Vries wrote:
>>
>>> Hi,
>>>
>>> Atm this test in pr45882.c:
>>> ...
>>>   int d = a[i];  /* { dg-final { gdb-test 16 "d" "112" } } */
>>> ...
>>> fails as follows with -flto:
>>> ...
>>> FAIL: gcc.dg/guality/pr45882.c   -O2 -flto -fuse-linker-plugin \
>>>       -fno-fat-lto-objects  line 16 d == 112
>>> ...
>>>
>>> In more detail, gdb fails to print the value of d:
>>> ...
>>> Breakpoint 1, foo (i=i@entry=7, j=j@entry=7) at pr45882.c:16
>>> 16        ++v;
>>> $1 = <optimized out>
>>> $2 = 112
>>> <optimized out> != 112
>>> ...
>>>
>>> Variable d is a local variable in function foo, initialized from global array a.
>>> When compiling, first cddce1 removes the initialization of d in foo, given
>>> that d is not used afterwards.  Then ipa marks array a as write-only, and
>>> removes the stores to array a in main.  This invalidates the location
>>> expression for d, which points to a[i], so it is removed, which is why gdb
>>> ends up printing <optimized out> for d.
>>>
>>> This patches fixes the fail by adding attribute used to array a, preventing
>>> array a from being marked as write-only.
>>>
>>> Tested on x86_64.
>>>
>>> OK for trunk?
>>
>> OK.
>>
> 
> I committed this patch, but thought about it some more, and realized that this
> is the right solution for optimized code, but not for non-optimized code.
> 
> For optimized code, we generate debug info, but as best effort, and we might
> need to inhibit the optimizers here and there by adding attribute used and
> volatile etc in testcases, in order to allow sufficient debug information to
> be generated.
> 
> But for non-optimized code (-O0), we shouldn't need such additions in the
> testcases, since we're not supposed to optimize in the first place.
> 
> So, this patch conditionalizes attribute used in pr45882.c, depending on
> whether we're optimizing or not.
> 
> This patch as is makes the test-case fail due to adding lines in front of
> gdb-test, and gdb-test using absolute line numbers.  So, we either need
> "[testsuite/guality] Use line number vars in gdb-test" (
> https://gcc.gnu.org/ml/gcc-patches/2018-06/msg01825.html ), or the absolute
> line numbers need to be updated.
> 
> OK for trunk if bootstrap and reg-test succeeds?
> 
> Thanks,
> - Tom
> 
> [testsuite/guality] Don't use attribute used in pr45882.c for -O0
> 
> 2018-06-28  Tom de Vries  <tdevries@suse.de>
> 
> 	* gcc.dg/guality/guality.exp (guality_transform_options): New proc.
> 	(toplevel): Apply guality_transform_options on DG_TORTURE_OPTIONS and
> 	LTO_TORTURE_OPTIONS.
> 	* gcc.dg/guality/prevent-optimization.h: New file.
> 	* gcc.dg/guality/pr45882.c: Include prevent-optimization.h.
> 	(a): Replace __attribute__((used)) with ATTRIBUTE_USED.
OK.
Jeff
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/guality/guality.exp b/gcc/testsuite/gcc.dg/guality/guality.exp
index 04e889caa2f..d9994341477 100644
--- a/gcc/testsuite/gcc.dg/guality/guality.exp
+++ b/gcc/testsuite/gcc.dg/guality/guality.exp
@@ -48,6 +48,28 @@  if ![info exists ::env(GUALITY_GDB_NAME)] {
 }
 report_gdb $::env(GUALITY_GDB_NAME) [info script]
 
+proc guality_transform_options { args } {
+    set res [list]
+    foreach opt [lindex $args 0] {
+	#
+	if { ! [regexp -- "-O0" $opt] } {
+	    set opt "$opt -DPREVENT_OPTIMIZATION"
+	}
+	lappend res $opt
+    }
+
+    return $res
+}
+
+global DG_TORTURE_OPTIONS
+set guality_dg_torture_options [guality_transform_options $DG_TORTURE_OPTIONS]
+set guality_lto_torture_options [guality_transform_options $LTO_TORTURE_OPTIONS]
+torture-init
+set-torture-options \
+    $guality_dg_torture_options \
+    [list {}] \
+    $guality_lto_torture_options
+
 if {[check_guality "
   #include \"$srcdir/$subdir/guality.h\"
   volatile long int varl = 6;
@@ -65,4 +87,5 @@  if [info exists guality_gdb_name] {
     unsetenv GUALITY_GDB_NAME
 }
 
+torture-finish
 dg-finish
diff --git a/gcc/testsuite/gcc.dg/guality/pr45882.c b/gcc/testsuite/gcc.dg/guality/pr45882.c
index ece35238a30..47572f6a7a4 100644
--- a/gcc/testsuite/gcc.dg/guality/pr45882.c
+++ b/gcc/testsuite/gcc.dg/guality/pr45882.c
@@ -2,8 +2,10 @@ 
 /* { dg-do run } */
 /* { dg-options "-g" } */
 
+#include "prevent-optimization.h"
+
 extern void abort (void);
-int a[1024] __attribute__((used));
+int a[1024] ATTRIBUTE_USED;
 volatile short int v;
 
 __attribute__((noinline,noclone,used)) int
diff --git a/gcc/testsuite/gcc.dg/guality/prevent-optimization.h b/gcc/testsuite/gcc.dg/guality/prevent-optimization.h
new file mode 100644
index 00000000000..0ef84a3d9c8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/guality/prevent-optimization.h
@@ -0,0 +1,28 @@ 
+/* Copyright (C) 2018 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef PREVENT_OPTIMIZATION_H
+#define PREVENT_OPTIMIZATION_H
+
+#ifdef PREVENT_OPTIMIZATION
+#define ATTRIBUTE_USED __attribute__((used))
+#else
+#define ATTRIBUTE_USED
+#endif
+
+#endif