Skip to content

Commit 016e4eb

Browse files
committed
[DWARF] Allow toolchain to adjust specified DWARF version.
This is needed for CUDA compilation where NVPTX back-end only supports DWARF2, but host compilation should be allowed to use newer DWARF versions. Differential Revision: https://reviews.llvm.org/D92617
1 parent 2ea8c69 commit 016e4eb

File tree

8 files changed

+111
-52
lines changed

8 files changed

+111
-52
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ def warn_drv_unsupported_opt_for_target : Warning<
292292
def warn_drv_unsupported_debug_info_opt_for_target : Warning<
293293
"debug information option '%0' is not supported for target '%1'">,
294294
InGroup<UnsupportedTargetOpt>;
295+
def warn_drv_dwarf_version_limited_by_target : Warning<
296+
"debug information option '%0' is not supported. It needs DWARF-%2 but target '%1' only provides DWARF-%3.">,
297+
InGroup<UnsupportedTargetOpt>;
295298
def warn_c_kext : Warning<
296299
"ignoring -fapple-kext which is valid for C++ and Objective-C++ only">;
297300
def warn_ignoring_fdiscard_for_bitcode : Warning<

clang/include/clang/Driver/ToolChain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Support/VersionTuple.h"
2828
#include "llvm/Target/TargetOptions.h"
2929
#include <cassert>
30+
#include <climits>
3031
#include <memory>
3132
#include <string>
3233
#include <utility>
@@ -489,6 +490,11 @@ class ToolChain {
489490
// to the contrary.
490491
virtual unsigned GetDefaultDwarfVersion() const { return 4; }
491492

493+
// Some toolchains may have different restrictions on the DWARF version and
494+
// may need to adjust it. E.g. NVPTX may need to enforce DWARF2 even when host
495+
// compilation uses DWARF5.
496+
virtual unsigned getMaxDwarfVersion() const { return UINT_MAX; }
497+
492498
// True if the driver should assume "-fstandalone-debug"
493499
// in the absence of an option specifying otherwise,
494500
// provided that debugging was requested in the first place.

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,26 +3821,33 @@ static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
38213821
}
38223822
}
38233823

3824-
unsigned DWARFVersion = 0;
3824+
unsigned RequestedDWARFVersion = 0; // DWARF version requested by the user
3825+
unsigned EffectiveDWARFVersion = 0; // DWARF version TC can generate. It may
3826+
// be lower than what the user wanted.
38253827
unsigned DefaultDWARFVersion = ParseDebugDefaultVersion(TC, Args);
38263828
if (EmitDwarf) {
38273829
// Start with the platform default DWARF version
3828-
DWARFVersion = TC.GetDefaultDwarfVersion();
3829-
assert(DWARFVersion && "toolchain default DWARF version must be nonzero");
3830+
RequestedDWARFVersion = TC.GetDefaultDwarfVersion();
3831+
assert(RequestedDWARFVersion &&
3832+
"toolchain default DWARF version must be nonzero");
38303833

38313834
// If the user specified a default DWARF version, that takes precedence
38323835
// over the platform default.
38333836
if (DefaultDWARFVersion)
3834-
DWARFVersion = DefaultDWARFVersion;
3837+
RequestedDWARFVersion = DefaultDWARFVersion;
38353838

38363839
// Override with a user-specified DWARF version
38373840
if (GDwarfN)
38383841
if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling()))
3839-
DWARFVersion = ExplicitVersion;
3842+
RequestedDWARFVersion = ExplicitVersion;
3843+
// Clamp effective DWARF version to the max supported by the toolchain.
3844+
EffectiveDWARFVersion =
3845+
std::min(RequestedDWARFVersion, TC.getMaxDwarfVersion());
38403846
}
38413847

38423848
// -gline-directives-only supported only for the DWARF debug info.
3843-
if (DWARFVersion == 0 && DebugInfoKind == codegenoptions::DebugDirectivesOnly)
3849+
if (RequestedDWARFVersion == 0 &&
3850+
DebugInfoKind == codegenoptions::DebugDirectivesOnly)
38443851
DebugInfoKind = codegenoptions::NoDebugInfo;
38453852

38463853
// We ignore flag -gstrict-dwarf for now.
@@ -3900,9 +3907,15 @@ static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
39003907
// fallen back to the target default, so if this is still not at least 5
39013908
// we emit an error.
39023909
const Arg *A = Args.getLastArg(options::OPT_gembed_source);
3903-
if (DWARFVersion < 5)
3910+
if (RequestedDWARFVersion < 5)
39043911
D.Diag(diag::err_drv_argument_only_allowed_with)
39053912
<< A->getAsString(Args) << "-gdwarf-5";
3913+
else if (EffectiveDWARFVersion < 5)
3914+
// The toolchain has reduced allowed dwarf version, so we can't enable
3915+
// -gembed-source.
3916+
D.Diag(diag::warn_drv_dwarf_version_limited_by_target)
3917+
<< A->getAsString(Args) << TC.getTripleString() << 5
3918+
<< EffectiveDWARFVersion;
39063919
else if (checkDebugInfoOption(A, Args, D, TC))
39073920
CmdArgs.push_back("-gembed-source");
39083921
}
@@ -3931,7 +3944,7 @@ static void RenderDebugOptions(const ToolChain &TC, const Driver &D,
39313944
DebugInfoKind <= codegenoptions::DebugDirectivesOnly)
39323945
DebugInfoKind = codegenoptions::DebugLineTablesOnly;
39333946

