Add cuda_wrappers include feature for clang CUDA toolchains#481
Merged
Conversation
Clang ships cuda_wrappers/ shims under its resource directory (lib/clang/<v>/include/cuda_wrappers/) that #undef __noinline__ around libstdc++ headers, working around the CUDA host_defines.h macro conflict with libstdc++'s __attribute__((__noinline__)) (seen in bits/shared_ptr_base.h). Clang's CUDA driver already adds that directory automatically, but only as -internal-isystem. Clang merges all system-tier include groups in argument order, and -internal-isystem sorts after -cxx-isystem. So when the C++ standard library is injected explicitly via -nostdinc++ -cxx-isystem, the real bits/shared_ptr_base.h is found before clang's automatic cuda_wrappers entry; the wrapper never intercepts it via #include_next and the build fails on the __noinline__ clash. This worked before because clang was allowed to discover libstdc++ through its own GCC detection, which adds the stdlib as -internal-isystem *after* the cuda_wrappers entry the driver injects first, so the wrapper came first and fired automatically. Tightening the sandbox so clang no longer auto-detects the host toolchain (passing the stdlib via -cxx-isystem instead) reorders the stdlib ahead of the automatic wrapper and exposes the conflict. Fix it by detecting lib/clang/<v>/include in the cc_toolchain's built-in include directories and adding the cuda_wrappers subdir via -isystem to cuda_compile actions. -isystem (System group) sorts ahead of -cxx-isystem (CXXSystem), so the wrapper intercepts again. Clang dedups the now-redundant automatic -internal-isystem copy (RemoveDuplicates keeps the earlier occurrence), so this is safe even when the driver also adds it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Clang ships cuda_wrappers/ shims under its resource directory (lib/clang//include/cuda_wrappers/) that #undef noinline around libstdc++ headers, working around the CUDA host_defines.h macro conflict with libstdc++'s attribute((noinline)) (seen in bits/shared_ptr_base.h).
Clang's CUDA driver already adds that directory automatically, but only as -internal-isystem. Clang merges all system-tier include groups in argument order, and -internal-isystem sorts after -cxx-isystem. So when the C++ standard library is injected explicitly via -nostdinc++ -cxx-isystem, the real bits/shared_ptr_base.h is found before clang's automatic cuda_wrappers entry; the wrapper never intercepts it via #include_next and the build fails on the noinline clash.
This worked before because clang was allowed to discover libstdc++ through its own GCC detection, which adds the stdlib as -internal-isystem after the cuda_wrappers entry the driver injects first, so the wrapper came first and fired automatically. Tightening the sandbox so clang no longer auto-detects the host toolchain (passing the stdlib via -cxx-isystem instead) reorders the stdlib ahead of the automatic wrapper and exposes the conflict.
Fix it by detecting lib/clang//include in the cc_toolchain's built-in include directories and adding the cuda_wrappers subdir via -isystem to cuda_compile actions. -isystem (System group) sorts ahead of -cxx-isystem (CXXSystem), so the wrapper intercepts again. Clang dedups the now-redundant automatic -internal-isystem copy (RemoveDuplicates keeps the earlier occurrence), so this is safe even when the driver also adds it.