[lld] [lld][COFF]Expose UnifiedLTO options (PR #69904)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 01:55:24 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-coff

Author: Zhang (Naville)

<details>
<summary>Changes</summary>

https://github.com/llvm/llvm-project/commit/ab9b3c84a588f86e7f66eeb577bea7155817ff06 added UnifiedLTO support and  related arguments to lld-ELF.
This patch attempts to do the same with lld-COFF.

I'm not familiar with LLVM's TableGen based OptParser so a second pair of eyes might be required

---
Full diff: https://github.com/llvm/llvm-project/pull/69904.diff


4 Files Affected:

- (modified) lld/COFF/Config.h (+3) 
- (modified) lld/COFF/Driver.cpp (+14) 
- (modified) lld/COFF/LTO.cpp (+1-1) 
- (modified) lld/COFF/Options.td (+3) 


``````````diff
diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 1c338cc63fa87d2..186fb17745e21e2 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -17,6 +17,7 @@
 #include "llvm/Object/COFF.h"
 #include "llvm/Support/CachePruning.h"
 #include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/LTO/LTO.h"
 #include <cstdint>
 #include <map>
 #include <set>
@@ -27,6 +28,7 @@ namespace lld::coff {
 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
 using llvm::COFF::WindowsSubsystem;
 using llvm::StringRef;
+using llvm::lto::LTO;
 class DefinedAbsolute;
 class StringChunk;
 class Symbol;
@@ -317,6 +319,7 @@ struct Configuration {
   bool writeCheckSum = false;
   EmitKind emit = EmitKind::Obj;
   bool allowDuplicateWeak = false;
+  LTO::LTOKind ltoKind;
 };
 
 } // namespace lld::coff
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 5613c2e6993a5af..2b127de80e89531 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -59,6 +59,7 @@ using namespace llvm;
 using namespace llvm::object;
 using namespace llvm::COFF;
 using namespace llvm::sys;
+using namespace llvm::lto;
 
 namespace lld::coff {
 
@@ -1886,6 +1887,19 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
         error("/opt: unknown option: " + s);
     }
   }
+  // Handle /lto
+  config->ltoKind = LTO::LTOK_Default;
+  if (auto *arg = args.getLastArg(OPT_lto)) {
+    StringRef s = arg->getValue();
+    if (s == "thin")
+      config->ltoKind = LTO::LTOK_UnifiedThin;
+    else if (s == "full")
+      config->ltoKind = LTO::LTOK_UnifiedRegular;
+    else if (s == "default")
+      config->ltoKind = LTO::LTOK_Default;
+    else
+      error("unknown LTO mode: " + s);
+  }
 
   if (!icfLevel)
     icfLevel = doGC ? ICFLevel::All : ICFLevel::None;
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 7df931911213672..c559449ae3b4ef2 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -127,7 +127,7 @@ BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
   }
 
   ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
-                                      ctx.config.ltoPartitions);
+                                      ctx.config.ltoPartitions,ctx.config.ltoKind);
 }
 
 BitcodeCompiler::~BitcodeCompiler() = default;
diff --git a/lld/COFF/Options.td b/lld/COFF/Options.td
index 977657a433dc581..6eec0cf44bb0c4f 100644
--- a/lld/COFF/Options.td
+++ b/lld/COFF/Options.td
@@ -309,6 +309,9 @@ def print_search_paths : F<"print-search-paths">;
 def show_timing : F<"time">;
 def summary : F<"summary">;
 
+// Flags for LTO
+def lto: P<"lto=","Set LTO backend">,MetaVarName<"[full,thin]">;
+
 //==============================================================================
 // The flags below do nothing. They are defined only for link.exe compatibility.
 //==============================================================================

``````````

</details>


https://github.com/llvm/llvm-project/pull/69904


More information about the llvm-commits mailing list