diff options
author | Jonas Hahnfeld <hahnjo@hahnjo.de> | 2018-08-25 13:42:40 +0000 |
---|---|---|
committer | Jonas Hahnfeld <hahnjo@hahnjo.de> | 2018-08-25 13:42:40 +0000 |
commit | 931939bf922b45ad8c00e6a0b36d927e42b51661 (patch) | |
tree | 9ea1b929d159061137d3e1bfe44a479f117b90b4 /clang/lib | |
parent | 7ded6a909bb8baddeee706f5591ecec601270758 (diff) | |
download | bcm5719-llvm-931939bf922b45ad8c00e6a0b36d927e42b51661.tar.gz bcm5719-llvm-931939bf922b45ad8c00e6a0b36d927e42b51661.zip |
[CUDA/OpenMP] Define only some host macros during device compilation
When compiling CUDA or OpenMP device code Clang parses header files
that expect certain predefined macros from the host architecture. To
make this work the compiler passes the host triple via the -aux-triple
argument and (until now) pulls in all macros for that "auxiliary triple"
unconditionally.
However this results in defines like __SSE_MATH__ that will trigger
inline assembly making use of the "advertised" target features. See
the discussion of D47849 and PR38464 for a detailed explanation of
the encountered problems.
Instead of blacklisting "known bad" examples this patch starts adding
defines that are needed for certain headers like bits/wordsize.h and
bits/mathinline.h.
The disadvantage of this approach is that it decouples the definitions
from their target toolchain. However in my opinion it's more important
to keep definitions for one header close together. For one this will
include a clear documentation why these particular defines are needed.
Furthermore it simplifies maintenance because adding defines for a new
header or support for a new aux-triple only needs to touch one piece
of code.
Differential Revision: https://reviews.llvm.org/D50845
llvm-svn: 340681
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Frontend/InitPreprocessor.cpp | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index e576fc098da..05192555c56 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -1099,6 +1099,44 @@ static void InitializePredefinedMacros(const TargetInfo &TI, TI.getTargetDefines(LangOpts, Builder); } +/// Initialize macros based on AuxTargetInfo. +static void InitializePredefinedAuxMacros(const TargetInfo &AuxTI, + const LangOptions &LangOpts, + MacroBuilder &Builder) { + auto AuxTriple = AuxTI.getTriple(); + + // Define basic target macros needed by at least bits/wordsize.h and + // bits/mathinline.h + switch (AuxTriple.getArch()) { + case llvm::Triple::x86_64: + Builder.defineMacro("__x86_64__"); + break; + case llvm::Triple::ppc64: + case llvm::Triple::ppc64le: + Builder.defineMacro("__powerpc64__"); + break; + default: + break; + } + + // libc++ needs to find out the object file format and threading API. + if (AuxTriple.getOS() == llvm::Triple::Linux) { + Builder.defineMacro("__ELF__"); + Builder.defineMacro("__linux__"); + // Used in features.h. If this is omitted, math.h doesn't declare float + // versions of the functions in bits/mathcalls.h. + if (LangOpts.CPlusPlus) + Builder.defineMacro("_GNU_SOURCE"); + } else if (AuxTriple.isOSDarwin()) { + Builder.defineMacro("__APPLE__"); + Builder.defineMacro("__MACH__"); + } else if (AuxTriple.isOSWindows()) { + Builder.defineMacro("_WIN32"); + if (AuxTriple.isWindowsGNUEnvironment()) + Builder.defineMacro("__MINGW32__"); + } +} + /// InitializePreprocessor - Initialize the preprocessor getting it and the /// environment ready to process a single file. This returns true on error. /// @@ -1120,13 +1158,9 @@ void clang::InitializePreprocessor( // Install things like __POWERPC__, __GNUC__, etc into the macro table. if (InitOpts.UsePredefines) { - // FIXME: This will create multiple definitions for most of the predefined - // macros. This is not the right way to handle this. - if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice) && PP.getAuxTargetInfo()) - InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts, - Builder); - InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder); + if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice) && PP.getAuxTargetInfo()) + InitializePredefinedAuxMacros(*PP.getAuxTargetInfo(), LangOpts, Builder); // Install definitions to make Objective-C++ ARC work well with various // C++ Standard Library implementations. |