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#include "llvm/Plugins/PassPlugin.h"
10#include "llvm/ADT/DenseMap.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/Support/VirtualFileSystem.h"
13#include "llvm/Support/raw_ostream.h"
14
15#include <cstdint>
16
17using namespace llvm;
18
19Expected<PassPlugin> PassPlugin::load(StringRef Filename) {
20 std::string Error;
21 auto Library =
22 sys::DynamicLibrary::getPermanentLibrary(filename: Filename.str().c_str(), errMsg: &Error);
23 if (!Library.isValid())
24 return make_error<StringError>(Args: Twine("Could not load library '") +
25 Filename + "': " + Error,
26 Args: inconvertibleErrorCode());
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(symbolName: "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>(Args: Twine("Plugin entry point not found in '") +
39 Filename + "'. Is this a legacy plugin?",
40 Args: inconvertibleErrorCode());
41
42 P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
43
44 if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
45 return make_error<StringError>(
46 Args: Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
47 Twine(P.Info.APIVersion) + ", supported version is " +
48 Twine(LLVM_PLUGIN_API_VERSION) + ".",
49 Args: inconvertibleErrorCode());
50
51 return P;
52}
53
54bool PassPlugin::invokePreCodeGenCallback(
55 TargetMachine &TM, Module &M, raw_pwrite_stream &OS,
56 raw_pwrite_stream *DwoOS, CodeGenFileType CGFT, bool DisableVerify,
57 bool DisableSimplifyLibCalls, const ModuleSummaryIndex *MSI,
58 IntrusiveRefCntPtr<vfs::FileSystem> VFS) const {
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
66Error llvm::passPluginArguments(ArrayRef<PassPluginLibraryInfo> Infos,
67 ArrayRef<std::string> Args) {
68 DenseMap<StringRef, unsigned> Index;
69 for (auto [I, Info] : enumerate(First&: Infos))
70 if (!Index.try_emplace(Key: Info.PluginName, Args&: I).second)
71 return createStringError(S: "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(Separator: ',');
78 // Rest is null without a comma and empty for an empty argument.
79 if (!Rest.data())
80 return createStringError(S: "expected <plugin>,<arg> in -plugin-arg=" + Arg);
81 auto It = Index.find(Val: Name);
82 if (It == Index.end())
83 return createStringError(S: "no pass plugin named '" + Name +
84 "' is loaded, in -plugin-arg=" + Arg);
85 PluginArgs[It->second].push_back(Elt: Rest.data());
86 }
87 for (auto [Info, PArgs] : zip_equal(t&: Infos, u&: PluginArgs)) {
88 if (PArgs.empty())
89 continue;
90 if (!Info.ParseArguments)
91 return createStringError(S: "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}
98