public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Aditya Kamath1 <Aditya.Kamath1@ibm.com>,
	Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>,
	"tom@tromey.com" <tom@tromey.com>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>,
	"simon.marchi@efficios.com" <simon.marchi@efficios.com>
Subject: Re: [PATCH] Modify align-c/align-c++ test case for AIX
Date: Wed, 29 Mar 2023 14:36:40 +0100	[thread overview]
Message-ID: <af6f689f-b699-5756-0abe-4ba9c1346458@palves.net> (raw)
In-Reply-To: <CH2PR15MB35442C0ED1105DF259AFCCEBD6899@CH2PR15MB3544.namprd15.prod.outlook.com>

On 2023-03-29 12:28 p.m., Aditya Kamath1 wrote:

>>I'm wondering whether __attribute__((used)) on the global variable makes any
>>difference.  IIRC with LLVM it prevents link-time stripping on Mach-O and PE/COFF,
>>but I don't know about XCOFF + GCC.  (It doesn't on ELF, you
>>need 'retain' for that.)
> 
> Hi Pedro. So thanks for this suggestion. I learnt something new from you. But this __attribute__((used)) did not make any difference in our environment.

Thanks for checking.

I also recalled that Clang with LTO has a similar issue, and there __attribute__((used))
tends to help.  Seems to me that it's best to tweak the code generator to just emit uses
for all variables then, instead of special casing some platform or some specific variable.

Like below.  Does this work for AIX?

From 576c9c77ff3ac297a58662471fd0a2546a1d5f42 Mon Sep 17 00:00:00 2001
From: Pedro Alves <pedro@palves.net>
Date: Wed, 29 Mar 2023 13:21:20 +0100
Subject: [PATCH] Fix gdb.base/align-*.exp and Clang + LTO and AIX GCC

Clang with LTO (clang -flto) garbage collects unused global variables,
Thus, gdb.base/align-c.exp and gdb.base/align-c++.exp fail with
hundreds of FAILs like so:

 $ make check \
    TESTS="gdb.*/align-*.exp" \
    RUNTESTFLAGS="CC_FOR_TARGET='clang -flto' CXX_FOR_TARGET='clang++ -flto'"
 ...
 FAIL: gdb.base/align-c.exp: get integer valueof "a_char"
 FAIL: gdb.base/align-c.exp: print _Alignof(char)
 FAIL: gdb.base/align-c.exp: get integer valueof "a_char_x_char"
 FAIL: gdb.base/align-c.exp: print _Alignof(struct align_pair_char_x_char)
 FAIL: gdb.base/align-c.exp: get integer valueof "a_char_x_unsigned_char"
 ...

AIX GCC has the same issue, and there the easier way of adding
__attribute__((used)) to globals does not help.

So add explicit uses of all globals to the generated code.

For the C++ test, that reveals that the static variable members of the
generated structs are not defined anywhere, leading to undefined
references.  Fixed by emitting initialization for all static members.

Lastly, I noticed that CXX_FOR_TARGET was being ignored -- that's
because the align-c++.exp testcase is compiling with the C compiler
driver.  Fixed by passing "c++" as option to prepare_for_testing.

Change-Id: I874b717afde7b6fb1e45e526912b518a20a12716
---
 gdb/testsuite/gdb.base/align.exp.tcl | 49 ++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.base/align.exp.tcl b/gdb/testsuite/gdb.base/align.exp.tcl
index 6a75a14d887..550afe1c47d 100644
--- a/gdb/testsuite/gdb.base/align.exp.tcl
+++ b/gdb/testsuite/gdb.base/align.exp.tcl
@@ -94,12 +94,15 @@ proc prepare_test_source_file { lang } {
 		puts $outfile "DEF_WITH_1_STATIC ($utype, $uinner);"
 		set joined "static_${utype}_x_${uinner}"
 		puts $outfile "struct align_pair_$joined item_${joined};"
+		puts $outfile "$utype align_pair_${joined}::one = 0;"
 		puts $outfile "unsigned a_${joined}"
 		puts $outfile "  = ${align_func} (struct align_pair_${joined});"
 
 		puts $outfile "DEF_WITH_2_STATIC ($utype, $uinner);"
 		set joined "static_${utype}_x_static_${uinner}"
 		puts $outfile "struct align_pair_$joined item_${joined};"
+		puts $outfile "$utype align_pair_${joined}::one = 0;"
+		puts $outfile "$uinner align_pair_${joined}::two = 0;"
 		puts $outfile "unsigned a_${joined}"
 		puts $outfile "  = ${align_func} (struct align_pair_${joined});"
 	    }
