Hahnfeld created this revision.
Hahnfeld added reviewers: tra, gtbercea, hfinkel.
Herald added subscribers: cfe-commits, guansong.

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 https://reviews.llvm.org/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.


Repository:
  rC Clang

https://reviews.llvm.org/D50845

Files:
  lib/Frontend/InitPreprocessor.cpp
  test/Preprocessor/aux-triple.c
  test/SemaCUDA/builtins.cu

Index: test/SemaCUDA/builtins.cu
===================================================================
--- test/SemaCUDA/builtins.cu
+++ test/SemaCUDA/builtins.cu
@@ -12,8 +12,8 @@
 // RUN:     -aux-triple x86_64-unknown-unknown \
 // RUN:     -fsyntax-only -verify %s
 
-#if !(defined(__amd64__) && defined(__PTX__))
-#error "Expected to see preprocessor macros from both sides of compilation."
+#if !defined(__x86_64__)
+#error "Expected to see preprocessor macros from the host."
 #endif
 
 void hf() {
Index: test/Preprocessor/aux-triple.c
===================================================================
--- /dev/null
+++ test/Preprocessor/aux-triple.c
@@ -0,0 +1,48 @@
+// Ensure that Clang sets some very basic target defines based on -aux-triple.
+
+// RUN: %clang_cc1 -E -dM -ffreestanding < /dev/null \
+// RUN:     -triple nvptx64-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,NONE %s
+// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding < /dev/null \
+// RUN:     -triple nvptx64-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,NONE %s
+// RUN: %clang_cc1 -x cuda -E -dM -ffreestanding < /dev/null \
+// RUN:     -triple nvptx64-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,NONE %s
+
+// CUDA:
+// RUN: %clang_cc1 -x cuda -E -dM -ffreestanding < /dev/null \
+// RUN:     -triple nvptx64-none-none -aux-triple powerpc64le-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,PPC64 %s
+// RUN: %clang_cc1 -x cuda -E -dM -ffreestanding < /dev/null \
+// RUN:     -triple nvptx64-none-none -aux-triple x86_64-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,X86_64 %s
+
+// OpenMP:
+// RUN: %clang_cc1 -E -dM -ffreestanding < /dev/null \
+// RUN:     -fopenmp -fopenmp-is-device \
+// RUN:     -triple nvptx64-none-none -aux-triple powerpc64le-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,PPC64 %s
+// RUN: %clang_cc1 -E -dM -ffreestanding < /dev/null \
+// RUN:     -fopenmp -fopenmp-is-device \
+// RUN:     -triple nvptx64-none-none -aux-triple x86_64-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,X86_64 %s
+// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding < /dev/null \
+// RUN:     -fopenmp -fopenmp-is-device \
+// RUN:     -triple nvptx64-none-none -aux-triple powerpc64le-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,PPC64 %s
+// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding < /dev/null \
+// RUN:     -fopenmp -fopenmp-is-device \
+// RUN:     -triple nvptx64-none-none -aux-triple x86_64-none-none \
+// RUN:     | FileCheck -match-full-lines -check-prefixes NVPTX64,X86_64 %s
+
+// NVPTX64:#define _LP64 1
+// NVPTX64:#define __LP64__ 1
+// NVPTX64:#define __NVPTX__ 1
+// NVPTX64:#define __PTX__ 1
+
+// NONE-NOT:#define __powerpc64__
+// NONE-NOT:#define __x86_64__
+
+// PPC64:#define __powerpc64__ 1
+// X86_64:#define __x86_64__ 1
Index: lib/Frontend/InitPreprocessor.cpp
===================================================================
--- lib/Frontend/InitPreprocessor.cpp
+++ lib/Frontend/InitPreprocessor.cpp
@@ -1099,6 +1099,24 @@
   TI.getTargetDefines(LangOpts, Builder);
 }
 
+/// Initialize macros based on AuxTargetInfo.
+static void InitializePredefinedAuxMacros(const TargetInfo &AuxTI,
+                                          MacroBuilder &Builder) {
+  // Define basic target macros needed by at least bits/wordsize.h and
+  // bits/mathinline.h
+  switch (AuxTI.getTriple().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;
+  }
+}
+
 /// InitializePreprocessor - Initialize the preprocessor getting it and the
 /// environment ready to process a single file. This returns true on error.
 ///
@@ -1120,13 +1138,9 @@
 
   // 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(), Builder);
 
     // Install definitions to make Objective-C++ ARC work well with various
     // C++ Standard Library implementations.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to