LLVM 24.0.0git
PassPlugin.cpp
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
10#include "llvm/ADT/DenseMap.h"
11#include "llvm/ADT/STLExtras.h"
14
15#include <cstdint>
16
17using namespace llvm;
18
20 std::string Error;
21 auto Library =
22 sys::DynamicLibrary::getPermanentLibrary(Filename.str().c_str(), &Error);
23 if (!Library.isValid())
24 return make_error<StringError>(Twine("Could not load library '") +
25 Filename + "': " + Error,
27
28 PassPlugin P{Filename.str(), Library};
29
30 // llvmGetPassPluginInfo should be resolved to the definition from the plugin
31 // we are currently loading.
32 intptr_t getDetailsFn =
33 (intptr_t)Library.getAddressOfSymbol("llvmGetPassPluginInfo");
34
35 if (!getDetailsFn)
36 // If the symbol isn't found, this is probably a legacy plugin, which is an
37 // error
38 return make_error<StringError>(Twine("Plugin entry point not found in '") +
39 Filename + "'. Is this a legacy plugin?",
41
42 P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
43
44 if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
46 Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
47 Twine(P.Info.APIVersion) + ", supported version is " +
50
51 return P;
52}
53
56 raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, bool DisableVerify,
57 bool DisableSimplifyLibCalls, const ModuleSummaryIndex *MSI,
59 if (Info.PreCodeGenCallback)
60 return Info.PreCodeGenCallback(TM, M, OS, DwoOS, CGFT, DisableVerify,
61 DisableSimplifyLibCalls, MSI,
62 std::move(VFS));
63 return false;
64}
65
69 for (auto [I, Info] : enumerate(Infos))
70 if (!Index.try_emplace(Info.PluginName, I).second)
71 return createStringError("multiple pass plugins are named '" +
72 Twine(Info.PluginName) + "'");
73 // ParseArguments takes argv-style C strings. The argument is the suffix of
74 // Arg after the first comma, so it is NUL-terminated and needs no copy.
75 SmallVector<SmallVector<const char *, 0>, 0> PluginArgs(Infos.size());
76 for (const std::string &Arg : Args) {
77 auto [Name, Rest] = StringRef(Arg).split(',');
78 // Rest is null without a comma and empty for an empty argument.
79 if (!Rest.data())
80 return createStringError("expected <plugin>,<arg> in -plugin-arg=" + Arg);
81 auto It = Index.find(Name);
82 if (It == Index.end())
83 return createStringError("no pass plugin named '" + Name +
84 "' is loaded, in -plugin-arg=" + Arg);
85 PluginArgs[It->second].push_back(Rest.data());
86 }
87 for (auto [Info, PArgs] : zip_equal(Infos, PluginArgs)) {
88 if (PArgs.empty())
89 continue;
90 if (!Info.ParseArguments)
91 return createStringError("pass plugin '" + Twine(Info.PluginName) +
92 "' does not accept arguments");
93 if (Error E = Info.ParseArguments(PArgs))
94 return E;
95 }
96 return Error::success();
97}
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
#define LLVM_PLUGIN_API_VERSION
\macro LLVM_PLUGIN_API_VERSION Identifies the API version understood by this plugin.
Definition PassPlugin.h:42
LLVM_ABI_EXPORT::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo()
The public entry point for a pass plugin.
This file contains some templates that are useful if you are working with the STL at all.
Defines the virtual file system interface vfs::FileSystem.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
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
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.
static LLVM_ABI Expected< PassPlugin > load(StringRef Filename)
Attempts to load a pass plugin from a given file.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:728
Primary interface to the complete machine description for the target machine.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
An abstract base class for streams implementations that also support a pwrite operation.
static LLVM_ABI DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=nullptr)
This function permanently loads the dynamic library at the given path using the library load operatio...
This is an optimization pass for GlobalISel generic memory operations.
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:856
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2575
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
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
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340