Skip to content

Make the DWARF version emitted by the Swift compiler configurable. #69254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/swift/ABI/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace swift {

/// Represents the nine reflection sections used by Swift
/// Represents the nine reflection sections used by Swift + the Swift AST
/// section used by the debugger.
enum ReflectionSectionKind : uint8_t {
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) KIND,
#include "llvm/BinaryFormat/Swift.def"
Expand Down
5 changes: 2 additions & 3 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class IRGenOptions {
std::string DebugCompilationDir;

/// The DWARF version of debug info.
unsigned DWARFVersion;
uint8_t DWARFVersion = 4;

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

IRGenOptions()
: DWARFVersion(2),
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
: OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
Verify(true), OptMode(OptimizationMode::NotSet),
Sanitizers(OptionSet<SanitizerKind>()),
SanitizersWithRecoveryInstrumentation(OptionSet<SanitizerKind>()),
Expand Down
33 changes: 0 additions & 33 deletions include/swift/Basic/Dwarf.h

This file was deleted.

3 changes: 3 additions & 0 deletions include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class OutputInfo {
/// What kind of debug info to generate.
IRGenDebugInfoFormat DebugInfoFormat = IRGenDebugInfoFormat::None;

/// DWARF output format version number.
std::optional<uint8_t> DWARFVersion;

/// Whether or not the driver should generate a module.
bool ShouldGenerateModule = false;

Expand Down
8 changes: 6 additions & 2 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,8 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
Group<g_Group>, Flags<[FrontendOption]>,
HelpText<"Emit full DWARF type info.">;
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
Flags<[FrontendOption]>,
HelpText<"Remap source paths in debug info">, MetaVarName<"<prefix=replacement>">;
Flags<[FrontendOption]>,
HelpText<"Remap source paths in debug info">, MetaVarName<"<prefix=replacement>">;
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
Flags<[FrontendOption]>,
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
Expand All @@ -1007,6 +1007,10 @@ def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
def debug_info_format : Joined<["-"], "debug-info-format=">,
Flags<[FrontendOption]>,
HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">;
def dwarf_version : Joined<["-"], "dwarf-version=">,
Flags<[FrontendOption]>,
HelpText<"DWARF debug info version to produce if requested">,
MetaVarName<"<version>">;

def prefix_serialized_debugging_options : Flag<["-"], "prefix-serialized-debugging-options">,
Flags<[FrontendOption]>,
Expand Down
1 change: 0 additions & 1 deletion lib/ASTSectionImporter/ASTSectionImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "swift/ASTSectionImporter/ASTSectionImporter.h"
#include "../Serialization/ModuleFormat.h"
#include "swift/AST/ASTContext.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Serialization/Validation.h"
#include "llvm/Support/Debug.h"
Expand Down
33 changes: 32 additions & 1 deletion lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "swift/AST/DiagnosticsDriver.h"
#include "swift/AST/PlatformKind.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
Expand Down Expand Up @@ -626,6 +625,28 @@ toolchains::Darwin::addDeploymentTargetArgs(ArgStringList &Arguments,
}
}

static unsigned getDWARFVersionForTriple(const llvm::Triple &triple) {
llvm::VersionTuple osVersion;
const DarwinPlatformKind kind = getDarwinPlatformKind(triple);
switch (kind) {
case DarwinPlatformKind::MacOS:
triple.getMacOSXVersion(osVersion);
if (osVersion < llvm::VersionTuple(10, 11))
return 2;
return 4;
case DarwinPlatformKind::IPhoneOSSimulator:
case DarwinPlatformKind::IPhoneOS:
case DarwinPlatformKind::TvOS:
case DarwinPlatformKind::TvOSSimulator:
osVersion = triple.getiOSVersion();
if (osVersion < llvm::VersionTuple(9))
return 2;
return 4;
default:
return 4;
}
}

void toolchains::Darwin::addCommonFrontendArgs(
const OutputInfo &OI, const CommandOutput &output,
const llvm::opt::ArgList &inputArgs,
Expand All @@ -644,6 +665,16 @@ void toolchains::Darwin::addCommonFrontendArgs(
inputArgs.MakeArgString(variantSDKVersion->getAsString()));
}
}
std::string dwarfVersion;
{
llvm::raw_string_ostream os(dwarfVersion);
os << "-dwarf-version=";
if (OI.DWARFVersion)
os << *OI.DWARFVersion;
else
os << getDWARFVersionForTriple(getTriple());
}
arguments.push_back(inputArgs.MakeArgString(dwarfVersion));
}

/// Add the frontend arguments needed to find external plugins in standard
Expand Down
10 changes: 10 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,16 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
: "-gdwarf_types");
}

if (const Arg *A = Args.getLastArg(options::OPT_dwarf_version)) {
unsigned vers;
if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 &&
vers <= 5)
OI.DWARFVersion = vers;
else
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
}

if (Args.hasArg(options::OPT_emit_module, options::OPT_emit_module_path)) {
// The user has requested a module, so generate one and treat it as
// top-level output.
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "ToolChains.h"

#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
Expand Down Expand Up @@ -262,6 +261,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
inputArgs.AddLastArg(arguments, options::OPT_debug_info_format);
inputArgs.AddLastArg(arguments, options::OPT_dwarf_version);
inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module);
inputArgs.AddLastArg(arguments, options::OPT_module_cache_path);
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);
Expand Down
1 change: 0 additions & 1 deletion lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "ToolChains.h"

#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
Expand Down
1 change: 0 additions & 1 deletion lib/Driver/WindowsToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "ToolChains.h"

