llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29k stars 11.95k forks source link

[AArch64][ARM] Why fpack-struct=4 has a higher priority than attribute #103495

Open LukeSTM opened 2 months ago

LukeSTM commented 2 months ago
struct s1
{
  int __attribute__ ((aligned (8))) a;
};

struct
{
  char c;
  struct s1 m;
} v;

int
main (void)
{
  if ((int)&v.m & 7)
    abort ();
  exit (0);
}

compile with only one option:-fpack-struct=4

gcc could pass this testcase, but clang could not. https://godbolt.org/z/h5T5rdYWb

The code can be simplified as follows:

struct s1
{
  int __attribute__ ((aligned (8))) a;
}s;

https://godbolt.org/z/EbGsEz6eP image

if compile without -fpack-struct=4, the alignment of struct is 8, as same as attribue. But if compile with -fpack-struct=4, the alignment of struct is 4. Why fpack-struct=4 has a higher priority than attribute? I think attribute should has a higher priority than Compilation options

llvmbot commented 2 months ago

@llvm/issue-subscribers-clang-codegen

Author: Luke (LukeSTM)

``` struct s1 { int __attribute__ ((aligned (8))) a; }; struct { char c; struct s1 m; } v; int main (void) { if ((int)&v.m & 7) abort (); exit (0); } ``` compile with only one option:-fpack-struct=4 gcc could pass this testcase, but clang could not. https://godbolt.org/z/h5T5rdYWb The code can be simplified as follows: ``` struct s1 { int __attribute__ ((aligned (8))) a; }s; ``` https://godbolt.org/z/EbGsEz6eP ![image](https://github.com/user-attachments/assets/a052017d-5d28-4ceb-862c-e400c022492c) if compile without -fpack-struct=4, the alignment of struct is 8, as same as attribue. But if compile with -fpack-struct=4, the alignment of struct is 4. Why fpack-struct=4 has a higher priority than attribute? I think attribute should has a higher priority than Compilation options
LukeSTM commented 2 months ago

attribute ((packed)) has a higher priority than fpack-struct=4

https://godbolt.org/z/j3x8P93z3 image

LukeSTM commented 2 months ago

It seem like llvm prefer a small alignment. If attribute ((align (4))) and fpack-struct=8, alignment = 4. If attribute ((align (4))) and fpack-struct, alignment = 1. https://godbolt.org/z/ddeMvzEGE image

hstk30-hw commented 2 months ago

Is this an expected behavior? CC @AaronBallman @zygoloid

LukeSTM commented 2 months ago

image

LukeSTM commented 2 months ago

I understand that the alignment rules of the structures of gcc and llvm should be the same, but the default alignment rules are different. gcc arm struct alignment default 4byte,and could not less than 4byte。llvm does not have this restriction.