llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang and GCC disagree about whether union containing empty struct can be HFA

Open eleanor-arm opened this issue 2 years ago • 2 comments

clang and GCC disagree about whether this union type is an HFA, and so whether it should be passed in floating-point or integer registers

empty_struct.cpp

struct empty {};
union maybe_hfa {
  float a;
  empty b;
};

float bar(float a, maybe_hfa b) { return b.a; }

HFA

The ABI isn't totally clear about what the right answer is here, but it seems more consistent to always treat empty as a type with size and alignment of 1 in all contexts in C++, so I think GCC's behaviour is more likely to be correct.

Note that the meaning of an empty struct varies between C and C++:

In C, it is technically undefined behaviour, but GCC defines it as having size zero as an extension, and I think clang tries to match that: https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Empty-Structures.html In C++, an empty struct (or class, or union) is well-defined, and the Itanium C++ ABI says it has a size and alignment of 1 byte.

This is LLVM's codegen, for both AArch32 and AArch64, which shows it passing the maybe_hfa argument in s1, since it considers it to be an HFA:

clang --target=arm--none-eabi -march=armv8-a -O1 -c empty_struct.cpp -o - -S -mfloat-abi=hard
        .text
        .syntax unified
        .eabi_attribute 67, "2.09"      @ Tag_conformance
        .eabi_attribute 6, 14   @ Tag_CPU_arch
        .eabi_attribute 7, 65   @ Tag_CPU_arch_profile
        .eabi_attribute 8, 1    @ Tag_ARM_ISA_use
        .eabi_attribute 9, 2    @ Tag_THUMB_ISA_use
        .fpu    crypto-neon-fp-armv8
        .eabi_attribute 12, 3   @ Tag_Advanced_SIMD_arch
        .eabi_attribute 36, 1   @ Tag_FP_HP_extension
        .eabi_attribute 42, 1   @ Tag_MPextension_use
        .eabi_attribute 34, 1   @ Tag_CPU_unaligned_access
        .eabi_attribute 68, 3   @ Tag_Virtualization_use
        .eabi_attribute 17, 1   @ Tag_ABI_PCS_GOT_use
        .eabi_attribute 20, 1   @ Tag_ABI_FP_denormal
        .eabi_attribute 21, 0   @ Tag_ABI_FP_exceptions
        .eabi_attribute 23, 3   @ Tag_ABI_FP_number_model
        .eabi_attribute 24, 1   @ Tag_ABI_align_needed
        .eabi_attribute 25, 1   @ Tag_ABI_align_preserved
        .eabi_attribute 28, 1   @ Tag_ABI_VFP_args
        .eabi_attribute 38, 1   @ Tag_ABI_FP_16bit_format
        .eabi_attribute 18, 4   @ Tag_ABI_PCS_wchar_t
        .eabi_attribute 26, 2   @ Tag_ABI_enum_size
        .eabi_attribute 14, 0   @ Tag_ABI_PCS_R9_use
        .file   "empty_struct.cpp"
        .globl  _Z3barf9maybe_hfa               @ -- Begin function _Z3barf9maybe_hfa
        .p2align        2
        .type   _Z3barf9maybe_hfa,%function
        .code   32                              @ @_Z3barf9maybe_hfa
_Z3barf9maybe_hfa:
        .fnstart
@ %bb.0:                                @ %entry
        vmov.f32        s0, s1
        bx      lr
.Lfunc_end0:
        .size   _Z3barf9maybe_hfa, .Lfunc_end0-_Z3barf9maybe_hfa
        .cantunwind
        .fnend
                                        @ -- End function
        .ident  "clang version 19.0.0git ([email protected]:llvm/llvm-project.git 79ce70b8033815b6abd3a9a5cc2335de70f1aaab)"
        .section        ".note.GNU-stack","",%progbits
        .addrsig
        .eabi_attribute 30, 1   @ Tag_ABI_optimization_goals
clang --target=aarch64--none-elf -march=armv8-a -O1 -c empty_struct.cpp -o - -S
        .text
        .file   "empty_struct.cpp"
        .globl  _Z3barf9maybe_hfa               // -- Begin function _Z3barf9maybe_hfa
        .p2align        2
        .type   _Z3barf9maybe_hfa,@function
_Z3barf9maybe_hfa:                      // @_Z3barf9maybe_hfa
// %bb.0:                               // %entry
        fmov    s0, s1
        ret
