https://github.com/weliveindetail updated 
https://github.com/llvm/llvm-project/pull/231516

From 0c419fe352f9c1186e6e503137d68e44d7a6ed41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <[email protected]>
Date: Tue, 10 Nov 2026 10:16:59 +0100
Subject: [PATCH] Squash and rebase after runCodeGenPipeline() refactor

---
 clang/test/CodeGen/codegen-plugins-thinlto.c | 21 +++++++++++
 lld/test/ELF/lto/pre-codegen-plugin.ll       | 32 ++++++++++++++++
 llvm/include/llvm/LTO/Config.h               |  9 +++--
 llvm/include/llvm/LTO/LTO.h                  |  7 ++++
 llvm/lib/LTO/LTO.cpp                         | 21 +++++++++++
 llvm/lib/LTO/LTOBackend.cpp                  | 39 ++++++++++++++------
 llvm/test/Feature/codegen-plugin.ll          | 10 +++++
 7 files changed, 124 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGen/codegen-plugins-thinlto.c
 create mode 100644 lld/test/ELF/lto/pre-codegen-plugin.ll

diff --git a/clang/test/CodeGen/codegen-plugins-thinlto.c 
b/clang/test/CodeGen/codegen-plugins-thinlto.c
new file mode 100644
index 00000000000000..9f5fae5c1c65b4
--- /dev/null
+++ b/clang/test/CodeGen/codegen-plugins-thinlto.c
@@ -0,0 +1,21 @@
+// REQUIRES: plugins, llvm-examples
+// UNSUPPORTED: target={{.*windows.*}}
+// Plugins are currently broken on AIX, at least in the CI.
+// XFAIL: target={{.*}}-aix{{.*}}
+
+// RUN: %clang_cc1 -O2 -flto=thin -emit-llvm-bc %s -o %t.o
+// RUN: llvm-lto -thinlto -o %t %t.o
+// RUN: %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -S -o - 
-fpass-plugin=%llvmshlibdir/Bye%pluginext 2>&1 | FileCheck %s 
--check-prefix=CHECK-INACTIVE
+// RUN: %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -S -o - 
-fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words | FileCheck %s 
--check-prefix=CHECK-ACTIVE
+// RUN: %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -emit-llvm -o 
- -fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words | FileCheck %s 
--check-prefix=CHECK-LLVM
+// RUN: not %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -emit-obj 
-o - -fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words 2>&1 | 
FileCheck %s --check-prefix=CHECK-ERR
+
+// CHECK-INACTIVE-NOT: Bye
+// CHECK-INACTIVE: f:
+// CHECK-ACTIVE: CodeGen Bye
+// CHECK-LLVM: define{{.*}} i32 @f
+// CHECK-ERR: error: last words unsupported for binary output
+
+int f(int x) {
+  return x;
+}
diff --git a/lld/test/ELF/lto/pre-codegen-plugin.ll 
b/lld/test/ELF/lto/pre-codegen-plugin.ll
new file mode 100644
index 00000000000000..a1fc31d52424f4
--- /dev/null
+++ b/lld/test/ELF/lto/pre-codegen-plugin.ll
@@ -0,0 +1,32 @@
+; REQUIRES: x86, plugins, examples
+; UNSUPPORTED: target={{.*windows.*}}
+; Plugins are currently broken on AIX, at least in the CI.
+; XFAIL: target={{.*}}-aix{{.*}}
+
+; RUN: opt %s -o %t.o
+; RUN: ld.lld -shared --lto-emit-asm -%loadnewpmbye %t.o -o %t.inactive 2>&1 | 
count 0
+; RUN: FileCheck %s --check-prefix=INACTIVE < %t.inactive.lto.s
+; RUN: ld.lld -shared --lto-emit-asm -%loadnewpmbye -mllvm=%loadbye 
-mllvm=-last-words %t.o -o %t.active
+; RUN: FileCheck %s --check-prefix=ACTIVE < %t.active.lto.s
+; RUN: not ld.lld -shared -%loadnewpmbye -mllvm=%loadbye -mllvm=-last-words 
%t.o -o %t.err 2>&1 | FileCheck %s --check-prefix=ERR
+; RUN: ld.lld -shared --lto-emit-asm --lto-partitions=2 -%loadnewpmbye 
-mllvm=%loadbye -mllvm=-last-words %t.o -o %t.parallel
+; RUN: FileCheck %s --check-prefix=PARALLEL0 < %t.parallel.lto.s
+; RUN: FileCheck %s --check-prefix=PARALLEL1 < %t.parallel.lto.1.s
+
+;; Errors from loading plugins are reported by the linker.
+; RUN: not ld.lld -shared --load-pass-plugin=%t.dir/nonexistent.so %t.o -o 
/dev/null 2>&1 | FileCheck %s --check-prefix=LOADERR
+
+; INACTIVE-NOT: Bye
+; INACTIVE: f:
+; ACTIVE: CodeGen Bye
+; ERR: error: last words unsupported for binary output
+; PARALLEL0: CodeGen Bye
+; PARALLEL1: CodeGen Bye
+; LOADERR: error: Could not load library {{.*}}nonexistent.so
+
+target datalayout = 
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @f(i32 %x) {
+  ret i32 %x
+}
diff --git a/llvm/include/llvm/LTO/Config.h b/llvm/include/llvm/LTO/Config.h
index f3b7e8ccf0f930..bb4e41a28e32cc 100644
--- a/llvm/include/llvm/LTO/Config.h
+++ b/llvm/include/llvm/LTO/Config.h
@@ -55,11 +55,12 @@ struct Config {
   TargetOptions Options;
   std::vector<std::string> MAttrs;
   std::vector<std::string> MllvmArgs;
-  // LTO will register both lists of plugins, but
-  // if an LTO client has already loaded a set of plugins,
-  // they should register them via LoadedPassPlugins.
-  // LoadedPassPlugins is currently used by distributed thin-lto.
+  // Pass-plugins already loaded by the client. LTO does not take ownership, so
+  // they must outlive the lto::LTO object.
   std::vector<llvm::PassPlugin *> LoadedPassPlugins;
+  // Plugins that are passed by filename are loaded in lto::LTO::run(). Clients
+  // that call lto::backend() or lto::thinBackend() directly must use
+  // LoadedPassPlugins.
   std::vector<std::string> PassPluginFilenames;
   std::function<void(PassBuilder &)> PassBuilderCallback;
   std::optional<Reloc::Model> RelocModel = Reloc::PIC_;
diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h
index cea66f99b99bb0..68c8c51afd7833 100644
--- a/llvm/include/llvm/LTO/LTO.h
+++ b/llvm/include/llvm/LTO/LTO.h
@@ -26,6 +26,7 @@
 #include "llvm/IR/ModuleSummaryIndex.h"
 #include "llvm/LTO/Config.h"
 #include "llvm/Object/IRSymtab.h"
+#include "llvm/Plugins/PassPlugin.h"
 #include "llvm/Support/Caching.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/StringSaver.h"
@@ -669,6 +670,12 @@ class LLVM_ABI LTO {
   // Setup optimization remarks according to the provided configuration.
   Error setupOptimizationRemarks();
 
+  // Pass-plugins that we load from Conf.PassPluginFilenames.
+  SmallVector<std::unique_ptr<PassPlugin>, 0> PassPlugins;
+
+  // Load pass-plugins from file once.
+  Error loadPassPlugins();
+
   // LibFuncs that were implemented in bitcode but were not extracted
   // from their libraries. Such functions cannot safely be called, since
   // they have lost their opportunity to be defined.
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index a2ba9384899aa6..c36e5daac2cd21 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -1313,11 +1313,32 @@ Error LTO::checkPartiallySplit() {
   return Error::success();
 }
 
+Error LTO::loadPassPlugins() {
+  if (Conf.PassPluginFilenames.empty())
+    return Error::success();
+  assert(PassPlugins.empty() && "Pass-plugins must be loaded only once");
+
+  // Load plugins from file and append them to the list of client loaded ones.
+  for (const std::string &PluginFN : Conf.PassPluginFilenames) {
+    Expected<PassPlugin> Plugin = PassPlugin::load(PluginFN);
+    if (!Plugin)
+      return Plugin.takeError();
+    PassPlugins.push_back(std::make_unique<PassPlugin>(std::move(*Plugin)));
+    Conf.LoadedPassPlugins.push_back(PassPlugins.back().get());
+  }
+
+  Conf.PassPluginFilenames.clear();
+  return Error::success();
+}
+
 Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
   // Call the base class cleanup() explicitly since run() may be invoked on a
   // derived LTO object.
   llvm::scope_exit CleanUp([this]() { LTO::cleanup(); });
 
+  if (Error Err = loadPassPlugins())
+    return Err;
+
   // Compute "dead" symbols, we don't want to import/export these!
   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
   DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index f544da3117e70a..537b8006f65e55 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -200,23 +200,30 @@ Error Config::addSaveTemps(std::string OutputFileName, 
bool UseInputModulePath,
 #include "llvm/Support/Extension.def"
 #undef HANDLE_EXTENSION
 
+// Obtain dynamic pass-plugin infos for the given config in registration order
+static SmallVector<PassPluginLibraryInfo, 0>
+getPassPlugins(const Config &Conf) {
+  // lto::LTO loads plugins requested by file name before running backends
+  assert(Conf.PassPluginFilenames.empty() &&
+         "Pass-plugins must be loaded before running LTO backends");
+  SmallVector<PassPluginLibraryInfo, 0> Plugins;
+  for (const PassPlugin *Plugin : Conf.LoadedPassPlugins)
+    Plugins.push_back(Plugin->getInfo());
+  return Plugins;
+}
+
+// Call pass builder registration callbacks for all pass-plugins
 static void RegisterPassPlugins(const Config &Conf, PassBuilder &PB) {
+  // Static built-in plugins
 #define HANDLE_EXTENSION(Ext)                                                  
\
   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
 #include "llvm/Support/Extension.def"
 #undef HANDLE_EXTENSION
 
-  // Load requested pass plugins and let them register pass builder callbacks
-  for (auto &PluginFN : Conf.PassPluginFilenames) {
-    auto PassPlugin = PassPlugin::load(PluginFN);
-    if (!PassPlugin)
-      reportFatalUsageError(PassPlugin.takeError());
-    PassPlugin->registerPassBuilderCallbacks(PB);
-  }
-
-  // Register already loaded plugins
-  for (auto *LoadedPlugin : Conf.LoadedPassPlugins)
-    LoadedPlugin->registerPassBuilderCallbacks(PB);
+  // Dynamic plugins
+  for (const PassPluginLibraryInfo &Plugin : getPassPlugins(Conf))
+    if (Plugin.RegisterPassBuilderCallbacks)
+      Plugin.RegisterPassBuilderCallbacks(PB);
 }
 
 static std::unique_ptr<TargetMachine>
@@ -483,6 +490,16 @@ static void codegen(const Config &Conf, TargetMachine *TM,
   std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
   TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName;
 
+  // Allow plugins to handle or even take over code generation entirely.
+  for (const PassPluginLibraryInfo &Plugin : getPassPlugins(Conf)) {
+    if (Plugin.PreCodeGenCallback &&
+        Plugin.PreCodeGenCallback(Mod, *TM, Conf.CGFileType, *Stream->OS)) {
+      if (Error Err = Stream->commit())
+        reportFatalUsageError(std::move(Err));
+      return;
+    }
+  }
+
   const ModuleSummaryIndex *MSI =
       !isEmptyModule(Mod) ? &CombinedIndex : nullptr;
   if (runCodeGenPipeline(*TM, Mod, *Stream->OS,
diff --git a/llvm/test/Feature/codegen-plugin.ll 
b/llvm/test/Feature/codegen-plugin.ll
index 571c7e348520e9..bdbca8723c3afb 100644
--- a/llvm/test/Feature/codegen-plugin.ll
+++ b/llvm/test/Feature/codegen-plugin.ll
@@ -2,6 +2,15 @@
 ; RUN: llc < %s %loadnewpmbye | FileCheck %s --check-prefix=CHECK-ASM
 ; RUN: llc < %s %loadnewpmbye -plugin-arg=Bye,-last-words | FileCheck %s 
--check-prefix=CHECK-ACTIVE
 ; RUN: not llc < %s %loadnewpmbye -plugin-arg=Bye,-last-words -filetype=obj 
2>&1 | FileCheck %s --check-prefix=CHECK-ERR
+
+; LTO tools do not support -plugin-arg yet, so load Bye early to register its 
options
+; RUN: llvm-as %s -o %t.bc
+; RUN: llvm-lto2 run %t.bc -o %t.inactive -r %t.bc,somefunk,plx -r 
%t.bc,junk,plx -filetype=asm %loadnewpmbye
+; RUN: FileCheck %s --check-prefix=CHECK-ASM < %t.inactive.0
+; RUN: llvm-lto2 run %t.bc -o %t.active -r %t.bc,somefunk,plx -r 
%t.bc,junk,plx -filetype=asm %loadbye %loadnewpmbye -last-words
+; RUN: FileCheck %s --check-prefix=CHECK-ACTIVE < %t.active.0
+; RUN: not llvm-lto2 run %t.bc -o %t.err -r %t.bc,somefunk,plx -r 
%t.bc,junk,plx -filetype=obj %loadbye %loadnewpmbye -last-words 2>&1 | 
FileCheck %s --check-prefix=CHECK-LTO2-ERR
+
 ; REQUIRES: plugins, examples
 ; UNSUPPORTED: target={{.*windows.*}}
 ; Plugins are currently broken on AIX, at least in the CI.
@@ -9,6 +18,7 @@
 ; CHECK-ASM: somefunk:
 ; CHECK-ACTIVE: CodeGen Bye
 ; CHECK-ERR: error: last words unsupported for binary output
+; CHECK-LTO2-ERR: last words unsupported for binary output
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to