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 <absl/container/flat_hash_map.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 = absl::flat_hash_map<flags_t, NormalizeFn>;
28
29/// Maps an axiom of a Phase to a function that creates one.
30using Flags2Phases = absl::flat_hash_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, Markdown.
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, Markdown.
44};
45///@}
46
47/// @name Plugin Argument Lookup
48/// Picks the `-X <plugin>:<arg>` strings of Driver::args / Phase::args apart.
49/// Each helper matches any of @p keys - `arg_value(args(), "o", "output")` - and the last occurrence wins.
50///@{
51namespace detail {
52/// `<key>` ↦ `""`, `<key>=<value>` ↦ `<value>`, anything else ↦ `std::nullopt`.
53inline std::optional<std::string_view> arg_split(std::string_view arg, std::string_view key) {
54 if (!arg.starts_with(key)) return {};
55 auto val = arg.substr(key.size());
56 if (val.empty()) return val;
57 if (val.front() == '=') return val.substr(1);
58 return {};
59}
60} // namespace detail
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 (auto val = detail::arg_split(arg, key); val && !val->empty()) res = val;
69 return res;
70}
71
72/// An @p on key ↦ `true`, an @p off key ↦ `false`; `std::nullopt` if neither occurs.
73inline std::optional<bool> arg_bool(fe::View<std::string> args,
74 std::initializer_list<std::string_view> on,
75 std::initializer_list<std::string_view> off) {
76 std::optional<bool> res;
77 for (std::string_view arg : args) {
78 for (auto key : on)
79 if (arg == key) res = true;
80 for (auto key : off)
81 if (arg == key) res = false;
82 }
83 return res;
84}
85
86/// Whether any of @p keys occurs.
87template<class... Keys>
88bool arg_flag(fe::View<std::string> args, Keys... keys) {
89 for (std::string_view arg : args)
90 for (std::string_view key : {std::string_view(keys)...})
91 if (arg == key) return true;
92 return false;
93}
94
95///@}
96
97/// A file name from the command line and the stream to write to; @see arg_value.
98class Out {
99public:
100 Out() = default;
101 explicit Out(std::string name)
102 : name_(std::move(name)) {}
103
104 std::string& name() { return name_; } ///< Bound to a `fe::Cli` option, e.g. `--output-mim`.
105
106 /// The stream to write to; `nullptr` if this output was not requested, `std::cout` for `"-"`.
107 /// Opens the file upon first use, so an output no one writes to leaves no file behind.
108 std::ostream* os() {
109 if (name_.empty()) return nullptr;
110 if (name_ == "-") return &std::cout;
111 if (!ofs_.is_open()) {
112 ofs_.open(name_);
113 if (!ofs_) fe::throwf("cannot open output file `{}`", name_);
114 }
115 return &ofs_;
116 }
117
118private:
119 std::string name_;
120 std::ofstream ofs_;
121};
122
123struct Version {
124 int major;
125 int minor;
126 const char* suffix;
127 const char* hash;
128
129 /// Compares major/minor/suffix, ignores hash.
130 constexpr auto operator<=>(const Version& other) const noexcept {
131 auto cmp = std::tie(major, minor) <=> std::tie(other.major, other.minor);
132 if (cmp != 0) return cmp;
133
134 return std::strcmp(suffix, other.suffix) <=> 0;
135 }
136
137 /// Compares major/minor/suffix, ignores hash.
138 constexpr bool operator==(const Version& other) const noexcept {
139 return major == other.major && minor == other.minor && std::strcmp(suffix, other.suffix) == 0;
140 }
141
142 friend std::ostream& operator<<(std::ostream& os, const Version& v) {
143 return os << v.major << '.' << v.minor << v.suffix << " (" << v.hash << ")";
144 }
145};
146
147extern "C" {
148
149#define MIM_VERSION \
150 Version { MIM_VER_MAJOR, MIM_VER_MINOR, MIM_VER_SUFFIX, MIM_GIT_HASH }
151
152/// Basic info and registration function pointer to be returned from a specific plugin.
153/// Use Driver to load such a plugin.
154struct Plugin {
155 using Handle = std::unique_ptr<void, void (*)(void*)>;
156
157 const char* name; ///< Name of the Plugin.
158 Version version; ///< Version of the Plugin.
159
160 /// Callback for registering the mapping from axm ids to normalizer functions in the given @p normalizers map.
162 /// Callback for registering the Plugin's callbacks for Phase%s.
164
165 // No default member initializers: only a POD is C-compatible as an `extern "C"` return type.
166 const PluginArg* args; ///< The `-X` arguments this Plugin understands; see PluginArg.
167 size_t num_args; ///< Number of Plugin::args.
168 const PluginEnv* envs; ///< The environment variables this Plugin reads; see PluginEnv.
169 size_t num_envs; ///< Number of Plugin::envs.
170};
171
172/// @name Plugin Interface
173/// @see Plugin
174///@{
175/// To be implemented and exported by a plugin.
176/// @returns a filled Plugin.
178///@}
179}
180
181/// Holds info about an entity defined within a Plugin (called *Annex*).
182struct Annex {
183 Annex() = delete;
184
185 /// @name Mangling Plugin Name
186 ///@{
187 static constexpr size_t Max_Plugin_Size = 8;
188 static constexpr plugin_t Global_Plugin = 0xffff'ffff'ffff'0000_u64;
189
190 /// Mangles @p s into a dense 48-bit representation.
191 /// The layout is as follows:
192 /// ```
193 /// |---7--||---6--||---5--||---4--||---3--||---2--||---1--||---0--|
194 /// 7654321076543210765432107654321076543210765432107654321076543210
195 /// Char67Char66Char65Char64Char63Char62Char61Char60|---reserved---|
196 /// ```
197 /// The `reserved` part is used for the Axm::tag and the Axm::sub.
198 /// Each `Char6x` is 6-bit wide and hence a plugin name has at most Axm::Max_Plugin_Size = 8 chars.
199 /// It uses this encoding:
200 /// | `Char6` | ASCII |
201 /// |---------|---------|
202 /// | 1: | `_` |
203 /// | 2-27: | `a`-`z` |
204 /// | 28-53: | `A`-`Z` |
205 /// | 54-63: | `0`-`9` |
206 /// The 0 is special and marks the end of the name if the name has less than 8 chars.
207 /// @returns `std::nullopt` if encoding is not possible.
208 static std::optional<plugin_t> mangle(std::string_view plugin);
209
210 /// Reverts an Axm::mangle%d @p plugin back to its name; never longer than Annex::Max_Plugin_Size.
211 /// Ignores lower 16-bit of @p plugin.
212 static std::string demangle(plugin_t plugin);
213
214 ///@}
215
216 /// @name Annex Name
217 /// @anchor annex_name
218 /// Anatomy of an Annex name:
219 /// ```
220 /// plugin.tag.sub
221 /// | 48 | 8 | 8 | <-- Number of bits per field.
222 /// ```
223 /// * Def::name() retrieves the full name as Sym.
224 /// * Def::flags() retrieves the full name as Axm::mangle%d 64-bit integer.
225 ///@{
226 /// Yields the `plugin` part of the name as integer.
227 /// It consists of 48 relevant bits that are returned in the highest 6 bytes of a 64-bit integer.
228 static constexpr plugin_t flags2plugin(flags_t f) { return f & Global_Plugin; }
229
230 /// Yields the `tag` part of the name as integer.
231 static constexpr tag_t flags2tag(flags_t f) { return tag_t((f & 0x0000'0000'0000'ff00_u64) >> 8_u64); }
232
233 /// Yields the `sub` part of the name as integer.
234 static constexpr sub_t flags2sub(flags_t f) { return sub_t(f & 0x0000'0000'0000'00ff_u64); }
235
236 /// Includes Axm::plugin() and Axm::tag() but **not** Axm::sub.
237 static constexpr flags_t flags2base(flags_t f) { return f & ~0xff_u64; }
238
239 /// Assembles the full flags from its `plugin`, `tag`, and `sub` fields.
240 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); }
241 ///@}
242
243 /// @name Helpers for Matching
244 /// These are set via template specialization.
245 ///@{
246 // clang-format off
247 template<class Id> static constexpr size_t Num = size_t(-1); ///< Number of Axm::sub%tags.
248 template<class Id> static constexpr flags_t Base = flags_t(-1); ///< @see Axm::base.
249 template<class Id> static consteval size_t num () { return Num <Id>; }
250 template<class Id> static consteval flags_t base() { return Base<Id>; }
251 // clang-format of
252 ///@}
253};
254
255} // namespace mim
256
257#ifndef DOXYGEN
258template<> struct std::formatter<mim::Version> : fe::ostream_formatter {};
259#endif
Some "global" variables needed all over the place.
Definition driver.h:63
Out()=default
Out(std::string name)
Definition plugin.h:101
std::string & name()
Bound to a fe::Cli option, e.g. --output-mim.
Definition plugin.h:104
std::ostream * os()
The stream to write to; nullptr if this output was not requested, std::cout for "-".
Definition plugin.h:108
A Phase performs one self-contained task over the whole World.
Definition phase.h:25
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:40
#define MIM_EXPORT
Definition config.h:21
Definition ast.h:16
const char * descr
What it does; one sentence, Markdown.
Definition plugin.h:36
bool arg_flag(fe::View< std::string > args, Keys... keys)
Whether any of keys occurs.
Definition plugin.h:88
u8 sub_t
Definition types.h:42
u64 flags_t
Definition types.h:39
absl::flat_hash_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
mim::Plugin mim_get_plugin()
absl::flat_hash_map< flags_t, NormalizeFn > Normalizers
Definition plugin.h:27
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:27
const char * descr
What it does; one sentence, Markdown.
Definition plugin.h:43
const char * name
Name of the variable, e.g. "CUDA_HOME".
Definition plugin.h:42
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:73
u8 tag_t
Definition types.h:41
std::optional< std::string_view > arg_value(fe::View< std::string > args, Keys... keys)
Value of <key>=<value>; std::nullopt if none of keys carries one.
Definition plugin.h:64
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
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:240
Annex()=delete
static constexpr plugin_t Global_Plugin
Definition plugin.h:188
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:33
static std::optional< plugin_t > mangle(std::string_view plugin)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:5
static constexpr tag_t flags2tag(flags_t f)
Yields the tag part of the name as integer.
Definition plugin.h:231
static constexpr size_t Max_Plugin_Size
Definition plugin.h:187
static constexpr sub_t flags2sub(flags_t f)
Yields the sub part of the name as integer.
Definition plugin.h:234
static constexpr plugin_t flags2plugin(flags_t f)
Definition plugin.h:228
static consteval size_t num()
Definition plugin.h:249
static constexpr size_t Num
Number of Axm::subtags.
Definition plugin.h:247
static consteval flags_t base()
Definition plugin.h:250
static constexpr flags_t Base
Definition plugin.h:248
static constexpr flags_t flags2base(flags_t f)
Includes Axm::plugin() and Axm::tag() but not Axm::sub.
Definition plugin.h:237
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:154
const PluginEnv * envs
The environment variables this Plugin reads; see PluginEnv.
Definition plugin.h:168
size_t num_envs
Number of Plugin::envs.
Definition plugin.h:169
size_t num_args
Number of Plugin::args.
Definition plugin.h:167
const PluginArg * args
The -X arguments this Plugin understands; see PluginArg.
Definition plugin.h:166
void(*) register_normalizers(Normalizers &)
Callback for registering the mapping from axm ids to normalizer functions in the given normalizers ma...
Definition plugin.h:161
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:155
const char * name
Name of the Plugin.
Definition plugin.h:157
void(*) register_phases(Flags2Phases &)
Callback for registering the Plugin's callbacks for Phases.
Definition plugin.h:163
Version version
Version of the Plugin.
Definition plugin.h:158
friend std::ostream & operator<<(std::ostream &os, const Version &v)
Definition plugin.h:142
constexpr bool operator==(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
Definition plugin.h:138
constexpr auto operator<=>(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
Definition plugin.h:130
const char * hash
Definition plugin.h:127
const char * suffix
Definition plugin.h:126