3934-
RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, DWARFVersion,
3947+
RenderDebugEnablingArgs(Args, CmdArgs, DebugInfoKind, EffectiveDWARFVersion,
39353948
DebuggerTuning);
39363949

39373950
// -fdebug-macro turns on macro debug info generation.

clang/lib/Driver/ToolChains/Cuda.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ class LLVM_LIBRARY_VISIBILITY CudaToolChain : public ToolChain {
185185
const llvm::opt::ArgList &Args) const override;
186186

187187
unsigned GetDefaultDwarfVersion() const override { return 2; }
188+
// NVPTX supports only DWARF2.
189+
unsigned getMaxDwarfVersion() const override { return 2; }
188190

189191
const ToolChain &HostTC;
190192
CudaInstallationDetector CudaInstallation;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// REQUIRES: clang-driver
2+
// REQUIRES: x86-registered-target
3+
// REQUIRES: nvptx-registered-target
4+
// REQUIRES: zlib
5+
6+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -g -gz 2>&1 \
7+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
8+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf -fdebug-info-for-profiling 2>&1 \
9+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
10+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf-2 -gsplit-dwarf 2>&1 \
11+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
12+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf-3 -glldb 2>&1 \
13+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
14+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf-4 -gcodeview 2>&1 \
15+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
16+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf-5 -gmodules 2>&1 \
17+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
18+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -ggdb1 -fdebug-macro 2>&1 \
19+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
20+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -ggdb2 -ggnu-pubnames 2>&1 \
21+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
22+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -ggdb3 -gdwarf-aranges 2>&1 \
23+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
24+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -g -gcolumn-info -fdebug-types-section 2>&1 \
25+
// RUN: | FileCheck %s --check-prefixes WARN,COMMON
26+
27+
// Same tests for OpenMP
28+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
29+
// RUN: -g -gz 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
30+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
31+
// RUN: -gdwarf -fdebug-info-for-profiling 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
32+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
33+
// RUN: -gdwarf-2 -gsplit-dwarf 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
34+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
35+
// RUN: -gdwarf-3 -glldb 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
36+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
37+
// RUN: -gdwarf-4 -gcodeview 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
38+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
39+
// RUN: -gdwarf-5 -gmodules 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
40+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
41+
// RUN: -ggdb1 -fdebug-macro 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
42+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
43+
// RUN: -ggdb2 -ggnu-pubnames 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
44+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
45+
// RUN: -ggdb3 -gdwarf-aranges 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
46+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
47+
// RUN: -g -gcolumn-info -fdebug-types-section 2>&1 | FileCheck %s --check-prefixes WARN,COMMON
48+
49+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf-5 -gembed-source 2>&1 \
50+
// RUN: | FileCheck %s --check-prefixes WARN-GES,COMMON
51+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -ggdb -gembed-source -gdwarf-5 2>&1 \
52+
// RUN: | FileCheck %s --check-prefixes WARN-GES,COMMON
53+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
54+
// RUN: -gdwarf-5 -gembed-source 2>&1 | FileCheck %s --check-prefixes WARN-GES,COMMON
55+
// RUN: %clang -### -target x86_64-linux-gnu -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -c %s \
56+
// RUN: -ggdb -gembed-source -gdwarf-5 2>&1 | FileCheck %s --check-prefixes WARN-GES,COMMON
57+
58+
// COMMON: warning: debug information option '{{-gz|-fdebug-info-for-profiling|-gsplit-dwarf|-glldb|-gcodeview|-gmodules|-gembed-source|-fdebug-macro|-ggnu-pubnames|-gdwarf-aranges|-fdebug-types-section}}' is not supported
59+
// WARN-SAME: for target 'nvptx64-nvidia-cuda' [-Wunsupported-target-opt]
60+
// WARN-GES-SAME: It needs DWARF-5 but target 'nvptx64-nvidia-cuda' only provides DWARF-2. [-Wunsupported-target-opt]
61+
// COMMON-NOT: debug information option '{{.*}}' is not supported for target 'x86
62+
// COMMON: "-triple" "nvptx64-nvidia-cuda"
63+
// COMMON-NOT: {{-compress-debug|-fdebug-info-for-profiling|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}}
64+
// COMMON: "-triple" "x86_64
65+
// COMMON-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}}

clang/test/Driver/cuda-unsupported-debug-options.cu

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: clang-driver
2+
// REQUIRES: x86-registered-target
3+
// REQUIRES: nvptx-registered-target
4+
5+
// Verify that DWARF version is properly clamped for nvptx, but not for the host.
6+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -gdwarf-5 -gembed-source 2>&1 \
7+
// RUN: | FileCheck %s --check-prefix=DWARF-CLAMP
8+
// RUN: %clang -### -target x86_64-linux-gnu -c %s -ggdb -gembed-source -gdwarf-5 2>&1 \
9+
// RUN: | FileCheck %s --check-prefix=DWARF-CLAMP
10+
11+
// DWARF-CLAMP: "-triple" "nvptx64-nvidia-cuda"
12+
// DWARF-CLAMP-SAME: -dwarf-version=2
13+
// DWARF-CLAMP: "-triple" "x86_64
14+
// DWARF-CLAMP-SAME: -dwarf-version=5

clang/test/Driver/openmp-unsupported-debug-options.c

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)