https://github.com/weliveindetail updated https://github.com/llvm/llvm-project/pull/241314
From bdc3c5f0e566b2d3abe89fe0b1b192e97c87b39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <[email protected]> Date: Wed, 11 Nov 2026 14:17:52 +0100 Subject: [PATCH 1/4] [Plugins] Pass the .dwo stream to PreCodeGenCallback Plugins that take over code generation in PreCodeGenCallback had no way to emit split DWARF so far. They only received the main output stream, so the .dwo contents were missing whenever a plugin generated the code. This patch adds a DwoOS parameter, which aligns the callback signature with llvm::runCodeGenPipeline(). It is null unless the .dwo contents go to a separate file. If set, it behaves like OS: then the callback returns true, the plugin wrote the .dwo contents. Otherwise another plugin or the default pipeline writes them. Hosts now open the .dwo output before invoking the plugins and keep it when a plugin takes over. The plugin API already changed in this release cycle, so LLVM_PLUGIN_API_VERSION remains at 3. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- clang/lib/CodeGen/BackendUtil.cpp | 15 ++++++++------- clang/test/CodeGen/codegen-plugins.c | 4 ++++ flang/lib/Frontend/FrontendActions.cpp | 18 ++++++++++++------ flang/test/Driver/pass-plugin-codegen.f90 | 9 +++++++++ llvm/docs/ReleaseNotes.md | 13 +++++++++++-- llvm/examples/Bye/Bye.cpp | 4 +++- llvm/include/llvm/Plugins/PassPlugin.h | 16 +++++++++------- llvm/test/Feature/codegen-plugin.ll | 4 ++++ llvm/tools/llc/lib/llcdriver.cpp | 5 ++++- 9 files changed, 64 insertions(+), 24 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index efe92ed8a83276..bf6b225e507dff 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1274,19 +1274,20 @@ void EmitAssemblyHelper::RunCodegenPipeline( // this also adds codegenerator level optimization passes. CodeGenFileType CGFT = getCodeGenFileType(Action); - // Invoke pre-codegen callback from plugin, which might want to take over the - // entire code generation itself. - for (const std::unique_ptr<llvm::PassPlugin> &Plugin : CI.getPassPlugins()) { - if (Plugin->invokePreCodeGenCallback(*TheModule, *TM, CGFT, *OS)) - return; - } - if (!CodeGenOpts.SplitDwarfOutput.empty()) { DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput); if (!DwoOS) return; } + // Invoke pre-codegen callback from plugin, which might want to take over the + // entire code generation itself. + for (const std::unique_ptr<llvm::PassPlugin> &Plugin : CI.getPassPlugins()) { + if (Plugin->invokePreCodeGenCallback(*TheModule, *TM, CGFT, *OS, + DwoOS ? &DwoOS->os() : nullptr)) + return; + } + TimeCodegenPasses([&]() { Error CodeGenError = runCodeGenPipeline(*TM, *TheModule, *OS, DwoOS ? &DwoOS->os() : nullptr, diff --git a/clang/test/CodeGen/codegen-plugins.c b/clang/test/CodeGen/codegen-plugins.c index 1506563f492953..bf482b1965a155 100644 --- a/clang/test/CodeGen/codegen-plugins.c +++ b/clang/test/CodeGen/codegen-plugins.c @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext 2>&1 | FileCheck %s --check-prefix=CHECK-INACTIVE // RUN: %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words | FileCheck %s --check-prefix=CHECK-ACTIVE +// RUN: %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words -split-dwarf-output %t.dwo | FileCheck %s --check-prefix=CHECK-ACTIVE +// RUN: FileCheck %s --check-prefix=CHECK-DWO < %t.dwo // RUN: %clang_cc1 -emit-llvm < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words | FileCheck %s --check-prefix=CHECK-LLVM // RUN: not %clang_cc1 -emit-obj < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -mllvm -last-words 2>&1 | FileCheck %s --check-prefix=CHECK-ERR // REQUIRES: plugins, llvm-examples @@ -8,6 +10,8 @@ // XFAIL: target={{.*}}-aix{{.*}} // CHECK-INACTIVE-NOT: Bye // CHECK-ACTIVE: CodeGen Bye +// CHECK-ACTIVE-NOT: DwoGen +// CHECK-DWO: DwoGen Bye // CHECK-LLVM: define{{.*}} i32 @f // CHECK-ERR: error: last words unsupported for binary output diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index d7f6658668a40c..ed46ff6f326936 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -934,12 +934,6 @@ static void generateMachineCodeOrAssemblyImpl( ? llvm::CodeGenFileType::AssemblyFile : llvm::CodeGenFileType::ObjectFile; - // Invoke pre-codegen callback from plugin, which might want to take over the - // entire code generation itself. - for (const std::unique_ptr<llvm::PassPlugin> &plugin : ci.getPassPlugins()) - if (plugin->invokePreCodeGenCallback(llvmModule, tm, cgft, os)) - return; - std::unique_ptr<llvm::ToolOutputFile> dwoOS; if (!codeGenOpts.SplitDwarfOutput.empty()) { std::error_code ec; @@ -951,6 +945,18 @@ static void generateMachineCodeOrAssemblyImpl( return; } } + + // Invoke pre-codegen callback from plugin, which might want to take over the + // entire code generation itself. + for (const std::unique_ptr<llvm::PassPlugin> &plugin : ci.getPassPlugins()) { + if (plugin->invokePreCodeGenCallback(llvmModule, tm, cgft, os, + dwoOS ? &dwoOS->os() : nullptr)) { + if (dwoOS) + dwoOS->keep(); + return; + } + } + llvm::Error codeGenError = runCodeGenPipeline( tm, llvmModule, os, dwoOS ? &dwoOS->os() : nullptr, cgft); if (codeGenError) { diff --git a/flang/test/Driver/pass-plugin-codegen.f90 b/flang/test/Driver/pass-plugin-codegen.f90 index c7dec9d6806c82..bdc7bd0b4ec4b0 100644 --- a/flang/test/Driver/pass-plugin-codegen.f90 +++ b/flang/test/Driver/pass-plugin-codegen.f90 @@ -17,6 +17,13 @@ ! RUN: %flang_fc1 -S %s -o - %loadbye -mllvm -last-words \ ! RUN: | FileCheck %s --check-prefix=CHECK-ACTIVE +! With split DWARF output to a separate file, the pass receives the .dwo stream +! and writes to it as well. +! RUN: %flang_fc1 -S %s -o - %loadbye -mllvm -last-words \ +! RUN: -debug-info-kind=standalone -split-dwarf-file %t.dwo \ +! RUN: -split-dwarf-output %t.dwo | FileCheck %s --check-prefix=CHECK-ACTIVE +! RUN: FileCheck %s --check-prefix=CHECK-DWO < %t.dwo + ! When emitting LLVM IR, no back-end is executed and therefore no ! pre-codegen hook of LLVM pass plugins are executed. ! RUN: %flang_fc1 -emit-llvm %s -o - %loadbye -mllvm -last-words \ @@ -30,6 +37,8 @@ ! CHECK-INACTIVE-NOT: Bye ! CHECK-INACTIVE: empty_: ! CHECK-ACTIVE: CodeGen Bye +! CHECK-ACTIVE-NOT: DwoGen +! CHECK-DWO: DwoGen Bye ! CHECK-LLVM-NOT: Bye ! CHECK-LLVM: define{{.*}} void @empty_ ! CHECK-ERR: error: last words unsupported for binary output diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md index 40bc91021ce5ce..7787eb710b4817 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -551,12 +551,21 @@ Makes programs 10x faster by doing Special New Thing. * `llvm-mc` now supports `--verify[=<prefixes>]` to check diagnostics against `<prefix>-<kind> {{...}}` comments in the input instead of printing them. +* `opt` and `llc` load `-load-pass-plugin` plugins after parsing the command line, so a loaded plugin's options are no longer accepted as ordinary options. + Pass them with `-plugin-arg=<plugin>,<arg>` (see below). + * `opt` and `llc` accept `-plugin-arg=<plugin>,<arg>`, which passes `<arg>` to the new `PassPluginLibraryInfo::ParseArguments` callback of the pass plugin named `<plugin>`. A plugin that defines `cl::opt` has to call `cl::ParseCommandLineOptions` itself inside `ParseArguments`. `LLVM_PLUGIN_API_VERSION` is now 3. -* `opt` and `llc` load `-load-pass-plugin` plugins after parsing the command line, so a loaded plugin's options are no longer accepted as ordinary options. - Pass them with `-plugin-arg=<plugin>,<arg>`. +* `PassPluginLibraryInfo::PreCodeGenCallback` receives the output stream for + split DWARF as an additional `raw_pwrite_stream *DwoOS` parameter, which + aligns its signature with the new `llvm::runCodeGenPipeline()`. + `DwoOS` is null unless the `.dwo` contents go to a separate file. + If set, it behaves like `OS` with respect to the result of + `invokePreCodeGenCallback()`: returning true means that the plugin wrote the + `.dwo` contents, otherwise another plugin or the default pipeline writes them. + This is also covered by the `LLVM_PLUGIN_API_VERSION` bump to 3. * llvm-offload-binary can now compress packaged binaries using zstd or zlib. diff --git a/llvm/examples/Bye/Bye.cpp b/llvm/examples/Bye/Bye.cpp index f201e36453055e..21b99cf06152d6 100644 --- a/llvm/examples/Bye/Bye.cpp +++ b/llvm/examples/Bye/Bye.cpp @@ -62,7 +62,7 @@ void registerPassBuilderCallbacks(PassBuilder &PB) { } bool preCodeGenCallback(Module &M, TargetMachine &, CodeGenFileType CGFT, - raw_pwrite_stream &OS) { + raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS) { if (LastWords) { if (CGFT != CodeGenFileType::AssemblyFile) { // Test error emission. @@ -70,6 +70,8 @@ bool preCodeGenCallback(Module &M, TargetMachine &, CodeGenFileType CGFT, return false; } OS << "CodeGen Bye\n"; + if (DwoOS) + *DwoOS << "DwoGen Bye\n"; return true; // Suppress remaining compilation pipeline. } // Do nothing. diff --git a/llvm/include/llvm/Plugins/PassPlugin.h b/llvm/include/llvm/Plugins/PassPlugin.h index deb6e7d0a4cfbb..d2b22b7f9304ff 100644 --- a/llvm/include/llvm/Plugins/PassPlugin.h +++ b/llvm/include/llvm/Plugins/PassPlugin.h @@ -58,11 +58,13 @@ struct PassPluginLibraryInfo { void (*RegisterPassBuilderCallbacks)(PassBuilder &) = nullptr; /// Callback called before running the back-end passes on the module. The - /// callback can generate code itself by writing the expected output to OS and - /// returning true to prevent the default pipeline and further plugin - /// callbacks from running. + /// callback can generate code itself by writing the expected output to OS + /// and returning true to prevent the default pipeline and further plugin + /// callbacks from running. DwoOS behaves the same way with respect to the + /// return value. It is null unless the .dwo contents go to a separate file. bool (*PreCodeGenCallback)(Module &, TargetMachine &, CodeGenFileType, - raw_pwrite_stream &OS) = nullptr; + raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS) = nullptr; /// Callback receiving the arguments given to the plugin, e.g. by /// -plugin-arg=<PluginName>,<arg>, in order. @@ -105,10 +107,10 @@ class PassPlugin { /// Invoke the pre-codegen callback. bool invokePreCodeGenCallback(Module &M, TargetMachine &TM, - CodeGenFileType CGFT, - raw_pwrite_stream &OS) const { + CodeGenFileType CGFT, raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS) const { if (Info.PreCodeGenCallback) - return Info.PreCodeGenCallback(M, TM, CGFT, OS); + return Info.PreCodeGenCallback(M, TM, CGFT, OS, DwoOS); return false; } diff --git a/llvm/test/Feature/codegen-plugin.ll b/llvm/test/Feature/codegen-plugin.ll index 571c7e348520e9..ec1ed5e3c93122 100644 --- a/llvm/test/Feature/codegen-plugin.ll +++ b/llvm/test/Feature/codegen-plugin.ll @@ -1,6 +1,8 @@ ; REQUIRES: x86-registered-target ; 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: llc < %s %loadnewpmbye -plugin-arg=Bye,-last-words -split-dwarf-output=%t.dwo | FileCheck %s --check-prefix=CHECK-ACTIVE +; RUN: FileCheck %s --check-prefix=CHECK-DWO < %t.dwo ; RUN: not llc < %s %loadnewpmbye -plugin-arg=Bye,-last-words -filetype=obj 2>&1 | FileCheck %s --check-prefix=CHECK-ERR ; REQUIRES: plugins, examples ; UNSUPPORTED: target={{.*windows.*}} @@ -8,6 +10,8 @@ ; XFAIL: target={{.*}}-aix{{.*}} ; CHECK-ASM: somefunk: ; CHECK-ACTIVE: CodeGen Bye +; CHECK-ACTIVE-NOT: DwoGen +; CHECK-DWO: DwoGen Bye ; CHECK-ERR: error: last words unsupported for binary output target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/llvm/tools/llc/lib/llcdriver.cpp b/llvm/tools/llc/lib/llcdriver.cpp index 7a8d7a210ca5a6..e3dd6b3b6859c4 100644 --- a/llvm/tools/llc/lib/llcdriver.cpp +++ b/llvm/tools/llc/lib/llcdriver.cpp @@ -724,11 +724,14 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList, for (auto &Plugin : PluginList) { CodeGenFileType CGFT = codegen::getFileType(Context.getLibraryOptions()); - if (Plugin.invokePreCodeGenCallback(*M, *Target, CGFT, Out->os())) { + if (Plugin.invokePreCodeGenCallback(*M, *Target, CGFT, Out->os(), + DwoOut ? &DwoOut->os() : nullptr)) { // TODO: Deduplicate code with below and the NewPMDriver. if (Context.getDiagHandlerPtr()->HasErrors) exit(1); Out->keep(); + if (DwoOut) + DwoOut->keep(); return 0; } } From 3d503a63b54af276f7864d48d99f05bd5fdeb4e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <[email protected]> Date: Thu, 12 Nov 2026 12:41:24 +0100 Subject: [PATCH 2/4] Match the full parameter list of runCodeGenPipeline() --- clang/lib/CodeGen/BackendUtil.cpp | 6 +++-- flang/lib/Frontend/FrontendActions.cpp | 6 +++-- llvm/docs/ReleaseNotes.md | 9 ++----- llvm/examples/Bye/Bye.cpp | 6 +++-- llvm/include/llvm/Passes/RunCodeGen.h | 1 + llvm/include/llvm/Plugins/PassPlugin.h | 34 +++++++++++++++++--------- llvm/lib/Plugins/PassPlugin.cpp | 13 ++++++++++ llvm/tools/llc/lib/llcdriver.cpp | 7 ++++-- 8 files changed, 56 insertions(+), 26 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index bf6b225e507dff..61864100756044 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1283,8 +1283,10 @@ void EmitAssemblyHelper::RunCodegenPipeline( // Invoke pre-codegen callback from plugin, which might want to take over the // entire code generation itself. for (const std::unique_ptr<llvm::PassPlugin> &Plugin : CI.getPassPlugins()) { - if (Plugin->invokePreCodeGenCallback(*TheModule, *TM, CGFT, *OS, - DwoOS ? &DwoOS->os() : nullptr)) + if (Plugin->invokePreCodeGenCallback( + *TM, *TheModule, *OS, DwoOS ? &DwoOS->os() : nullptr, CGFT, + !CodeGenOpts.VerifyModule, /*DisableSimplifyLibCalls=*/false, + /*MSI=*/nullptr, CI.getVirtualFileSystemPtr())) return; } diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index ed46ff6f326936..e586a0c29caae9 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -949,8 +949,10 @@ static void generateMachineCodeOrAssemblyImpl( // Invoke pre-codegen callback from plugin, which might want to take over the // entire code generation itself. for (const std::unique_ptr<llvm::PassPlugin> &plugin : ci.getPassPlugins()) { - if (plugin->invokePreCodeGenCallback(llvmModule, tm, cgft, os, - dwoOS ? &dwoOS->os() : nullptr)) { + if (plugin->invokePreCodeGenCallback( + tm, llvmModule, os, dwoOS ? &dwoOS->os() : nullptr, cgft, + /*DisableVerify=*/true, /*DisableSimplifyLibCalls=*/false, + /*MSI=*/nullptr, llvm::vfs::getRealFileSystem())) { if (dwoOS) dwoOS->keep(); return; diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md index 7787eb710b4817..2f643dbfc2cf9e 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -558,13 +558,8 @@ Makes programs 10x faster by doing Special New Thing. A plugin that defines `cl::opt` has to call `cl::ParseCommandLineOptions` itself inside `ParseArguments`. `LLVM_PLUGIN_API_VERSION` is now 3. -* `PassPluginLibraryInfo::PreCodeGenCallback` receives the output stream for - split DWARF as an additional `raw_pwrite_stream *DwoOS` parameter, which - aligns its signature with the new `llvm::runCodeGenPipeline()`. - `DwoOS` is null unless the `.dwo` contents go to a separate file. - If set, it behaves like `OS` with respect to the result of - `invokePreCodeGenCallback()`: returning true means that the plugin wrote the - `.dwo` contents, otherwise another plugin or the default pipeline writes them. +* `PassPluginLibraryInfo::PreCodeGenCallback` now matches the parameters of the + new `llvm::runCodeGenPipeline()`. This is also covered by the `LLVM_PLUGIN_API_VERSION` bump to 3. * llvm-offload-binary can now compress packaged binaries using zstd or zlib. diff --git a/llvm/examples/Bye/Bye.cpp b/llvm/examples/Bye/Bye.cpp index 21b99cf06152d6..ae4d114f14399b 100644 --- a/llvm/examples/Bye/Bye.cpp +++ b/llvm/examples/Bye/Bye.cpp @@ -61,8 +61,10 @@ void registerPassBuilderCallbacks(PassBuilder &PB) { }); } -bool preCodeGenCallback(Module &M, TargetMachine &, CodeGenFileType CGFT, - raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS) { +bool preCodeGenCallback(TargetMachine &, Module &M, raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, bool, + bool, const ModuleSummaryIndex *, + IntrusiveRefCntPtr<vfs::FileSystem>) { if (LastWords) { if (CGFT != CodeGenFileType::AssemblyFile) { // Test error emission. diff --git a/llvm/include/llvm/Passes/RunCodeGen.h b/llvm/include/llvm/Passes/RunCodeGen.h index 5ee8026765fc2c..44792ed634c340 100644 --- a/llvm/include/llvm/Passes/RunCodeGen.h +++ b/llvm/include/llvm/Passes/RunCodeGen.h @@ -17,6 +17,7 @@ namespace llvm { +// PassPlugin::invokePreCodeGenCallback() wants to match the parameter list LLVM_ABI Error runCodeGenPipeline( TargetMachine &TM, Module &M, raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, bool DisableVerify = true, diff --git a/llvm/include/llvm/Plugins/PassPlugin.h b/llvm/include/llvm/Plugins/PassPlugin.h index d2b22b7f9304ff..9a62b00b783445 100644 --- a/llvm/include/llvm/Plugins/PassPlugin.h +++ b/llvm/include/llvm/Plugins/PassPlugin.h @@ -14,6 +14,7 @@ #define LLVM_PLUGINS_PASSPLUGIN_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/Compiler.h" @@ -24,8 +25,12 @@ namespace llvm { class Module; +class ModuleSummaryIndex; class PassBuilder; class TargetMachine; +namespace vfs { +class FileSystem; +} // namespace vfs /// \macro LLVM_PLUGIN_API_VERSION /// Identifies the API version understood by this plugin. @@ -62,9 +67,16 @@ struct PassPluginLibraryInfo { /// and returning true to prevent the default pipeline and further plugin /// callbacks from running. DwoOS behaves the same way with respect to the /// return value. It is null unless the .dwo contents go to a separate file. - bool (*PreCodeGenCallback)(Module &, TargetMachine &, CodeGenFileType, - raw_pwrite_stream &OS, - raw_pwrite_stream *DwoOS) = nullptr; + /// + /// The parameters match those of \c llvm::runCodeGenPipeline(), so the + /// callback can forward them to run an LLVM-based pipeline of its own. + /// Both signatures must be kept in lockstep. + bool (*PreCodeGenCallback)(TargetMachine &TM, Module &M, + raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS, + CodeGenFileType CGFT, bool DisableVerify, + bool DisableSimplifyLibCalls, + const ModuleSummaryIndex *MSI, + IntrusiveRefCntPtr<vfs::FileSystem> VFS) = nullptr; /// Callback receiving the arguments given to the plugin, e.g. by /// -plugin-arg=<PluginName>,<arg>, in order. @@ -105,14 +117,14 @@ class PassPlugin { Info.RegisterPassBuilderCallbacks(PB); } - /// Invoke the pre-codegen callback. - bool invokePreCodeGenCallback(Module &M, TargetMachine &TM, - CodeGenFileType CGFT, raw_pwrite_stream &OS, - raw_pwrite_stream *DwoOS) const { - if (Info.PreCodeGenCallback) - return Info.PreCodeGenCallback(M, TM, CGFT, OS, DwoOS); - return false; - } + /// Invoke the pre-codegen callback. Keep the signature in sync with + /// llvm::runCodeGenPipeline(). + LLVM_ABI bool + invokePreCodeGenCallback(TargetMachine &TM, Module &M, raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, + bool DisableVerify, bool DisableSimplifyLibCalls, + const ModuleSummaryIndex *MSI, + IntrusiveRefCntPtr<vfs::FileSystem> VFS) const; private: PassPlugin(const std::string &Filename, const sys::DynamicLibrary &Library) diff --git a/llvm/lib/Plugins/PassPlugin.cpp b/llvm/lib/Plugins/PassPlugin.cpp index a2fa5262bb685b..f7b7b67a6befbd 100644 --- a/llvm/lib/Plugins/PassPlugin.cpp +++ b/llvm/lib/Plugins/PassPlugin.cpp @@ -9,6 +9,7 @@ #include "llvm/Plugins/PassPlugin.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/raw_ostream.h" #include <cstdint> @@ -50,6 +51,18 @@ Expected<PassPlugin> PassPlugin::load(StringRef Filename) { return P; } +bool PassPlugin::invokePreCodeGenCallback( + TargetMachine &TM, Module &M, raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, bool DisableVerify, + bool DisableSimplifyLibCalls, const ModuleSummaryIndex *MSI, + IntrusiveRefCntPtr<vfs::FileSystem> VFS) const { + if (Info.PreCodeGenCallback) + return Info.PreCodeGenCallback(TM, M, OS, DwoOS, CGFT, DisableVerify, + DisableSimplifyLibCalls, MSI, + std::move(VFS)); + return false; +} + Error llvm::passPluginArguments(ArrayRef<PassPluginLibraryInfo> Infos, ArrayRef<std::string> Args) { DenseMap<StringRef, unsigned> Index; diff --git a/llvm/tools/llc/lib/llcdriver.cpp b/llvm/tools/llc/lib/llcdriver.cpp index e3dd6b3b6859c4..a32e00195932cb 100644 --- a/llvm/tools/llc/lib/llcdriver.cpp +++ b/llvm/tools/llc/lib/llcdriver.cpp @@ -50,6 +50,7 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Support/TimeProfiler.h" #include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/WithColor.h" #include "llvm/Target/TargetMachine.h" #include "llvm/TargetParser/Host.h" @@ -724,8 +725,10 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList, for (auto &Plugin : PluginList) { CodeGenFileType CGFT = codegen::getFileType(Context.getLibraryOptions()); - if (Plugin.invokePreCodeGenCallback(*M, *Target, CGFT, Out->os(), - DwoOut ? &DwoOut->os() : nullptr)) { + if (Plugin.invokePreCodeGenCallback( + *Target, *M, Out->os(), DwoOut ? &DwoOut->os() : nullptr, CGFT, + NoVerify, DisableSimplifyLibCalls, /*MSI=*/nullptr, + vfs::getRealFileSystem())) { // TODO: Deduplicate code with below and the NewPMDriver. if (Context.getDiagHandlerPtr()->HasErrors) exit(1); From 4e1e48a23418bf37a54de733fc56abb56557f358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <[email protected]> Date: Thu, 12 Nov 2026 18:22:01 +0100 Subject: [PATCH 3/4] Test forwarding PreCodeGenCallback to runCodeGenPipeline Extend Ciao with a PreCodeGenCallback that takes over code generation and forwards it to runCodeGenPipeline() when passed -plugin-arg=Ciao,forward-codegen. This is how plugins that run an LLVM-based pipeline of their own hook into llc. The new codegen-plugin-forward.ll test covers it, while codegen-plugin-legacy.ll keeps covering the legacy registration path only. Static builds of llc don't link runCodeGenPipeline(), so the new test requires the LLVM dylib. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- llvm/examples/Ciao/Ciao.cpp | 41 +++++++++++++++++++-- llvm/test/Feature/codegen-plugin-forward.ll | 17 +++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 llvm/test/Feature/codegen-plugin-forward.ll diff --git a/llvm/examples/Ciao/Ciao.cpp b/llvm/examples/Ciao/Ciao.cpp index a02dc37470ae97..1ebcdb856e9a4b 100644 --- a/llvm/examples/Ciao/Ciao.cpp +++ b/llvm/examples/Ciao/Ciao.cpp @@ -9,6 +9,9 @@ #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/TargetPassConfig.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Passes/RunCodeGen.h" #include "llvm/Plugins/PassPlugin.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/RegisterTargetPassConfigCallback.h" @@ -41,9 +44,41 @@ static RegisterTargetPassConfigCallback RegisterLegacyCiao{ TPC->insertPass(&GCLoweringID, &LegacyCiao::ID); }}; -// We don't use the pass-plugin interface (yet), but -load-pass-plugin requires -// the entry point. +namespace { +bool ForwardCodeGen = false; + +// Take over code generation and forward it to the default pipeline. Plugins +// that run an LLVM-based pipeline of their own work this way. +bool preCodeGenCallback(TargetMachine &TM, Module &M, raw_pwrite_stream &OS, + raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, + bool DisableVerify, bool DisableSimplifyLibCalls, + const ModuleSummaryIndex *MSI, + IntrusiveRefCntPtr<vfs::FileSystem> VFS) { + if (!ForwardCodeGen) + return false; + // Leave a trace in the output that tests can check for. + for (Function &F : M) + if (!F.isDeclaration()) + F.setName("ciao_" + F.getName()); + if (Error Err = + runCodeGenPipeline(TM, M, OS, DwoOS, CGFT, DisableVerify, + DisableSimplifyLibCalls, MSI, std::move(VFS))) + M.getContext().emitError(toString(std::move(Err))); + return true; +} + +Error parseArguments(ArrayRef<const char *> Args) { + for (StringRef Arg : Args) + if (Arg == "forward-codegen") + ForwardCodeGen = true; + return Error::success(); +} +} // namespace + +// We use the pass-plugin for preCodeGenCallback and argument parsing. The +// legacy codegen pipeline relies on the static initialization. extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() { - return {LLVM_PLUGIN_API_VERSION, "Ciao", LLVM_VERSION_STRING}; + return {LLVM_PLUGIN_API_VERSION, "Ciao", LLVM_VERSION_STRING, nullptr, + preCodeGenCallback, parseArguments}; } diff --git a/llvm/test/Feature/codegen-plugin-forward.ll b/llvm/test/Feature/codegen-plugin-forward.ll new file mode 100644 index 00000000000000..530b26a21450cf --- /dev/null +++ b/llvm/test/Feature/codegen-plugin-forward.ll @@ -0,0 +1,17 @@ +; Check that we can forward PreCodeGenCallback to llvm::runCodeGenPipeline() +; REQUIRES: system-linux, plugins, examples, x86-registered-target + +; Resolve runCodeGenPipeline() from libLLVM, since llc doesn't use it yet +; REQUIRES: llvm-dylib + +; The plugin prefixes functions with ciao_ and forwards to runCodeGenPipeline() +; RUN: llc %s -o %t.o -mtriple=x86_64 -filetype=obj \ +; RUN: -load-pass-plugin %llvmshlibdir/Ciao%pluginext \ +; RUN: -plugin-arg=Ciao,forward-codegen +; RUN: llvm-nm -j %t.o | FileCheck %s + +; CHECK: {{^}}ciao_main{{$}} + +define void @main() { + ret void +} From 225908911befcfc2de4be5cf1df50942ea215c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <[email protected]> Date: Sat, 14 Nov 2026 18:00:34 +0100 Subject: [PATCH 4/4] Pending clang-format --- llvm/examples/Ciao/Ciao.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/examples/Ciao/Ciao.cpp b/llvm/examples/Ciao/Ciao.cpp index 1ebcdb856e9a4b..98e137615ad669 100644 --- a/llvm/examples/Ciao/Ciao.cpp +++ b/llvm/examples/Ciao/Ciao.cpp @@ -79,6 +79,6 @@ Error parseArguments(ArrayRef<const char *> Args) { // legacy codegen pipeline relies on the static initialization. extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() { - return {LLVM_PLUGIN_API_VERSION, "Ciao", LLVM_VERSION_STRING, nullptr, - preCodeGenCallback, parseArguments}; + return {LLVM_PLUGIN_API_VERSION, "Ciao", LLVM_VERSION_STRING, nullptr, + preCodeGenCallback, parseArguments}; } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