.Lfunc_end0:
        .size   _Z3barf9maybe_hfa, .Lfunc_end0-_Z3barf9maybe_hfa
                                        // -- End function
        .ident  "clang version 19.0.0git ([email protected]:llvm/llvm-project.git 79ce70b8033815b6abd3a9a5cc2335de70f1aaab)"
        .section        ".note.GNU-stack","",@progbits
        .addrsig

For comparison, this is gcc's output with the same options, showing that is passes the argument in r0/w0:

arm-none-eabi-gcc -march=armv8-a -mfpu=fp-armv8 -c empty_struct.cpp -O1 -o - -S -mfloat-abi=hard
        .arch armv8-a
        .fpu fp-armv8
        .eabi_attribute 28, 1
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 1
        .eabi_attribute 30, 1
        .eabi_attribute 34, 1
        .eabi_attribute 18, 4
        .file   "empty_struct.cpp"
        .text
        .align  2
        .global _Z3barf9maybe_hfa
        .syntax unified
        .arm
        .type   _Z3barf9maybe_hfa, %function
_Z3barf9maybe_hfa:
        .fnstart
.LFB0:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        vmov    s0, r0
        bx      lr
        .cantunwind
        .fnend
        .size   _Z3barf9maybe_hfa, .-_Z3barf9maybe_hfa
        .ident  "GCC: (fsf-trunk.3697) 15.0.0 20240514 (experimental)"
aarch64-none-elf-gcc -march=armv8-a -c -c empty_struct.cpp -O1 -o - -S
        .arch armv8-a
        .file   "empty_struct.cpp"
        .text
        .align  2
        .global _Z3barf9maybe_hfa
        .type   _Z3barf9maybe_hfa, %function
_Z3barf9maybe_hfa:
.LFB0:
        .cfi_startproc
        fmov    s0, w0
        ret
        .cfi_endproc
.LFE0:
        .size   _Z3barf9maybe_hfa, .-_Z3barf9maybe_hfa
        .ident  "GCC: (fsf-trunk.3697) 15.0.0 20240514 (experimental)"

eleanor-arm avatar Jul 19 '24 12:07 eleanor-arm

Maybe related to https://github.com/llvm/llvm-project/blob/c5ded52d5f3603182b41be64f1f8ac3bbdaa3daa/clang/lib/CodeGen/ABIInfo.cpp#L115-L116

ChuanqiXu9 avatar Sep 25 '25 09:09 ChuanqiXu9

@llvm/issue-subscribers-backend-aarch64

Author: Eleanor Bonnici (eleanor-arm)

clang and GCC disagree about whether this union type is an HFA, and so whether it should be passed in floating-point or integer registers

empty_struct.cpp

struct empty {};
union maybe_hfa {
  float a;
  empty b;
};

float bar(float a, maybe_hfa b) { return b.a; }

HFA

The ABI isn't totally clear about what the right answer is here, but it seems more consistent to always treat empty as a type with size and alignment of 1 in all contexts in C++, so I think GCC's behaviour is more likely to be correct.

Note that the meaning of an empty struct varies between C and C++:

In C, it is technically undefined behaviour, but GCC defines it as having size zero as an extension, and I think clang tries to match that: https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Empty-Structures.html In C++, an empty struct (or class, or union) is well-defined, and the Itanium C++ ABI says it has a size and alignment of 1 byte.

This is LLVM's codegen, for both AArch32 and AArch64, which shows it passing the maybe_hfa argument in s1, since it considers it to be an HFA:

