public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* stor-layout: Set TYPE_TYPELESS_STORAGE consistently for type variants
@ 2023-03-20 19:46 Joseph Myers
  2023-03-21  7:29 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Joseph Myers @ 2023-03-20 19:46 UTC (permalink / raw)
  To: gcc-patches

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

I've observed an LTO wrong-code bug with a large testcase in GCC 12,
that results from TYPE_TYPELESS_STORAGE not being set consistently on
type variants.

Specifically, in the LTO stage of compilation, there is an aggregate
type passed to get_alias_set, whose TYPE_MAIN_VARIANT does not have
TYPE_TYPELESS_STORAGE set.  However, the TYPE_CANONICAL of that main
variant *does* have have TYPE_TYPELESS_STORAGE set; note that the use
of TYPE_CANONICAL in get_alias_set comes after the check of
TYPE_TYPELESS_STORAGE.  The effect is that when (one-argument)
record_component_aliases is called, the recursive call to
get_alias_set gives alias set 0, and the aggregate type ends up not
being considered to alias its members, with wrong-code consequences.

I haven't managed to produce a self-contained executable testcase to
demonstrate this, but it clearly seems appropriate for
TYPE_TYPELESS_STORAGE to be consistent on type variants, so this patch
makes it so, which appears to be sufficient to resolve the bug.  I've
attached a reduced test that does at least demonstrate main-variant
versions of a type (SB in this test) being written out to LTO IR both
with and without TYPE_TYPELESS_STORAGE, although not the subsequent
consequences of a type without TYPE_TYPELESS_STORAGE with a
TYPE_CANONICAL (as constructed after LTO type merging) with
TYPE_TYPELESS_STORAGE and following wrong-code.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  OK to commit?

	* stor-layout.cc (finalize_type_size): Copy TYPE_TYPELESS_STORAGE
	to variants.

diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index 45bf2d18639..023de8c37db 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1996,6 +1996,7 @@ finalize_type_size (tree type)
       unsigned int user_align = TYPE_USER_ALIGN (type);
       machine_mode mode = TYPE_MODE (type);
       bool empty_p = TYPE_EMPTY_P (type);
+      bool typeless = AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type);
 
       /* Copy it into all variants.  */
       for (variant = TYPE_MAIN_VARIANT (type);
@@ -2020,6 +2021,8 @@ finalize_type_size (tree type)
 	  TYPE_PRECISION (variant) = precision;
 	  SET_TYPE_MODE (variant, mode);
 	  TYPE_EMPTY_P (variant) = empty_p;
+	  if (AGGREGATE_TYPE_P (variant))
+	    TYPE_TYPELESS_STORAGE (variant) = typeless;
 	}
     }
 }

-- 
Joseph S. Myers
joseph@codesourcery.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-c++src; name="typeless.cc", Size: 369 bytes --]

struct S {
  int a;
  char b[8];
};
template <int> class SB {
public:
  operator bool() { return true; };
  S x;
};
class T : public SB<0> {};
template <typename TT> class m1 {
public:
  m1(TT) {}
  void m2() {};
};
class U {
public:
  U(int, T c) {
    auto v = m1([] {});
    if (c)
      v.m2();
  }
};
void f() {
  T c = {};
  U(0, c);
}

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

* Re: stor-layout: Set TYPE_TYPELESS_STORAGE consistently for type variants
  2023-03-20 19:46 stor-layout: Set TYPE_TYPELESS_STORAGE consistently for type variants Joseph Myers
@ 2023-03-21  7:29 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-03-21  7:29 UTC (permalink / raw)
  To: Joseph Myers; +Cc: gcc-patches

On Mon, Mar 20, 2023 at 8:47 PM Joseph Myers <joseph@codesourcery.com> wrote:
>
> I've observed an LTO wrong-code bug with a large testcase in GCC 12,
> that results from TYPE_TYPELESS_STORAGE not being set consistently on
> type variants.
>
> Specifically, in the LTO stage of compilation, there is an aggregate
> type passed to get_alias_set, whose TYPE_MAIN_VARIANT does not have
> TYPE_TYPELESS_STORAGE set.  However, the TYPE_CANONICAL of that main
> variant *does* have have TYPE_TYPELESS_STORAGE set; note that the use
> of TYPE_CANONICAL in get_alias_set comes after the check of
> TYPE_TYPELESS_STORAGE.  The effect is that when (one-argument)
> record_component_aliases is called, the recursive call to
> get_alias_set gives alias set 0, and the aggregate type ends up not
> being considered to alias its members, with wrong-code consequences.
>
> I haven't managed to produce a self-contained executable testcase to
> demonstrate this, but it clearly seems appropriate for
> TYPE_TYPELESS_STORAGE to be consistent on type variants, so this patch
> makes it so, which appears to be sufficient to resolve the bug.  I've
> attached a reduced test that does at least demonstrate main-variant
> versions of a type (SB in this test) being written out to LTO IR both
> with and without TYPE_TYPELESS_STORAGE, although not the subsequent
> consequences of a type without TYPE_TYPELESS_STORAGE with a
> TYPE_CANONICAL (as constructed after LTO type merging) with
> TYPE_TYPELESS_STORAGE and following wrong-code.
>
> Bootstrapped with no regressions for x86_64-pc-linux-gnu.  OK to commit?

OK for trunk and branches.

Thanks,
Richard.

>         * stor-layout.cc (finalize_type_size): Copy TYPE_TYPELESS_STORAGE
>         to variants.
>
> diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
> index 45bf2d18639..023de8c37db 100644
> --- a/gcc/stor-layout.cc
> +++ b/gcc/stor-layout.cc
> @@ -1996,6 +1996,7 @@ finalize_type_size (tree type)
>        unsigned int user_align = TYPE_USER_ALIGN (type);
>        machine_mode mode = TYPE_MODE (type);
>        bool empty_p = TYPE_EMPTY_P (type);
> +      bool typeless = AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type);
>
>        /* Copy it into all variants.  */
>        for (variant = TYPE_MAIN_VARIANT (type);
> @@ -2020,6 +2021,8 @@ finalize_type_size (tree type)
>           TYPE_PRECISION (variant) = precision;
>           SET_TYPE_MODE (variant, mode);
>           TYPE_EMPTY_P (variant) = empty_p;
> +         if (AGGREGATE_TYPE_P (variant))
> +           TYPE_TYPELESS_STORAGE (variant) = typeless;
>         }
>      }
>  }
>
> --
> Joseph S. Myers
> joseph@codesourcery.com

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

end of thread, other threads:[~2023-03-21  7:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-20 19:46 stor-layout: Set TYPE_TYPELESS_STORAGE consistently for type variants Joseph Myers
2023-03-21  7:29 ` Richard Biener

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