[CUDA][OPENMP][NVPTX]Improve logic of the debug info support.

Summary:
Added support for the -gline-directives-only option + fixed logic of the
debug info for CUDA devices. If optimization level is O0, then options
--[no-]cuda-noopt-device-debug do not affect the debug info level. If
the optimization level is >O0, debug info options are used +
--no-cuda-noopt-device-debug is used or no --cuda-noopt-device-debug is
used, the optimization level for the device code is kept and the
emission of the debug directives is used.
If the opt level is > O0, debug info is requested +
--cuda-noopt-device-debug option is used, the optimization is disabled
for the device code + required debug info is emitted.

Reviewers: tra, echristo

Subscribers: aprantl, guansong, JDevlieghere, cfe-commits

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

llvm-svn: 348930
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index a8363b2..57b8d43 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -278,32 +278,44 @@
 }
 
 namespace {
-  /// Debug info kind.
-enum DebugInfoKind {
-  NoDebug,       /// No debug info.
-  LineTableOnly, /// Line tables only.
-  FullDebug      /// Full debug info.
+/// Debug info level for the NVPTX devices. We may need to emit different debug
+/// info level for the host and for the device itselfi. This type controls
+/// emission of the debug info for the devices. It either prohibits disable info
+/// emission completely, or emits debug directives only, or emits same debug
+/// info as for the host.
+enum DeviceDebugInfoLevel {
+  DisableDebugInfo,        /// Do not emit debug info for the devices.
+  DebugDirectivesOnly,     /// Emit only debug directives.
+  EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the
+                           /// host.
 };
 } // 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;
+/// Define debug info level for the NVPTX devices. If the debug info for both
+/// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
+/// only debug directives are requested for the both host and device
+/// (-gline-directvies-only), or the debug info only for the device is disabled
+/// (optimization is on and --cuda-noopt-device-debug was not specified), the
+/// debug directves only must be emitted for the device. Otherwise, use the same
+/// debug info level just like for the host (with the limitations of only
+/// supported DWARF2 standard).
+static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
+  const Arg *A = Args.getLastArg(options::OPT_O_Group);
+  bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) ||
+                        Args.hasFlag(options::OPT_cuda_noopt_device_debug,
+                                     options::OPT_no_cuda_noopt_device_debug,
+                                     /*Default=*/false);
+  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 DisableDebugInfo;
+      if (Opt.matches(options::OPT_gline_directives_only))
+        return DebugDirectivesOnly;
     }
+    return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly;
   }
-  return NoDebug;
+  return DisableDebugInfo;
 }
 
 void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
@@ -337,8 +349,8 @@
 
   ArgStringList CmdArgs;
   CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
-  DebugInfoKind DIKind = mustEmitDebugInfo(Args);
-  if (DIKind == FullDebug) {
+  DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args);
+  if (DIKind == EmitSameDebugInfoAsHost) {
     // 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");
@@ -374,7 +386,7 @@
     // to no optimizations, but ptxas's default is -O3.
     CmdArgs.push_back("-O0");
   }
-  if (DIKind == LineTableOnly)
+  if (DIKind == DebugDirectivesOnly)
     CmdArgs.push_back("-lineinfo");
 
   // Pass -v to ptxas if it was passed to the driver.
@@ -445,7 +457,7 @@
   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)
+  if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
     CmdArgs.push_back("-g");
 
   for (const auto& II : Inputs) {
@@ -498,7 +510,7 @@
     CmdArgs.push_back(Output.getFilename());
   } else
     assert(Output.isNothing() && "Invalid output.");
-  if (mustEmitDebugInfo(Args) == FullDebug)
+  if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
     CmdArgs.push_back("-g");
 
   if (Args.hasArg(options::OPT_v))
@@ -704,6 +716,21 @@
          O.matches(options::OPT_gcolumn_info);
 }
 
+void CudaToolChain::adjustDebugInfoKind(
+    codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const {
+  switch (mustEmitDebugInfo(Args)) {
+  case DisableDebugInfo:
+    DebugInfoKind = codegenoptions::NoDebugInfo;
+    break;
+  case DebugDirectivesOnly:
+    DebugInfoKind = codegenoptions::DebugDirectivesOnly;
+    break;
+  case EmitSameDebugInfoAsHost:
+    // Use same debug info level as the host.
+    break;
+  }
+}
+
 void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
                                        ArgStringList &CC1Args) const {
   // Check our CUDA version if we're going to include the CUDA headers.