clang --target=arm--none-eabi -march=armv8-a -O1 -c empty_struct.cpp -o - -S -mfloat-abi=hard
        .text
        .syntax unified
        .eabi_attribute 67, "2.09"      @ Tag_conformance
        .eabi_attribute 6, 14   @ Tag_CPU_arch
        .eabi_attribute 7, 65   @ Tag_CPU_arch_profile
        .eabi_attribute 8, 1    @ Tag_ARM_ISA_use
        .eabi_attribute 9, 2    @ Tag_THUMB_ISA_use
        .fpu    crypto-neon-fp-armv8
        .eabi_attribute 12, 3   @ Tag_Advanced_SIMD_arch
        .eabi_attribute 36, 1   @ Tag_FP_HP_extension
        .eabi_attribute 42, 1   @ Tag_MPextension_use
        .eabi_attribute 34, 1   @ Tag_CPU_unaligned_access
        .eabi_attribute 68, 3   @ Tag_Virtualization_use
        .eabi_attribute 17, 1   @ Tag_ABI_PCS_GOT_use
        .eabi_attribute 20, 1   @ Tag_ABI_FP_denormal
        .eabi_attribute 21, 0   @ Tag_ABI_FP_exceptions
        .eabi_attribute 23, 3   @ Tag_ABI_FP_number_model
        .eabi_attribute 24, 1   @ Tag_ABI_align_needed
        .eabi_attribute 25, 1   @ Tag_ABI_align_preserved
        .eabi_attribute 28, 1   @ Tag_ABI_VFP_args
        .eabi_attribute 38, 1   @ Tag_ABI_FP_16bit_format
        .eabi_attribute 18, 4   @ Tag_ABI_PCS_wchar_t
        .eabi_attribute 26, 2   @ Tag_ABI_enum_size
        .eabi_attribute 14, 0   @ Tag_ABI_PCS_R9_use
        .file   "empty_struct.cpp"
        .globl  _Z3barf9maybe_hfa               @ -- Begin function _Z3barf9maybe_hfa
        .p2align        2
        .type   _Z3barf9maybe_hfa,%function
        .code   32                              @ @<!-- -->_Z3barf9maybe_hfa
_Z3barf9maybe_hfa:
        .fnstart
@ %bb.0:                                @ %entry
        vmov.f32        s0, s1
        bx      lr
.Lfunc_end0:
        .size   _Z3barf9maybe_hfa, .Lfunc_end0-_Z3barf9maybe_hfa
        .cantunwind
        .fnend
                                        @ -- End function
        .ident  "clang version 19.0.0git (git@<!-- -->github.com:llvm/llvm-project.git 79ce70b8033815b6abd3a9a5cc2335de70f1aaab)"
        .section        ".note.GNU-stack","",%progbits
        .addrsig
        .eabi_attribute 30, 1   @ Tag_ABI_optimization_goals
clang --target=aarch64--none-elf -march=armv8-a -O1 -c empty_struct.cpp -o - -S
        .text
        .file   "empty_struct.cpp"
        .globl  _Z3barf9maybe_hfa               // -- Begin function _Z3barf9maybe_hfa
        .p2align        2
        .type   _Z3barf9maybe_hfa,@<!-- -->function
_Z3barf9maybe_hfa:                      // @<!-- -->_Z3barf9maybe_hfa
// %bb.0:                               // %entry
        fmov    s0, s1
        ret
.Lfunc_end0:
        .size   _Z3barf9maybe_hfa, .Lfunc_end0-_Z3barf9maybe_hfa
                                        // -- End function
        .ident  "clang version 19.0.0git (git@<!-- -->github.com:llvm/llvm-project.git 79ce70b8033815b6abd3a9a5cc2335de70f1aaab)"
        .section        ".note.GNU-stack","",@<!-- -->progbits
        .addrsig

For comparison, this is gcc's output with the same options, showing that is passes the argument in r0/w0:

arm-none-eabi-gcc -march=armv8-a -mfpu=fp-armv8 -c empty_struct.cpp -O1 -o - -S -mfloat-abi=hard
        .arch armv8-a
        .fpu fp-armv8
        .eabi_attribute 28, 1
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 1
        .eabi_attribute 30, 1
        .eabi_attribute 34, 1
        .eabi_attribute 18, 4
        .file   "empty_struct.cpp"
        .text
        .align  2
        .global _Z3barf9maybe_hfa
        .syntax unified
        .arm
        .type   _Z3barf9maybe_hfa, %function
_Z3barf9maybe_hfa:
        .fnstart
.LFB0:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        vmov    s0, r0
        bx      lr
        .cantunwind
        .fnend
        .size   _Z3barf9maybe_hfa, .-_Z3barf9maybe_hfa
        .ident  "GCC: (fsf-trunk.3697) 15.0.0 20240514 (experimental)"
aarch64-none-elf-gcc -march=armv8-a -c -c empty_struct.cpp -O1 -o - -S
        .arch armv8-a
        .file   "empty_struct.cpp"
        .text
        .align  2
        .global _Z3barf9maybe_hfa
        .type   _Z3barf9maybe_hfa, %function
_Z3barf9maybe_hfa:
.LFB0:
        .cfi_startproc
        fmov    s0, w0
        ret
        .cfi_endproc
.LFE0:
        .size   _Z3barf9maybe_hfa, .-_Z3barf9maybe_hfa
        .ident  "GCC: (fsf-trunk.3697) 15.0.0 20240514 (experimental)"

llvmbot avatar Sep 25 '25 09:09 llvmbot