stor-layout: Set TYPE_TYPELESS_STORAGE consistently for type variants
authorJoseph Myers <joseph@codesourcery.com>
Tue, 21 Mar 2023 22:35:49 +0000 (22:35 +0000)
committerJoseph Myers <joseph@codesourcery.com>
Tue, 21 Mar 2023 22:35:49 +0000 (22:35 +0000)
commitb33213135983fd5b591476dc2336f396d642302f
tree43256c1469714e489baf042f32ba38a82b211b3e
parentfac64bf456cf56f0c6309d21286b7eaf170f668e
stor-layout: Set TYPE_TYPELESS_STORAGE consistently for type variants

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 to
<https://gcc.gnu.org/pipermail/gcc-patches/2023-March/614278.html>
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.

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