https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129230

            Bug ID: 129230
           Summary: [modules] aligned / warn_if_not_aligned attributes on
                    typedefs are dropped on import
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adrian.herrmann.xyz at gmail dot com
  Target Milestone: ---

Exported typedefs with __attribute__((aligned(N))) lose their custom alignment
when imported from a C++20 module. For under-aligned vector typedefs this
results in wrong-code (aligned SIMD loads/stores instead of unaligned ones).

--- mod.cppm ---
export module m;

export typedef int AlignedInt16 __attribute__((aligned(16)));
export typedef unsigned long long UnalignedVec16
__attribute__((vector_size(16), aligned(1)));
export typedef unsigned long long OveralignedVec16
__attribute__((vector_size(16), aligned(32)));

static_assert(alignof(AlignedInt16) == 16);
static_assert(alignof(UnalignedVec16) == 1);
static_assert(alignof(OveralignedVec16) == 32);

--- main.cpp ---
import m;

static_assert(alignof(AlignedInt16) == 16);
static_assert(alignof(UnalignedVec16) == 1);
static_assert(alignof(OveralignedVec16) == 32);

$ g++ -std=c++20 -fmodules -c mod.cppm -o mod.o
$ g++ -std=c++20 -fmodules -c main.cpp -o main.o
main.cpp:3:37: error: static assertion failed
    3 | static_assert(alignof(AlignedInt16) == 16);
      |               ~~~~~~~~~~~~~~~~~~~~~~^~~~~
  * the comparison reduces to '(4 == 16)'
main.cpp:4:39: error: static assertion failed
    4 | static_assert(alignof(UnalignedVec16) == 1);
      |               ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
  * the comparison reduces to '(16 == 1)'
main.cpp:5:41: error: static assertion failed
    5 | static_assert(alignof(OveralignedVec16) == 32);
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
  * the comparison reduces to '(16 == 32)'

common_handle_aligned_attribute stores TYPE_USER_ALIGN / TYPE_ALIGN_RAW (and
TYPE_WARN_IF_NOT_ALIGN_RAW) on TREE_TYPE (decl), not DECL_ORIGINAL_TYPE (decl).
In cp/module.cc, trees_out::core_vals skips TREE_TYPE for regular typedefs,
trees_out::decl_value only encodes is_typedef and dependent-alias bits in
tdef_flags, and trees_in::decl_value reconstructs TREE_TYPE via
set_underlying_type with default alignment.

I have a patch to stream and restore TYPE_ALIGN_RAW /
TYPE_WARN_IF_NOT_ALIGN_RAW in decl_value and plan to send it to gcc-patches.

Tested on master 48688cfeadf6ec3b2dd22783f5048db6fc444d04 (also reproduces on
5a61875b0cd983e57dd44b7d6e12b9eb11985bae):
Using built-in specs.
COLLECT_GCC=xg++
Target: x86_64-elf
Configured with: src/configure --target=x86_64-elf --enable-languages=c++
--disable-nls --disable-lto --disable-libssp --disable-multilib
--without-headers --without-isl --without-zstd --enable-checking=yes
Thread model: single
gcc version 17.0.0 20261126 (experimental) (GCC)

Reply via email to