Author: Stefan Gränitz Date: 2026-11-17T10:30:49+01:00 New Revision: 8f9d30c5879aa3a330cca7b414688926f721fc95
URL: https://github.com/llvm/llvm-project/commit/8f9d30c5879aa3a330cca7b414688926f721fc95 DIFF: https://github.com/llvm/llvm-project/commit/8f9d30c5879aa3a330cca7b414688926f721fc95.diff LOG: [Plugins] Provide PreCodeGenCallback with all runCodeGenPipeline params (#241314) Provide plugins with all the parameters that llvm::runCodeGenPipeline() takes. It lowers the bar for them to run an LLVM-based pipeline of their own. 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]> Added: llvm/test/Feature/codegen-plugin-forward.ll Modified: clang/lib/CodeGen/BackendUtil.cpp clang/test/CodeGen/codegen-plugins.c flang/lib/Frontend/FrontendActions.cpp flang/test/Driver/pass-plugin-codegen.f90 llvm/docs/ReleaseNotes.md llvm/examples/Bye/Bye.cpp llvm/examples/Ciao/Ciao.cpp llvm/include/llvm/Passes/RunCodeGen.h llvm/include/llvm/Plugins/PassPlugin.h llvm/lib/Plugins/PassPlugin.cpp llvm/test/Feature/codegen-plugin.ll llvm/tools/llc/lib/llcdriver.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index efe92ed8a83276..61864100756044 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1274,19 +1274,22 @@ 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( + *TM, *TheModule, *OS, DwoOS ? &DwoOS->os() : nullptr, CGFT, + !CodeGenOpts.VerifyModule, /*DisableSimplifyLibCalls=*/false, + /*MSI=*/nullptr, CI.getVirtualFileSystemPtr())) + 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..e586a0c29caae9 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,20 @@ 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( + tm, llvmModule, os, dwoOS ? &dwoOS->os() : nullptr, cgft, + /*DisableVerify=*/true, /*DisableSimplifyLibCalls=*/false, + /*MSI=*/nullptr, llvm::vfs::getRealFileSystem())) { + 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 9b1b92f9a82e45..4857ef3a2003f3 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -557,12 +557,16 @@ 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` 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 f201e36453055e..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) { +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. @@ -70,6 +72,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/examples/Ciao/Ciao.cpp b/llvm/examples/Ciao/Ciao.cpp index a02dc37470ae97..98e137615ad669 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/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 deb6e7d0a4cfbb..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. @@ -58,11 +63,20 @@ 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. - bool (*PreCodeGenCallback)(Module &, TargetMachine &, CodeGenFileType, - raw_pwrite_stream &OS) = nullptr; + /// 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. + /// + /// 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. @@ -103,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) const { - if (Info.PreCodeGenCallback) - return Info.PreCodeGenCallback(M, TM, CGFT, OS); - 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/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 +} 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 be9c8000ef8d6d..2ac9d36791746a 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" @@ -720,11 +721,16 @@ 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( + *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); Out->keep(); + if (DwoOut) + DwoOut->keep(); return 0; } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
