Skip to content

Commit be0bbd3

Browse files
committed
[libc++] Protect the libc++ implementation from CUDA SDK's __noinline__ macro
The CUDA SDK contains an unfortunate definition for the `__noinline__` macro. I don't want to quote the code here, but you can find online plenty of information about this macro definition being problematic and creating conflicts for numerous other libraries, for example [on StackOverflow](https://stackoverflow.com/questions/70301375/noinline-macro-conflict-between-glib-and-cuda). This patch does the following: * following the existing pattern, it adds `__noinline__` to `_LIBCPP_PUSH_MACROS`, `_LIBCPP_POP_MACROS`, `__undef_macros`, and the test generation script, * wraps all of `include/__config` in a push/pop bracket pair and includes `__undef_macros` in `__config`. Macro protection brackets in `__config` are necessary because `__config` uses the `__noinline__` identifier in a `__has_attribute()` preprocessor expression, and that usage suffers from CUDA's definition. This problem can be reproduced with the `system_reserved_names.gen.py` tests if one applies this patch except for the changes to `__undef_macros`.
1 parent 740f14e commit be0bbd3

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

libcxx/include/__config

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
# endif
4747
# endif
4848

49+
# define _LIBCPP_PUSH_MACROS _Pragma("push_macro(\"min\")") _Pragma("push_macro(\"max\")") _Pragma("push_macro(\"refresh()\")") _Pragma("push_macro(\"move(int, int)\")") _Pragma("push_macro(\"erase()\")") _Pragma("push_macro(\"__noinline__\")")
50+
# define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh()\")") _Pragma("pop_macro(\"move(int, int)\")") _Pragma("pop_macro(\"erase()\")") _Pragma("pop_macro(\"__noinline__\")")
51+
52+
_LIBCPP_PUSH_MACROS
53+
# include <__undef_macros>
54+
4955
// The attributes supported by clang are documented at https://clang.llvm.org/docs/AttributeReference.html
5056

5157
// _LIBCPP_VERSION represents the version of libc++, which matches the version of LLVM.
@@ -1208,11 +1214,6 @@ __sanitizer_verify_double_ended_contiguous_container(const void*, const void*, c
12081214
# define _LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS
12091215
# endif // _LIBCPP_ENABLE_CXX20_REMOVED_FEATURES
12101216

1211-
// clang-format off
1212-
# define _LIBCPP_PUSH_MACROS _Pragma("push_macro(\"min\")") _Pragma("push_macro(\"max\")") _Pragma("push_macro(\"refresh()\")") _Pragma("push_macro(\"move(int, int)\")") _Pragma("push_macro(\"erase()\")")
1213-
# define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh()\")") _Pragma("pop_macro(\"move(int, int)\")") _Pragma("pop_macro(\"erase()\")")
1214-
// clang-format on
1215-
12161217
# ifndef _LIBCPP_NO_AUTO_LINK
12171218
# if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
12181219
# if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
@@ -1489,6 +1490,8 @@ __sanitizer_verify_double_ended_contiguous_container(const void*, const void*, c
14891490
# define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
14901491
# endif
14911492

1493+
_LIBCPP_POP_MACROS
1494+
14921495
#endif // __cplusplus
14931496

14941497
#endif // _LIBCPP___CONFIG

libcxx/include/__undef_macros

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@
2626
#ifdef erase
2727
# undef erase
2828
#endif
29+
30+
#ifdef __noinline__
31+
# undef __noinline__
32+
#endif

libcxx/test/libcxx/system_reserved_names.gen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,8 @@
158158
#define erase SYSTEM_RESERVED_NAME
159159
#define refresh SYSTEM_RESERVED_NAME
160160
161+
// Macros from the CUDA SDKs
162+
#define __noinline__ __attribute__((noinline))
163+
161164
#include <{header}>
162165
""")

0 commit comments

Comments
 (0)