[clang] [flang] [llvm] [Plugins] Pass the .dwo stream to PreCodeGenCallback (PR #241314)

Stefan Gränitz via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 08:57:39 PST 2026


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

>From 2f51819f45a6114be2b4a58a6c11625a656ff801 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <stefan.graenitz at gmail.com>
Date: Wed, 11 Nov 2026 14:17:52 +0100
Subject: [PATCH 1/2] [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 <noreply at anthropic.com>
---
 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 f1f03716bdff5..5995751c248c4 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 1506563f49295..bf482b1965a15 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 d7f6658668a40..ed46ff6f32693 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 c7dec9d6806c8..bdc7bd0b4ec4b 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 687025c958221..d8dcd53c6f253 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -521,12 +521,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 f201e36453055..21b99cf06152d 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 deb6e7d0a4cfb..d2b22b7f9304f 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 571c7e348520e..ec1ed5e3c9312 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 d5f5c73593617..40ac4c7461ac1 100644
--- a/llvm/tools/llc/lib/llcdriver.cpp
+++ b/llvm/tools/llc/lib/llcdriver.cpp
@@ -716,11 +716,14 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList,
 
   for (auto &Plugin : PluginList) {
     CodeGenFileType CGFT = codegen::getFileType();
-    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 70d96d1a7f85ade0aba3540b59214d4d57907b9d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <stefan.graenitz at gmail.com>
Date: Thu, 12 Nov 2026 12:41:24 +0100
Subject: [PATCH 2/2] 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 5995751c248c4..2a9f22cebfaf4 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 ed46ff6f32693..e586a0c29caae 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 d8dcd53c6f253..fc72c667f18ab 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -528,13 +528,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 21b99cf06152d..ae4d114f14399 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 5ee8026765fc2..44792ed634c34 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 d2b22b7f9304f..9a62b00b78344 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 a2fa5262bb685..f7b7b67a6befb 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 40ac4c7461ac1..d92e7d6999e34 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"
@@ -716,8 +717,10 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList,
 
   for (auto &Plugin : PluginList) {
     CodeGenFileType CGFT = codegen::getFileType();
-    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);



More information about the cfe-commits mailing list