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"
17#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Error.h"
22#include <cstdint>
23#include <string>
24
25namespace llvm {
26class Module;
27class PassBuilder;
28class TargetMachine;
29
30/// \macro LLVM_PLUGIN_API_VERSION
31/// Identifies the API version understood by this plugin.
32///
33/// When a plugin is loaded, the driver will check it's supported plugin version
34/// against that of the plugin. A mismatch is an error. The supported version
35/// will be incremented for ABI-breaking changes to the \c PassPluginLibraryInfo
36/// struct, i.e. when callbacks are added, removed, or reordered.
37#define LLVM_PLUGIN_API_VERSION 3
38
39extern "C" {
40/// Information about the plugin required to load its passes
41///
42/// This struct defines the core interface for pass plugins and is supposed to
43/// be filled out by plugin implementors. Unused function pointers can be set to
44/// nullptr. LLVM-side users of a plugin are expected to use the \c PassPlugin
45/// class below to interface with it.
47 /// The API version understood by this plugin, usually \c
48 /// LLVM_PLUGIN_API_VERSION
50 /// A meaningful name of the plugin. -plugin-arg=<PluginName>,<arg> passes
51 /// <arg> to the plugin with this name.
52 const char *PluginName;
53 /// The version of the plugin.
54 const char *PluginVersion;
55
56 /// The callback for registering plugin passes with a \c PassBuilder
57 /// instance
59
60 /// Callback called before running the back-end passes on the module. The
61 /// callback can generate code itself by writing the expected output to OS and
62 /// returning true to prevent the default pipeline and further plugin
63 /// callbacks from running.
65 raw_pwrite_stream &OS) = nullptr;
66
67 /// Callback receiving the arguments given to the plugin, e.g. by
68 /// -plugin-arg=<PluginName>,<arg>, in order.
70};
71}
72
73/// A loaded pass plugin.
74///
75/// An instance of this class wraps a loaded pass plugin and gives access to
76/// its interface defined by the \c PassPluginLibraryInfo it exposes.
77class PassPlugin {
78public:
79 /// Attempts to load a pass plugin from a given file.
80 ///
81 /// \returns Returns an error if either the library cannot be found or loaded,
82 /// there is no public entry point, or the plugin implements the wrong API
83 /// version.
85
86 /// Get the filename of the loaded plugin.
87 StringRef getFilename() const { return Filename; }
88
89 /// Get the plugin name
90 StringRef getPluginName() const { return Info.PluginName; }
91
92 /// Get the plugin version
93 StringRef getPluginVersion() const { return Info.PluginVersion; }
94
95 /// Get the plugin API version
96 uint32_t getAPIVersion() const { return Info.APIVersion; }
97
98 const PassPluginLibraryInfo &getInfo() const { return Info; }
99
100 /// Invoke the PassBuilder callback registration
102 if (Info.RegisterPassBuilderCallbacks)
103 Info.RegisterPassBuilderCallbacks(PB);
104 }
105
106 /// Invoke the pre-codegen callback.
108 CodeGenFileType CGFT,
109 raw_pwrite_stream &OS) const {
110 if (Info.PreCodeGenCallback)
111 return Info.PreCodeGenCallback(M, TM, CGFT, OS);
112 return false;
113 }
114
115private:
116 PassPlugin(const std::string &Filename, const sys::DynamicLibrary &Library)
117 : Filename(Filename), Library(Library), Info() {}
118
119 std::string Filename;
120 sys::DynamicLibrary Library;
121 PassPluginLibraryInfo Info;
122};
123
124/// Passes each "<PluginName>,<arg>" in \p Args to the \c ParseArguments
125/// callback of the extension in \p Infos with that name. Two extensions with
126/// the same name are an error.
129} // namespace llvm
130
131// The function returns a struct with default initializers.
132#ifdef __clang__
133#pragma clang diagnostic push
134#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
135#endif
136/// The public entry point for a pass plugin.
137///
138/// When a plugin is loaded by the driver, it will call this entry point to
139/// obtain information about this plugin and about how to register its passes.
140/// This function needs to be implemented by the plugin, see the example below:
141///
142/// ```
143/// extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
144/// llvmGetPassPluginInfo() {
145/// return {
146/// LLVM_PLUGIN_API_VERSION, "MyPlugin", "v0.1", [](PassBuilder &PB) { ... }
147/// };
148/// }
149/// ```
150extern "C" LLVM_ABI_NOT_EXPORTED ::llvm::PassPluginLibraryInfo
153#ifdef __clang__
154#pragma clang diagnostic pop
155#endif
156
157#endif /* LLVM_PLUGINS_PASSPLUGIN_H */
AMDGPU Mark last scratch load
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_ATTRIBUTE_WEAK
Definition Compiler.h:319
static constexpr StringLiteral Filename
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
LLVM_ABI_NOT_EXPORTED::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 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:77
bool invokePreCodeGenCallback(Module &M, TargetMachine &TM, CodeGenFileType CGFT, raw_pwrite_stream &OS) const
Invoke the pre-codegen callback.
Definition PassPlugin.h:107
uint32_t getAPIVersion() const
Get the plugin API version.
Definition PassPlugin.h:96
void registerPassBuilderCallbacks(PassBuilder &PB) const
Invoke the PassBuilder callback registration.
Definition PassPlugin.h:101
const PassPluginLibraryInfo & getInfo() const
Definition PassPlugin.h:98
StringRef getPluginVersion() const
Get the plugin version.
Definition PassPlugin.h:93
StringRef getPluginName() const
Get the plugin name.
Definition PassPlugin.h:90
StringRef getFilename() const
Get the filename of the loaded plugin.
Definition PassPlugin.h:87
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...
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:256
Information about the plugin required to load its passes.
Definition PassPlugin.h:46
const char * PluginName
A meaningful name of the plugin.
Definition PassPlugin.h:52
uint32_t APIVersion
The API version understood by this plugin, usually LLVM_PLUGIN_API_VERSION.
Definition PassPlugin.h:49
Error(* ParseArguments)(ArrayRef< const char * > Args)
Callback receiving the arguments given to the plugin, e.g.
Definition PassPlugin.h:69
bool(* PreCodeGenCallback)(Module &, TargetMachine &, CodeGenFileType, raw_pwrite_stream &OS)
Callback called before running the back-end passes on the module.
Definition PassPlugin.h:64
void(* RegisterPassBuilderCallbacks)(PassBuilder &)
The callback for registering plugin passes with a PassBuilder instance.
Definition PassPlugin.h:58
const char * PluginVersion
The version of the plugin.
Definition PassPlugin.h:54