LLVM 24.0.0git
PassPlugin.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This defines the public entry point for new-PM pass plugins.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_PLUGINS_PASSPLUGIN_H
14#define LLVM_PLUGINS_PASSPLUGIN_H
15
16#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include <cstdint>
24#include <string>
25
26namespace llvm {
27class Module;
29class PassBuilder;
30class TargetMachine;
31namespace vfs {
32class FileSystem;
33} // namespace vfs
34
35/// \macro LLVM_PLUGIN_API_VERSION
36/// Identifies the API version understood by this plugin.
37///
38/// When a plugin is loaded, the driver will check it's supported plugin version
39/// against that of the plugin. A mismatch is an error. The supported version
40/// will be incremented for ABI-breaking changes to the \c PassPluginLibraryInfo
41/// struct, i.e. when callbacks are added, removed, or reordered.
42#define LLVM_PLUGIN_API_VERSION 3
43
44extern "C" {
45/// Information about the plugin required to load its passes
46///
47/// This struct defines the core interface for pass plugins and is supposed to
48/// be filled out by plugin implementors. Unused function pointers can be set to
49/// nullptr. LLVM-side users of a plugin are expected to use the \c PassPlugin
50/// class below to interface with it. Hosts may invoke callbacks concurrently,
51/// e.g. for parallel ThinLTO backends, so plugins must guard any state they
52/// share between invocations.
54 /// The API version understood by this plugin, usually \c
55 /// LLVM_PLUGIN_API_VERSION
57 /// A meaningful name of the plugin. -plugin-arg=<PluginName>,<arg> passes
58 /// <arg> to the plugin with this name.
59 const char *PluginName;
60 /// The version of the plugin.
61 const char *PluginVersion;
62
63 /// The callback for registering plugin passes with a \c PassBuilder
64 /// instance
66
67 /// Callback called before running the back-end passes on the module. The
68 /// callback can generate code itself by writing the expected output to OS
69 /// and returning true to prevent the default pipeline and further plugin
70 /// callbacks from running. DwoOS behaves the same way with respect to the
71 /// return value. It is null unless the .dwo contents go to a separate file.
72 ///
73 /// The parameters match those of \c llvm::runCodeGenPipeline(), so the
74 /// callback can forward them to run an LLVM-based pipeline of its own.
75 /// Both signatures must be kept in lockstep.
76 ///
77 /// The callback might be invoked from a worker thread, so exiting inside the
78 /// callback is unsafe. Failures should be reported through the module's
79 /// context instead and return false.
82 CodeGenFileType CGFT, bool DisableVerify,
83 bool DisableSimplifyLibCalls,
84 const ModuleSummaryIndex *MSI,
86
87 /// Callback receiving the arguments given to the plugin, e.g. by
88 /// -plugin-arg=<PluginName>,<arg>, in order.
90};
91}
92
93/// A loaded pass plugin.
94///
95/// An instance of this class wraps a loaded pass plugin and gives access to
96/// its interface defined by the \c PassPluginLibraryInfo it exposes.
97class PassPlugin {
98public:
99 /// Attempts to load a pass plugin from a given file.
100 ///
101 /// \returns Returns an error if either the library cannot be found or loaded,
102 /// there is no public entry point, or the plugin implements the wrong API
103 /// version.
105
106 /// Get the filename of the loaded plugin.
107 StringRef getFilename() const { return Filename; }
108
109 /// Get the plugin name
110 StringRef getPluginName() const { return Info.PluginName; }
111
112 /// Get the plugin version
113 StringRef getPluginVersion() const { return Info.PluginVersion; }
114
115 /// Get the plugin API version
116 uint32_t getAPIVersion() const { return Info.APIVersion; }
117
118 const PassPluginLibraryInfo &getInfo() const { return Info; }
119
120 /// Invoke the PassBuilder callback registration
122 if (Info.RegisterPassBuilderCallbacks)
123 Info.RegisterPassBuilderCallbacks(PB);
124 }
125
126 /// Invoke the pre-codegen callback. Keep the signature in sync with
127 /// llvm::runCodeGenPipeline().
128 LLVM_ABI bool
131 bool DisableVerify, bool DisableSimplifyLibCalls,
132 const ModuleSummaryIndex *MSI,
134
135private:
136 PassPlugin(const std::string &Filename, const sys::DynamicLibrary &Library)
137 : Filename(Filename), Library(Library), Info() {}
138
139 std::string Filename;
140 sys::DynamicLibrary Library;
141 PassPluginLibraryInfo Info;
142};
143
144/// Passes each "<PluginName>,<arg>" in \p Args to the \c ParseArguments
145/// callback of the extension in \p Infos with that name. Two extensions with
146/// the same name are an error.
149} // namespace llvm
150
151// The function returns a struct with default initializers.
152#ifdef __clang__
153#pragma clang diagnostic push
154#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
155#endif
156/// The public entry point for a pass plugin.
157///
158/// When a plugin is loaded by the driver, it will call this entry point to
159/// obtain information about this plugin and about how to register its passes.
160/// This function needs to be implemented by the plugin, see the example below:
161///
162/// ```
163/// extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
164/// llvmGetPassPluginInfo() {
165/// return {
166/// LLVM_PLUGIN_API_VERSION, "MyPlugin", "v0.1", [](PassBuilder &PB) { ... }
167/// };
168/// }
169/// ```
170extern "C" LLVM_ABI_EXPORT ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
172#ifdef __clang__
173#pragma clang diagnostic pop
174#endif
175
176#endif /* LLVM_PLUGINS_PASSPLUGIN_H */
AMDGPU Mark last scratch load
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_ATTRIBUTE_WEAK
Definition Compiler.h:318
This file defines the RefCountedBase, ThreadSafeRefCountedBase, and IntrusiveRefCntPtr classes.
static constexpr StringLiteral Filename
PassBuilder PB(Mod->getContext(), Machine, PassOpts->PTO, std::nullopt, &PIC)
LLVM_ABI_EXPORT::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo()
The public entry point for a pass plugin.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This class provides access to building LLVM's passes.
A loaded pass plugin.
Definition PassPlugin.h:97
uint32_t getAPIVersion() const
Get the plugin API version.
Definition PassPlugin.h:116
void registerPassBuilderCallbacks(PassBuilder &PB) const
Invoke the PassBuilder callback registration.
Definition PassPlugin.h:121
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
Invoke the pre-codegen callback.
const PassPluginLibraryInfo & getInfo() const
Definition PassPlugin.h:118
StringRef getPluginVersion() const
Get the plugin version.
Definition PassPlugin.h:113
StringRef getPluginName() const
Get the plugin name.
Definition PassPlugin.h:110
StringRef getFilename() const
Get the filename of the loaded plugin.
Definition PassPlugin.h:107
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
An abstract base class for streams implementations that also support a pwrite operation.
This class provides a portable interface to dynamic libraries which also might be known as shared lib...
The virtual file system interface.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Error passPluginArguments(ArrayRef< PassPluginLibraryInfo > Infos, ArrayRef< std::string > Args)
Passes each "<PluginName>,<arg>" in Args to the ParseArguments callback of the extension in Infos wit...
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:309
Information about the plugin required to load its passes.
Definition PassPlugin.h:53
const char * PluginName
A meaningful name of the plugin.
Definition PassPlugin.h:59
uint32_t APIVersion
The API version understood by this plugin, usually LLVM_PLUGIN_API_VERSION.
Definition PassPlugin.h:56
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)
Callback called before running the back-end passes on the module.
Definition PassPlugin.h:80
Error(* ParseArguments)(ArrayRef< const char * > Args)
Callback receiving the arguments given to the plugin, e.g.
Definition PassPlugin.h:89
void(* RegisterPassBuilderCallbacks)(PassBuilder &)
The callback for registering plugin passes with a PassBuilder instance.
Definition PassPlugin.h:65
const char * PluginVersion
The version of the plugin.
Definition PassPlugin.h:61