Skip to content

Conversation

mstorsjo
Copy link
Member

This can cause breakage with user code that does "#define A ...".

This can cause breakage with user code that does "#define A ...".
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics labels Jul 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang

Author: Martin Storsjö (mstorsjo)

Changes

This can cause breakage with user code that does "#define A ...".


Full diff: https://github.com/llvm/llvm-project/pull/98478.diff

1 Files Affected:

  • (modified) clang/lib/Headers/avx512fp16intrin.h (+36-36)
diff --git a/clang/lib/Headers/avx512fp16intrin.h b/clang/lib/Headers/avx512fp16intrin.h
index 4123f10c39513..3e11795757f98 100644
--- a/clang/lib/Headers/avx512fp16intrin.h
+++ b/clang/lib/Headers/avx512fp16intrin.h
@@ -282,75 +282,75 @@ _mm512_zextph256_ph512(__m256h __a) {
 #define _mm_comi_sh(A, B, pred)                                                \
   _mm_comi_round_sh((A), (B), (pred), _MM_FROUND_CUR_DIRECTION)
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comieq_sh(__m128h A,
-                                                          __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_EQ_OS,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comieq_sh(__m128h __A,
+                                                          __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_EQ_OS,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comilt_sh(__m128h A,
-                                                          __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_LT_OS,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comilt_sh(__m128h __A,
+                                                          __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_LT_OS,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comile_sh(__m128h A,
-                                                          __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_LE_OS,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comile_sh(__m128h __A,
+                                                          __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_LE_OS,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comigt_sh(__m128h A,
-                                                          __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_GT_OS,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comigt_sh(__m128h __A,
+                                                          __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_GT_OS,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comige_sh(__m128h A,
-                                                          __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_GE_OS,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comige_sh(__m128h __A,
+                                                          __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_GE_OS,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comineq_sh(__m128h A,
-                                                           __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_NEQ_US,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_comineq_sh(__m128h __A,
+                                                           __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_NEQ_US,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomieq_sh(__m128h A,
-                                                           __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_EQ_OQ,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomieq_sh(__m128h __A,
+                                                           __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_EQ_OQ,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomilt_sh(__m128h A,
-                                                           __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_LT_OQ,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomilt_sh(__m128h __A,
+                                                           __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_LT_OQ,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomile_sh(__m128h A,
-                                                           __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_LE_OQ,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomile_sh(__m128h __A,
+                                                           __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_LE_OQ,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomigt_sh(__m128h A,
-                                                           __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_GT_OQ,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomigt_sh(__m128h __A,
+                                                           __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_GT_OQ,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomige_sh(__m128h A,
-                                                           __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_GE_OQ,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomige_sh(__m128h __A,
+                                                           __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_GE_OQ,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 
-static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomineq_sh(__m128h A,
-                                                            __m128h B) {
-  return __builtin_ia32_vcomish((__v8hf)A, (__v8hf)B, _CMP_NEQ_UQ,
+static __inline__ int __DEFAULT_FN_ATTRS128 _mm_ucomineq_sh(__m128h __A,
+                                                            __m128h __B) {
+  return __builtin_ia32_vcomish((__v8hf)__A, (__v8hf)__B, _CMP_NEQ_UQ,
                                 _MM_FROUND_CUR_DIRECTION);
 }
 

@KanRobert
Copy link
Contributor

Hmm, what's your usage?

#define A

before

#include <avx512fp16intrin.h>

?

@mstorsjo
Copy link
Member Author

Hmm, what's your usage?

#define A

before

#include <avx512fp16intrin.h>

?

Yes, exactly

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other unreserved name I could find in the file is:

_mm512_set1_pch(_Float16 _Complex h) {
(uses h)

so we might as well fix this at the same time. We should probably have tests using https://clang.llvm.org/extra/clang-tidy/checks/bugprone/reserved-identifier.html that runs over all of the system headers we produce to issue a diagnostic when we use an unreserved identifier so that we catch this issue more explicitly.

@mstorsjo
Copy link
Member Author

The only other unreserved name I could find in the file is:

_mm512_set1_pch(_Float16 _Complex h) {

(uses h)
so we might as well fix this at the same time.

Done, thanks!

We should probably have tests using https://clang.llvm.org/extra/clang-tidy/checks/bugprone/reserved-identifier.html that runs over all of the system headers we produce to issue a diagnostic when we use an unreserved identifier so that we catch this issue more explicitly.

Oh, good to know that we do have a way to check these things automatically! Yeah, setting up such tests would indeed be good.

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mstorsjo mstorsjo merged commit 6f04f46 into llvm:main Jul 11, 2024
@mstorsjo mstorsjo deleted the clang-avx512-header-reserved-names branch July 11, 2024 20:45
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
…lvm#98478)

This can cause breakage with user code that does "#define A ...".
@mstorsjo
Copy link
Member Author

mstorsjo commented Oct 3, 2025

The only other unreserved name I could find in the file is:

_mm512_set1_pch(_Float16 _Complex h) {

(uses h)
so we might as well fix this at the same time. We should probably have tests using https://clang.llvm.org/extra/clang-tidy/checks/bugprone/reserved-identifier.html that runs over all of the system headers we produce to issue a diagnostic when we use an unreserved identifier so that we catch this issue more explicitly.

We seem to have gotten more cases of such unreserved identifiers in 83ad644 (see the fallout of it in #161736), so I think it's somewhat urgent to set up such automated testing of the Clang headers.

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 3, 2025

We already have header test coverage, and clang\test\Headers\x86-intrinsics-headers-clean.cpp in particular is there to catch this kind of thing:

https://github.com/RKSimon/llvm-project/blob/7eb5c08ac3a5bc524a5fe4e2e91db3a5b1ffe3cd/clang/test/Headers/x86-intrinsics-headers-clean.cpp#L7-L9

Are there any other warnings that we can add to help catch this?

@mstorsjo
Copy link
Member Author

mstorsjo commented Oct 3, 2025

We already have header test coverage, and clang\test\Headers\x86-intrinsics-headers-clean.cpp in particular is there to catch this kind of thing:

https://github.com/RKSimon/llvm-project/blob/7eb5c08ac3a5bc524a5fe4e2e91db3a5b1ffe3cd/clang/test/Headers/x86-intrinsics-headers-clean.cpp#L7-L9

Are there any other warnings that we can add to help catch this?

I'm not sure if there are warning in Clang proper; @AaronBallman 's comment above suggested using https://clang.llvm.org/extra/clang-tidy/checks/bugprone/reserved-identifier.html. The libcxx testcase, https://github.com/llvm/llvm-project/blob/main/libcxx/test/libcxx/system_reserved_names.gen.py, uses a bunch of defines of every known potentially problematic symbol name, before testing including the header - e.g. like this:

#define SYSTEM_RESERVED_NAME This name should not be used in libc++
#define A SYSTEM_RESERVED_NAME
#define Arg SYSTEM_RESERVED_NAME
#define Args SYSTEM_RESERVED_NAME
#define As SYSTEM_RESERVED_NAME
#define B SYSTEM_RESERVED_NAME
#define Bs SYSTEM_RESERVED_NAME
#define C SYSTEM_RESERVED_NAME
#define Cp SYSTEM_RESERVED_NAME
#define Cs SYSTEM_RESERVED_NAME
...

@mstorsjo
Copy link
Member Author

mstorsjo commented Oct 3, 2025

I filed #161808 for the new issue that cropped up.

mstorsjo added a commit that referenced this pull request Oct 6, 2025
…es (#161817)

This mirrors a similar test that libcxx does, to make sure that the
libcxx headers don't use any unreserved symbols.

The header for polluting with defines is based very far on the libcxx
one; some parts of it could possibly be omitted, but I included most of
it for completeness here.

This should allow catching these issues earlier, to avoid issues like
#161808 and #98478 happening again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants