public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack
@ 2024-05-06  3:35 hanwei62 at huawei dot com
  2024-05-06  3:38 ` [Bug target/114955] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: hanwei62 at huawei dot com @ 2024-05-06  3:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114955
           Summary: marco DATA_ALIGNMENT may conflict with pragma pack
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hanwei62 at huawei dot com
  Target Milestone: ---

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

* [Bug target/114955] marco DATA_ALIGNMENT may conflict with pragma pack
  2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
@ 2024-05-06  3:38 ` pinskia at gcc dot gnu.org
  2024-05-06  3:58 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-06  3:38 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |target
             Status|UNCONFIRMED                 |WAITING
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-05-06

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
No comment in the bug. Maybe press return too soon.

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

* [Bug target/114955] marco DATA_ALIGNMENT may conflict with pragma pack
  2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
  2024-05-06  3:38 ` [Bug target/114955] " pinskia at gcc dot gnu.org
@ 2024-05-06  3:58 ` pinskia at gcc dot gnu.org
  2024-05-06  6:39 ` hanwei62 at huawei dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-06  3:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also DATA_ALIGNMENT (and LOCAL_ALIGNMENT) does not conflict with `pragma pack`.

The documentation says
https://gcc.gnu.org/onlinedocs/gcc/Structure-Layout-Pragmas.html :
```
that change the maximum alignment of members of structures (other than
zero-width bit-fields), unions, and classes subsequently defined.
```

This does not mean a target is not free to increase the alignment of a global
(or local) variable to begin with. It just means the alignment of a struct is
that so that pointers of that struct will be with the lower alignment.

Targets/GCC could increase alignment of global and local variables even without
worrying about the alignment of the struct.

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

* [Bug target/114955] marco DATA_ALIGNMENT may conflict with pragma pack
  2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
  2024-05-06  3:38 ` [Bug target/114955] " pinskia at gcc dot gnu.org
  2024-05-06  3:58 ` pinskia at gcc dot gnu.org
@ 2024-05-06  6:39 ` hanwei62 at huawei dot com
  2024-05-06  6:44 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: hanwei62 at huawei dot com @ 2024-05-06  6:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from hanwei (K) <hanwei62 at huawei dot com> ---
Yeap, press return too soon. 

Code like:

#include <stdio.h>

#pragma pack(1)

char a_global[2] = {0,0};
int g_int[3] = {1, 2, 4};
char b_global[2] = {0, 0};

struct A {
    char c;
    long long a;
    int i;
};
#pragma pack()

struct TMP g_struct = {0};
---

We set pragma pack(1) hope the address of variable (also first member of
struct) with struct A type align to 1.

https://github.com/gcc-mirror/gcc/blame/ce343444c019f34172320d0873cc993d43750f41/gcc/config/aarch64/aarch64.h#L128

#define AARCH64_EXPAND_ALIGNMENT(COND, EXP, ALIGN)                      \
  (((COND) && ((ALIGN) < BITS_PER_WORD)                                 \
    && (TREE_CODE (EXP) == ARRAY_TYPE                                   \
        || TREE_CODE (EXP) == UNION_TYPE                                \
        || TREE_CODE (EXP) == RECORD_TYPE)) ? BITS_PER_WORD : (ALIGN))

force the mininal align of arrays, unions and structures (in aarch64 is 8) even
if we set pragma pack for it.

The address of global variable and static variable is implementation-defined,
but the data align behavior of GCC seem conflict with pragma pack?

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

* [Bug target/114955] marco DATA_ALIGNMENT may conflict with pragma pack
  2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
                   ` (2 preceding siblings ...)
  2024-05-06  6:39 ` hanwei62 at huawei dot com
@ 2024-05-06  6:44 ` pinskia at gcc dot gnu.org
  2024-05-06  7:20 ` hanwei62 at huawei dot com
  2024-05-06  7:26 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-06  6:44 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|WAITING                     |RESOLVED

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>We set pragma pack(1) hope the address of variable (also first member of struct) with struct A type align to 1.

But that is not what #pragma pack is documented doing.

You need to use an explicit align attribute (or _Align or alignas [which is the
portable way of doing it for C11 and C++11]).

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

* [Bug target/114955] marco DATA_ALIGNMENT may conflict with pragma pack
  2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
                   ` (3 preceding siblings ...)
  2024-05-06  6:44 ` pinskia at gcc dot gnu.org
@ 2024-05-06  7:20 ` hanwei62 at huawei dot com
  2024-05-06  7:26 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: hanwei62 at huawei dot com @ 2024-05-06  7:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from hanwei (K) <hanwei62 at huawei dot com> ---
You mean the pragma pack just apply to the inner members of struct, not the
first member. The align of struct (first member also) is controlled by
__attribute__((aligned(...))). Right?

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

* [Bug target/114955] marco DATA_ALIGNMENT may conflict with pragma pack
  2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
                   ` (4 preceding siblings ...)
  2024-05-06  7:20 ` hanwei62 at huawei dot com
@ 2024-05-06  7:26 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-06  7:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
It is not about the alignment of the first field but rather the alignment of a
struct. BUT variable alignment is controlled separately from struct alignment.

That is the point I am trying to make. If you want a variable to have a smaller
alignment than what the target backend wants to give it, then you need to use
the aligned attributes.

What pragma pack does is change the alignment of the struct so that if you do:

struct f *t;
....

t->field1 = 10;

It will be an unaligned access there.

But if you have a variable, the alignment will be different and the compiler
can increase still to be more efficient.

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

end of thread, other threads:[~2024-05-06  7:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-06  3:35 [Bug c/114955] New: marco DATA_ALIGNMENT may conflict with pragma pack hanwei62 at huawei dot com
2024-05-06  3:38 ` [Bug target/114955] " pinskia at gcc dot gnu.org
2024-05-06  3:58 ` pinskia at gcc dot gnu.org
2024-05-06  6:39 ` hanwei62 at huawei dot com
2024-05-06  6:44 ` pinskia at gcc dot gnu.org
2024-05-06  7:20 ` hanwei62 at huawei dot com
2024-05-06  7:26 ` pinskia at gcc dot gnu.org

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