xref: /llvm-project/mlir/lib/Tools/Plugins/DialectPlugin.cpp (revision 5e2afe5c665ab3ea344a9c3fb34c6b9930a9094d)
1 //===- lib/Tools/Plugins/DialectPlugin.cpp - Load Dialect Plugins ---------===//
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 "mlir/Tools/Plugins/DialectPlugin.h"
10 #include "llvm/Support/raw_ostream.h"
11 
12 #include <cstdint>
13 
14 using namespace mlir;
15 
load(const std::string & filename)16 llvm::Expected<DialectPlugin> DialectPlugin::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   DialectPlugin plugin{filename, library};
26 
27   // mlirGetDialectPluginInfo should be resolved to the definition from the
28   // plugin we are currently loading.
29   intptr_t getDetailsFn =
30       (intptr_t)library.getAddressOfSymbol("mlirGetDialectPluginInfo");
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(mlirGetDialectPluginInfo) *>(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.registerDialectRegistryCallbacks)
48     return llvm::make_error<llvm::StringError>(
49         Twine("Empty entry callback in plugin '") + filename + "'.'",
50         llvm::inconvertibleErrorCode());
51 
52   return plugin;
53 }
54