Skip to content

Commit

Permalink
[COFF, Mach-O] Include -mllvm options in thinlto cache key
Browse files Browse the repository at this point in the history
Like D134013, but for COFF and Mach-O.

Also expand the ELF test a bit. I at first didn't realize that `getValue()` for
`-mllvm -foo=bar` would return `-foo=bar` instead of just `bar`, and so
I wrote the test to check if we indeed get this wrong. We don't, but
having the test for it seems nice, so I'm including it.

Differential Revision: https://reviews.llvm.org/D137971
  • Loading branch information
nico committed Nov 14, 2022
1 parent afc159b commit 67d311a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lld/COFF/Config.h
Expand Up @@ -11,6 +11,7 @@

#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Object/COFF.h"
Expand Down Expand Up @@ -123,6 +124,7 @@ struct Configuration {
bool showTiming = false;
bool showSummary = false;
unsigned debugTypes = static_cast<unsigned>(DebugType::None);
llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
std::vector<std::string> natvisFiles;
llvm::StringMap<std::string> namedStreams;
llvm::SmallString<128> pdbAltPath;
Expand Down
4 changes: 3 additions & 1 deletion lld/COFF/Driver.cpp
Expand Up @@ -1425,8 +1425,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
// Parse and evaluate -mllvm options.
std::vector<const char *> v;
v.push_back("lld-link (LLVM option parsing)");
for (auto *arg : args.filtered(OPT_mllvm))
for (const auto *arg : args.filtered(OPT_mllvm)) {
v.push_back(arg->getValue());
config->mllvmOpts.emplace_back(arg->getValue());
}
cl::ResetAllOptionOccurrences();
cl::ParseCommandLineOptions(v.size(), v.data());

Expand Down
2 changes: 2 additions & 0 deletions lld/COFF/LTO.cpp
Expand Up @@ -63,6 +63,8 @@ static lto::Config createConfig() {
lto::Config c;
c.Options = initTargetOptionsFromCodeGenFlags();
c.Options.EmitAddrsig = true;
for (StringRef C : config->mllvmOpts)
c.MllvmArgs.emplace_back(C.str());

// Always emit a section per function/datum with LTO. LLVM LTO should get most
// of the benefit of linker GC, but there are still opportunities for ICF.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Driver.cpp
Expand Up @@ -1370,7 +1370,7 @@ static void readConfigs(opt::InputArgList &args) {
config->passPlugins = args::getStrings(args, OPT_load_pass_plugins);

// Parse -mllvm options.
for (auto *arg : args.filtered(OPT_mllvm)) {
for (const auto *arg : args.filtered(OPT_mllvm)) {
parseClangOption(arg->getValue(), arg->getSpelling());
config->mllvmOpts.emplace_back(arg->getValue());
}
Expand Down
1 change: 1 addition & 0 deletions lld/MachO/Config.h
Expand Up @@ -209,6 +209,7 @@ struct Configuration {

SymtabPresence localSymbolsPresence = SymtabPresence::All;
SymbolPatterns localSymbolPatterns;
llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;

bool zeroModTime = true;

Expand Down
4 changes: 3 additions & 1 deletion lld/MachO/Driver.cpp
Expand Up @@ -1785,8 +1785,10 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())),
arg->getSpelling());

for (const Arg *arg : args.filtered(OPT_mllvm))
for (const Arg *arg : args.filtered(OPT_mllvm)) {
parseClangOption(arg->getValue(), arg->getSpelling());
config->mllvmOpts.emplace_back(arg->getValue());
}

createSyntheticSections();
createSyntheticSymbols();
Expand Down
2 changes: 2 additions & 0 deletions lld/MachO/LTO.cpp
Expand Up @@ -35,6 +35,8 @@ static lto::Config createConfig() {
lto::Config c;
c.Options = initTargetOptionsFromCodeGenFlags();
c.Options.EmitAddrsig = config->icfLevel == ICFLevel::safe;
for (StringRef C : config->mllvmOpts)
c.MllvmArgs.emplace_back(C.str());
c.CodeModel = getCodeModelFromCMModel();
c.CPU = getCPUStr();
c.MAttrs = getMAttrs();
Expand Down
45 changes: 45 additions & 0 deletions lld/test/COFF/lto-cache.ll
Expand Up @@ -14,6 +14,51 @@
; Two cached objects, plus a timestamp file and "foo", minus the file we removed.
; RUN: ls %t.cache | count 4

;; Check that mllvm options participate in the cache key
; RUN: rm -rf %t.cache && mkdir %t.cache
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o
; RUN: ls %t.cache | count 3
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default
; RUN: ls %t.cache | count 5

;; Adding another option resuls in 2 more cache entries
; RUN: rm -rf %t.cache && mkdir %t.cache
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o
; RUN: ls %t.cache | count 3
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default
; RUN: ls %t.cache | count 5
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default /mllvm:-max-devirt-iterations=1
; RUN: ls %t.cache | count 7

;; Changing order may matter - e.g. if overriding /mllvm:options - so we get 2 more entries
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-max-devirt-iterations=1 /mllvm:-enable-ml-inliner=default
; RUN: ls %t.cache | count 9

;; Going back to a pre-cached order doesn't create more entries.
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default /mllvm:-max-devirt-iterations=1
; RUN: ls %t.cache | count 9

;; Different flag values matter
; RUN: rm -rf %t.cache && mkdir %t.cache
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default /mllvm:-max-devirt-iterations=2
; RUN: ls %t.cache | count 3
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default /mllvm:-max-devirt-iterations=1
; RUN: ls %t.cache | count 5

;; Same flag value passed to different flags matters, and switching the order
;; of the two flags matters.
; RUN: rm -rf %t.cache && mkdir %t.cache
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default
; RUN: ls %t.cache | count 3
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-emit-dwarf-unwind=default
; RUN: ls %t.cache | count 5
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default
; RUN: ls %t.cache | count 5
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-enable-ml-inliner=default /mllvm:-emit-dwarf-unwind=default
; RUN: ls %t.cache | count 7
; RUN: lld-link /lldltocache:%t.cache /entry:main /out:%t3 %t2.o %t.o /mllvm:-emit-dwarf-unwind=default /mllvm:-enable-ml-inliner=default
; RUN: ls %t.cache | count 9

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-msvc"

Expand Down
14 changes: 14 additions & 0 deletions lld/test/ELF/lto/cache.ll
Expand Up @@ -81,6 +81,20 @@
; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o -mllvm -enable-ml-inliner=default -mllvm -max-devirt-iterations=1
; RUN: ls %t.cache | count 5

;; Same flag value passed to different flags matters, and switching the order
;; of the two flags matters.
; RUN: rm -rf %t.cache && mkdir %t.cache
; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o -mllvm -enable-ml-inliner=default
; RUN: ls %t.cache | count 3
; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o -mllvm -emit-dwarf-unwind=default
; RUN: ls %t.cache | count 5
; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o -mllvm -enable-ml-inliner=default
; RUN: ls %t.cache | count 5
; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o -mllvm -enable-ml-inliner=default -mllvm -emit-dwarf-unwind=default
; RUN: ls %t.cache | count 7
; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o -mllvm -emit-dwarf-unwind=default -mllvm -enable-ml-inliner=default
; RUN: ls %t.cache | count 9

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

Expand Down
45 changes: 45 additions & 0 deletions lld/test/MachO/lto-cache.ll
Expand Up @@ -69,6 +69,51 @@
; CHECK: llvmcache-newer
; CHECK-NOT: llvmcache-old

;; Check that mllvm options participate in the cache key
; RUN: rm -rf %t/cache && mkdir %t/cache
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o
; RUN: ls %t/cache | count 3
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default
; RUN: ls %t/cache | count 5

;; Adding another option resuls in 2 more cache entries
; RUN: rm -rf %t/cache && mkdir %t/cache
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o
; RUN: ls %t/cache | count 3
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default
; RUN: ls %t/cache | count 5
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default -mllvm -max-devirt-iterations=1
; RUN: ls %t/cache | count 7

;; Changing order may matter - e.g. if overriding -mllvm options - so we get 2 more entries
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -max-devirt-iterations=1 -mllvm -enable-ml-inliner=default
; RUN: ls %t/cache | count 9

;; Going back to a pre-cached order doesn't create more entries.
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default -mllvm -max-devirt-iterations=1
; RUN: ls %t/cache | count 9

;; Different flag values matter
; RUN: rm -rf %t/cache && mkdir %t/cache
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default -mllvm -max-devirt-iterations=2
; RUN: ls %t/cache | count 3
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default -mllvm -max-devirt-iterations=1
; RUN: ls %t/cache | count 5

;; Same flag value passed to different flags matters, and switching the order
;; of the two flags matters.
; RUN: rm -rf %t/cache && mkdir %t/cache
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default
; RUN: ls %t/cache | count 3
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -emit-dwarf-unwind=default
; RUN: ls %t/cache | count 5
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default
; RUN: ls %t/cache | count 5
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -enable-ml-inliner=default -mllvm -emit-dwarf-unwind=default
; RUN: ls %t/cache | count 7
; RUN: %lld -cache_path_lto %t/cache -o %t/test %t/foo.o %t/bar.o -mllvm -emit-dwarf-unwind=default -mllvm -enable-ml-inliner=default
; RUN: ls %t/cache | count 9

;--- foo.ll

target triple = "x86_64-apple-macosx10.15.0"
Expand Down

0 comments on commit 67d311a

Please sign in to comment.