tra created this revision.
tra added reviewers: dblaikie, ABataev.
Herald added a subscriber: bixia.
tra requested review of this revision.
Herald added a project: clang.

This is needed for CUDA compilation where NVPTX back-end only supports DWARF2,
but host compilation should be allowed to use newer DWARF versions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92617

Files:
  clang/include/clang/Driver/ToolChain.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Cuda.h
  clang/test/Driver/cuda-unsupported-debug-options.cu


Index: clang/test/Driver/cuda-unsupported-debug-options.cu
===================================================================
--- clang/test/Driver/cuda-unsupported-debug-options.cu
+++ clang/test/Driver/cuda-unsupported-debug-options.cu
@@ -18,5 +18,7 @@
 // CHECK-NOT: debug information option '{{.*}}' is not supported for target 
'x86
 // CHECK: "-triple" "nvptx64-nvidia-cuda"
 // CHECK-NOT: 
{{-compress-debug|-fdebug-info-for-profiling|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}}
+// Make sure we do not see any dwarf version other than 2, regardless of 
what's used on the host side.
+// CHECK-NOT: {{-dwarf-version=[^2]}}
 // CHECK: "-triple" "x86_64
 // CHECK-SAME: 
{{-compress-debug|-fdebug-info-for-profiling|split-dwarf|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}}
Index: clang/lib/Driver/ToolChains/Cuda.h
===================================================================
--- clang/lib/Driver/ToolChains/Cuda.h
+++ clang/lib/Driver/ToolChains/Cuda.h
@@ -185,6 +185,8 @@
                      const llvm::opt::ArgList &Args) const override;
 
   unsigned GetDefaultDwarfVersion() const override { return 2; }
+  // NVPTX supports only DWARF2.
+  unsigned GetAdjustedDwarfVersion(unsigned v) const override { return 2; }
 
   const ToolChain &HostTC;
   CudaInstallationDetector CudaInstallation;
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3854,6 +3854,8 @@
         DWARFVersion = ExplicitVersion;
   }
 
+  auto AdjustedDwarfVersion = TC.GetAdjustedDwarfVersion(DWARFVersion);
+
   // -gline-directives-only supported only for the DWARF debug info.
   if (DWARFVersion == 0 && DebugInfoKind == 
codegenoptions::DebugDirectivesOnly)
     DebugInfoKind = codegenoptions::NoDebugInfo;
@@ -3918,6 +3920,11 @@
     if (DWARFVersion < 5)
       D.Diag(diag::err_drv_argument_only_allowed_with)
           << A->getAsString(Args) << "-gdwarf-5";
+    else if (AdjustedDwarfVersion < 5)
+      // The toolchain has reduced allowed dwarf version, so we can't enable
+      // -gembed-source.
+      D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
+          << A->getAsString(Args) << TC.getTripleString();
     else if (checkDebugInfoOption(A, Args, D, TC))
       CmdArgs.push_back("-gembed-source");
   }
@@ -3946,7 +3953,7 @@
       DebugInfoKind <= codegenoptions::DebugDirectivesOnly)
     DebugInfoKind = codegenoptions::DebugLineTablesOnly;
 
-  RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
+  RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, AdjustedDwarfVersion,
                           DebuggerTuning);
 
   // -fdebug-macro turns on macro debug info generation.
Index: clang/include/clang/Driver/ToolChain.h
===================================================================
--- clang/include/clang/Driver/ToolChain.h
+++ clang/include/clang/Driver/ToolChain.h
@@ -489,6 +489,11 @@
   // to the contrary.
   virtual unsigned GetDefaultDwarfVersion() const { return 4; }
 
+  // Some toolchains may have different restrictions on the DWARF version and
+  // may need to adjust it. E.g. NVPTX may need to enforce DWARF2 even when 
host
+  // compilation uses DWARF5.
+  virtual unsigned GetAdjustedDwarfVersion(unsigned v) const { return v; }
+
   // True if the driver should assume "-fstandalone-debug"
   // in the absence of an option specifying otherwise,
   // provided that debugging was requested in the first place.


Index: clang/test/Driver/cuda-unsupported-debug-options.cu
===================================================================
--- clang/test/Driver/cuda-unsupported-debug-options.cu
+++ clang/test/Driver/cuda-unsupported-debug-options.cu
@@ -18,5 +18,7 @@
 // CHECK-NOT: debug information option '{{.*}}' is not supported for target 'x86
 // CHECK: "-triple" "nvptx64-nvidia-cuda"
 // CHECK-NOT: {{-compress-debug|-fdebug-info-for-profiling|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}}
+// Make sure we do not see any dwarf version other than 2, regardless of what's used on the host side.
+// CHECK-NOT: {{-dwarf-version=[^2]}}
 // CHECK: "-triple" "x86_64
 // CHECK-SAME: {{-compress-debug|-fdebug-info-for-profiling|split-dwarf|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}}
Index: clang/lib/Driver/ToolChains/Cuda.h
===================================================================
--- clang/lib/Driver/ToolChains/Cuda.h
+++ clang/lib/Driver/ToolChains/Cuda.h
@@ -185,6 +185,8 @@
                      const llvm::opt::ArgList &Args) const override;
 
   unsigned GetDefaultDwarfVersion() const override { return 2; }
+  // NVPTX supports only DWARF2.
+  unsigned GetAdjustedDwarfVersion(unsigned v) const override { return 2; }
 
   const ToolChain &HostTC;
   CudaInstallationDetector CudaInstallation;
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3854,6 +3854,8 @@
         DWARFVersion = ExplicitVersion;
   }
 
+  auto AdjustedDwarfVersion = TC.GetAdjustedDwarfVersion(DWARFVersion);
+
   // -gline-directives-only supported only for the DWARF debug info.
   if (DWARFVersion == 0 && DebugInfoKind == codegenoptions::DebugDirectivesOnly)
     DebugInfoKind = codegenoptions::NoDebugInfo;
@@ -3918,6 +3920,11 @@
     if (DWARFVersion < 5)
       D.Diag(diag::err_drv_argument_only_allowed_with)
           << A->getAsString(Args) << "-gdwarf-5";
+    else if (AdjustedDwarfVersion < 5)
+      // The toolchain has reduced allowed dwarf version, so we can't enable
+      // -gembed-source.
+      D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
+          << A->getAsString(Args) << TC.getTripleString();
     else if (checkDebugInfoOption(A, Args, D, TC))
       CmdArgs.push_back("-gembed-source");
   }
@@ -3946,7 +3953,7 @@
       DebugInfoKind <= codegenoptions::DebugDirectivesOnly)
     DebugInfoKind = codegenoptions::DebugLineTablesOnly;
 
-  RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
+  RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, AdjustedDwarfVersion,
                           DebuggerTuning);
 
   // -fdebug-macro turns on macro debug info generation.
Index: clang/include/clang/Driver/ToolChain.h
===================================================================
--- clang/include/clang/Driver/ToolChain.h
+++ clang/include/clang/Driver/ToolChain.h
@@ -489,6 +489,11 @@
   // to the contrary.
   virtual unsigned GetDefaultDwarfVersion() const { return 4; }
 
+  // Some toolchains may have different restrictions on the DWARF version and
+  // may need to adjust it. E.g. NVPTX may need to enforce DWARF2 even when host
+  // compilation uses DWARF5.
+  virtual unsigned GetAdjustedDwarfVersion(unsigned v) const { return v; }
+
   // True if the driver should assume "-fstandalone-debug"
   // in the absence of an option specifying otherwise,
   // provided that debugging was requested in the first place.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to