MLIR  20.0.0git
DialectPlugin.h
Go to the documentation of this file.
1 //===- mlir/Tools/Plugins/DialectPlugin.h - Public Plugin API -------------===//
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 // This defines the public entry point for dialect plugins.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_TOOLS_PLUGINS_DIALECTPLUGIN_H
14 #define MLIR_TOOLS_PLUGINS_DIALECTPLUGIN_H
15 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/DynamicLibrary.h"
21 #include "llvm/Support/Error.h"
22 #include <cstdint>
23 #include <string>
24 
25 namespace mlir {
26 extern "C" {
27 /// Information about the plugin required to load its dialects & passes
28 ///
29 /// This struct defines the core interface for dialect plugins and is supposed
30 /// to be filled out by plugin implementors. MLIR-side users of a plugin are
31 /// expected to use the \c DialectPlugin class below to interface with it.
33  /// The API version understood by this plugin, usually
34  /// \c MLIR_PLUGIN_API_VERSION
35  uint32_t apiVersion;
36  /// A meaningful name of the plugin.
37  const char *pluginName;
38  /// The version of the plugin.
39  const char *pluginVersion;
40 
41  /// The callback for registering dialect plugin with a \c DialectRegistry
42  /// instance
44 };
45 }
46 
47 /// A loaded dialect plugin.
48 ///
49 /// An instance of this class wraps a loaded dialect plugin and gives access to
50 /// its interface defined by the \c DialectPluginLibraryInfo it exposes.
52 public:
53  /// Attempts to load a dialect plugin from a given file.
54  ///
55  /// \returns Returns an error if either the library cannot be found or loaded,
56  /// there is no public entry point, or the plugin implements the wrong API
57  /// version.
58  static llvm::Expected<DialectPlugin> load(const std::string &filename);
59 
60  /// Get the filename of the loaded plugin.
61  StringRef getFilename() const { return filename; }
62 
63  /// Get the plugin name
64  StringRef getPluginName() const { return info.pluginName; }
65 
66  /// Get the plugin version
67  StringRef getPluginVersion() const { return info.pluginVersion; }
68 
69  /// Get the plugin API version
70  uint32_t getAPIVersion() const { return info.apiVersion; }
71 
72  /// Invoke the DialectRegistry callback registration
73  void
75  info.registerDialectRegistryCallbacks(&dialectRegistry);
76  }
77 
78 private:
79  DialectPlugin(const std::string &filename,
80  const llvm::sys::DynamicLibrary &library)
81  : filename(filename), library(library), info() {}
82 
83  std::string filename;
84  llvm::sys::DynamicLibrary library;
85  DialectPluginLibraryInfo info;
86 };
87 } // namespace mlir
88 
89 /// The public entry point for a dialect plugin.
90 ///
91 /// When a plugin is loaded by the driver, it will call this entry point to
92 /// obtain information about this plugin and about how to register its dialects.
93 /// This function needs to be implemented by the plugin, see the example below:
94 ///
95 /// ```
96 /// extern "C" ::mlir::DialectPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
97 /// mlirGetDialectPluginInfo() {
98 /// return {
99 /// MLIR_PLUGIN_API_VERSION, "MyPlugin", "v0.1", [](DialectRegistry) { ... }
100 /// };
101 /// }
102 /// ```
103 extern "C" ::mlir::DialectPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
105 
106 #endif /* MLIR_TOOLS_PLUGINS_DIALECTPLUGIN_H */
::mlir::DialectPluginLibraryInfo LLVM_ATTRIBUTE_WEAK mlirGetDialectPluginInfo()
The public entry point for a dialect plugin.
A loaded dialect plugin.
Definition: DialectPlugin.h:51
static llvm::Expected< DialectPlugin > load(const std::string &filename)
Attempts to load a dialect plugin from a given file.
uint32_t getAPIVersion() const
Get the plugin API version.
Definition: DialectPlugin.h:70
StringRef getFilename() const
Get the filename of the loaded plugin.
Definition: DialectPlugin.h:61
void registerDialectRegistryCallbacks(DialectRegistry &dialectRegistry) const
Invoke the DialectRegistry callback registration.
Definition: DialectPlugin.h:74
StringRef getPluginName() const
Get the plugin name.
Definition: DialectPlugin.h:64
StringRef getPluginVersion() const
Get the plugin version.
Definition: DialectPlugin.h:67
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Include the generated interface declarations.
Information about the plugin required to load its dialects & passes.
Definition: DialectPlugin.h:32
const char * pluginName
A meaningful name of the plugin.
Definition: DialectPlugin.h:37
const char * pluginVersion
The version of the plugin.
Definition: DialectPlugin.h:39
uint32_t apiVersion
The API version understood by this plugin, usually MLIR_PLUGIN_API_VERSION.
Definition: DialectPlugin.h:35
void(* registerDialectRegistryCallbacks)(DialectRegistry *)
The callback for registering dialect plugin with a DialectRegistry instance.
Definition: DialectPlugin.h:43