[clang] [clang] Stub out gcc_struct attribute (PR #71148)

Martin Storsjö via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 29 12:17:18 PST 2023


mstorsjo wrote:

> `-mms-bitfields` is a GCC x86 specific option (`aarch64-linux-gnu-gcc -mms-bitfields -xc /dev/null -E` => `error: unrecognized command-line option ‘-mms-bitfields’`).

While it is implemented as an x86 specific option in GCC right now, that doesn't mean that it only is supposed to have an effect for x86. Upstream GCC only supports Windows on x86, and my recollection is that lots of the Windows specific logic is located in x86 specific files, even if the same logic also should apply for Windows on any other architecture - it's just not implemented (yet).

As implemented in Clang, the option works for any MinGW target.

As an example:
```c
struct field {
        unsigned char a : 4;
        unsigned int b : 4;
};
int size = sizeof(struct field);
```
```console
$ clang -target x86_64-windows-gnu -S -o - bitfields.c | grep -A1 size
        .globl  size                            # @size
        .p2align        2, 0x0
size:
        .long   8                               # 0x8
$ clang -target x86_64-windows-gnu -S -o - bitfields.c -mno-ms-bitfields | grep -A1 size
        .globl  size                            # @size
        .p2align        2, 0x0
size:
        .long   4                               # 0x4
$ clang -target aarch64-windows-gnu -S -o - bitfields.c | grep -A1 size
        .globl  size                            // @size
        .p2align        2, 0x0
size:
        .word   8                               // 0x8
$ clang -target aarch64-windows-gnu -S -o - bitfields.c -mno-ms-bitfields | grep -A1 size
        .globl  size                            // @size
        .p2align        2, 0x0
size:
        .word   4                               // 0x4
```

---

> https://gitlab.com/qemu-project/qemu/-/issues/1782#note_1495842591 seems like ignored `gcc_struct` for windows-gnu targets (feature request #24757).
> I agree that if there are real needs and the needs seem genuine, Clang should support `gcc_struct`.

Yes, this would clearly be good to have. For orthogonality, it would be good to have both `gcc_struct` and `ms_struct` available. GCC does support them on non-windows targets as well; I think that can be useful for implementing things like Wine.

---
As noted somewhere (I don't see the comment to quote right now), the MS bitfield packing logic (as enabled by `-mms-bitfields`) is enabled by default when using the MSVC C++ ABI, but not when using the Itanium C++ ABI. But as referenced, since https://reviews.llvm.org/D81795, we do enable the option `-mms-bitfields` automatically for MinGW targets which otherwise do use the Itanium C++ ABI. Being able to override this back to the default format for individual structs would be great.

I don't know and can't speculate about what the implications would be for being able to switch to GCC struct layout in the MSVC C++ ABI, though. For individual structs, I guess it should be doable (I'm only thinking for trivial-ish structs like the one in my example above, right now though). For anything relating to C++ classes and the mechanisms behind them, I'm pretty sure one doesn't want to change how anything of that works. Therefore I don't think it's too relevant to implement `-mno-ms-bitfields` for MSVC targets (as I guess it could open a huge can of worms).

https://github.com/llvm/llvm-project/pull/71148


More information about the cfe-commits mailing list