Skip to content

Commit

Permalink
[lld][COFF] Add command line options for LTO with new pass manager
Browse files Browse the repository at this point in the history
This is more or less a port of rL329598 (D45275) to the COFF linker.
Since there were already LTO-related settings under -opt:, I added
them there instead of new flags.

Differential Revision: https://reviews.llvm.org/D90624
  • Loading branch information
rojamd committed Nov 5, 2020
1 parent 5b30d9a commit b79e990
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lld/COFF/Config.h
Expand Up @@ -157,6 +157,11 @@ struct Configuration {
// Used for /opt:lldltocachepolicy=policy
llvm::CachePruningPolicy ltoCachePolicy;

// Used for /opt:[no]ltonewpassmanager
bool ltoNewPassManager = false;
// Used for /opt:[no]ltodebugpassmanager
bool ltoDebugPassManager = false;

// Used for /merge:from=to (e.g. /merge:.rdata=.text)
std::map<StringRef, StringRef> merge;

Expand Down
12 changes: 12 additions & 0 deletions lld/COFF/Driver.cpp
Expand Up @@ -1513,6 +1513,8 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
unsigned icfLevel =
args.hasArg(OPT_profile) ? 0 : 1; // 0: off, 1: limited, 2: on
unsigned tailMerge = 1;
bool ltoNewPM = false;
bool ltoDebugPM = false;
for (auto *arg : args.filtered(OPT_opt)) {
std::string str = StringRef(arg->getValue()).lower();
SmallVector<StringRef, 1> vec;
Expand All @@ -1530,6 +1532,14 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
tailMerge = 2;
} else if (s == "nolldtailmerge") {
tailMerge = 0;
} else if (s == "ltonewpassmanager") {
ltoNewPM = true;
} else if (s == "noltonewpassmanager") {
ltoNewPM = false;
} else if (s == "ltodebugpassmanager") {
ltoDebugPM = true;
} else if (s == "noltodebugpassmanager") {
ltoDebugPM = false;
} else if (s.startswith("lldlto=")) {
StringRef optLevel = s.substr(7);
if (optLevel.getAsInteger(10, config->ltoo) || config->ltoo > 3)
Expand Down Expand Up @@ -1559,6 +1569,8 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
config->doGC = doGC;
config->doICF = icfLevel > 0;
config->tailMerge = (tailMerge == 1 && config->doICF) || tailMerge == 2;
config->ltoNewPassManager = ltoNewPM;
config->ltoDebugPassManager = ltoDebugPM;

// Handle /lldsavetemps
if (args.hasArg(OPT_lldsavetemps))
Expand Down
2 changes: 2 additions & 0 deletions lld/COFF/LTO.cpp
Expand Up @@ -82,6 +82,8 @@ static lto::Config createConfig() {
c.MAttrs = getMAttrs();
c.CGOptLevel = args::getCGOptLevel(config->ltoo);
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
c.UseNewPM = config->ltoNewPassManager;
c.DebugPassManager = config->ltoDebugPassManager;

if (config->saveTemps)
checkError(c.addSaveTemps(std::string(config->outputFile) + ".",
Expand Down
20 changes: 20 additions & 0 deletions lld/test/COFF/lto-new-pass-manager.ll
@@ -0,0 +1,20 @@
; REQUIRES: x86
; RUN: llvm-as %s -o %s.obj

; RUN: lld-link %s.obj -entry:main -opt:ltonewpassmanager -opt:ltodebugpassmanager 2>&1 | FileCheck %s --check-prefix=ENABLED
; ENABLED: Starting llvm::Module pass manager run.
; ENABLED: Finished llvm::Module pass manager run.

; Passing -time just to avoid empty FileCheck input
; RUN: lld-link %s.obj -entry:main -time -opt:ltonewpassmanager -opt:ltodebugpassmanager -opt:noltonewpassmanager 2>&1 | FileCheck %s --check-prefix=DISABLED
; RUN: lld-link %s.obj -entry:main -time -opt:ltonewpassmanager -opt:ltodebugpassmanager -opt:noltodebugpassmanager 2>&1 | FileCheck %s --check-prefix=DISABLED
; DISABLED-NOT: Starting llvm::Module pass manager run.
; DISABLED-NOT: Finished llvm::Module pass manager run.

target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.11.0"

define dso_local i32 @main(i32 %argc, i8** nocapture readnone %0) local_unnamed_addr {
entry:
ret i32 %argc
}

0 comments on commit b79e990

Please sign in to comment.