MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
gen.cpp
Go to the documentation of this file.
1#include "mim/plug/mach/gen.h"
2
3#include <cctype>
4
5#include <print>
6#include <ranges>
7#include <span>
8
9#include <fe/hash.h>
10
11#include <mim/driver.h>
12#include <mim/plugin.h>
13#include <mim/rewrite.h>
14#include <mim/rule.h>
15#include <mim/world.h>
16
17#include <mim/plug/core/core.h>
18#include <mim/plug/math/math.h>
20
21#include "mim/plug/mach/mach.h"
22
23namespace mim::plug::mach {
24
25using rules::Ref;
26using rules::Ty;
27using rules::Want;
28
29namespace {
30
31enum class Kind : u8 { Hole, Axm, Lit, Nat, Idx, App, Tuple, Pack, Extract, Sigma, Arr, Pi, Bot };
32
33/// Flags of an App node.
34enum : u64 {
35 Implicit = 1, ///< Its callee takes an implicit argument.
36 Inner = 2, ///< It saturates an axiom into a term, so in a term position of an lhs it is an inner node.
37 Commutes = 4, ///< It applies a commuting axiom to two operands.
38};
39
40/// Flag of an Extract node: it is the memory of a memory operation.
41constexpr u64 Mem = 1;
42
43constexpr std::string_view name(Ty ty) {
44 switch (ty) {
45#define X(t) \
46 case Ty::t: return #t;
48#undef X
49 }
50 std::unreachable();
51}
52
53constexpr std::string_view name(Want want) {
54 switch (want) {
55#define X(w) \
56 case Want::w: return #w;
58#undef X
59 }
60 std::unreachable();
61}
62
63/// Does @p def use @p meta, a projection of the Var @p var or a Var of its own, directly or by @p var as a whole?
64bool uses(const Def* def, const Def* meta, const Var* var, DefSet& done) {
65 if (def == meta) return true;
66 if (!def || !done.emplace(def).second) return false;
67 if (auto ex = meta->isa<Extract>(); def == var && ex && ex->tuple() == var) return true;
68 if (auto ex = def->isa<Extract>(); ex && ex->tuple() == var && Lit::isa(ex->index())) return false;
69 if (auto type = def->type(); type && uses(type, meta, var, done)) return true;
70 for (auto op : def->ops())
71 if (uses(op, meta, var, done)) return true;
72 return false;
73}
74
75bool uses(const Def* def, const Def* meta, const Var* var) {
76 auto done = DefSet();
77 return uses(def, meta, var, done);
78}
79
80/// The conditions of the `refly.check`s in @p def, e.g. the one of `mach.fpe pe`.
81void checks(const Def* def, DefVec& conds, DefSet& done) {
82 if (def->isa_mut() || !done.emplace(def).second) return;
83 if (auto check = Axm::isa<refly::check>(def)) conds.emplace_back(check->arg(3, 0));
84 for (auto op : def->ops())
85 checks(op, conds, done);
86}
87
88/// A ref with its owner, the node it is an operand of (0 for the root, `k + 1` for cover `k`).
89struct RefInfo {
90 u8 cover;
91 u8 owner;
92 Ref kind;
93};
94
95/// The shape of an lhs that a matcher records; covers are in walk order until `Code::match` puts them into preorder.
96struct Shape {
97 std::vector<u32> covers;
98 std::vector<RefInfo> refs;
99 std::vector<rules::Use> uses;
100 std::vector<std::pair<u32, u32>> swaps; ///< The operands of each commuting node.
101};
102
103class Table {
104public:
105 explicit Table(const Gen& gen)
106 : gen_(gen) {}
107
108 /// The axioms in @p def whose two operands a rule may also match swapped.
109 void commutes(const Def* def) {
110 for (auto op : def->projs())
111 commutes_.emplace(op->as<Axm>()->flags());
112 }
113
114 /// The values of an implicit family parameter of type `«2; Nat»` or `Nat` that gen instantiates.
115 void universes(const Def* formats, const Def* sizes) {
116 for (auto op : formats->projs())
117 formats_.emplace_back(op);
118 for (auto op : sizes->projs())
119 sizes_.emplace_back(op);
120 }
121
122 void collect(const Def* def) {
123 if (auto rule = def->isa<Rule>())
124 add(rule);
125 else if (auto lam = def->isa_mut<Lam>(); lam && lam->type()->as<Pi>()->is_implicit())
126 family(lam);
127 else if (auto tuple = def->isa<Tuple>())
128 for (auto op : tuple->ops())
129 collect(op);
130 }
131
132 /// Puts the rewrites with the same root next to each other, in their order.
133 void sort() { std::ranges::stable_sort(rewrites_, {}, &Rewrite::root); }
134 void dump(std::ostream& os) const;
135
136private:
137 /// What an operand of @p type is: an address form, `Scaled` for `[I64, I2]`, `FReg`, else `Reg`.
138 Want classify(const Def* type) const;
139 /// The non-terminal of a value of @p type in @p rule.
140 Ty nonterminal(const Rule* rule, const Def* type) const;
141 /// The non-terminal the rhs of @p rule yields; that of a memory operation is its value.
142 Ty type(const Rule* rule) const;
143 /// The summed Gen::cost of the instructions of the backend in @p def.
144 size_t cost(const Def* def, DefSet& done) const;
145 size_t cost(const Def* def) const {
146 auto done = DefSet();
147 return cost(def, done);
148 }
149 /// Is @p def the marker or guard @p flags of the isel plugin applied to an argument?
150 const App* isa_marker(flags_t flags, const Def* def) const {
151 if (flags == 0) return nullptr;
152 auto [axm, curry, _] = Axm::get(def);
153 return axm && curry == 0 && axm->flags() == flags ? def->as<App>() : nullptr;
154 }
155
156 /// Rejects a rule set that cannot cover every program: each root needs an atomic rule and each hole a filler.
157 void check(const std::vector<Shape>&, const std::vector<size_t>& ids) const;
158
159 struct Node {
160 Kind kind;
161 u64 val = 0;
162 std::vector<u32> ops;
163 bool operator==(const Node&) const = default;
164
165 struct Hash {
166 size_t operator()(const Node& n) const noexcept {
167 auto h = fe::hash_combine(size_t(n.kind), n.val);
168 for (auto op : n.ops)
169 h = fe::hash_combine(h, op);
170 return h;
171 }
172 };
173 };
174
175 struct Meta {
176 Want want;
177 u32 type;
178 };
179
180 struct Conv {
181 std::string name;
182 std::string rho;
183 size_t cost;
184 Ty from, to;
185 u32 rhs;
186 size_t arity;
187 };
188
189 struct Rewrite {
190 std::string name;
191 std::string rho;
192 size_t cost;
193 flags_t root;
194 Ty type;
195 u32 lhs, rhs;
196 u32 mapped; ///< The memory Extract the rhs replaces, or `u32(-1)`.
197 u32 tied; ///< The meta variables that a two-address instruction of the rhs overwrites.
198 std::vector<Meta> metas;
199 };
200
201 void family(Lam*);
202 void add(const Rule*, const Var* param = nullptr);
203 std::vector<Meta> metas(const Rule*);
204 void guard(const Def*, std::vector<Meta>&);
205 u32 node(const Def*);
206 u32 node_(const Def*);
207 u32 make(Node, std::string comment = {});
208 u32 hole(u64 i) { return make({Kind::Hole, i, {}}); }
209 /// The meta variable @p meta under a marker, as an operand of type @p want.
210 u32 marked(const Def* meta, Want want) {
211 auto id = node(meta);
212 if (nodes_[id].kind != Kind::Hole) meta->blame("{}: `{}` must be a meta variable", gen_.isel(), meta).bail();
213 marks_[nodes_[id].val] = want;
214 return id;
215 }
216 /// The register operand @p op, which may also be an immediate of type `rules::literal_of(want)`.
217 u32 widened(const Def* op, Want want) {
218 auto id = node(op);
219 if (nodes_[id].kind != Kind::Hole) op->blame("{}: `{}` must be a meta variable", gen_.isel(), op).bail();
220 marks_[nodes_[id].val] = want;
221 return id;
222 }
223
224 size_t num_metas() const { return metas_.size(); }
225 /// The meta variable @p def is, under its markers and a `mach.zext`, which reads its register.
226 std::optional<size_t> meta(const Def* def) const {
227 for (;;) {
228 auto [axm, curry, _] = Axm::get(def);
229 if (!axm || curry != 0 || (axm->plugin() != gen_.isel_id() && axm->flags() != Annex::base<mach::zext>()))
230 break;
231 def = def->as<App>()->arg();
232 }
233 if (auto i = fe::lookup(meta_ids_, def)) return *i;
234 return {};
235 }
236 u32 tied(const Def* rhs) const;
237
238 const Gen& gen_;
239 DefSet seen_;
240 DefVec formats_;
241 DefVec sizes_;
242 ankerl::unordered_dense::map<u64, Want> marks_; ///< The marker of each meta variable of the current rule.
243 const Var* var_ = nullptr; ///< The Var of the current rule.
244 DefVec metas_; ///< The meta variables of the current rule: a family parameter, then the Var's projections.
245 DefMap<size_t> meta_ids_;
246 DefMap<u32> def2node_;
247 ankerl::unordered_dense::map<Node, u32, Node::Hash> node2id_;
248 fe::Vector<Node> nodes_;
249 fe::Vector<std::string> comments_;
250 fe::Vector<Rewrite> rewrites_;
251 fe::Vector<Conv> convs_;
252 ankerl::unordered_dense::set<flags_t> commutes_;
253 friend class Code;
254 friend std::string sig(const Table&, u32, const std::vector<Meta>&, bool);
255};
256
257Want Table::classify(const Def* type) const {
258 if (auto want = gen_.classify(type)) return *want;
259 if (math::isa_f(type)) return Want::FReg;
260 auto& w = type->world();
261 if (type == w.sigma({w.type_i64(), w.type_i2()})) return Want::Scaled;
262 return Want::Reg;
263}
264
265Ty Table::nonterminal(const Rule* rule, const Def* type) const {
266 switch (classify(type)) {
267 case Want::Base: return Ty::Base;
268 case Want::Index: return Ty::Index;
269 case Want::Scaled: return Ty::Scaled;
270 case Want::Reg: return Ty::Reg;
271 case Want::FReg: return Ty::FReg;
272 default: rule->blame("{}: `{}` must fix its address form in `{}`", gen_.isel(), type, rule).bail();
273 }
274}
275
276Ty Table::type(const Rule* rule) const {
277 auto rhs = rule->rhs();
278 if (auto t = rhs->isa<Tuple>(); t && t->num_ops() == 2 && core::isa_mem(t->op(0))) rhs = t->op(1);
279 if (isa_marker(gen_.annexes().zx, rhs)) return Ty::Zx;
280 auto type = rule->rhs()->type();
281 if (type->num_projs() == 2 && Axm::isa<core::M>(type->proj(2, 0))) type = type->proj(2, 1);
282 return nonterminal(rule, type);
283}
284
285size_t Table::cost(const Def* def, DefSet& done) const {
286 if (def->isa_mut() || !done.emplace(def).second) return 0;
287 size_t n = 0;
288 if (auto [axm, curry, _] = Axm::get(def); axm && curry == 0 && axm->plugin() == gen_.backend_id())
289 n += gen_.cost(def->as<App>());
290 for (auto op : def->ops())
291 n += cost(op, done);
292 return n;
293}
294
295/// Adds the Rule in the body of the implicit family @p lam, possibly under further implicit families.
296/// If a `refly.check` in that Rule or its guard mentions the family parameter, it is instantiated at each value of its
297/// universe that the checks admit, and the guard decides once all are instantiated. Otherwise, the parameter is a meta
298/// variable of the Rule.
299void Table::family(Lam* lam) {
300 if (!seen_.emplace(lam).second) return;
301 auto body = lam->body();
302 while (body->isa_mut<Lam>() && body->type()->as<Pi>()->is_implicit())
303 body = body->as_mut<Lam>()->body();
304 auto rule = body->isa<Rule>();
305 if (!rule) lam->blame("{}: the body of the family `{}` is no rule", gen_.isel(), lam).bail();
306 auto param = lam->has_var();
307 if (!param) return collect(lam->body());
308
309 // A condition that also mentions an inner family parameter is decided once that one is instantiated.
310 auto conds = DefVec();
311 auto done = DefSet();
312 for (auto def : {rule->type()->dom(), rule->type()->codom(), rule->rhs()})
313 checks(def, conds, done);
314 std::erase_if(conds, [&](const Def* c) {
315 auto fvs = c->free_vars();
316 return fvs.size() != 1 || !fvs.contains(param);
317 });
318 if (conds.empty() && !rule->guard()->has_free_var(param)) {
319 if (lam->body() != rule)
320 lam->blame("{}: `{}` of `{}` must be instantiated", gen_.isel(), lam->dom(), lam).bail();
321 return add(rule, param);
322 }
323
324 auto& w = lam->world();
325 auto dom = lam->dom();
326 auto universe = dom == w.type_nat() ? &sizes_ : dom == w.arr(2, w.type_nat()) ? &formats_ : nullptr;
327 if (!universe) lam->blame("{}: no values to instantiate `{}` of `{}` with", gen_.isel(), dom, lam).bail();
328 for (auto val : *universe) {
329 auto ok = true;
330 for (auto cond : conds) {
331 auto c = Lit::isa<bool>(VarRewriter(param, val).rewrite(cond));
332 if (!c) lam->blame("{}: cannot decide `{}` of `{}` at `{}`", gen_.isel(), cond, lam, val).bail();
333 ok &= *c;
334 }
335 if (ok) collect(lam->reduce_body(val));
336 }
337}
338
339/// Adds @p rule, whose meta variables are preceded by the family parameter @p param, if any.
340void Table::add(const Rule* rule, const Var* param) {
341 if (!seen_.emplace(rule).second) return;
342 auto fvs = rule->free_vars();
343 if (!(fvs.empty() || (param && fvs.size() == 1 && fvs.contains(param))))
344 rule->blame("{}: rule `{}` is not closed", gen_.isel(), rule).bail();
345 if (rule->guard() == rule->world().lit_ff()) return;
346
347 var_ = rule->has_var();
348 metas_.clear();
349 meta_ids_.clear();
350 if (param) metas_.emplace_back(param);
351 if (var_) {
352 auto n = var_->num_projs();
353 for (size_t i = 0; i != n; ++i)
354 metas_.emplace_back(n == 1 ? var_ : var_->proj(n, i));
355 }
356 for (size_t i = 0; i != metas_.size(); ++i)
357 meta_ids_[metas_[i]] = i;
358 marks_.clear();
359 auto lhs = rule->lhs();
360 auto mapped = u32(-1);
361 auto zx = isa_marker(gen_.annexes().zx, lhs);
362 if (var_ && (lhs == var_ || (zx && zx->arg() == var_))) {
363 auto from = var_->type();
364 if (!Idx::isa(from) && classify(from) == Want::Reg)
365 rule->blame("{}: the meta variable of conversion `{}` must be a register or a non-terminal", gen_.isel(),
366 rule)
367 .bail();
368 // `lit x` makes it a conversion of a literal.
369 auto lit = isa_marker(gen_.annexes().lit, rule->guard());
370 bool literal = lit && lit->arg() == var_;
371 if (!literal && rule->guard() != rule->world().lit_tt())
372 rule->blame("{}: the guard of conversion `{}` is no `lit` of its meta variable", gen_.isel(), rule).bail();
373 convs_.emplace_back(Conv{.name = rule->sym().str(),
374 .rho = std::format("{} ↝ {}", rule->dom(), rule->codom()),
375 .cost = cost(rule->rhs()),
376 .from = literal ? Ty::Lit
377 : zx ? Ty::Zx
378 : nonterminal(rule, from),
379 .to = type(rule),
380 .rhs = node(rule->rhs()),
381 .arity = num_metas()});
382 var_ = nullptr;
383 return;
384 }
385 // `(m, e)`: the memory `m` of an inner node of `e` becomes that of the rhs.
386 if (auto t = lhs->isa<Tuple>(); t && t->num_ops() == 2 && core::isa_mem(t->op(0))) {
387 mapped = node(t->op(0));
388 lhs = t->op(1);
389 }
390 auto [root, _, __] = Axm::get(lhs);
391 if (!root && !lhs->isa<Extract>())
392 rule->blame("{}: the lhs of `{}` must apply an axiom or extract", gen_.isel(), rule).bail();
393
394 // A family that takes the operation names all of its instances alike.
395 auto name = root ? std::format("{}({})", rule->sym().str(), root->sym().str()) : rule->sym().str();
396 rewrites_.emplace_back(Rewrite{.name = std::move(name),
397 .rho = std::format("{} ↝ {}", rule->dom(), rule->codom()),
398 .cost = cost(rule->rhs()),
399 .root = root ? root->flags() : 0,
400 .type = type(rule),
401 .lhs = node(lhs),
402 .rhs = node(rule->rhs()),
403 .mapped = mapped,
404 .tied = tied(rule->rhs()),
405 .metas = metas(rule)});
406 var_ = nullptr;
407}
408
409u32 Table::tied(const Def* rhs) const {
410 u32 res = 0;
411 auto done = DefSet();
412 auto todo = DefVec{rhs};
413 while (!todo.empty()) {
414 auto def = todo.back();
415 todo.pop_back();
416 if (def->isa_mut() || !done.emplace(def).second) continue;
417 if (auto [axm, curry, _] = Axm::get(def); axm && curry == 0 && axm->plugin() == gen_.backend_id())
418 if (auto i = gen_.tied(def->as<App>())) {
419 auto arg = def->as<App>()->arg();
420 if (auto m = meta(arg->proj(arg->num_projs(), *i))) res |= 1u << *m;
421 }
422 for (auto op : def->ops())
423 todo.emplace_back(op);
424 }
425 return res;
426}
427
428std::vector<Table::Meta> Table::metas(const Rule* rule) {
429 auto res = std::vector<Meta>();
430 auto n = num_metas();
431 for (size_t i = 0; i != n; ++i) {
432 auto meta = metas_[i];
433 auto type = meta->type();
434 bool param = !uses(rule->rhs(), meta, var_);
435 for (size_t j = 0; j != n && !param; ++j)
436 param = j != i && uses(metas_[j]->type(), meta, var_);
437
438 auto want = Want::Param;
439 if (!param) {
440 auto mark = marks_.find(i);
441 auto marked = [&](Want dflt) { return mark == marks_.end() ? dflt : mark->second; };
442 if (Idx::isa(type)) {
443 want = marked(Want::Reg);
444 } else if (math::isa_f(type)) {
445 want = marked(Want::FReg);
446 } else if (type->isa<Nat>() || Axm::isa<core::M>(type) || type->isa<Pi>()) {
447 want = Want::Param;
448 } else if (auto addr = gen_.classify(type)) {
449 want = *addr;
450 } else {
451 rule->blame("{}: meta variable `{}` of `{}` has type `{}`, which is no operand", gen_.isel(), meta,
452 rule, type)
453 .bail();
454 }
455 }
456 res.emplace_back(want, node(type));
457 }
458 guard(rule->guard(), res);
459 return res;
460}
461
462void Table::guard(const Def* g, std::vector<Meta>& metas) {
463 auto& w = g->world();
464 if (g == w.lit_tt()) return;
465 if (auto conj = Axm::isa(core::bit2::and_, g)) {
466 auto [a, b] = conj->args<2>();
467 guard(a, metas);
468 guard(b, metas);
469 return;
470 }
471
472 auto set = [&](const Def* meta, Want from, Want to) {
473 for (size_t i = 0; i != num_metas(); ++i)
474 if (metas_[i] == meta && metas[i].want == from) {
475 metas[i].want = to;
476 return;
477 }
478 g->blame("{}: guard `{}` must constrain an operand of type `Idx s` or, for `lit`, a `Nat`", gen_.isel(), g)
479 .bail();
480 };
481
482 if (auto imm = isa_marker(gen_.annexes().imm, g))
483 set(imm->arg(), Want::Reg, Want::Imm);
484 else if (auto lit = isa_marker(gen_.annexes().lit, g))
485 set(lit->arg(), Want::Param, Want::Lit);
486 else
487 g->blame("{}: guard `{}` is no conjunction of `imm` and `lit`", gen_.isel(), g).bail();
488}
489
490u32 Table::node(const Def* def) {
491 if (auto id = fe::lookup(def2node_, def)) return *id;
492 auto id = node_(def);
493 return def2node_[def] = id;
494}
495
496u32 Table::node_(const Def* def) {
497 if (auto i = fe::lookup(meta_ids_, def)) return hole(*i);
498 if (var_ && def == var_) {
499 auto ops = std::vector<u32>();
500 for (size_t i = num_metas() - var_->num_projs(); i != num_metas(); ++i)
501 ops.emplace_back(hole(i));
502 return make({Kind::Tuple, 0, std::move(ops)});
503 }
504
505 const auto& a = gen_.annexes();
506 if (auto addr = isa_marker(a.addr, def)) return marked(addr->arg(), Want::Addr);
507 if (auto ri = isa_marker(a.reg_imm, def)) return widened(ri->arg(), Want::RegImm);
508 if (auto rs = isa_marker(a.reg_sh, def)) return widened(rs->arg(), Want::RegSh);
509 if (auto scaled = isa_marker(a.scaled, def)) return marked(scaled->arg(), Want::Scaled);
510 if (auto zx = isa_marker(a.zx, def))
511 return meta_ids_.contains(zx->arg()) ? marked(zx->arg(), Want::Zx) : node(zx->arg());
512 if (auto axm = def->isa<Axm>()) return make({Kind::Axm, axm->flags(), {}}, axm->sym().str());
513 if (auto lit = def->isa<Lit>()) return make({Kind::Lit, lit->get(), {node(lit->type())}});
514 if (def->isa<Nat>()) return make({Kind::Nat, 0, {}});
515 if (def->isa<Idx>()) return make({Kind::Idx, 0, {}});
516 if (auto app = def->isa<App>()) {
517 auto [axm, curry, _] = Axm::get(app);
518 u64 flags = app->callee_type()->is_implicit() ? u64(Implicit) : 0;
519 auto arg = node(app->arg());
520 if (axm && curry == 0 && !app->type()->isa<Type>()) {
521 flags |= Inner;
522 // The arg may also be the meta variables as a whole.
523 if (commutes_.contains(axm->flags()) && nodes_[arg].kind == Kind::Tuple && nodes_[arg].ops.size() == 2)
524 flags |= Commutes;
525 }
526 return make({Kind::App, flags, {node(app->callee()), arg}});
527 }
528 if (auto ex = def->isa<Extract>()) {
529 auto flags = core::isa_mem(ex) ? Mem : 0;
530 return make({Kind::Extract, flags, {node(ex->tuple()), node(ex->index())}});
531 }
532 if (auto pack = def->isa_imm<Pack>()) return make({Kind::Pack, 0, {node(pack->arity()), node(pack->body())}});
533 if (auto arr = def->isa_imm<Arr>()) return make({Kind::Arr, 0, {node(arr->arity()), node(arr->body())}});
534 if (auto pi = def->isa_imm<Pi>()) return make({Kind::Pi, pi->is_implicit(), {node(pi->dom()), node(pi->codom())}});
535 if (def->isa<Bot>()) return make({Kind::Bot, 0, {}});
536
537 auto ops = std::vector<u32>();
538 if (def->isa<Tuple>() || def->isa_imm<Sigma>())
539 for (auto op : def->ops())
540 ops.emplace_back(node(op));
541 if (def->isa<Tuple>()) return make({Kind::Tuple, 0, std::move(ops)});
542 if (def->isa_imm<Sigma>()) return make({Kind::Sigma, 0, std::move(ops)});
543
544 def->blame("{}: cannot put `{}` into a rule table", gen_.isel(), def).bail();
545}
546
547u32 Table::make(Node node, std::string comment) {
548 if (auto id = fe::lookup(node2id_, node)) return *id;
549 auto id = u32(nodes_.size());
550 node2id_.emplace(node, id);
551 nodes_.emplace_back(std::move(node));
552 comments_.emplace_back(std::move(comment));
553 return id;
554}
555
556/// Emits C++ that matches and builds the terms of the node pool of a Table.
557class Code {
558public:
559 Code(std::ostream& os, const Table& table)
560 : os_(os)
561 , t_(table) {}
562
563 Shape match(size_t id, u32 lhs, std::span<const u32> types);
564 void build(u32 rhs);
565
566private:
567 template<class... Args>
568 void line(std::format_string<Args...> fmt, Args&&... args) {
569 std::println(os_, " {}", std::format(fmt, std::forward<Args>(args)...));
570 }
571
572 void walk(u32 lhs);
573 std::string fresh(char prefix) { return std::format("{}{}", prefix, next_++); }
574 const Table::Node& node(u32 n) const { return t_.nodes_[n]; }
575 const std::string& comment(u32 n) const { return t_.comments_[n]; }
576 bool inner(u32 n) const { return !in_type_ && node(n).kind == Kind::App && node(n).val & Inner && n != root_; }
577 /// Binds @p expr to a fresh variable, unless it is one.
578 std::string name(const std::string& expr);
579 /// Matches @p n against @p expr; on the root's callee @p spine, isel already checked the axiom.
580 void match(u32 n, const std::string& expr, bool spine = false);
581 void match_inner(u32 n, const std::string& expr);
582 void match_app(u32 n, const std::string& expr, bool spine = false);
583 void match2(u32 n, const std::string& expr, std::string_view isa, std::string_view op0, std::string_view op1);
584 void ref(u32 n, const std::string& expr, Ref kind);
585 std::string build_(u32 n);
586
587 std::ostream& os_;
588 const Table& t_;
589 size_t next_ = 0;
590 bool in_type_ = false;
591 u32 root_ = 0;
592 u8 owner_ = 0;
593 std::vector<bool> bound_;
594 ankerl::unordered_dense::map<u32, std::string> cover_vars_;
595 std::span<const u32> types_;
596 ankerl::unordered_dense::map<u32, std::string> built_;
597 Shape shape_;
598};
599
600Shape Code::match(size_t id, u32 lhs, std::span<const u32> types) {
601 types_ = types;
602 std::println(os_,
603 "inline bool match_{}(const Def* def, [[maybe_unused]] u32 swap, [[maybe_unused]] const Def** m, "
604 "[[maybe_unused]] const Def** r) {{",
605 id);
606 walk(lhs);
607 line("return true;");
608 std::println(os_, "}}");
609 std::println(os_);
610
611 // Number the covers in preorder of the Value refs, so a cover's operands follow it.
612 auto children = std::vector<std::vector<u8>>(shape_.covers.size() + 1);
613 for (const auto& r : shape_.refs)
614 if (r.kind == Ref::Value) children[r.owner].emplace_back(r.cover);
615 auto perm = std::vector<u8>(shape_.covers.size(), u8(-1));
616 auto stack = std::vector<u8>(children[0].rbegin(), children[0].rend());
617 u8 next = 0;
618 while (!stack.empty()) {
619 auto c = stack.back();
620 stack.pop_back();
621 if (perm[c] != u8(-1)) continue;
622 perm[c] = next++;
623 stack.insert(stack.end(), children[c + 1].rbegin(), children[c + 1].rend());
624 }
625 if (next != shape_.covers.size()) fe::throwf("{}: an inner node is no operand of its rule", t_.gen_.isel());
626 auto res = std::move(shape_);
627 auto owner = [&](u8 o) { return o == 0 ? o : u8(perm[o - 1] + 1); };
628 for (auto& r : res.refs)
629 r.cover = perm[r.cover], r.owner = owner(r.owner);
630 for (auto& u : res.uses)
631 u.owner = owner(u.owner);
632 auto covers = res.covers;
633 for (size_t k = 0; k != covers.size(); ++k)
634 res.covers[perm[k]] = covers[k];
635 return res;
636}
637
638void Code::walk(u32 lhs) {
639 next_ = 0;
640 owner_ = 0;
641 root_ = lhs;
642 shape_ = {};
643 cover_vars_.clear();
644 bound_.assign(types_.size(), false);
645 match(lhs, "def", true);
646}
647
648std::string Code::name(const std::string& expr) {
649 if (std::ranges::all_of(expr, [](char c) { return std::isalnum(c) || c == '_'; })) return expr;
650 auto res = fresh('d');
651 line("auto {} = {};", res, expr);
652 return res;
653}
654
655void Code::match2(u32 n, const std::string& e, std::string_view isa, std::string_view op0, std::string_view op1) {
656 auto x = fresh('d');
657 line("auto {} = {}->{};", x, e, isa);
658 line("if (!{}) return false;", x);
659 match(node(n).ops[0], std::format("{}->{}()", x, op0));
660 match(node(n).ops[1], std::format("{}->{}()", x, op1));
661}
662
663void Code::ref(u32 n, const std::string& e, Ref kind) {
664 auto k = std::ranges::find(shape_.covers, n) - shape_.covers.begin();
665 if (size_t(k) == shape_.covers.size()) shape_.covers.emplace_back(n);
666 auto j = shape_.refs.size();
667 shape_.refs.emplace_back(RefInfo{u8(k), owner_, kind});
668 if (kind == Ref::Value) shape_.uses.emplace_back(rules::Use{owner_, u8(j), true});
669 line("r[{}] = {};", j, e);
670}
671
672void Code::match_inner(u32 n, const std::string& e) {
673 if (auto var = cover_vars_.find(n); var != cover_vars_.end()) {
674 line("if ({} != {}) return false;", e, var->second);
675 return;
676 }
677 auto var = name(e);
678 cover_vars_[n] = var;
679 auto outer = owner_;
680 owner_ = u8(std::ranges::find(shape_.covers, n) - shape_.covers.begin() + 1);
681 match_app(n, var);
682 owner_ = outer;
683}
684
685void Code::match_app(u32 n, const std::string& e, bool spine) {
686 const auto& x = node(n);
687 auto app = fresh('d');
688 line("auto {} = {}->isa<App>();", app, e);
689 line("if (!{}) return false;", app);
690 match(x.ops[0], app + "->callee()", spine);
691 if (in_type_ || !(x.val & Commutes)) return match(x.ops[1], app + "->arg()");
692 auto arg = name(app + "->arg()");
693 auto bit = shape_.swaps.size();
694 auto& t = node(x.ops[1]);
695 shape_.swaps.emplace_back(t.ops[0], t.ops[1]);
696 line("if (Lit::isa({}->arity()) != 2) return false;", arg);
697 match(t.ops[0], std::format("{}->proj(2, swap >> {} & 1)", arg, bit));
698 match(t.ops[1], std::format("{}->proj(2, ~swap >> {} & 1)", arg, bit));
699}
700
701void Code::match(u32 n, const std::string& e, bool spine) {
702 const auto& x = node(n);
703 switch (x.kind) {
704 case Kind::Hole: {
705 auto i = x.val;
706 if (!in_type_) shape_.uses.emplace_back(rules::Use{owner_, u8(i), false});
707 if (bound_[i]) {
708 line("if (m[{}] != {}) return false;", i, e);
709 return;
710 }
711 bound_[i] = true;
712 line("m[{}] = {};", i, e);
713 auto outer = std::exchange(in_type_, true);
714 match(types_[i], std::format("m[{}]->type()", i));
715 in_type_ = outer;
716 return;
717 }
718 case Kind::Axm: {
719 if (spine) return;
720 auto d = name(e);
721 line("if (!{0}->isa<Axm>() || {0}->flags() != {1:#x}) return false; // {2}", d, x.val, comment(n));
722 return;
723 }
724 case Kind::Lit: {
725 auto lit = fresh('d');
726 line("auto {} = {}->isa<Lit>();", lit, e);
727 line("if (!{0} || {0}->get() != {1:#x}) return false;", lit, x.val);
728 auto outer = std::exchange(in_type_, true);
729 match(x.ops[0], lit + "->type()");
730 in_type_ = outer;
731 return;
732 }
733 case Kind::Nat: line("if (!{}->isa<Nat>()) return false;", e); return;
734 case Kind::Idx: line("if (!{}->isa<Idx>()) return false;", e); return;
735 case Kind::App: {
736 if (!inner(n)) return match_app(n, e, spine);
737 auto d = name(e);
738 if (!cover_vars_.contains(n)) ref(n, d, Ref::Value);
739 return match_inner(n, d);
740 }
741 case Kind::Extract: {
742 auto tuple = x.ops[0];
743 if (!inner(tuple)) return match2(n, e, "isa<Extract>()", "tuple", "index");
744 auto ex = fresh('d');
745 line("auto {} = {}->isa<Extract>();", ex, e);
746 line("if (!{}) return false;", ex);
747 ref(tuple, ex, x.val & Mem ? Ref::Mem : Ref::Value);
748 match_inner(tuple, ex + "->tuple()");
749 match(x.ops[1], ex + "->index()");
750 return;
751 }
752 case Kind::Pack: return match2(n, e, "isa_imm<Pack>()", "arity", "body");
753 case Kind::Arr: return match2(n, e, "isa_imm<Arr>()", "arity", "body");
754 case Kind::Pi: return match2(n, e, "isa_imm<Pi>()", "dom", "codom");
755 case Kind::Bot: line("if (!{}->isa<Bot>()) return false;", e); return;
756 case Kind::Sigma: {
757 auto sigma = fresh('d');
758 line("auto {} = {}->isa_imm<Sigma>();", sigma, e);
759 line("if (!{0} || {0}->num_ops() != {1}) return false;", sigma, x.ops.size());
760 for (size_t j = 0; j != x.ops.size(); ++j)
761 match(x.ops[j], std::format("{}->op({})", sigma, j));
762 return;
763 }
764 case Kind::Tuple: {
765 // η: any term of the tuple's arity matches by its projections.
766 auto d = name(e);
767 line("if (Lit::isa({}->arity()) != {}) return false;", d, x.ops.size());
768 for (size_t j = 0; j != x.ops.size(); ++j)
769 match(x.ops[j], std::format("{}->proj({}, {})", d, x.ops.size(), j));
770 return;
771 }
772 }
773 std::unreachable();
774}
775
776void Code::build(u32 rhs) {
777 next_ = 0;
778 built_.clear();
779 std::println(os_, "inline const Def* build_{}([[maybe_unused]] World& w, [[maybe_unused]] const Def* const* h) {{",
780 rhs);
781 line("return {};", build_(rhs));
782 std::println(os_, "}}");
783 std::println(os_);
784}
785
786std::string Code::build_(u32 n) {
787 if (auto i = built_.find(n); i != built_.end()) return i->second;
788 const auto& x = node(n);
789 if (x.kind == Kind::Hole) return built_[n] = std::format("h[{}]", x.val);
790
791 // The holes determine the implicit arguments, which may differ from the rule's, e.g. `i` of an `x86.Addr i`.
792 const auto& callee = node(x.kind == Kind::App ? x.ops[0] : n);
793 if (x.kind == Kind::App && callee.kind == Kind::App && callee.val & Implicit) {
794 auto f = build_(callee.ops[0]);
795 auto a = build_(x.ops[1]);
796 auto res = fresh('b');
797 line("auto {} = w.implicit_app({}, {});", res, f, a);
798 return built_[n] = res;
799 }
800
801 auto ops = std::vector<std::string>();
802 for (auto op : x.ops)
803 ops.emplace_back(build_(op));
804 auto res = fresh('b');
805 switch (x.kind) {
806 case Kind::Axm: line("auto {} = w.annex({:#x}); // {}", res, x.val, comment(n)); break;
807 case Kind::Lit: line("auto {} = w.lit({}, {:#x});", res, ops[0], x.val); break;
808 case Kind::Nat: line("auto {} = w.type_nat();", res); break;
809 case Kind::Idx: line("auto {} = w.type_idx();", res); break;
810 case Kind::App: line("auto {} = w.app({}, {});", res, ops[0], ops[1]); break;
811 case Kind::Extract: line("auto {} = w.extract({}, {});", res, ops[0], ops[1]); break;
812 case Kind::Pack: line("auto {} = w.pack({}, {});", res, ops[0], ops[1]); break;
813 case Kind::Arr: line("auto {} = w.arr({}, {});", res, ops[0], ops[1]); break;
814 case Kind::Tuple: line("auto {} = w.tuple({{{}}});", res, fe::Join(ops, ", ")); break;
815 case Kind::Sigma: line("auto {} = w.sigma({{{}}});", res, fe::Join(ops, ", ")); break;
816 case Kind::Pi: line("auto {} = w.pi({}, {}, {});", res, ops[0], ops[1], x.val != 0); break;
817 case Kind::Bot: line("auto {} = w.bot(w.type());", res); break;
818 case Kind::Hole: std::unreachable();
819 }
820 return built_[n] = res;
821}
822
823/// Hashes an lhs followed by the types of its meta variables.
824struct LhsHash {
825 size_t operator()(const std::vector<u32>& v) const noexcept {
826 size_t h = 0;
827 for (auto x : v)
828 h = fe::hash_combine(h, x);
829 return h;
830 }
831};
832
833/// The signature of the subpattern @p n under @p metas: equal ones match the same terms.
834/// If @p reg, a RegImm or RegSh hole is a Reg.
835std::string sig(const Table& t, u32 n, const std::vector<Table::Meta>& metas, bool reg);
836
837void Table::check(const std::vector<Shape>& shapes, const std::vector<size_t>& ids) const {
838 auto chains = rules::Chains(convs_);
839 auto atomic = ankerl::unordered_dense::set<flags_t>();
840 for (size_t i = 0, e = rewrites_.size(); i != e; ++i) {
841 const auto& r = rewrites_[i];
842 if (chains.conv(r.type, rules::reg_want(r.type)) == rules::Inf)
843 fe::throwf("{}: rule `{}` yields a value that does not convert to a register", gen_.isel(), r.name);
844 for (auto m : r.metas)
845 if (!rules::is_param(m.want) && !rules::is_literal(m.want) && !chains.reachable(m.want))
846 fe::throwf("{}: no rule type converts to a hole of `{}`", gen_.isel(), r.name);
847 auto total = [&](Meta m) { return rules::is_param(m.want) || chains.total(m.want); };
848 if (shapes[ids[i]].covers.empty() && std::ranges::all_of(r.metas, total)) atomic.emplace(r.root);
849 }
850 for (const auto& r : rewrites_)
851 if (!atomic.contains(r.root)) fe::throwf("{}: no atomic rule shares the root of `{}`", gen_.isel(), r.name);
852}
853
854void Table::dump(std::ostream& os) const {
855 std::println(os, "// Generated by `{}.gen`; do not edit.", gen_.isel());
856 std::println(os, "#pragma once");
857 std::println(os);
858 std::println(os, "#include <mim/world.h>");
859 std::println(os);
860 std::println(os, "#include <mim/plug/mach/rule_table.h>");
861 std::println(os);
862 std::println(os, "#include \"mim/plug/{0}/{0}.h\"", gen_.backend());
863 std::println(os);
864 std::println(os, "namespace mim::plug::{}::rules {{", gen_.backend());
865 std::println(os);
866 std::println(os, "using namespace mim::plug::mach::rules;");
867 std::println(os);
868
869 auto code = Code(os, *this);
870 auto matchers = ankerl::unordered_dense::map<std::vector<u32>, size_t, LhsHash>();
871 auto shapes = std::vector<Shape>();
872 auto ids = std::vector<size_t>();
873 for (const auto& r : rewrites_) {
874 auto types = std::vector<u32>{r.lhs};
875 for (auto m : r.metas)
876 types.emplace_back(m.type);
877 auto [i, fresh] = matchers.emplace(types, matchers.size());
878 if (fresh) shapes.emplace_back(code.match(i->second, r.lhs, std::span(types).subspan(1)));
879 ids.emplace_back(i->second);
880 }
881 check(shapes, ids);
882
883 auto built = ankerl::unordered_dense::set<u32>();
884 for (const auto& r : rewrites_)
885 if (built.emplace(r.rhs).second) code.build(r.rhs);
886 for (const auto& c : convs_)
887 if (built.emplace(c.rhs).second) code.build(c.rhs);
888
889 std::println(os, "// clang-format off");
890 std::println(os, "inline constexpr Want wants[] = {{");
891 size_t num_wants = 0;
892 for (const auto& r : rewrites_) {
893 if (r.metas.empty()) continue;
894 std::print(os, " ");
895 for (auto m : r.metas)
896 std::print(os, " Want::{},", name(m.want));
897 std::println(os, " // {}: {}", r.name, r.rho);
898 num_wants += r.metas.size();
899 }
900 // An empty array is ill-formed.
901 if (num_wants == 0) std::println(os, " Want::Param,");
902 std::println(os, "}};");
903 std::println(os);
904
905 constexpr std::string_view refs[] = {"Value", "Mem"};
906 std::println(os, "inline constexpr Use all_uses[] = {{");
907 std::println(os, " {{0, 0, false}},");
908 for (const auto& s : shapes)
909 for (auto u : s.uses)
910 std::println(os, " {{{}, {}, {}}},", u.owner, u.index, u.ref);
911 std::println(os, "}};");
912 std::println(os);
913 std::println(os, "inline constexpr RefInfo all_refs[] = {{");
914 std::println(os, " {{0, Ref::Value}},");
915 for (const auto& s : shapes)
916 for (auto r : s.refs)
917 std::println(os, " {{{}, Ref::{}}},", r.cover, refs[size_t(r.kind)]);
918 std::println(os, "}};");
919 std::println(os);
920 auto max = [&](auto f) { return std::ranges::max(shapes | std::views::transform(f)); };
921 std::println(os, "inline constexpr size_t Max_Refs = {};", max([](const Shape& s) { return s.refs.size(); }));
922 std::println(os, "inline constexpr size_t Max_Covers = {};", max([](const Shape& s) { return s.covers.size(); }));
923 std::println(os, "inline constexpr size_t Max_Metas = {};",
924 std::ranges::max(rewrites_ | std::views::transform([](const Rewrite& r) { return r.metas.size(); })));
925 std::println(os);
926 std::println(os, "inline constexpr Matcher matchers[] = {{");
927 size_t num_uses = 1, num_refs = 1;
928 for (size_t i = 0; i != shapes.size(); ++i) {
929 const auto& s = shapes[i];
930 std::println(os, " {{&match_{}, {}, {}, {}, {}, {}}},", i, num_uses, s.uses.size(), num_refs, s.refs.size(),
931 s.covers.size());
932 num_uses += s.uses.size();
933 num_refs += s.refs.size();
934 }
935 std::println(os, "}};");
936 std::println(os);
937
938 std::println(os, "inline constexpr Rewrite rewrites[] = {{");
939 size_t first = 0;
940 for (size_t i = 0, e = rewrites_.size(); i != e; ++i) {
941 const auto& r = rewrites_[i];
942 const auto& s = shapes[ids[i]];
943 u32 swaps = 0, reg_swaps = 0;
944 for (size_t b = 0; b != s.swaps.size(); ++b) {
945 auto [x, y] = s.swaps[b];
946 if (sig(*this, x, r.metas, false) != sig(*this, y, r.metas, false)) swaps |= 1u << b;
947 if (sig(*this, x, r.metas, true) != sig(*this, y, r.metas, true)) reg_swaps |= 1u << b;
948 }
949 size_t maps = 0;
950 if (r.mapped != u32(-1)) {
951 auto k = std::ranges::find(s.covers, nodes_[r.mapped].ops[0]) - s.covers.begin();
952 if (size_t(k) == s.covers.size())
953 fe::throwf("{}: `{}` maps the memory of no inner node", gen_.isel(), r.name);
954 maps = k + 1;
955 }
956 std::println(
957 os, " {{\"{}\", {}, {:#x}, Ty::{}, &matchers[{}], &build_{}, {}, {}, {:#x}, {:#x}, {:#x}, {}}}, // {}",
958 r.name, r.cost, r.root, name(r.type), ids[i], r.rhs, first, r.metas.size(), swaps, swaps & ~reg_swaps,
959 r.tied, maps, r.rho);
960 first += r.metas.size();
961 }
962 std::println(os, "}};");
963 std::println(os);
964 std::println(os, "inline constexpr Root roots[] = {{");
965 for (size_t i = 0, e = rewrites_.size(); i != e;) {
966 auto j = i;
967 while (j != e && rewrites_[j].root == rewrites_[i].root)
968 ++j;
969 std::println(os, " {{{:#x}, {}, {}}},", rewrites_[i].root, i, j - i);
970 i = j;
971 }
972 std::println(os, "}};");
973 std::println(os);
974 // An empty array is ill-formed.
975 if (convs_.empty()) fe::throwf("{}: there is no conversion", gen_.isel());
976 std::println(os, "inline constexpr Conversion conversions[] = {{");
977 for (const auto& c : convs_)
978 std::println(os, " {{\"{}\", {}, Ty::{}, Ty::{}, {}, &build_{}}}, // {}", c.name, c.cost, name(c.from),
979 name(c.to), c.arity, c.rhs, c.rho);
980 std::println(os, "}};");
981 std::println(os);
982 std::println(os, "inline constexpr Chains chains{{conversions}};");
983 std::println(
984 os, "inline constexpr Table table{{wants, all_uses, all_refs, matchers, rewrites, roots, conversions, chains,");
985 std::println(os, " Max_Refs, Max_Covers, Max_Metas}};");
986 std::println(os, "// clang-format on");
987 std::println(os);
988 std::println(os, "}} // namespace mim::plug::{}::rules", gen_.backend());
989}
990
991std::string sig(const Table& t, u32 n, const std::vector<Table::Meta>& metas, bool reg) {
992 const auto& x = t.nodes_[n];
993 if (x.kind == Kind::Hole) {
994 auto w = metas[x.val].want;
995 if (reg && rules::literal_of(w) != w) w = Want::Reg;
996 return std::format("{}:{}", name(w), sig(t, metas[x.val].type, metas, reg));
997 }
998 auto res = std::format("{}.{}(", size_t(x.kind), x.val);
999 for (auto op : x.ops)
1000 res += sig(t, op, metas, reg) + ",";
1001 return res + ")";
1002}
1003
1004} // namespace
1005
1006void mim_mach_gen(Gen& gen) {
1007 auto& lib = *gen.driver().library();
1008 auto flags = std::vector<flags_t>();
1009 for (const auto& [f, _] : lib.annexes())
1010 if (Annex::flags2plugin(f) == gen.isel_id()) flags.emplace_back(f);
1011 std::ranges::sort(flags);
1012
1013 auto& w = gen.world();
1014 auto table = Table(gen);
1015 table.commutes(w.annex(gen.annexes().commutes));
1016 table.universes(w.annex(gen.annexes().formats), w.annex(gen.annexes().sizes));
1017 for (auto f : flags)
1018 table.collect(w.annex(f));
1019 table.sort();
1020
1021 auto out = Out(std::string(arg_value(gen.args(), "o", "output").value_or("-")));
1022 table.dump(*out.os());
1023}
1024
1025} // namespace mim::plug::mach
static auto isa(const Def *def)
Definition axm.h:112
static std::tuple< const Axm *, u8, u8 > get(const Def *def)
Yields currying counter of def.
Definition axm.cpp:38
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:659
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
Definition def.h:329
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
Definition def.h:1137
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:492
static std::optional< T > isa(const Def *def)
Definition def.h:974
Out()=default
const Def * type() const
Definition def.h:866
Writes the closed rules among the annexes of a backend's isel plugin as the C++ matchers and builders...
Definition gen.h:21
virtual std::optional< mach::rules::Want > classify(const Def *) const
What an operand of the backend's address type type is: Want::Base, Want::Index, or Want::Addr.
Definition gen.h:67
std::string_view backend() const
Definition gen.h:54
flags_t lit
The guard lit x: a literal.
Definition gen.h:31
virtual std::optional< size_t > tied(const App *) const
The operand whose register the result of the two-address instruction insn takes.
Definition gen.h:65
flags_t zx
zx x: Want::Zx of a meta variable, else the non-terminal Zx of a rhs or conversion.
Definition gen.h:29
std::string_view isel() const
Definition gen.h:56
plugin_t backend_id() const
Definition gen.h:55
virtual u32 cost(const App *insn) const =0
flags_t imm
The guard imm x: Want::Imm.
Definition gen.h:30
const Annexes & annexes() const
Definition gen.h:58
u32 cost(const App *app)
The cost of the instruction app: its latency plus one, so of two equally fast covers the shorter wins...
Definition insn.h:24
constexpr Chains chains
constexpr Table table
constexpr Matcher matchers[]
Ref
How the lhs refers to one of its inner nodes.
Definition rule_table.h:50
@ Mem
By its memory, which only the match uses.
Definition rule_table.h:52
Want
What a rule wants of the term a meta variable matches; a Param is bound but no operand.
Definition rule_table.h:28
constexpr Want literal_of(Want want)
The literal form of a hole of type want, or want itself.
Definition rule_table.h:132
Ty
The non-terminal a rule yields by the type of its rhs: a register of either class,...
Definition rule_table.h:38
The mach Plugin
Definition asm.h:15
void mim_mach_gen(Gen &)
Gen::start, compiled once into libmim_mach; Gen reaches it via GET_FUN_PTR.
Definition gen.cpp:1006
std::optional< size_t > tied(flags_t base)
The operand whose register the result of a two-address instruction base takes.
Definition insn.h:20
u64 flags_t
Definition types.h:39
GIDMap< const Def *, To > DefMap
Definition def.h:91
fe::Vector< const Def * > DefVec
Definition def.h:96
GIDSet< const Def * > DefSet
Definition def.h:92
uint32_t u32
Definition types.h:27
TExt< false > Bot
Definition variant.h:39
uint64_t u64
Definition types.h:27
@ Meta
Meta rules for universe and Type levels.
Definition def.h:166
uint8_t u8
Definition types.h:27
std::optional< std::string_view > arg_value(fe::View< std::string > args, Keys... keys)
Definition plugin.h:65
Node
Definition def.h:121
@ Nat
Definition def.h:123
@ Pi
Definition def.h:123
@ Lam
Definition def.h:123
@ Idx
Definition def.h:123
@ Arr
Definition def.h:123
@ Pack
Definition def.h:123
@ Var
Definition def.h:123
@ Hole
Definition def.h:123
@ Axm
Definition def.h:123
@ Sigma
Definition def.h:123
@ Extract
Definition def.h:123
@ Rule
Definition def.h:123
@ App
Definition def.h:123
@ Tuple
Definition def.h:123
@ Lit
Definition def.h:123
#define X(w)
Definition rule_table.h:29
#define MIM_MACH_RULE_WANTS(X)
Definition rule_table.h:18
#define MIM_MACH_RULE_TYS(X)
Definition rule_table.h:34
static constexpr plugin_t flags2plugin(flags_t f)
Definition plugin.h:256
static consteval flags_t base()
Definition plugin.h:278
constexpr u32 conv(Ty from, Want want) const
Cost of converting a replacement or operand of non-terminal from into a hole of type want.
Definition rule_table.h:197
constexpr bool reachable(Want w) const
Does some non-terminal of the register class of a rule convert into a hole of type w?
Definition rule_table.h:211
constexpr bool total(Want w) const
Does every non-terminal of w's register class convert into a hole of type w, as an atomic rule's hole...
Definition rule_table.h:204