[lld] 67d311a - [COFF, Mach-O] Include -mllvm options in thinlto cache key

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 14 12:20:20 PST 2022


Author: Nico Weber
Date: 2022-11-14T15:18:09-05:00
New Revision: 67d311a5fd5065059754098a5d1de6a8566d1fcd

URL: https://github.com/llvm/llvm-project/commit/67d311a5fd5065059754098a5d1de6a8566d1fcd
DIFF: https://github.com/llvm/llvm-project/commit/67d311a5fd5065059754098a5d1de6a8566d1fcd.diff

LOG: [COFF, Mach-O] Include -mllvm options in thinlto cache key

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

Added: 
    

Modified: 
    lld/COFF/Config.h
    lld/COFF/Driver.cpp
    lld/COFF/LTO.cpp
    lld/ELF/Driver.cpp
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/LTO.cpp
    lld/test/COFF/lto-cache.ll
    lld/test/ELF/lto/cache.ll
    lld/test/MachO/lto-cache.ll

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 4a0938a1bb040..b49b21e773519 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -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"
@@ -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;

diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index b34d952ad2975..164af3f791fef 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -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());
 

diff  --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index cab761f333bc7..a4b7c44a7e73b 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -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.

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 271776ddd32b8..d2dd0124e047a 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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());
   }

diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index fff740e5a2661..2d5c2e16c489e 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -209,6 +209,7 @@ struct Configuration {
 
   SymtabPresence localSymbolsPresence = SymtabPresence::All;
   SymbolPatterns localSymbolPatterns;
+  llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
 
   bool zeroModTime = true;
 

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 07db434260d84..f4486343ea155 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -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();

diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index 9ec9185768bba..192a5fcd31d63 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -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();

diff  --git a/lld/test/COFF/lto-cache.ll b/lld/test/COFF/lto-cache.ll
index 559f7a9db91eb..61601c75ff905 100644
--- a/lld/test/COFF/lto-cache.ll
+++ b/lld/test/COFF/lto-cache.ll
@@ -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 
diff erent 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"
 

diff  --git a/lld/test/ELF/lto/cache.ll b/lld/test/ELF/lto/cache.ll
index 9878cd292c7af..603bfce3bc12a 100644
--- a/lld/test/ELF/lto/cache.ll
+++ b/lld/test/ELF/lto/cache.ll
@@ -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 
diff erent 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"
 

diff  --git a/lld/test/MachO/lto-cache.ll b/lld/test/MachO/lto-cache.ll
index 8f14100a90bdb..c86dd0a1bc221 100644
--- a/lld/test/MachO/lto-cache.ll
+++ b/lld/test/MachO/lto-cache.ll
@@ -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 
diff erent 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"


        


More information about the llvm-commits mailing list