diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h --- a/clang/include/clang/Driver/ToolChain.h +++ b/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. diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/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. diff --git a/clang/lib/Driver/ToolChains/Cuda.h b/clang/lib/Driver/ToolChains/Cuda.h --- a/clang/lib/Driver/ToolChains/Cuda.h +++ b/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; diff --git a/clang/test/Driver/cuda-unsupported-debug-options.cu b/clang/test/Driver/cuda-unsupported-debug-options.cu --- a/clang/test/Driver/cuda-unsupported-debug-options.cu +++ b/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}}