MimIR: include/mim/plugin.h Source File
MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
plugin.h
Go to the documentation of this file.
1#pragma once
2
3#include <compare>
4
5#include <fstream>
6#include <functional>
7#include <initializer_list>
8#include <iostream>
9#include <memory>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <tuple>
14
15#include <ankerl/unordered_dense.h>
16
17#include "mim/config.h"
18#include "mim/def.h"
19
20namespace mim {
21
22class Driver;
23class Phase;
24
25/// @name Plugin Interface
26///@{
27using Normalizers = ankerl::unordered_dense::map<flags_t, NormalizeFn>;
28
29/// Maps an axiom of a Phase to a function that creates one.
30using Flags2Phases = ankerl::unordered_dense::map<flags_t, std::function<std::unique_ptr<Phase>(World&)>>;
31
32/// One `-X <plugin>:<arg>` a Plugin understands; see @ref clipluginargs.
33/// A Plugin declares these next to the code that picks them apart, so that `mim -p <plugin> -h` can list them.
34struct PluginArg {
35 const char* syntax; ///< How to spell the argument, e.g. `"o=<file>, output=<file>"`.
36 const char* descr; ///< What it does; one sentence of Markdown, but no links: the terminal help prints it as is.
37};
38
39/// One environment variable a Plugin reads; see @ref clipluginenv.
40/// A Plugin declares these next to the code that reads them, so that `mim -p <plugin> -h` can list them.
41struct PluginEnv {
42 const char* name; ///< Name of the variable, e.g. `"CUDA_HOME"`.
43 const char* descr; ///< What it does; one sentence of Markdown, but no links: the terminal help prints it as is.
44};
45
46/// One symbol a Plugin offers to other Plugin%s; see Driver::get_fun_ptr.
47/// A statically linked Plugin has no shared object to look it up in.
48struct PluginSym {
49 const char* name; ///< Name of the symbol, e.g. `"mim_ll_convert"`.
50 void* ptr; ///< Address of the symbol.
51};
52
53/// Builds a PluginSym for @p f, so name and address cannot drift apart.
54#define MIM_PLUGIN_SYM(f) \
55 PluginSym { #f, (void*)&f }
56///@}
57
58/// @name Plugin Argument Lookup
59/// Picks the `-X <plugin>:<arg>` strings of Driver::args / Phase::args apart.
60/// Each helper matches any of @p keys - `arg_value(args(), "o", "output")` - and the last occurrence wins.
61///@{
62/// Value of `<key>=<value>`; `std::nullopt` if none of @p keys carries one.
63template<class... Keys>
64std::optional<std::string_view> arg_value(fe::View<std::string> args, Keys... keys) {
65 std::optional<std::string_view> res;
66 for (std::string_view arg : args)
67 for (std::string_view key : {std::string_view(keys)...})
68 if (arg.starts_with(key) && arg.size() > key.size() + 1 && arg[key.size()] == '=')
69 res = arg.substr(key.size() + 1);
70 return res;
71}
72
73/// An @p on key ↦ `true`, an @p off key ↦ `false`; `std::nullopt` if neither occurs.
74inline std::optional<bool> arg_bool(fe::View<std::string> args,
75 std::initializer_list<std::string_view> on,
76 std::initializer_list<std::string_view> off) {
77 std::optional<bool> res;
78 for (std::string_view arg : args) {
79 for (auto key : on)
80 if (arg == key) res = true;
81 for (auto key : off)
82 if (arg == key) res = false;
83 }
84 return res;
85}
86
87/// Whether any of @p keys occurs.
88template<class... Keys>
89bool arg_flag(fe::View<std::string> args, Keys... keys) {
90 for (std::string_view arg : args)
91 for (std::string_view key : {std::string_view(keys)...})
92 if (arg == key) return true;
93 return false;
94}
95
96///@}
97
98/// A file name from the command line and the stream to write to; @see arg_value.
99class Out {
100public:
101 Out() = default;
102 explicit Out(std::string name)
103 : name_(std::move(name)) {}
104
105 std::string& name() { return name_; } ///< Bound to a `fe::Cli` option, e.g. `--output-mim`.
106
107 /// The stream to write to; `nullptr` if this output was not requested, `std::cout` for `"-"`.
108 /// Opens the file upon first use, so an output no one writes to leaves no file behind.
109 std::ostream* os() {
110 if (name_.empty()) return nullptr;
111 if (name_ == "-") return &std::cout;
112 if (!ofs_.is_open()) {
113 ofs_.open(name_);
114 if (!ofs_) fe::throwf("cannot open output file `{}`", name_);
115 }
116 return &ofs_;
117 }
118
119private:
120 std::string name_;
121 std::ofstream ofs_;
122};
123
124struct Version {
125 int major;
126 int minor;
127 const char* suffix;
128 const char* hash;
129
130 /// Compares major/minor/suffix, ignores hash.
131 constexpr auto operator<=>(const Version& other) const noexcept {
132 auto cmp = std::tie(major, minor) <=> std::tie(other.major, other.minor);
133 if (cmp != 0) return cmp;
134
135 return std::strcmp(suffix, other.suffix) <=> 0;
136 }
137
138 /// Compares major/minor/suffix, ignores hash.
139 constexpr bool operator==(const Version& other) const noexcept {
140 return major == other.major && minor == other.minor && std::strcmp(suffix, other.suffix) == 0;
141 }
142
143 friend std::ostream& operator<<(std::ostream& os, const Version& v) {
144 return os << v.major << '.' << v.minor << v.suffix << " (" << v.hash << ")";
145 }
146};
147
148extern "C" {
149
150#define MIM_VERSION \
151 Version { MIM_VER_MAJOR, MIM_VER_MINOR, MIM_VER_SUFFIX, MIM_GIT_HASH }
152
153/// Basic info and registration function pointer to be returned from a specific plugin.
154/// Use Driver to load such a plugin.
155struct Plugin {
156 using Handle = std::unique_ptr<void, void (*)(void*)>;
157
158 const char* name; ///< Name of the Plugin.
159 Version version; ///< Version of the Plugin.
160
161 /// Callback for registering the mapping from axm ids to normalizer functions in the given @p normalizers map.
163 /// Callback for registering the Plugin's callbacks for Phase%s.
165
166 // No default member initializers, and hence no designated ones either: clang's
167 // -Wreturn-type-c-linkage only accepts a POD as an `extern "C"` return type.
168 // MIM_PLUGIN_ENTRY hands out a zeroed Plugin instead.
169 const PluginArg* args; ///< The `-X` arguments this Plugin understands; see PluginArg.
170 size_t num_args; ///< Number of Plugin::args.
171 const PluginEnv* envs; ///< The environment variables this Plugin reads; see PluginEnv.
172 size_t num_envs; ///< Number of Plugin::envs.
173 const PluginSym* syms; ///< The symbols other Plugin%s may look up; see PluginSym.
174 size_t num_syms; ///< Number of Plugin::syms.
175};
176
177/// @name Plugin Interface
178/// @see Plugin
179///@{
180/// To be implemented and exported by a plugin.
181/// @returns a filled Plugin.
183///@}
184}
185
186#ifdef MIM_STATIC_PLUGINS
187# define MIM_PLUGIN_ENTRY_NAME(p) mim_get_plugin_##p
188#else
189# define MIM_PLUGIN_ENTRY_NAME(p) mim_get_plugin
190#endif
191
192/// Defines a Plugin's entry point; the body fills in the `plugin` handed to it, as in
193/// `MIM_PLUGIN_ENTRY(demo) { plugin.register_normalizers = demo::register_normalizers; }`.
194/// Plugin::name and Plugin::version are already set, and every other field is zeroed.
195/// @p p must be the Plugin's name: a `MIM_STATIC_PLUGINS` build needs one entry point per Plugin.
196#define MIM_PLUGIN_ENTRY(p) \
197 static void mim_plugin_##p(mim::Plugin&); \
198 extern "C" MIM_EXPORT mim::Plugin MIM_PLUGIN_ENTRY_NAME(p)() { \
199 auto plugin = mim::Plugin{}; \
200 plugin.name = #p; \
201 plugin.version = MIM_VERSION; \
202 mim_plugin_##p(plugin); \
203 return plugin; \
204 } \
205 static void mim_plugin_##p([[maybe_unused]] mim::Plugin& plugin)
206
207/// Holds info about an entity defined within a Plugin (called *Annex*).
208struct Annex {
209 Annex() = delete;
210
211 /// @name Mangling Plugin Name
212 ///@{
213 static constexpr size_t Max_Plugin_Size = 8;
214 static constexpr plugin_t Global_Plugin = 0xffff'ffff'ffff'0000_u64;
215
216 /// Mangles @p s into a dense 48-bit representation.
217 /// The layout is as follows:
218 /// ```
219 /// |---7--||---6--||---5--||---4--||---3--||---2--||---1--||---0--|
220 /// 7654321076543210765432107654321076543210765432107654321076543210
221 /// Char67Char66Char65Char64Char63Char62Char61Char60|---reserved---|
222 /// ```
223 /// The `reserved` part is used for the Axm::tag and the Axm::sub.
224 /// Each `Char6x` is 6-bit wide and hence a plugin name has at most Axm::Max_Plugin_Size = 8 chars.
225 /// It uses this encoding:
226 /// | `Char6` | ASCII |
227 /// |---------|---------|
228 /// | 1: | `_` |
229 /// | 2-27: | `a`-`z` |
230 /// | 28-53: | `A`-`Z` |
231 /// | 54-63: | `0`-`9` |
232 /// The 0 is special and marks the end of the name if the name has less than 8 chars.
233 /// @returns `std::nullopt` if encoding is not possible.
234 static std::optional<plugin_t> mangle(std::string_view plugin);
235
236 /// Reverts an Axm::mangle%d @p plugin back to its name; never longer than Annex::Max_Plugin_Size.
237 /// Ignores lower 16-bit of @p plugin.
238 static std::string demangle(plugin_t plugin);
239
240 ///@}
241
242 /// @name Annex Name
243 /// @anchor annex_name
244 /// Anatomy of an Annex name:
245 /// ```
246 /// plugin.tag.sub
247 /// | 48 | 8 | 8 | <-- Number of bits per field.
248 /// ```
249 /// * Def::name() retrieves the full name as Sym.
250 /// * Def::flags() retrieves the full name as Axm::mangle%d 64-bit integer.
251 ///@{
252 /// Yields the `plugin` part of the name as integer.
253 /// It consists of 48 relevant bits that are returned in the highest 6 bytes of a 64-bit integer.
254 static constexpr plugin_t flags2plugin(flags_t f) { return f & Global_Plugin; }
255
256 /// Yields the `tag` part of the name as integer.
257 static constexpr tag_t flags2tag(flags_t f) { return tag_t((f & 0x0000'0000'0000'ff00_u64) >> 8_u64); }
258
259 /// Yields the `sub` part of the name as integer.
260 static constexpr sub_t flags2sub(flags_t f) { return sub_t(f & 0x0000'0000'0000'00ff_u64); }
261
262 /// Includes Axm::plugin() and Axm::tag() but **not** Axm::sub.
263 static constexpr flags_t flags2base(flags_t f) { return f & ~0xff_u64; }
264
265 /// Assembles the full flags from its `plugin`, `tag`, and `sub` fields.
266 static constexpr flags_t flags(plugin_t p, tag_t t, sub_t s = 0) { return p | (flags_t(t) << 8_u64) | flags_t(s); }
267 ///@}
268
269 /// @name Helpers for Matching
270 /// These are set via template specialization.
271 ///@{
272 // clang-format off
273 template<class Id> static constexpr size_t Num = size_t(-1); ///< Number of Axm::sub%tags.
274 template<class Id> static constexpr flags_t Base = flags_t(-1); ///< @see Axm::base.
275 template<class Id> static consteval size_t num () { return Num <Id>; }
276 template<class Id> static consteval flags_t base() { return Base<Id>; }
277 // clang-format on
278 ///@}
279};
280
281} // namespace mim
282
283#ifndef DOXYGEN
284template<>
285struct std::formatter<mim::Version> : fe::ostream_formatter {};
286#endif
Some "global" variables needed all over the place.
Definition driver.h:64
Out()=default
Out(std::string name)
Definition plugin.h:102
std::string & name()
Bound to a fe::Cli option, e.g. --output-mim.
Definition plugin.h:105
std::ostream * os()
The stream to write to; nullptr if this output was not requested, std::cout for "-".
Definition plugin.h:109
A Phase performs one self-contained task over the whole World.
Definition phase.h:23
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
#define MIM_EXPORT
Definition config.h:21
Definition ast.h:16
const char * descr
What it does; one sentence of Markdown, but no links: the terminal help prints it as is.
Definition plugin.h:36
bool arg_flag(fe::View< std::string > args, Keys... keys)
Whether any of keys occurs.
Definition plugin.h:89
u8 sub_t
Definition types.h:42
u64 flags_t
Definition types.h:39
ankerl::unordered_dense::map< flags_t, NormalizeFn > Normalizers
Definition plugin.h:27
mim::Plugin mim_get_plugin()
const char * name
Name of the symbol, e.g. "mim_ll_convert".
Definition plugin.h:49
u64 plugin_t
Definition types.h:40
fe::SymTab< ast::Tok::Tag, ast::Num_Keys+ast::Num_Subst > Keys
The reserved words the ast::Lexer looks up, keyed by the Sym it has just interned.
Definition driver.h:28
const char * descr
What it does; one sentence of Markdown, but no links: the terminal help prints it as is.
Definition plugin.h:43
const char * name
Name of the variable, e.g. "CUDA_HOME".
Definition plugin.h:42
void * ptr
Address of the symbol.
Definition plugin.h:50
const char * syntax
How to spell the argument, e.g. "o=<file>, output=<file>".
Definition plugin.h:35
std::optional< bool > arg_bool(fe::View< std::string > args, std::initializer_list< std::string_view > on, std::initializer_list< std::string_view > off)
An on key ↦ true, an off key ↦ false; std::nullopt if neither occurs.
Definition plugin.h:74
u8 tag_t
Definition types.h:41
std::optional< std::string_view > arg_value(fe::View< std::string > args, Keys... keys)
Definition plugin.h:64
ankerl::unordered_dense::map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition plugin.h:30
One -X <plugin>:<arg> a Plugin understands; see Arguments.
Definition plugin.h:34
One environment variable a Plugin reads; see Environment Variables.
Definition plugin.h:41
One symbol a Plugin offers to other Plugins; see Driver::get_fun_ptr.
Definition plugin.h:48
static constexpr flags_t flags(plugin_t p, tag_t t, sub_t s=0)
Assembles the full flags from its plugin, tag, and sub fields.
Definition plugin.h:266
Annex()=delete
static constexpr plugin_t Global_Plugin
Definition plugin.h:214
static std::string demangle(plugin_t plugin)
Reverts an Axm::mangled plugin back to its name; never longer than Annex::Max_Plugin_Size.
Definition plugin.cpp:29
static std::optional< plugin_t > mangle(std::string_view plugin)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:9
static constexpr tag_t flags2tag(flags_t f)
Yields the tag part of the name as integer.
Definition plugin.h:257
static constexpr size_t Max_Plugin_Size
Definition plugin.h:213
static constexpr sub_t flags2sub(flags_t f)
Yields the sub part of the name as integer.
Definition plugin.h:260
static constexpr plugin_t flags2plugin(flags_t f)
Definition plugin.h:254
static consteval size_t num()
Definition plugin.h:275
static constexpr size_t Num
Number of Axm::subtags.
Definition plugin.h:273
static consteval flags_t base()
Definition plugin.h:276
static constexpr flags_t Base
Definition plugin.h:274
static constexpr flags_t flags2base(flags_t f)
Includes Axm::plugin() and Axm::tag() but not Axm::sub.
Definition plugin.h:263
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:155
const PluginEnv * envs
The environment variables this Plugin reads; see PluginEnv.
Definition plugin.h:171
size_t num_syms
Number of Plugin::syms.
Definition plugin.h:174
size_t num_envs
Number of Plugin::envs.
Definition plugin.h:172
size_t num_args
Number of Plugin::args.
Definition plugin.h:170
const PluginSym * syms
The symbols other Plugins may look up; see PluginSym.
Definition plugin.h:173
const PluginArg * args
The -X arguments this Plugin understands; see PluginArg.
Definition plugin.h:169
void(*) register_normalizers(Normalizers &)
Callback for registering the mapping from axm ids to normalizer functions in the given normalizers ma...
Definition plugin.h:162
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:156
const char * name
Name of the Plugin.
Definition plugin.h:158
void(*) register_phases(Flags2Phases &)
Callback for registering the Plugin's callbacks for Phases.
Definition plugin.h:164
Version version
Version of the Plugin.
Definition plugin.h:159
friend std::ostream & operator<<(std::ostream &os, const Version &v)
Definition plugin.h:143
constexpr bool operator==(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
Definition plugin.h:139
constexpr auto operator<=>(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
Definition plugin.h:131
const char * hash
Definition plugin.h:128
const char * suffix
Definition plugin.h:127