public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: Sunil Pandey <skpgkp1@gmail.com>,
	"Joseph S. Myers" <joseph@codesourcery.com>,
	 Sunil K Pandey <skpgkp2@gmail.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Add TARGET_LOWER_LOCAL_DECL_ALIGNMENT [PR95237]
Date: Fri, 26 Jun 2020 13:11:01 -0700	[thread overview]
Message-ID: <CAMe9rOquWrpDS5GWvwet0ZBY5iGOYiM=BLeb8h_naLonFp-1Zw@mail.gmail.com> (raw)
In-Reply-To: <CAFiYyc1WRLNLZfhVRMG-ULSqSb1ocikHN-2tgb8KGKPRm96wQQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5074 bytes --]

On Thu, Jun 25, 2020 at 1:10 AM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Thu, Jun 25, 2020 at 2:53 AM Sunil Pandey <skpgkp1@gmail.com> wrote:
> >
> > On Wed, Jun 24, 2020 at 12:30 AM Richard Biener
> > <richard.guenther@gmail.com> wrote:
> > >
> > > On Tue, Jun 23, 2020 at 5:31 PM Sunil K Pandey via Gcc-patches
> > > <gcc-patches@gcc.gnu.org> wrote:
> > > >
> > > > From: Sunil K Pandey <skpgkp1@gmail.com>
> > > >
> > > > Default for this hook is NOP. For x86, in 32 bit mode, this hook
> > > > sets alignment of long long on stack to 32 bits if preferred stack
> > > > boundary is 32 bits.
> > > >
> > > >  - This patch fixes
> > > >         gcc.target/i386/pr69454-2.c
> > > >         gcc.target/i386/stackalign/longlong-1.c
> > > >  - Regression test on x86-64, no new fail introduced.
> > >
> > > I think the name is badly chosen, TARGET_LOWER_LOCAL_DECL_ALIGNMENT
> >
> > Yes, I can change the target hook name.
> >
> > > would be better suited (and then asks for LOCAL_DECL_ALIGNMENT to be
> > > renamed to INCREASE_LOCAL_DECL_ALIGNMENT).
> >
> > It seems like LOCAL_DECL_ALIGNMENT macro documentation is incorrect.
> > It increases as well as decreases alignment based on condition(-m32
> > -mpreferred-stack-boundary=2)
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95885
> >
> > >
> > > You're calling it from do_type_align which IMHO is dangerous since that's
> > > invoked from FIELD_DECL layout as well.  Instead invoke it from
> > > layout_decl itself where we do
> > >
> > >   if (code != FIELD_DECL)
> > >     /* For non-fields, update the alignment from the type.  */
> > >     do_type_align (type, decl);
> > >
> > > and invoke the hook _after_ do_type_align.  Also avoid
> > > invoking the hook on globals or hard regs and only
> > > invoke it on VAR_DECLs, thus only
> > >
> > >   if (VAR_P (decl) && !is_global_var (decl) && !DECL_HARD_REGISTER (decl))
> >
> > It seems like decl property is not fully populated at this point call
> > to is_global_var (decl) on global variable return false.
> >
> > $ cat foo.c
> > long long x;
> > int main()
> > {
> > if (__alignof__(x) != 8)
> >   __builtin_abort();
> > return 0;
> > }
> >
> > Breakpoint 1, layout_decl (decl=0x7ffff7ffbb40, known_align=0)
> >     at /local/skpandey/gccwork/gccwork/gcc/gcc/stor-layout.c:674
> > 674     do_type_align (type, decl);
> > Missing separate debuginfos, use: dnf debuginfo-install
> > gmp-6.1.2-10.fc31.x86_64 isl-0.16.1-9.fc31.x86_64
> > libmpc-1.1.0-4.fc31.x86_64 mpfr-3.1.6-5.fc31.x86_64
> > zlib-1.2.11-20.fc31.x86_64
> > (gdb) call debug_tree(decl)
> >  <var_decl 0x7ffff7ffbb40 x
> >     type <integer_type 0x7fffea801888 long long int DI
> >         size <integer_cst 0x7fffea7e8d38 constant 64>
> >         unit-size <integer_cst 0x7fffea7e8d50 constant 8>
> >         align:64 warn_if_not_align:0 symtab:0 alias-set -1
> > canonical-type 0x7fffea801888 precision:64 min <integer_cst
> > 0x7fffea7e8fd8 -9223372036854775808> max <integer_cst 0x7fffea806000
> > 9223372036854775807>
> >         pointer_to_this <pointer_type 0x7fffea8110a8>>
> >     DI foo.c:1:11 size <integer_cst 0x7fffea7e8d38 64> unit-size
> > <integer_cst 0x7fffea7e8d50 8>
> >     align:1 warn_if_not_align:0>
> >
> > (gdb) p is_global_var(decl)
> > $1 = false
> > (gdb)
> >
> >
> > What about calling hook here
> >
> >  603 do_type_align (tree type, tree decl)
> >  604 {
> >  605   if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
> >  606     {
> >  607       SET_DECL_ALIGN (decl, TYPE_ALIGN (type));
> >  608       if (TREE_CODE (decl) == FIELD_DECL)
> >  609         DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
> >  610       else
> >  611         /* Lower local decl alignment */
> >  612         if (VAR_P (decl)
> >  613             && !is_global_var (decl)
> >  614             && !DECL_HARD_REGISTER (decl)
> >  615             && cfun != NULL)
> >  616           targetm.lower_local_decl_alignment (decl);
> >  617     }
>
> But that doesn't change anything (obviously).  layout_decl
> is called quite early, too early it looks like.
>
> Now there doesn't seem to be any other good place where
> we are sure to catch the decl before we evaluate things
> like __alignof__
>
> void __attribute__((noipa))
> foo (__SIZE_TYPE__ align, long long *p)
> {
>   if ((__SIZE_TYPE__)p & (align-1))
>     __builtin_abort ();
> }
> int main()
> {
>   long long y;
>   foo (_Alignof y, &y);
>   return 0;
> }
>
> Joseph/Jason - do you have a good recommendation
> how to deal with targets where natural alignment
> is supposed to be lowered for optimization purposes?
> (this case is for i?86 to avoid dynamic stack re-alignment
> to align long long to 8 bytes with -mpreferred-stack-boundary=2)
>
> I note that for -mincoming-stack-boundary=2 we do perform
> dynamic stack re-alignment already.
>
> I can't find a suitable existing target macro/hook for this,
> but my gut feeling is that the default alignment should
> instead be the lower one and instead the alignment for
> globals should be raised as optimization?
>

Here is the updated patch from Sunil.

-- 
H.J.

[-- Attachment #2: 0001-Add-TARGET_LOWER_LOCAL_DECL_ALIGNMENT-PR95237.patch --]
[-- Type: application/octet-stream, Size: 9515 bytes --]

From d6fcbe8370a04b897273e31585bb932ee398289e Mon Sep 17 00:00:00 2001
From: Sunil K Pandey <skpgkp1@gmail.com>
Date: Thu, 18 Jun 2020 08:40:45 -0700
Subject: [PATCH] Add TARGET_LOWER_LOCAL_DECL_ALIGNMENT [PR95237]

Default for this hook is NOP. For x86, in 32 bit mode, this hook
sets alignment of long long on stack to 32 bits if preferred stack
boundary is 32 bits.

 - This patch fixes
 	gcc.target/i386/pr69454-2.c
	gcc.target/i386/stackalign/longlong-1.c
 - Regression test on x86-64, no new fail introduced.

Tested on x86-64.

gcc/ChangeLog:

	PR target/95237
	* config/i386/i386.c (ix86_lower_local_decl_alignment): New
	function.
	(TARGET_LOWER_LOCAL_DECL_ALIGNMENT): Define.
	* doc/tm.texi: Regenerate.
	* doc/tm.texi.in (TARGET_LOWER_LOCAL_DECL_ALIGNMENT): New
	hook.
	* stor-layout.c (do_type_align): Call target hook to lower
	local decl alignment.
	* target.def (lower_local_decl_alignment): New hook.

gcc/testsuite/ChangeLog:

	PR target/95237
	* gcc.target/i386/pr95237-1.c: New test.
	* gcc.target/i386/pr95237-2.c: New test.
	* gcc.target/i386/pr95237-3.c: New test.
	* gcc.target/i386/pr95237-4.c: New test.
	* gcc.target/i386/pr95237-5.c: New test.
	* gcc.target/i386/pr95237-6.c: New test.
	* gcc.target/i386/pr95237-7.c: New test.
---
 gcc/config/i386/i386.c                    | 13 ++++++++++++
 gcc/doc/tm.texi                           |  6 ++++++
 gcc/doc/tm.texi.in                        |  2 ++
 gcc/stor-layout.c                         |  6 ++++++
 gcc/target.def                            |  7 +++++++
 gcc/testsuite/gcc.target/i386/pr95237-1.c | 16 +++++++++++++++
 gcc/testsuite/gcc.target/i386/pr95237-2.c | 10 ++++++++++
 gcc/testsuite/gcc.target/i386/pr95237-3.c | 10 ++++++++++
 gcc/testsuite/gcc.target/i386/pr95237-4.c | 10 ++++++++++
 gcc/testsuite/gcc.target/i386/pr95237-5.c | 16 +++++++++++++++
 gcc/testsuite/gcc.target/i386/pr95237-6.c | 24 +++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr95237-7.c | 19 ++++++++++++++++++
 12 files changed, 139 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-3.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-4.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-5.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-6.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95237-7.c

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 37aaa49996d..0eb2930ac74 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -16917,6 +16917,16 @@ ix86_minimum_alignment (tree exp, machine_mode mode,
 
   return align;
 }
+
+/* Implement TARGET_LOWER_LOCAL_DECL_ALIGNMENT.  */
+
+static void
+ix86_lower_local_decl_alignment (tree decl)
+{
+  unsigned new_align = LOCAL_DECL_ALIGNMENT (decl);
+  if (new_align < DECL_ALIGN (decl))
+    SET_DECL_ALIGN (decl, new_align);
+}
 \f
 /* Find a location for the static chain incoming to a nested function.
    This is a register, unless all free registers are used by arguments.  */
@@ -23519,6 +23529,9 @@ ix86_run_selftests (void)
 #undef TARGET_CAN_CHANGE_MODE_CLASS
 #define TARGET_CAN_CHANGE_MODE_CLASS ix86_can_change_mode_class
 
+#undef TARGET_LOWER_LOCAL_DECL_ALIGNMENT
+#define TARGET_LOWER_LOCAL_DECL_ALIGNMENT ix86_lower_local_decl_alignment
+
 #undef TARGET_STATIC_RTX_ALIGNMENT
 #define TARGET_STATIC_RTX_ALIGNMENT ix86_static_rtx_alignment
 #undef TARGET_CONSTANT_ALIGNMENT
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index 6e7d9dc54a9..60421c3cb5a 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -1086,6 +1086,12 @@ On 32-bit ELF the largest supported section alignment in bits is
 @samp{(0x80000000 * 8)}, but this is not representable on 32-bit hosts.
 @end defmac
 
+@deftypefn {Target Hook} void TARGET_LOWER_LOCAL_DECL_ALIGNMENT
+(tree @var{decl})
+Define this hook to lower alignment of local decl
+@samp{(@var{decl}}.
+@end deftypefn
+
 @deftypefn {Target Hook} HOST_WIDE_INT TARGET_STATIC_RTX_ALIGNMENT (machine_mode @var{mode})
 This hook returns the preferred alignment in bits for a
 statically-allocated rtx, such as a constant pool entry.  @var{mode}
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index 3be984bbd5c..d76c85d5800 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -1036,6 +1036,8 @@ On 32-bit ELF the largest supported section alignment in bits is
 @samp{(0x80000000 * 8)}, but this is not representable on 32-bit hosts.
 @end defmac
 
+@hook TARGET_LOWER_LOCAL_DECL_ALIGNMENT
+
 @hook TARGET_STATIC_RTX_ALIGNMENT
 
 @defmac DATA_ALIGNMENT (@var{type}, @var{basic-align})
diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c
index bde6fa22b58..24c66e68083 100644
--- a/gcc/stor-layout.c
+++ b/gcc/stor-layout.c
@@ -607,6 +607,12 @@ do_type_align (tree type, tree decl)
       SET_DECL_ALIGN (decl, TYPE_ALIGN (type));
       if (TREE_CODE (decl) == FIELD_DECL)
 	DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
+      /* Lower local decl alignment.  */
+      else if (cfun != NULL
+	       && VAR_P (decl)
+	       && !is_global_var (decl)
+	       && !DECL_HARD_REGISTER (decl))
+	targetm.lower_local_decl_alignment (decl);
     }
   if (TYPE_WARN_IF_NOT_ALIGN (type) > DECL_WARN_IF_NOT_ALIGN (decl))
     SET_DECL_WARN_IF_NOT_ALIGN (decl, TYPE_WARN_IF_NOT_ALIGN (type));
diff --git a/gcc/target.def b/gcc/target.def
index 07059a87caf..6efaee62c19 100644
--- a/gcc/target.def
+++ b/gcc/target.def
@@ -3348,6 +3348,13 @@ HOOK_VECTOR_END (addr_space)
 #undef HOOK_PREFIX
 #define HOOK_PREFIX "TARGET_"
 
+DEFHOOK
+(lower_local_decl_alignment,
+ "Define this hook to lower alignment of local decl\n\
+@samp{(@var{decl}}.",
+ void, (tree decl),
+ hook_void_tree)
+
 DEFHOOK
 (static_rtx_alignment,
  "This hook returns the preferred alignment in bits for a\n\
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-1.c b/gcc/testsuite/gcc.target/i386/pr95237-1.c
new file mode 100644
index 00000000000..bc8a84ee0db
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-1.c
@@ -0,0 +1,16 @@
+/* { dg-do run } */
+/* { dg-require-effective-target ia32 } */
+/* { dg-options "-mpreferred-stack-boundary=2" } */
+typedef __UINTPTR_TYPE__ uintptr_t;
+void __attribute__((noipa)) foo (long long *p, uintptr_t a)
+{
+  if ((uintptr_t)p & (a-1))
+      __builtin_abort ();
+}
+int main()
+{
+	long long x;
+	uintptr_t a = __alignof__(x);
+	foo(&x, a);
+	return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-2.c b/gcc/testsuite/gcc.target/i386/pr95237-2.c
new file mode 100644
index 00000000000..82ff777669a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-2.c
@@ -0,0 +1,10 @@
+/* { dg-do run } */
+/* { dg-require-effective-target ia32 } */
+/* { dg-options "-mpreferred-stack-boundary=2" } */
+long long x;
+int main()
+{
+	if (__alignof__(x) != 8)
+	  __builtin_abort();
+	return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-3.c b/gcc/testsuite/gcc.target/i386/pr95237-3.c
new file mode 100644
index 00000000000..2fb1f630362
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-3.c
@@ -0,0 +1,10 @@
+/* { dg-do run } */
+/* { dg-require-effective-target ia32 } */
+/* { dg-options "-mpreferred-stack-boundary=2" } */
+int main()
+{
+	long long x;
+	if (__alignof__(x) != 4)
+	  __builtin_abort();
+	return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-4.c b/gcc/testsuite/gcc.target/i386/pr95237-4.c
new file mode 100644
index 00000000000..d52a770d703
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-4.c
@@ -0,0 +1,10 @@
+/* { dg-do run } */
+/* { dg-require-effective-target ia32 } */
+/* { dg-options "-mpreferred-stack-boundary=4" } */
+int main()
+{
+	long long x;
+	if (__alignof__(x) != 8)
+	  __builtin_abort();
+	return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-5.c b/gcc/testsuite/gcc.target/i386/pr95237-5.c
new file mode 100644
index 00000000000..4d9be06a045
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-5.c
@@ -0,0 +1,16 @@
+/* { dg-do compile { target ia32 } } */
+/* { dg-options "-mpreferred-stack-boundary=2 -Os -w" } */
+
+int a;
+
+long long
+b (void)
+{
+}
+
+void
+c (void)
+{
+  if (b())
+    a = 1;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-6.c b/gcc/testsuite/gcc.target/i386/pr95237-6.c
new file mode 100644
index 00000000000..a1357de2c48
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-6.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+#include <stddef.h>
+#ifdef  __x86_64__
+# define EXP_ALIGN 8
+#else
+# define EXP_ALIGN 4
+#endif
+
+struct test
+{
+  char a;
+  long long b;
+};
+struct test global_var;
+int main()
+{
+  	struct test local_var;
+	if (__alignof__(global_var) != EXP_ALIGN
+	    || __alignof__(local_var) != EXP_ALIGN
+	    || offsetof(struct test, b) != EXP_ALIGN)
+	  __builtin_abort();
+	return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr95237-7.c b/gcc/testsuite/gcc.target/i386/pr95237-7.c
new file mode 100644
index 00000000000..d612eb4a1f8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95237-7.c
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target ia32 } */
+/* { dg-options "-mpreferred-stack-boundary=2" } */
+#include <stddef.h>
+struct test
+{
+  char a;
+  long long b;
+};
+struct test global_var;
+int main()
+{
+  	struct test local_var;
+	if (__alignof__(global_var) != 4
+	    || __alignof__(local_var) != 4
+	    || offsetof(struct test, b) != 4)
+	  __builtin_abort();
+	return 0;
+}
-- 
2.26.2


  reply	other threads:[~2020-06-26 20:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-23 15:29 [PATCH] Add TARGET_UPDATE_DECL_ALIGNMENT [PR95237] Sunil K Pandey
2020-06-24  7:30 ` Richard Biener
2020-06-25  0:52   ` Sunil Pandey
2020-06-25  8:10     ` Richard Biener
2020-06-26 20:11       ` H.J. Lu [this message]
2020-06-29  9:00         ` [PATCH] Add TARGET_LOWER_LOCAL_DECL_ALIGNMENT [PR95237] Richard Biener
2020-07-03 21:16           ` Jason Merrill
2020-07-04 16:11             ` Richard Biener
2020-07-14 15:37               ` Sunil Pandey
2020-07-17  5:15                 ` Sunil Pandey
2020-07-17  8:22                   ` Richard Biener
2020-07-18  5:57                     ` Sunil Pandey
2020-07-20 12:06                       ` Richard Biener
2020-07-21  5:16                         ` Sunil Pandey
2020-07-21  7:50                           ` Richard Biener
2020-07-21 23:04                             ` Sunil Pandey
2020-07-22 14:24                               ` Dimitar Dimitrov
2020-07-22 14:37                                 ` H.J. Lu

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='CAMe9rOquWrpDS5GWvwet0ZBY5iGOYiM=BLeb8h_naLonFp-1Zw@mail.gmail.com' \
    --to=hjl.tools@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=richard.guenther@gmail.com \
    --cc=skpgkp1@gmail.com \
    --cc=skpgkp2@gmail.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).