From e36c67b35c26d7c9a2974ffd2f8ba6a9f0260f15 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Wed, 18 Apr 2018 16:31:09 +0000 Subject: [PATCH] [NVPTX] Emit debug info in DWARF-2 by default for Cuda devices. Summary: NVPTX target supports debug info in DWARF-2 format. Patch adds emission of debug info in DWARF-2 by default. Reviewers: tra, jlebar Subscribers: aprantl, JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D42581 llvm-svn: 330272 --- clang/lib/Driver/ToolChains/Cuda.cpp | 39 +++++++++++++++++-- clang/lib/Driver/ToolChains/Cuda.h | 2 + clang/test/Driver/cuda-dwarf-2.cu | 47 +++++++++++++++++++++++ clang/test/Driver/cuda-external-tools.cu | 2 +- clang/test/Driver/openmp-offload-gpu.c | 48 ++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 clang/test/Driver/cuda-dwarf-2.cu diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp index c83d066fa26e..fc74a8ebbf01 100644 --- a/clang/lib/Driver/ToolChains/Cuda.cpp +++ b/clang/lib/Driver/ToolChains/Cuda.cpp @@ -273,6 +273,35 @@ void CudaInstallationDetector::print(raw_ostream &OS) const { << CudaVersionToString(Version) << "\n"; } +namespace { + /// Debug info kind. +enum DebugInfoKind { + NoDebug, /// No debug info. + LineTableOnly, /// Line tables only. + FullDebug /// Full debug info. +}; +} // anonymous namespace + +static DebugInfoKind mustEmitDebugInfo(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_O_Group); + if (Args.hasFlag(options::OPT_cuda_noopt_device_debug, + options::OPT_no_cuda_noopt_device_debug, + !A || A->getOption().matches(options::OPT_O0))) { + if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { + const Option &Opt = A->getOption(); + if (Opt.matches(options::OPT_gN_Group)) { + if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0)) + return NoDebug; + if (Opt.matches(options::OPT_gline_tables_only) || + Opt.matches(options::OPT_ggdb1)) + return LineTableOnly; + } + return FullDebug; + } + } + return NoDebug; +} + void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, @@ -304,8 +333,8 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, ArgStringList CmdArgs; CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32"); - if (Args.hasFlag(options::OPT_cuda_noopt_device_debug, - options::OPT_no_cuda_noopt_device_debug, false)) { + DebugInfoKind DIKind = mustEmitDebugInfo(Args); + if (DIKind == FullDebug) { // ptxas does not accept -g option if optimization is enabled, so // we ignore the compiler's -O* options if we want debug info. CmdArgs.push_back("-g"); @@ -341,6 +370,8 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, // to no optimizations, but ptxas's default is -O3. CmdArgs.push_back("-O0"); } + if (DIKind == LineTableOnly) + CmdArgs.push_back("-lineinfo"); // Pass -v to ptxas if it was passed to the driver. if (Args.hasArg(options::OPT_v)) @@ -410,6 +441,8 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32"); CmdArgs.push_back(Args.MakeArgString("--create")); CmdArgs.push_back(Args.MakeArgString(Output.getFilename())); + if (mustEmitDebugInfo(Args) == FullDebug) + CmdArgs.push_back("-g"); for (const auto& II : Inputs) { auto *A = II.getAction(); @@ -461,7 +494,7 @@ void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(Output.getFilename()); } else assert(Output.isNothing() && "Invalid output."); - if (Args.hasArg(options::OPT_g_Flag)) + if (mustEmitDebugInfo(Args) == FullDebug) CmdArgs.push_back("-g"); if (Args.hasArg(options::OPT_v)) diff --git a/clang/lib/Driver/ToolChains/Cuda.h b/clang/lib/Driver/ToolChains/Cuda.h index 3d08cec1643e..9b34fa589e48 100644 --- a/clang/lib/Driver/ToolChains/Cuda.h +++ b/clang/lib/Driver/ToolChains/Cuda.h @@ -180,6 +180,8 @@ public: computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const override; + unsigned GetDefaultDwarfVersion() const override { return 2; } + const ToolChain &HostTC; CudaInstallationDetector CudaInstallation; diff --git a/clang/test/Driver/cuda-dwarf-2.cu b/clang/test/Driver/cuda-dwarf-2.cu new file mode 100644 index 000000000000..b6c7f500f5ca --- /dev/null +++ b/clang/test/Driver/cuda-dwarf-2.cu @@ -0,0 +1,47 @@ +// REQUIRES: clang-driver +// +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g -O0 --no-cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g -O3 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g -O3 --no-cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g0 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -ggdb0 -O3 --cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -ggdb1 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG -check-prefix LINE_TABLE +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -gline-tables-only -O2 --cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix NO_DEBUG -check-prefix LINE_TABLE + +// NO_DEBUG: ptxas +// NO_DEBUG-NOT: "-g" +// LINE_TABLE: "-lineinfo" +// NO_DEBUG: fatbinary +// NO_DEBUG-NOT: "-g" + +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g -O0 --cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g -O3 --cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g2 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -ggdb2 -O0 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -g3 -O2 --cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG +// RUN: %clang -### -target x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s -ggdb3 -O3 --cuda-noopt-device-debug 2>&1 | \ +// RUN: FileCheck %s -check-prefix HAS_DEBUG + +// HAS_DEBUG: "-fcuda-is-device" +// HAS_DEBUG-SAME: "-dwarf-version=2" +// HAS_DEBUG: ptxas +// HAS_DEBUG-SAME: "-g" +// HAS_DEBUG-SAME: "--dont-merge-basicblocks" +// HAS_DEBUG-SAME: "--return-at-end" +// HAS_DEBUG: fatbinary +// HAS_DEBUG-SAME: "-g" + diff --git a/clang/test/Driver/cuda-external-tools.cu b/clang/test/Driver/cuda-external-tools.cu index c15cc05a7d0d..367c6997ea87 100644 --- a/clang/test/Driver/cuda-external-tools.cu +++ b/clang/test/Driver/cuda-external-tools.cu @@ -23,7 +23,7 @@ // RUN: | FileCheck -check-prefixes=CHECK,ARCH64,SM20,RDC %s // With debugging enabled, ptxas should be run with with no ptxas optimizations. -// RUN: %clang -### -target x86_64-linux-gnu --cuda-noopt-device-debug -O2 -c %s 2>&1 \ +// RUN: %clang -### -target x86_64-linux-gnu --cuda-noopt-device-debug -O2 -g -c %s 2>&1 \ // RUN: | FileCheck -check-prefixes=CHECK,ARCH64,SM20,DBG %s // --no-cuda-noopt-device-debug overrides --cuda-noopt-device-debug. diff --git a/clang/test/Driver/openmp-offload-gpu.c b/clang/test/Driver/openmp-offload-gpu.c index 152e03a57c36..3d14041a13f8 100644 --- a/clang/test/Driver/openmp-offload-gpu.c +++ b/clang/test/Driver/openmp-offload-gpu.c @@ -165,3 +165,51 @@ // RUN: | FileCheck -check-prefix=CHK-BCLIB-WARN %s // CHK-BCLIB-WARN: No library 'libomptarget-nvptx-sm_20.bc' found in the default clang lib directory or in LIBRARY_PATH. Expect degraded performance due to no inlining of runtime functions on target devices. + +/// Check that debug info is emitted in dwarf-2 +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g -O0 --no-cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g -O3 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g -O3 --no-cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g0 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -ggdb0 -O3 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -gline-tables-only 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG -check-prefix=LINE_TABLE %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -ggdb1 -O2 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=NO_DEBUG -check-prefix=LINE_TABLE %s + +// NO_DEBUG: ptxas +// LINE_TABLE: "-lineinfo" +// NO_DEBUG-NOT: "-g" +// NO_DEBUG: nvlink +// NO_DEBUG-NOT: "-g" + +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g -O0 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g -O3 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g2 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -ggdb2 -O0 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g3 -O3 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s +// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -ggdb3 -O2 --cuda-noopt-device-debug 2>&1 \ +// RUN: | FileCheck -check-prefix=HAS_DEBUG %s + +// HAS_DEBUG: "-triple" "nvptx64-nvidia-cuda" +// HAS_DEBUG-SAME: "-dwarf-version=2" +// HAS_DEBUG-SAME: "-fopenmp-is-device" +// HAS_DEBUG: ptxas +// HAS_DEBUG-SAME: "-g" +// HAS_DEBUG-SAME: "--dont-merge-basicblocks" +// HAS_DEBUG-SAME: "--return-at-end" +// HAS_DEBUG: nvlink +// HAS_DEBUG-SAME: "-g" +