[libcxx-commits] [libcxx] [libc++] Protect the libc++ implementation from CUDA SDK's `__noinline__` macro (PR #73838)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Nov 29 10:21:42 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Dmitri Gribenko (gribozavr)
<details>
<summary>Changes</summary>
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`.
---
Full diff: https://github.com/llvm/llvm-project/pull/73838.diff
3 Files Affected:
- (modified) libcxx/include/__config (+8-5)
- (modified) libcxx/include/__undef_macros (+4)
- (modified) libcxx/test/libcxx/system_reserved_names.gen.py (+3)
``````````diff
diff --git a/libcxx/include/__config b/libcxx/include/__config
index ee77305162f7fc8..2a77b88562ef614 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -46,6 +46,12 @@
# endif
# endif
+# 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__\")")
+# 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__\")")
+
+_LIBCPP_PUSH_MACROS
+# include <__undef_macros>
+
// The attributes supported by clang are documented at https://clang.llvm.org/docs/AttributeReference.html
// _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
# define _LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS
# endif // _LIBCPP_ENABLE_CXX20_REMOVED_FEATURES
-// clang-format off
-# 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()\")")
-# 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()\")")
-// clang-format on
-
# ifndef _LIBCPP_NO_AUTO_LINK
# if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
# if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
@@ -1489,6 +1490,8 @@ __sanitizer_verify_double_ended_contiguous_container(const void*, const void*, c
# define _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
# endif
+_LIBCPP_POP_MACROS
+
#endif // __cplusplus
#endif // _LIBCPP___CONFIG
diff --git a/libcxx/include/__undef_macros b/libcxx/include/__undef_macros
index 29ab327e1c375ae..885057629d1b831 100644
--- a/libcxx/include/__undef_macros
+++ b/libcxx/include/__undef_macros
@@ -26,3 +26,7 @@
#ifdef erase
# undef erase
#endif
+
+#ifdef __noinline__
+# undef __noinline__
+#endif
diff --git a/libcxx/test/libcxx/system_reserved_names.gen.py b/libcxx/test/libcxx/system_reserved_names.gen.py
index 8c4be97897f657e..17ef50568bbb666 100644
--- a/libcxx/test/libcxx/system_reserved_names.gen.py
+++ b/libcxx/test/libcxx/system_reserved_names.gen.py
@@ -158,5 +158,8 @@
#define erase SYSTEM_RESERVED_NAME
#define refresh SYSTEM_RESERVED_NAME
+// Macros from the CUDA SDKs
+#define __noinline__ __attribute__((noinline))
+
#include <{header}>
""")
``````````
</details>
https://github.com/llvm/llvm-project/pull/73838
More information about the libcxx-commits
mailing list