Author: Nuri Amari Date: 2026-03-06T10:17:01-05:00 New Revision: 23cb4e5f46026da16de2a0457a4391a1a53f62d6
URL: https://github.com/llvm/llvm-project/commit/23cb4e5f46026da16de2a0457a4391a1a53f62d6 DIFF: https://github.com/llvm/llvm-project/commit/23cb4e5f46026da16de2a0457a4391a1a53f62d6.diff LOG: Support -fpass-plugin + -fthinlto-index together (#183525) Without this change, passing -fthinlto-index causes -fpass-plugin arguments to be ignored. We want to be able to use plugins with distributed thin-lto, so add support for this. Added: clang/test/CodeGen/distributed-thin-lto/pass-plugin.ll Modified: clang/lib/CodeGen/BackendUtil.cpp clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp lld/ELF/LTO.cpp lld/MachO/LTO.cpp llvm/include/llvm/LTO/Config.h llvm/lib/LTO/LTOBackend.cpp llvm/tools/llvm-lto2/llvm-lto2.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 82396d44a35b1..754bcc5d12b9b 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1383,6 +1383,8 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex, Conf.RemarksFormat = CGOpts.OptRecordFormat; Conf.SplitDwarfFile = CGOpts.SplitDwarfFile; Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput; + for (auto &Plugin : CI.getPassPlugins()) + Conf.LoadedPassPlugins.push_back(Plugin.get()); switch (Action) { case Backend_EmitNothing: Conf.PreCodeGenModuleHook = [](size_t Task, const llvm::Module &Mod) { diff --git a/clang/test/CodeGen/distributed-thin-lto/pass-plugin.ll b/clang/test/CodeGen/distributed-thin-lto/pass-plugin.ll new file mode 100644 index 0000000000000..5bb96722bfd72 --- /dev/null +++ b/clang/test/CodeGen/distributed-thin-lto/pass-plugin.ll @@ -0,0 +1,25 @@ +; REQUIRES: x86-registered-target, plugins, llvm-examples + +;; Validate that -fpass-plugin works for distributed ThinLTO backends. + +; RUN: opt -thinlto-bc -o %t.o %s + +; RUN: llvm-lto2 run -thinlto-distributed-indexes %t.o \ +; RUN: -o %t2.index \ +; RUN: -r=%t.o,main,px + +; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \ +; RUN: -O2 -emit-obj -fthinlto-index=%t.o.thinlto.bc \ +; RUN: -fpass-plugin=%llvmshlibdir/Bye%pluginext \ +; RUN: -mllvm -wave-goodbye \ +; RUN: -o %t.native.o -x ir %t.o 2>&1 | FileCheck %s + +; CHECK: Bye: main + +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-grtev4-linux-gnu" + +define i32 @main() { +entry: + ret i32 0 +} diff --git a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp index 3ebd4ea979322..70178568f76c6 100644 --- a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp +++ b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp @@ -380,7 +380,7 @@ Expected<std::unique_ptr<lto::LTO>> createLTO(const ArgList &Args) { Conf.DefaultTriple = Triple.getTriple(); Conf.OptPipeline = Args.getLastArgValue(OPT_lto_newpm_passes, ""); - Conf.PassPlugins = PassPlugins; + Conf.PassPluginFilenames = PassPlugins; Conf.DebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager); Conf.DiagHandler = diagnosticHandler; diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp index 6f916a501a262..ff8d44e0d7f64 100644 --- a/lld/ELF/LTO.cpp +++ b/lld/ELF/LTO.cpp @@ -124,7 +124,7 @@ static lto::Config createConfig(Ctx &ctx) { c.SampleProfile = std::string(ctx.arg.ltoSampleProfile); for (StringRef pluginFn : ctx.arg.passPlugins) - c.PassPlugins.push_back(std::string(pluginFn)); + c.PassPluginFilenames.push_back(std::string(pluginFn)); c.DebugPassManager = ctx.arg.ltoDebugPassManager; c.DwoDir = std::string(ctx.arg.dwoDir); diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp index df2b0cb8b4a29..c8b7a4e797250 100644 --- a/lld/MachO/LTO.cpp +++ b/lld/MachO/LTO.cpp @@ -43,7 +43,7 @@ static lto::Config createConfig() { for (StringRef C : config->mllvmOpts) c.MllvmArgs.emplace_back(C.str()); for (StringRef pluginFn : config->passPlugins) - c.PassPlugins.push_back(std::string(pluginFn)); + c.PassPluginFilenames.push_back(std::string(pluginFn)); c.OptPipeline = std::string(config->ltoNewPmPasses); c.CodeModel = getCodeModelFromCMModel(); c.CPU = getCPUStr(); diff --git a/llvm/include/llvm/LTO/Config.h b/llvm/include/llvm/LTO/Config.h index 566a87ed1a790..2aeb902bcfccf 100644 --- a/llvm/include/llvm/LTO/Config.h +++ b/llvm/include/llvm/LTO/Config.h @@ -34,6 +34,7 @@ class Error; class Module; class ModuleSummaryIndex; class raw_pwrite_stream; +class PassPlugin; namespace lto { @@ -50,7 +51,12 @@ struct Config { TargetOptions Options; std::vector<std::string> MAttrs; std::vector<std::string> MllvmArgs; - std::vector<std::string> PassPlugins; + // 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. + std::vector<llvm::PassPlugin *> LoadedPassPlugins; + std::vector<std::string> PassPluginFilenames; /// For adding passes that run right before codegen. std::function<void(legacy::PassManager &)> PreCodeGenPassesHook; std::optional<Reloc::Model> RelocModel = Reloc::PIC_; diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index 43e5c3b398372..7ee2557a68bd5 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -199,20 +199,23 @@ Error Config::addSaveTemps(std::string OutputFileName, bool UseInputModulePath, #include "llvm/Support/Extension.def" #undef HANDLE_EXTENSION -static void RegisterPassPlugins(ArrayRef<std::string> PassPlugins, - PassBuilder &PB) { +static void RegisterPassPlugins(const Config &Conf, PassBuilder &PB) { #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 : PassPlugins) { + 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); } static std::unique_ptr<TargetMachine> @@ -292,7 +295,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM, SI.registerCallbacks(PIC, &MAM); PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC); - RegisterPassPlugins(Conf.PassPlugins, PB); + RegisterPassPlugins(Conf, PB); std::unique_ptr<TargetLibraryInfoImpl> TLII( new TargetLibraryInfoImpl(TM->getTargetTriple(), TM->Options.VecLib)); diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp index 955c1130e9f4c..95e8053dab32d 100644 --- a/llvm/tools/llvm-lto2/llvm-lto2.cpp +++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -376,7 +376,7 @@ static int run(int argc, char **argv) { Conf.OptLevel = OptLevel - '0'; Conf.Freestanding = EnableFreestanding; - llvm::append_range(Conf.PassPlugins, PassPlugins); + llvm::append_range(Conf.PassPluginFilenames, PassPlugins); if (auto Level = CodeGenOpt::parseLevel(CGOptLevel)) { Conf.CGOptLevel = *Level; } else { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
