MLIR  20.0.0git
PassPlugin.cpp
Go to the documentation of this file.
1 //===- lib/Tools/Plugins/PassPlugin.cpp - Load Plugins for PR Passes ------===//
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/Support/raw_ostream.h"
11 
12 #include <cstdint>
13 
14 using namespace mlir;
15 
16 llvm::Expected<PassPlugin> PassPlugin::load(const std::string &filename) {
17  std::string error;
18  auto library =
19  llvm::sys::DynamicLibrary::getPermanentLibrary(filename.c_str(), &error);
20  if (!library.isValid())
21  return llvm::make_error<llvm::StringError>(
22  Twine("Could not load library '") + filename + "': " + error,
23  llvm::inconvertibleErrorCode());
24 
25  PassPlugin plugin{filename, library};
26 
27  // mlirGetPassPluginInfo should be resolved to the definition from the plugin
28  // we are currently loading.
29  intptr_t getDetailsFn =
30  (intptr_t)library.getAddressOfSymbol("mlirGetPassPluginInfo");
31 
32  if (!getDetailsFn)
33  return llvm::make_error<llvm::StringError>(
34  Twine("Plugin entry point not found in '") + filename,
35  llvm::inconvertibleErrorCode());
36 
37  plugin.info =
38  reinterpret_cast<decltype(mlirGetPassPluginInfo) *>(getDetailsFn)();
39 
40  if (plugin.info.apiVersion != MLIR_PLUGIN_API_VERSION)
41  return llvm::make_error<llvm::StringError>(
42  Twine("Wrong API version on plugin '") + filename + "'. Got version " +
43  Twine(plugin.info.apiVersion) + ", supported version is " +
44  Twine(MLIR_PLUGIN_API_VERSION) + ".",
45  llvm::inconvertibleErrorCode());
46 
47  if (!plugin.info.registerPassRegistryCallbacks)
48  return llvm::make_error<llvm::StringError>(
49  Twine("Empty entry callback in plugin '") + filename + "'.'",
50  llvm::inconvertibleErrorCode());
51 
52  return plugin;
53 }
#define MLIR_PLUGIN_API_VERSION
\macro MLIR_PLUGIN_API_VERSION Identifies the API version understood by this plugin.
Definition: PassPlugin.h:32
::mlir::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK mlirGetPassPluginInfo()
The public entry point for a pass plugin.
A loaded pass plugin.
Definition: PassPlugin.h:58
static llvm::Expected< PassPlugin > load(const std::string &filename)
Attempts to load a pass plugin from a given file.
Definition: PassPlugin.cpp:16
Include the generated interface declarations.