[CUDA][HIP] Add pre-defined macro `__CLANG_RDC__`
authorYaxun (Sam) Liu <yaxun.liu@amd.com>
Tue, 30 Nov 2021 19:45:16 +0000 (14:45 -0500)
committerYaxun (Sam) Liu <yaxun.liu@amd.com>
Tue, 7 Dec 2021 23:08:16 +0000 (18:08 -0500)
nvcc defines __CUDACC_RDC__ for both host and
device compilation when -rdc=true is specified
(https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#nvcc-identification-macro)

This patch defines __CLANG_RDC__ when -fgpu-rdc
is specified for CUDA/HIP.

Reviewed by: Artem Belevich

Differential Revision: https://reviews.llvm.org/D114812

clang/lib/Frontend/InitPreprocessor.cpp
clang/test/Preprocessor/predefined-macros.c

index 0c15344..629f991 100644 (file)
@@ -500,8 +500,12 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
   // Not "standard" per se, but available even with the -undef flag.
   if (LangOpts.AsmPreprocessor)
     Builder.defineMacro("__ASSEMBLER__");
-  if (LangOpts.CUDA && !LangOpts.HIP)
-    Builder.defineMacro("__CUDA__");
+  if (LangOpts.CUDA) {
+    if (LangOpts.GPURelocatableDeviceCode)
+      Builder.defineMacro("__CLANG_RDC__");
+    if (!LangOpts.HIP)
+      Builder.defineMacro("__CUDA__");
+  }
   if (LangOpts.HIP) {
     Builder.defineMacro("__HIP__");
     Builder.defineMacro("__HIPCC__");
index 46dd76b..2304eb8 100644 (file)
 // CHECK-SPIRV64-DAG: #define __SPIRV64__ 1
 // CHECK-SPIRV64-NOT: #define __SPIRV32__ 1
 
-// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa      \
+// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple x86_64-unknown-linux-gnu \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP
-// CHECK-HIP-NOT: #define __CUDA_ARCH__
 // CHECK-HIP: #define __HIPCC__ 1
-// CHECK-HIP-NOT: #define __HIP_DEVICE_COMPILE__ 1
 // CHECK-HIP: #define __HIP__ 1
 
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -triple x86_64-unknown-linux-gnu \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-CUDA-NEG
+// CHECK-CUDA-NEG-NOT: #define __CLANG_RDC__ 1
+
+// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple x86_64-unknown-linux-gnu \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP-NEG
+// CHECK-HIP-NEG-NOT: #define __CUDA_ARCH__
+// CHECK-HIP-NEG-NOT: #define __HIP_DEVICE_COMPILE__ 1
+// CHECK-HIP-NEG-NOT: #define __CLANG_RDC__ 1
+
 // RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa \
 // RUN:   -fcuda-is-device \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP-DEV
-// CHECK-HIP-DEV-NOT: #define __CUDA_ARCH__
 // CHECK-HIP-DEV: #define __HIPCC__ 1
 // CHECK-HIP-DEV: #define __HIP_DEVICE_COMPILE__ 1
 // CHECK-HIP-DEV: #define __HIP__ 1
+
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -triple nvptx \
+// RUN:   -fcuda-is-device \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-CUDA-DEV-NEG
+// CHECK-CUDA-DEV-NEG-NOT: #define __CLANG_RDC__ 1
+
+// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa \
+// RUN:   -fcuda-is-device \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP-DEV-NEG
+// CHECK-HIP-DEV-NEG-NOT: #define __CUDA_ARCH__
+// CHECK-HIP-DEV-NEG-NOT: #define __CLANG_RDC__ 1
+
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -triple x86_64-unknown-linux-gnu \
+// RUN:   -fgpu-rdc | FileCheck %s --check-prefix=CHECK-RDC
+// RUN: %clang_cc1 %s -E -dM -o - -x cuda -triple nvptx \
+// RUN:   -fgpu-rdc -fcuda-is-device \
+// RUN:   | FileCheck %s --check-prefix=CHECK-RDC
+// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple x86_64-unknown-linux-gnu \
+// RUN:   -fgpu-rdc | FileCheck %s --check-prefix=CHECK-RDC
+// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa \
+// RUN:   -fgpu-rdc -fcuda-is-device \
+// RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-RDC
+// CHECK-RDC: #define __CLANG_RDC__ 1