#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
Expand Down
11 changes: 10 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
Opts.DebugCompilationDir = std::string(cwd.str());
}
}

if (const Arg *A = Args.getLastArg(options::OPT_debug_info_format)) {
if (A->containsValue("dwarf"))
Opts.DebugInfoFormat = IRGenDebugInfoFormat::DWARF;
Expand Down Expand Up @@ -2502,6 +2501,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
: "-gdwarf_types");
}

if (auto A = Args.getLastArg(OPT_dwarf_version)) {
unsigned vers;
if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 &&
vers <= 5)
Opts.DWARFVersion = vers;
else
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
}

for (auto A : Args.getAllArgValues(options::OPT_file_prefix_map)) {
auto SplitMap = StringRef(A).split('=');
Opts.FilePrefixMap.addMapping(SplitMap.first, SplitMap.second);
Expand Down
5 changes: 2 additions & 3 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "swift/AST/TBDGenRequests.h"
#include "swift/AST/TypeRefinementContext.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/Edit.h"
#include "swift/Basic/FileSystem.h"
#include "swift/Basic/LLVMInitialize.h"
Expand Down Expand Up @@ -2241,9 +2240,9 @@ int swift::performFrontend(ArrayRef<const char *> Args,
trace.emplace(*buffer);
});

// Setting DWARF Version depend on platform
// Setting DWARF Version based on frontend options.
IRGenOptions &IRGenOpts = Invocation.getIRGenOptions();
IRGenOpts.DWARFVersion = swift::DWARFVersion;
IRGenOpts.DWARFVersion = IRGenOpts.DWARFVersion;

// The compiler invocation is now fully configured; notify our observer.
if (observer) {
Expand Down
24 changes: 15 additions & 9 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../Serialization/ModuleFormat.h"
#include "IRGenModule.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/ABI/ObjectFile.h"
#include "swift/AST/DiagnosticsIRGen.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/IRGenRequests.h"
Expand All @@ -26,7 +27,6 @@
#include "swift/AST/SILOptimizerRequests.h"
#include "swift/AST/TBDGenRequests.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/MD5Stream.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/STLExtras.h"
Expand Down Expand Up @@ -1630,6 +1630,7 @@ void swift::createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
auto *ASTSym = new llvm::GlobalVariable(M, Ty, /*constant*/ true,
llvm::GlobalVariable::InternalLinkage,
Data, "__Swift_AST");

std::string Section;
switch (IGM.TargetInfo.OutputObjectFormat) {
case llvm::Triple::DXContainer:
Expand All @@ -1638,19 +1639,24 @@ void swift::createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
case llvm::Triple::UnknownObjectFormat:
llvm_unreachable("unknown object format");
case llvm::Triple::XCOFF:
case llvm::Triple::COFF:
Section = COFFASTSectionName;
case llvm::Triple::COFF: {
SwiftObjectFileFormatCOFF COFF;
Section = COFF.getSectionName(ReflectionSectionKind::swiftast);
break;
}
case llvm::Triple::ELF:
Section = ELFASTSectionName;
break;
case llvm::Triple::MachO:
Section = std::string(MachOASTSegmentName) + "," + MachOASTSectionName;
case llvm::Triple::Wasm: {
SwiftObjectFileFormatELF ELF;
Section = ELF.getSectionName(ReflectionSectionKind::swiftast);
break;
case llvm::Triple::Wasm:
Section = WasmASTSectionName;
}
case llvm::Triple::MachO: {
SwiftObjectFileFormatMachO MachO;
Section = std::string(*MachO.getSegmentName()) + "," +
MachO.getSectionName(ReflectionSectionKind::swiftast).str();
break;
}
}
ASTSym->setSection(Section);
ASTSym->setAlignment(llvm::MaybeAlign(serialization::SWIFTMODULE_ALIGNMENT));
::performLLVM(Opts, Ctx.Diags, nullptr, nullptr, IGM.getModule(),
Expand Down
1 change: 0 additions & 1 deletion lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "swift/AST/Pattern.h"
#include "swift/AST/TypeDifferenceVisitor.h"
#include "swift/Basic/Compiler.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Version.h"
#include "swift/ClangImporter/ClangImporter.h"
Expand Down
1 change: 0 additions & 1 deletion lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/IRGenRequests.h"
#include "swift/AST/Module.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVMExtras.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/Demangling/ManglingMacros.h"
Expand Down
1 change: 0 additions & 1 deletion lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/FileSystem.h"
#include "swift/Basic/LLVMExtras.h"
#include "swift/Basic/PathRemapper.h"
Expand Down
8 changes: 8 additions & 0 deletions test/Driver/options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
// RUN: not %swiftc_driver -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix MISSING_OPTION_G_ERROR %s
// MISSING_OPTION_G_ERROR: error: option '-debug-info-format={{.*}}' is missing a required argument (-g)

// RUN: %swift_driver -### -g -dwarf-version=3 %s 2>&1 | %FileCheck -check-prefix DWARF_VERSION_3 %s
// DWARF_VERSION_3: -dwarf-version=3
// RUN: not %swift_driver -dwarf-version=1 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
// RUN: not %swift_driver -dwarf-version=6 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
// RUN: not %swiftc_driver -dwarf-version=1 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
// RUN: not %swiftc_driver -dwarf-version=6 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
// INVALID_DWARF_VERSION: invalid value '{{1|6}}' in '-dwarf-version={{1|6}}'

// RUN: not %swift_driver -gline-tables-only -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s
// RUN: not %swift_driver -gdwarf-types -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s
// RUN: not %swiftc_driver -gline-tables-only -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s
Expand Down
Loading