1414#define LLVM_PASSES_PASSPLUGIN_H
1515
1616#include " llvm/ADT/StringRef.h"
17+ #include " llvm/Support/CodeGen.h"
1718#include " llvm/Support/Compiler.h"
1819#include " llvm/Support/DynamicLibrary.h"
1920#include " llvm/Support/Error.h"
2021#include < cstdint>
2122#include < string>
2223
2324namespace llvm {
25+ class Module ;
2426class PassBuilder ;
27+ class TargetMachine ;
2528
2629// / \macro LLVM_PLUGIN_API_VERSION
2730// / Identifies the API version understood by this plugin.
@@ -30,14 +33,15 @@ class PassBuilder;
3033// / against that of the plugin. A mismatch is an error. The supported version
3134// / will be incremented for ABI-breaking changes to the \c PassPluginLibraryInfo
3235// / struct, i.e. when callbacks are added, removed, or reordered.
33- #define LLVM_PLUGIN_API_VERSION 1
36+ #define LLVM_PLUGIN_API_VERSION 2
3437
3538extern " C" {
3639// / Information about the plugin required to load its passes
3740// /
3841// / This struct defines the core interface for pass plugins and is supposed to
39- // / be filled out by plugin implementors. LLVM-side users of a plugin are
40- // / expected to use the \c PassPlugin class below to interface with it.
42+ // / be filled out by plugin implementors. Unused function pointers can be set to
43+ // / nullptr. LLVM-side users of a plugin are expected to use the \c PassPlugin
44+ // / class below to interface with it.
4145struct PassPluginLibraryInfo {
4246 // / The API version understood by this plugin, usually \c
4347 // / LLVM_PLUGIN_API_VERSION
@@ -49,7 +53,14 @@ struct PassPluginLibraryInfo {
4953
5054 // / The callback for registering plugin passes with a \c PassBuilder
5155 // / instance
52- void (*RegisterPassBuilderCallbacks)(PassBuilder &);
56+ void (*RegisterPassBuilderCallbacks)(PassBuilder &) = nullptr ;
57+
58+ // / Callback called before running the back-end passes on the module. The
59+ // / callback can generate code itself by writing the expected output to OS and
60+ // / returning true to prevent the default pipeline and further plugin
61+ // / callbacks from running.
62+ bool (*PreCodeGenCallback)(Module &, TargetMachine &, CodeGenFileType,
63+ raw_pwrite_stream &OS) = nullptr ;
5364};
5465}
5566
@@ -80,7 +91,17 @@ class PassPlugin {
8091
8192 // / Invoke the PassBuilder callback registration
8293 void registerPassBuilderCallbacks (PassBuilder &PB) const {
83- Info.RegisterPassBuilderCallbacks (PB);
94+ if (Info.RegisterPassBuilderCallbacks )
95+ Info.RegisterPassBuilderCallbacks (PB);
96+ }
97+
98+ // / Invoke the pre-codegen callback.
99+ bool invokePreCodeGenCallback (Module &M, TargetMachine &TM,
100+ CodeGenFileType CGFT,
101+ raw_pwrite_stream &OS) const {
102+ if (Info.PreCodeGenCallback )
103+ return Info.PreCodeGenCallback (M, TM, CGFT, OS);
104+ return false ;
84105 }
85106
86107private:
0 commit comments