xref: /llvm-project/mlir/include/mlir/Tools/Plugins/DialectPlugin.h (revision 5e2afe5c665ab3ea344a9c3fb34c6b9930a9094d)
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 
16 #include "mlir/IR/DialectRegistry.h"
17 #include "mlir/Tools/Plugins/PassPlugin.h"
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.
32 struct DialectPluginLibraryInfo {
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
43   void (*registerDialectRegistryCallbacks)(DialectRegistry *);
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.
51 class DialectPlugin {
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.
getFilename()61   StringRef getFilename() const { return filename; }
62 
63   /// Get the plugin name
getPluginName()64   StringRef getPluginName() const { return info.pluginName; }
65 
66   /// Get the plugin version
getPluginVersion()67   StringRef getPluginVersion() const { return info.pluginVersion; }
68 
69   /// Get the plugin API version
getAPIVersion()70   uint32_t getAPIVersion() const { return info.apiVersion; }
71 
72   /// Invoke the DialectRegistry callback registration
73   void
registerDialectRegistryCallbacks(DialectRegistry & dialectRegistry)74   registerDialectRegistryCallbacks(DialectRegistry &dialectRegistry) const {
75     info.registerDialectRegistryCallbacks(&dialectRegistry);
76   }
77 
78 private:
DialectPlugin(const std::string & filename,const llvm::sys::DynamicLibrary & library)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
104 mlirGetDialectPluginInfo();
105 
106 #endif /* MLIR_TOOLS_PLUGINS_DIALECTPLUGIN_H */
107