@@ -107,12 +110,53 @@ proc prepare_test_source_file { lang } {
     }
 
     # Epilogue.
-    puts $outfile {
+    puts $outfile "
 	int main() {
-	    return 0;
+    "
+
+    # Clang with LTO garbage collects unused global variables, even at
+    # -O0.  Likewise AIX GCC.  Add uses to all global variables to
+    # prevent it.
+
+    if { $lang == "c" } {
+	puts $outfile "a_void++;"
+    }
+
+    # First, add uses for single items.
+    foreach type $typelist {
+	set utype [join [split $type] _]
+	puts $outfile "item_$utype++;"
+	if { $lang == "c" } {
+	    puts $outfile "a_$utype++;"
+	}
+    }
+
+    # Now add uses for all pairs.
+    foreach type $typelist {
+	set utype [join [split $type] _]
+	foreach inner $typelist {
+	    set uinner [join [split $inner] _]
+	    set joined "${utype}_x_${uinner}"
+	    puts $outfile "item_${joined}.one++;"
+	    puts $outfile "a_${joined}++;"
+
+	    if { $lang == "c++" } {
+		set joined "static_${utype}_x_${uinner}"
+		puts $outfile "item_${joined}.one++;"
+		puts $outfile "a_${joined}++;"
+
+		set joined "static_${utype}_x_static_${uinner}"
+		puts $outfile "item_${joined}.one++;"
+		puts $outfile "a_${joined}++;"
+	    }
 	}
     }
 
+    puts $outfile "
+	    return 0;
+	}
+    "
+
     close $outfile
 
     return $filename
@@ -127,6 +171,7 @@ proc run_alignment_test { lang } {
 
     set flags {debug}
     if { "$lang" == "c++" } {
+	lappend flags "c++"
 	lappend flags "additional_flags=-std=c++11"
     }
     standard_testfile $filename

base-commit: b863b097ee7a79b1114b9dd386864bc3c23a51b0
-- 
2.36.0


  reply	other threads:[~2023-03-29 13:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10  8:57 Aditya Kamath1
2023-03-10 14:46 ` Tom Tromey
2023-03-13 13:04   ` Aditya Kamath1
2023-03-13 14:10     ` Ulrich Weigand
2023-03-15 11:52       ` Aditya Kamath1
2023-03-15 12:45         ` Tom Tromey
2023-03-16  7:01           ` Aditya Kamath1
2023-03-17 17:06             ` Ulrich Weigand
2023-03-17 22:03             ` Tom Tromey
2023-03-21  7:01               ` Aditya Kamath1
2023-03-21  7:41                 ` Ulrich Weigand
2023-03-21 11:05                   ` Pedro Alves
2023-03-21 14:17                   ` Tom Tromey
2023-03-21 14:26                     ` Ulrich Weigand
2023-03-29 11:28                       ` Aditya Kamath1
2023-03-29 13:36                         ` Pedro Alves [this message]
2023-03-31 12:29                           ` Aditya Kamath1
2023-04-04 13:24                             ` Aditya Kamath1
2023-04-05 16:33                             ` Pedro Alves
2023-04-06 13:15                               ` Aditya Kamath1
2023-04-06 16:18                                 ` Pedro Alves
  -- strict thread matches above, loose matches on Subject: below --
2023-03-10  8:56 Aditya Kamath1
2023-03-10 10:08 ` Aditya Kamath1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=af6f689f-b699-5756-0abe-4ba9c1346458@palves.net \
    --to=pedro@palves.net \
    --cc=Aditya.Kamath1@ibm.com \
    --cc=Ulrich.Weigand@de.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=sangamesh.swamy@in.ibm.com \
    --cc=simon.marchi@efficios.com \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).