LLVM 22.0.0git
PassPlugin.h
Go to the documentation of this file.
1//===- llvm/Passes/PassPlugin.h - Public Plugin API -----------------------===//
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_PASSES_PASSPLUGIN_H
14#define LLVM_PASSES_PASSPLUGIN_H
15
16#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Error.h"
21#include <cstdint>
22#include <string>
23
24namespace llvm {
25class Module;
26class PassBuilder;
27class TargetMachine;
28
29/// \macro LLVM_PLUGIN_API_VERSION
30/// Identifies the API version understood by this plugin.
31///
32/// When a plugin is loaded, the driver will check it's supported plugin version
33/// against that of the plugin. A mismatch is an error. The supported version
34/// will be incremented for ABI-breaking changes to the \c PassPluginLibraryInfo
35/// struct, i.e. when callbacks are added, removed, or reordered.
36#define LLVM_PLUGIN_API_VERSION 2
37
38extern "C" {
39/// Information about the plugin required to load its passes
40///
41/// This struct defines the core interface for pass plugins and is supposed to
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.
46 /// The API version understood by this plugin, usually \c
47 /// LLVM_PLUGIN_API_VERSION
49 /// A meaningful name of the plugin.
50 const char *PluginName;
51 /// The version of the plugin.
52 const char *PluginVersion;
53
54 /// The callback for registering plugin passes with a \c PassBuilder
55 /// instance
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.
63 raw_pwrite_stream &OS) = nullptr;
64};
65}
66
67/// A loaded pass plugin.
68///
69/// An instance of this class wraps a loaded pass plugin and gives access to
70/// its interface defined by the \c PassPluginLibraryInfo it exposes.
71class PassPlugin {
72public:
73 /// Attempts to load a pass plugin from a given file.
74 ///
75 /// \returns Returns an error if either the library cannot be found or loaded,
76 /// there is no public entry point, or the plugin implements the wrong API
77 /// version.
78 LLVM_ABI static Expected<PassPlugin> Load(const std::string &Filename);
79
80 /// Get the filename of the loaded plugin.
81 StringRef getFilename() const { return Filename; }
82
83 /// Get the plugin name
84 StringRef getPluginName() const { return Info.PluginName; }
85
86 /// Get the plugin version
87 StringRef getPluginVersion() const { return Info.PluginVersion; }
88
89 /// Get the plugin API version
90 uint32_t getAPIVersion() const { return Info.APIVersion; }
91
92 /// Invoke the PassBuilder callback registration
94 if (Info.RegisterPassBuilderCallbacks)
95 Info.RegisterPassBuilderCallbacks(PB);
96 }
97
98 /// Invoke the pre-codegen callback.
100 CodeGenFileType CGFT,
101 raw_pwrite_stream &OS) const {
102 if (Info.PreCodeGenCallback)
103 return Info.PreCodeGenCallback(M, TM, CGFT, OS);
104 return false;
105 }
106
107private:
108 PassPlugin(const std::string &Filename, const sys::DynamicLibrary &Library)
109 : Filename(Filename), Library(Library), Info() {}
110
111 std::string Filename;
112 sys::DynamicLibrary Library;
113 PassPluginLibraryInfo Info;
114};
115}
116
117// The function returns a struct with default initializers.
118#ifdef __clang__
119#pragma clang diagnostic push
120#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
121#endif
122/// The public entry point for a pass plugin.
123///
124/// When a plugin is loaded by the driver, it will call this entry point to
125/// obtain information about this plugin and about how to register its passes.
126/// This function needs to be implemented by the plugin, see the example below:
127///
128/// ```
129/// extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
130/// llvmGetPassPluginInfo() {
131/// return {
132/// LLVM_PLUGIN_API_VERSION, "MyPlugin", "v0.1", [](PassBuilder &PB) { ... }
133/// };
134/// }
135/// ```
138#ifdef __clang__
139#pragma clang diagnostic pop
140#endif
141
142#endif /* LLVM_PASSES_PASSPLUGIN_H */
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_ATTRIBUTE_WEAK
Definition Compiler.h:306
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo()
The public entry point for a pass plugin.
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:67
This class provides access to building LLVM's passes.
A loaded pass plugin.
Definition PassPlugin.h:71
bool invokePreCodeGenCallback(Module &M, TargetMachine &TM, CodeGenFileType CGFT, raw_pwrite_stream &OS) const
Invoke the pre-codegen callback.
Definition PassPlugin.h:99
static LLVM_ABI Expected< PassPlugin > Load(const std::string &Filename)
Attempts to load a pass plugin from a given file.
uint32_t getAPIVersion() const
Get the plugin API version.
Definition PassPlugin.h:90
void registerPassBuilderCallbacks(PassBuilder &PB) const
Invoke the PassBuilder callback registration.
Definition PassPlugin.h:93
StringRef getPluginVersion() const
Get the plugin version.
Definition PassPlugin.h:87
StringRef getPluginName() const
Get the plugin name.
Definition PassPlugin.h:84
StringRef getFilename() const
Get the filename of the loaded plugin.
Definition PassPlugin.h:81
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:111
Information about the plugin required to load its passes.
Definition PassPlugin.h:45
const char * PluginName
A meaningful name of the plugin.
Definition PassPlugin.h:50
uint32_t APIVersion
The API version understood by this plugin, usually LLVM_PLUGIN_API_VERSION.
Definition PassPlugin.h:48
bool(* PreCodeGenCallback)(Module &, TargetMachine &, CodeGenFileType, raw_pwrite_stream &OS)
Callback called before running the back-end passes on the module.
Definition PassPlugin.h:62
void(* RegisterPassBuilderCallbacks)(PassBuilder &)
The callback for registering plugin passes with a PassBuilder instance.
Definition PassPlugin.h:56
const char * PluginVersion
The version of the plugin.
Definition PassPlugin.h:52