This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Move up undefined macro checks
ClosedPublic

Authored by abrachet on Jan 9 2023, 7:24 AM.

Details

Summary

Previously HWCAP_ATOMIC and others were being used before checking if
they were defined. This moves up all the ifndef checks to define these
macros if they are not yet defined.

Diff Detail

Event Timeline

abrachet created this revision.Jan 9 2023, 7:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 7:24 AM
ilinpv added a comment.Jan 9 2023, 7:52 AM

LGTM, thanks for fixing this!

ilinpv accepted this revision.Jan 9 2023, 7:53 AM
This revision is now accepted and ready to land.Jan 9 2023, 7:53 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 7:54 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
dim added subscribers: emaste, dim.Apr 20 2023, 4:27 AM

What was the original reason for this particular change? Because after this, with clang and compiler-rt 16, I now see a whole bunch of:

In file included from /home/dim/src/freebsd/llvm-16-update/contrib/llvm-project/compiler-rt/lib/builtins/cpu_model.c:1019:
In file included from /home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/sys/auxv.h:35:
In file included from /home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/machine/elf.h:36:
In file included from /home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/sys/elf32.h:34:
/home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/sys/elf_common.h:980:9: error: 'AT_HWCAP' macro redefined [-Werror,-Wmacro-redefined]
#define AT_HWCAP        25      /* CPU feature flags. */
        ^
/home/dim/src/freebsd/llvm-16-update/contrib/llvm-project/compiler-rt/lib/builtins/cpu_model.c:843:9: note: previous definition is here
#define AT_HWCAP 16
        ^
In file included from /home/dim/src/freebsd/llvm-16-update/contrib/llvm-project/compiler-rt/lib/builtins/cpu_model.c:1019:
In file included from /home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/sys/auxv.h:35:
/home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/machine/elf.h:95:9: error: 'HWCAP_FP' macro redefined [-Werror,-Wmacro-redefined]
#define HWCAP_FP                0x00000001
        ^
/home/dim/src/freebsd/llvm-16-update/contrib/llvm-project/compiler-rt/lib/builtins/cpu_model.c:849:9: note: previous definition is here
#define HWCAP_FP (1 << 0)
        ^
In file included from /home/dim/src/freebsd/llvm-16-update/contrib/llvm-project/compiler-rt/lib/builtins/cpu_model.c:1019:
In file included from /home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/sys/auxv.h:35:
/home/dim/obj/home/dim/src/freebsd/llvm-16-update/arm64.aarch64/tmp/usr/include/machine/elf.h:96:9: error: 'HWCAP_ASIMD' macro redefined [-Werror,-Wmacro-redefined]
#define HWCAP_ASIMD             0x00000002
        ^
...

because it seems to me that checking whether a macro is defined should be done *after* including any system header where such a macro _could_ possibly be defined, not the other way around?

I think the order should instead be:

  1. include system headers that potentially defined HWCAP_xxx
  2. block with #ifndef HWCAP_xxx ... #define HWCAP_xxx ...
  3. block with inline functions actually using HWCAP_xxx

I think the order should instead be:

  1. include system headers that potentially defined HWCAP_xxx
  2. block with #ifndef HWCAP_xxx ... #define HWCAP_xxx ...
  3. block with inline functions actually using HWCAP_xxx

Absolutely correct. I think system headers includes

#if defined(__has_include)
#if __has_include(<sys/auxv.h>)
#include <sys/auxv.h>
#if __has_include(<asm/hwcap.h>)
#include <asm/hwcap.h>

#if defined(__ANDROID__)
#include <string.h>
#include <sys/system_properties.h>
#elif defined(__Fuchsia__)
#include <zircon/features.h>
#include <zircon/syscalls.h>
#endif

was moved after #ifndef HWCAP_xxx by mistake. Originally AT_HWCAP and HWCAP_ATOMICS were used before #ifndef HWCAP_xxx ... #define HWCAP_xxx ...