Be less conservative wrt. linker dead code elimination

This commit is contained in:
Martin Kinkelin 2023-02-10 21:39:10 +01:00
parent 95ac5cadb8
commit aa9eb33cb1
3 changed files with 22 additions and 21 deletions

View file

@ -1,6 +1,7 @@
# LDC master # LDC master
#### Big news #### Big news
- Linker-level dead code elimination is enabled by default for Apple, wasm and *all* ELF targets too now. (#4320)
#### Platform support #### Platform support

View file

@ -479,6 +479,8 @@ void ArgsBuilder::addSanitizers(const llvm::Triple &triple) {
void ArgsBuilder::build(llvm::StringRef outputPath, void ArgsBuilder::build(llvm::StringRef outputPath,
const std::vector<std::string> &defaultLibNames) { const std::vector<std::string> &defaultLibNames) {
const auto &triple = *global.params.targetTriple;
// object files // object files
for (auto objfile : global.params.objfiles) { for (auto objfile : global.params.objfiles) {
args.push_back(objfile); args.push_back(objfile);
@ -494,7 +496,7 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
// Link with profile-rt library when generating an instrumented binary. // Link with profile-rt library when generating an instrumented binary.
if (opts::isInstrumentingForPGO()) { if (opts::isInstrumentingForPGO()) {
addProfileRuntimeLinkFlags(*global.params.targetTriple); addProfileRuntimeLinkFlags(triple);
} }
if (opts::enableDynamicCompile) { if (opts::enableDynamicCompile) {
@ -521,10 +523,10 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
args.push_back("-o"); args.push_back("-o");
args.push_back(std::string(outputPath)); args.push_back(std::string(outputPath));
addSanitizers(*global.params.targetTriple); addSanitizers(triple);
if (opts::fXRayInstrument) { if (opts::fXRayInstrument) {
addXRayLinkFlags(*global.params.targetTriple); addXRayLinkFlags(triple);
} }
// Add LTO link flags before adding the user link switches, such that the user // Add LTO link flags before adding the user link switches, such that the user
@ -559,16 +561,14 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
addLdFlag("-rpath", rpath); addLdFlag("-rpath", rpath);
} }
if (global.params.targetTriple->getOS() == llvm::Triple::Linux || // Make sure we don't do --gc-sections when generating a profile-
(global.params.targetTriple->getOS() == llvm::Triple::FreeBSD && // instrumented binary. The runtime relies on magic sections, which
(useInternalLLDForLinking() || // would be stripped by gc-section on older version of ld, see bug:
(!opts::linker.empty() && opts::linker != "bfd") || // https://sourceware.org/bugzilla/show_bug.cgi?id=19161
(opts::linker.empty() && isLldDefaultLinker())))) { if (!opts::disableLinkerStripDead && !opts::isInstrumentingForPGO()) {
// Make sure we don't do --gc-sections when generating a profile- if (triple.isOSBinFormatMachO()) {
// instrumented binary. The runtime relies on magic sections, which addLdFlag("-dead_strip");
// would be stripped by gc-section on older version of ld, see bug: } else {
// https://sourceware.org/bugzilla/show_bug.cgi?id=19161
if (!opts::disableLinkerStripDead && !opts::isInstrumentingForPGO()) {
addLdFlag("--gc-sections"); addLdFlag("--gc-sections");
} }
} }

View file

@ -492,14 +492,14 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
break; break;
} }
// Right now, we only support linker-level dead code elimination on Linux // Linker-level dead code elimination for ELF/wasm binaries using GNU or LLD
// and FreeBSD using GNU or LLD linkers (based on the --gc-sections flag). // linkers (based on the --gc-sections flag) requires a separate section per
// The Apple ld on OS X supports a similar flag (-dead_strip) that doesn't // symbol.
// require emitting the symbols into different sections. The MinGW ld doesn't // The Apple ld64 on macOS supports a similar flag (-dead_strip) that doesn't
// seem to support --gc-sections at all. // require emitting the symbols into different sections.
if (!noLinkerStripDead && (triple.getOS() == llvm::Triple::Linux || // On Windows, the MSVC link.exe / lld-link.exe has `/REF`; LLD enforces
triple.getOS() == llvm::Triple::FreeBSD || // separate sections with LTO, so do the same here.
triple.getOS() == llvm::Triple::Win32)) { if (!noLinkerStripDead && !triple.isOSBinFormatMachO()) {
targetOptions.FunctionSections = true; targetOptions.FunctionSections = true;
targetOptions.DataSections = true; targetOptions.DataSections = true;
} }