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"
13
14#include <cstdint>
15
16using namespace llvm;
17
19 std::string Error;
20 auto Library =
21 sys::DynamicLibrary::getPermanentLibrary(Filename.str().c_str(), &Error);
22 if (!Library.isValid())
23 return make_error<StringError>(Twine("Could not load library '") +
24 Filename + "': " + Error,
26
27 PassPlugin P{Filename.str(), Library};
28
29 // llvmGetPassPluginInfo should be resolved to the definition from the plugin
30 // we are currently loading.
31 intptr_t getDetailsFn =
32 (intptr_t)Library.getAddressOfSymbol("llvmGetPassPluginInfo");
33
34 if (!getDetailsFn)
35 // If the symbol isn't found, this is probably a legacy plugin, which is an
36 // error
37 return make_error<StringError>(Twine("Plugin entry point not found in '") +
38 Filename + "'. Is this a legacy plugin?",
40
41 P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
42
43 if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
45 Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
46 Twine(P.Info.APIVersion) + ", supported version is " +
49
50 return P;
51}
52
56 for (auto [I, Info] : enumerate(Infos))
57 if (!Index.try_emplace(Info.PluginName, I).second)
58 return createStringError("multiple pass plugins are named '" +
59 Twine(Info.PluginName) + "'");
60 // ParseArguments takes argv-style C strings. The argument is the suffix of
61 // Arg after the first comma, so it is NUL-terminated and needs no copy.
62 SmallVector<SmallVector<const char *, 0>, 0> PluginArgs(Infos.size());
63 for (const std::string &Arg : Args) {
64 auto [Name, Rest] = StringRef(Arg).split(',');
65 // Rest is null without a comma and empty for an empty argument.
66 if (!Rest.data())
67 return createStringError("expected <plugin>,<arg> in -plugin-arg=" + Arg);
68 auto It = Index.find(Name);
69 if (It == Index.end())
70 return createStringError("no pass plugin named '" + Name +
71 "' is loaded, in -plugin-arg=" + Arg);
72 PluginArgs[It->second].push_back(Rest.data());
73 }
74 for (auto [Info, PArgs] : zip_equal(Infos, PluginArgs)) {
75 if (PArgs.empty())
76 continue;
77 if (!Info.ParseArguments)
78 return createStringError("pass plugin '" + Twine(Info.PluginName) +
79 "' does not accept arguments");
80 if (Error E = Info.ParseArguments(PArgs))
81 return E;
82 }
83 return Error::success();
84}
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:37
LLVM_ABI_NOT_EXPORTED::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.
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
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:736
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
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:2570
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...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340