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
14using namespace mlir;
15
16llvm::Expected<DialectPlugin> DialectPlugin::load(const std::string &filename) {
17 std::string error;
18 auto library =
19 llvm::sys::DynamicLibrary::getPermanentLibrary(filename: filename.c_str(), errMsg: &error);
20 if (!library.isValid())
21 return llvm::make_error<llvm::StringError>(
22 Args: Twine("Could not load library '") + filename + "': " + error,
23 Args: 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(symbolName: "mlirGetDialectPluginInfo");
31
32 if (!getDetailsFn)
33 return llvm::make_error<llvm::StringError>(
34 Args: Twine("Plugin entry point not found in '") + filename,
35 Args: 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 Args: Twine("Wrong API version on plugin '") + filename + "'. Got version " +
43 Twine(plugin.info.apiVersion) + ", supported version is " +
44 Twine(MLIR_PLUGIN_API_VERSION) + ".",
45 Args: llvm::inconvertibleErrorCode());
46
47 if (!plugin.info.registerDialectRegistryCallbacks)
48 return llvm::make_error<llvm::StringError>(
49 Args: Twine("Empty entry callback in plugin '") + filename + "'.'",
50 Args: llvm::inconvertibleErrorCode());
51
52 return plugin;
53}
54

source code of mlir/lib/Tools/Plugins/DialectPlugin.cpp