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 <functional>
6#include <iosfwd>
7#include <memory>
8#include <tuple>
9
10#include <absl/container/flat_hash_map.h>
11
12#include "mim/config.h"
13#include "mim/def.h"
14
15namespace mim {
16
17class Driver;
18class Stage;
19
20/// @name Plugin Interface
21///@{
22using Normalizers = absl::flat_hash_map<flags_t, NormalizeFn>;
23
24/// Maps an an axiom of a Stage to a function that creates one.
25using Flags2Stages = absl::flat_hash_map<flags_t, std::function<std::unique_ptr<Stage>(World&)>>;
26///@}
27
28struct Version {
29 int major;
30 int minor;
31 const char* suffix;
32 const char* hash;
33
34 /// Compares major/minor/suffix, ignores hash.
35 constexpr auto operator<=>(const Version& other) const noexcept {
36 auto cmp = std::tie(major, minor) <=> std::tie(other.major, other.minor);
37 if (cmp != 0) return cmp;
38
39 return std::strcmp(suffix, other.suffix) <=> 0;
40 }
41
42 /// Compares major/minor/suffix, ignores hash.
43 constexpr bool operator==(const Version& other) const noexcept {
44 return major == other.major && minor == other.minor && std::strcmp(suffix, other.suffix) == 0;
45 }
46
47 friend std::ostream& operator<<(std::ostream& os, const Version& v) {
48 return os << v.major << '.' << v.minor << v.suffix << " (" << v.hash << ")";
49 }
50};
51
52extern "C" {
53
54#define MIM_VERSION \
55 Version { MIM_VER_MAJOR, MIM_VER_MINOR, MIM_VER_SUFFIX, MIM_GIT_HASH }
56
57/// Basic info and registration function pointer to be returned from a specific plugin.
58/// Use Driver to load such a plugin.
59struct Plugin {
60 using Handle = std::unique_ptr<void, void (*)(void*)>;
61
62 const char* name; ///< Name of the Plugin.
63 Version version; ///< Version of the Plugin.
64
65 /// Callback for registering the mapping from axm ids to normalizer functions in the given @p normalizers map.
67 /// Callback for registering the Plugin's callbacks for Pass%es and Phase%s.
69};
70
71/// @name Plugin Interface
72/// @see Plugin
73///@{
74/// To be implemented and exported by a plugin.
75/// @returns a filled Plugin.
77///@}
78}
79
80/// Holds info about an entity defined within a Plugin (called *Annex*).
81struct Annex {
82 Annex() = delete;
83
84 /// @name Mangling Plugin Name
85 ///@{
86 static constexpr size_t Max_Plugin_Size = 8;
87 static constexpr plugin_t Global_Plugin = 0xffff'ffff'ffff'0000_u64;
88
89 /// Mangles @p s into a dense 48-bit representation.
90 /// The layout is as follows:
91 /// ```
92 /// |---7--||---6--||---5--||---4--||---3--||---2--||---1--||---0--|
93 /// 7654321076543210765432107654321076543210765432107654321076543210
94 /// Char67Char66Char65Char64Char63Char62Char61Char60|---reserved---|
95 /// ```
96 /// The `reserved` part is used for the Axm::tag and the Axm::sub.
97 /// Each `Char6x` is 6-bit wide and hence a plugin name has at most Axm::Max_Plugin_Size = 8 chars.
98 /// It uses this encoding:
99 /// | `Char6` | ASCII |
100 /// |---------|---------|
101 /// | 1: | `_` |
102 /// | 2-27: | `a`-`z` |
103 /// | 28-53: | `A`-`Z` |
104 /// | 54-63: | `0`-`9` |
105 /// The 0 is special and marks the end of the name if the name has less than 8 chars.
106 /// @returns `std::nullopt` if encoding is not possible.
107 static std::optional<plugin_t> mangle(Sym plugin);
108
109 /// Reverts an Axm::mangle%d string to a Sym.
110 /// Ignores lower 16-bit of @p u.
111 static Sym demangle(Driver&, plugin_t plugin);
112
113 static std::tuple<Sym, Sym, Sym> split(Driver&, Sym);
114 ///@}
115
116 /// @name Annex Name
117 /// @anchor annex_name
118 /// Anatomy of an Annex name:
119 /// ```
120 /// %plugin.tag.sub
121 /// | 48 | 8 | 8 | <-- Number of bits per field.
122 /// ```
123 /// * Def::name() retrieves the full name as Sym.
124 /// * Def::flags() retrieves the full name as Axm::mangle%d 64-bit integer.
125 ///@{
126 /// Yields the `plugin` part of the name as integer.
127 /// It consists of 48 relevant bits that are returned in the highest 6 bytes of a 64-bit integer.
128 static constexpr plugin_t flags2plugin(flags_t f) { return f & Global_Plugin; }
129
130 /// Yields the `tag` part of the name as integer.
131 static constexpr tag_t flags2tag(flags_t f) { return tag_t((f & 0x0000'0000'0000'ff00_u64) >> 8_u64); }
132
133 /// Yields the `sub` part of the name as integer.
134 static constexpr sub_t flags2sub(flags_t f) { return sub_t(f & 0x0000'0000'0000'00ff_u64); }
135
136 /// Includes Axm::plugin() and Axm::tag() but **not** Axm::sub.
137 static constexpr flags_t flags2base(flags_t f) { return f & ~0xff_u64; }
138
139 /// Assembles the full flags from its `plugin`, `tag`, and `sub` fields.
140 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); }
141 ///@}
142
143 /// @name Helpers for Matching
144 /// These are set via template specialization.
145 ///@{
146 // clang-format off
147 template<class Id> static constexpr size_t Num = size_t(-1); ///< Number of Axm::sub%tags.
148 template<class Id> static constexpr flags_t Base = flags_t(-1); ///< @see Axm::base.
149 template<class Id> static consteval size_t num () { return Num <Id>; }
150 template<class Id> static consteval flags_t base() { return Base<Id>; }
151 // clang-format of
152 ///@}
153};
154
155} // namespace mim
156
157#ifndef DOXYGEN
158template<> struct std::formatter<mim::Version> : fe::ostream_formatter {};
159#endif
Some "global" variables needed all over the place.
Definition driver.h:19
Common base for Phase and Pass.
Definition pass.h:26
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
#define MIM_EXPORT
Definition config.h:19
Definition ast.h:14
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< Stage >(World &)> > Flags2Stages
Maps an an axiom of a Stage to a function that creates one.
Definition plugin.h:25
mim::Plugin mim_get_plugin()
absl::flat_hash_map< flags_t, NormalizeFn > Normalizers
Definition plugin.h:22
u64 plugin_t
Definition types.h:40
u8 tag_t
Definition types.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:140
static std::tuple< Sym, Sym, Sym > split(Driver &, Sym)
Definition plugin.cpp:58
Annex()=delete
static constexpr plugin_t Global_Plugin
Definition plugin.h:87
static constexpr tag_t flags2tag(flags_t f)
Yields the tag part of the name as integer.
Definition plugin.h:131
static constexpr size_t Max_Plugin_Size
Definition plugin.h:86
static constexpr sub_t flags2sub(flags_t f)
Yields the sub part of the name as integer.
Definition plugin.h:134
static constexpr plugin_t flags2plugin(flags_t f)
Definition plugin.h:128
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axm::mangled string to a Sym.
Definition plugin.cpp:37
static std::optional< plugin_t > mangle(Sym plugin)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:9
static consteval size_t num()
Definition plugin.h:149
static constexpr size_t Num
Number of Axm::subtags.
Definition plugin.h:147
static consteval flags_t base()
Definition plugin.h:150
static constexpr flags_t Base
Definition plugin.h:148
static constexpr flags_t flags2base(flags_t f)
Includes Axm::plugin() and Axm::tag() but not Axm::sub.
Definition plugin.h:137
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:59
void(* register_stages)(Flags2Stages &)
Callback for registering the Plugin's callbacks for Passes and Phases.
Definition plugin.h:68
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:60
const char * name
Name of the Plugin.
Definition plugin.h:62
void(* register_normalizers)(Normalizers &)
Callback for registering the mapping from axm ids to normalizer functions in the given normalizers ma...
Definition plugin.h:66
Version version
Version of the Plugin.
Definition plugin.h:63
friend std::ostream & operator<<(std::ostream &os, const Version &v)
Definition plugin.h:47
constexpr bool operator==(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
Definition plugin.h:43
int minor
Definition plugin.h:30
int major
Definition plugin.h:29
constexpr auto operator<=>(const Version &other) const noexcept
Compares major/minor/suffix, ignores hash.
Definition plugin.h:35
const char * hash
Definition plugin.h:32
const char * suffix
Definition plugin.h:31