Skip to content

Commit 7781af5

Browse files
Merge pull request #69254 from adrian-prantl/dwarf-version
Make the DWARF version emitted by the Swift compiler configurable.
2 parents 899a389 + a26bbb0 commit 7781af5

File tree

19 files changed

+104
-69
lines changed

19 files changed

+104
-69
lines changed

include/swift/ABI/ObjectFile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
namespace swift {
1515

16-
/// Represents the nine reflection sections used by Swift
16+
/// Represents the nine reflection sections used by Swift + the Swift AST
17+
/// section used by the debugger.
1718
enum ReflectionSectionKind : uint8_t {
1819
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) KIND,
1920
#include "llvm/BinaryFormat/Swift.def"

include/swift/AST/IRGenOptions.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class IRGenOptions {
237237
std::string DebugCompilationDir;
238238

239239
/// The DWARF version of debug info.
240-
unsigned DWARFVersion;
240+
uint8_t DWARFVersion = 4;
241241

242242
/// The command line string that is to be stored in the debug info.
243243
std::string DebugFlags;
@@ -512,8 +512,7 @@ class IRGenOptions {
512512
bool EmitCASIDFile;
513513

514514
IRGenOptions()
515-
: DWARFVersion(2),
516-
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
515+
: OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
517516
Verify(true), OptMode(OptimizationMode::NotSet),
518517
Sanitizers(OptionSet<SanitizerKind>()),
519518
SanitizersWithRecoveryInstrumentation(OptionSet<SanitizerKind>()),

include/swift/Basic/Dwarf.h

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

include/swift/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class OutputInfo {
125125
/// What kind of debug info to generate.
126126
IRGenDebugInfoFormat DebugInfoFormat = IRGenDebugInfoFormat::None;
127127

128+
/// DWARF output format version number.
129+
std::optional<uint8_t> DWARFVersion;
130+
128131
/// Whether or not the driver should generate a module.
129132
bool ShouldGenerateModule = false;
130133

include/swift/Option/Options.td

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
991991
Group<g_Group>, Flags<[FrontendOption]>,
992992
HelpText<"Emit full DWARF type info.">;
993993
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
994-
Flags<[FrontendOption]>,
995-
HelpText<"Remap source paths in debug info">, MetaVarName<"<prefix=replacement>">;
994+
Flags<[FrontendOption]>,
995+
HelpText<"Remap source paths in debug info">, MetaVarName<"<prefix=replacement>">;
996996
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
997997
Flags<[FrontendOption]>,
998998
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
@@ -1007,6 +1007,10 @@ def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
10071007
def debug_info_format : Joined<["-"], "debug-info-format=">,
10081008
Flags<[FrontendOption]>,
10091009
HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">;
1010+
def dwarf_version : Joined<["-"], "dwarf-version=">,
1011+
Flags<[FrontendOption]>,
1012+
HelpText<"DWARF debug info version to produce if requested">,
1013+
MetaVarName<"<version>">;
10101014

10111015
def prefix_serialized_debugging_options : Flag<["-"], "prefix-serialized-debugging-options">,
10121016
Flags<[FrontendOption]>,

lib/ASTSectionImporter/ASTSectionImporter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "swift/ASTSectionImporter/ASTSectionImporter.h"
1919
#include "../Serialization/ModuleFormat.h"
2020
#include "swift/AST/ASTContext.h"
21-
#include "swift/Basic/Dwarf.h"
2221
#include "swift/Serialization/SerializedModuleLoader.h"
2322
#include "swift/Serialization/Validation.h"
2423
#include "llvm/Support/Debug.h"

lib/Driver/DarwinToolChains.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "swift/AST/DiagnosticsDriver.h"
1616
#include "swift/AST/PlatformKind.h"
17-
#include "swift/Basic/Dwarf.h"
1817
#include "swift/Basic/LLVM.h"
1918
#include "swift/Basic/Platform.h"
2019
#include "swift/Basic/Range.h"
@@ -626,6 +625,28 @@ toolchains::Darwin::addDeploymentTargetArgs(ArgStringList &Arguments,
626625
}
627626
}
628627

628+
static unsigned getDWARFVersionForTriple(const llvm::Triple &triple) {
629+
llvm::VersionTuple osVersion;
630+
const DarwinPlatformKind kind = getDarwinPlatformKind(triple);
631+
switch (kind) {
632+
case DarwinPlatformKind::MacOS:
633+
triple.getMacOSXVersion(osVersion);
634+
if (osVersion < llvm::VersionTuple(10, 11))
635+
return 2;
636+
return 4;
637+
case DarwinPlatformKind::IPhoneOSSimulator:
638+
case DarwinPlatformKind::IPhoneOS:
639+
case DarwinPlatformKind::TvOS:
640+
case DarwinPlatformKind::TvOSSimulator:
641+
osVersion = triple.getiOSVersion();
642+
if (osVersion < llvm::VersionTuple(9))
643+
return 2;
644+
return 4;
645+
default:
646+
return 4;
647+
}
648+
}
649+
629650
void toolchains::Darwin::addCommonFrontendArgs(
630651
const OutputInfo &OI, const CommandOutput &output,
631652
const llvm::opt::ArgList &inputArgs,
@@ -644,6 +665,16 @@ void toolchains::Darwin::addCommonFrontendArgs(
644665
inputArgs.MakeArgString(variantSDKVersion->getAsString()));
645666
}
646667
}
668+
std::string dwarfVersion;
669+
{
670+
llvm::raw_string_ostream os(dwarfVersion);
671+
os << "-dwarf-version=";
672+
if (OI.DWARFVersion)
673+
os << *OI.DWARFVersion;
674+
else
675+
os << getDWARFVersionForTriple(getTriple());
676+
}
677+
arguments.push_back(inputArgs.MakeArgString(dwarfVersion));
647678
}
648679

649680
/// Add the frontend arguments needed to find external plugins in standard

lib/Driver/Driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,16 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
16851685
: "-gdwarf_types");
16861686
}
16871687

1688+
if (const Arg *A = Args.getLastArg(options::OPT_dwarf_version)) {
1689+
unsigned vers;
1690+
if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 &&
1691+
vers <= 5)
1692+
OI.DWARFVersion = vers;
1693+
else
1694+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
1695+
A->getAsString(Args), A->getValue());
1696+
}
1697+
16881698
if (Args.hasArg(options::OPT_emit_module, options::OPT_emit_module_path)) {
16891699
// The user has requested a module, so generate one and treat it as
16901700
// top-level output.

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "ToolChains.h"
1414

1515
#include "swift/AST/DiagnosticsDriver.h"
16-
#include "swift/Basic/Dwarf.h"
1716
#include "swift/Basic/LLVM.h"
1817
#include "swift/Basic/Platform.h"
1918
#include "swift/Basic/Range.h"
@@ -262,6 +261,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
262261
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
263262
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
264263
inputArgs.AddLastArg(arguments, options::OPT_debug_info_format);
264+
inputArgs.AddLastArg(arguments, options::OPT_dwarf_version);
265265
inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module);
266266
inputArgs.AddLastArg(arguments, options::OPT_module_cache_path);
267267
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);

lib/Driver/UnixToolChains.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "ToolChains.h"
1414

15-
#include "swift/Basic/Dwarf.h"
1615
#include "swift/Basic/LLVM.h"
1716
#include "swift/Basic/Platform.h"
1817
#include "swift/Basic/Range.h"

0 commit comments

Comments
 (0)