diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp --- a/clang/lib/Driver/ToolChains/Cuda.cpp +++ b/clang/lib/Driver/ToolChains/Cuda.cpp @@ -626,6 +626,11 @@ if (DriverArgs.hasArg(options::OPT_nogpulib)) return; + // NVCC defines __CUDACC_DEBUG__ for device code when compiling with debug + // info. + if (mustEmitDebugInfo(DriverArgs) == EmitSameDebugInfoAsHost) + CC1Args.push_back("-D__CUDACC_DEBUG__"); + std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch); if (LibDeviceFile.empty()) { diff --git a/clang/test/Driver/cuda-options.cu b/clang/test/Driver/cuda-options.cu --- a/clang/test/Driver/cuda-options.cu +++ b/clang/test/Driver/cuda-options.cu @@ -183,6 +183,9 @@ // RUN: -c %s 2>&1 \ // RUN: | FileCheck -check-prefixes FATBIN-COMMON,PTX-SM35,PTX-SM30 %s +// Verify that -g adds __CUDACC_DEBUG__ define. +// RUN: %clang -### -target x86_64-linux-gnu -g %s 2>&1 \ +// RUN: | FileCheck -check-prefix DEBUG %s // ARCH-SM20: "-cc1"{{.*}}"-target-cpu" "sm_20" // NOARCH-SM20-NOT: "-cc1"{{.*}}"-target-cpu" "sm_20" @@ -287,3 +290,5 @@ // FATBIN-COMMON: "--image=profile=sm_35,file= // PTX-SM35: "--image=profile=compute_35,file= // NOPTX-SM35-NOT: "--image=profile=compute_35,file= + +// DEBUG: -D__CUDACC_DEBUG__