MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
check.h
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4
5#include "mim/def.h"
6
7namespace mim {
8
9/// This node is a hole in the IR that is inferred by its context later on.
10/// It is modelled as a *mut*able Def.
11/// If inference was successful, it's Infer::op will be set to the inferred Def.
12class Infer : public Def, public Setters<Infer> {
13private:
14 Infer(const Def* type)
15 : Def(Node, type, 1, 0) {}
16
17public:
18 using Setters<Infer>::set;
19
20 /// @name op
21 ///@{
22 const Def* op() const { return Def::op(0); }
23 Infer* set(const Def* op) { return Def::set(0, op)->as<Infer>(); }
24 Infer* reset(const Def* op) { return Def::reset(0, op)->as<Infer>(); }
25 Infer* unset() { return Def::unset()->as<Infer>(); }
26 ///@}
27
28 /// Eliminate Infer%s that may have been resolved in the meantime by rebuilding.
29 /// @returns `true`, if one of the arguements was in fact updated.
30 static bool eliminate(Vector<Ref*>);
31 static bool should_eliminate(Ref def) { return def->isa_imm() && def->has_dep(Dep::Infer); }
32
33 /// [Union-Find](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) to unify Infer nodes.
34 /// Def::flags is used to keep track of rank for
35 /// [Union by rank](https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Union_by_rank).
36 static const Def* find(const Def*);
37
38 Infer* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
39
40 static constexpr auto Node = Node::Infer;
41
42private:
43 flags_t rank() const { return flags(); }
44 flags_t& rank() { return flags_; }
45
46 Ref rebuild_(World&, Ref, Defs) const override;
47 Infer* stub_(World&, Ref) override;
48
49 friend class World;
50 friend class Check;
51};
52
53class Check {
54public:
56 : world_(world) {}
57
58 World& world() { return world_; }
59
60 /// Are d1 and d2 α-equivalent?
61 /// * In @p infer mode, type inference is happening and Infer%s will be resolved, if possible.
62 /// Also, two *free* but *different* Var%s **are** considered α-equivalent.
63 /// * When **not* in @p infer mode, no type inference is happening and Infer%s will not be touched.
64 /// Also, Two *free* but *different* Var%s are **not** considered α-equivalent.
65 template<bool infer = true> static bool alpha(Ref d1, Ref d2) { return Check(d1->world()).alpha_<infer>(d1, d2); }
66
67 /// Can @p value be assigned to sth of @p type?
68 /// @note This is different from `equiv(type, value->type())` since @p type may be dependent.
69 static bool assignable(Ref type, Ref value) { return Check(type->world()).assignable_(type, value); }
70
71 /// Yields `defs.front()`, if all @p defs are Check::alpha-equivalent (`infer = false`) and `nullptr` otherwise.
72 static Ref is_uniform(Defs defs);
73
74private:
75#ifdef MIM_ENABLE_CHECKS
76 template<bool> bool fail();
77#else
78 template<bool> bool fail() { return false; }
79#endif
80
81 template<bool infer> bool alpha_(Ref d1, Ref d2);
82 template<bool infer> bool alpha_internal(Ref, Ref);
83 bool assignable_(Ref type, Ref value);
84
85 World& world_;
86 using Vars = MutMap<Def*>;
87 Vars vars_;
88 MutMap<Ref> done_;
89};
90
91} // namespace mim
static bool alpha(Ref d1, Ref d2)
Are d1 and d2 α-equivalent?
Definition check.h:65
static bool assignable(Ref type, Ref value)
Can value be assigned to sth of type?
Definition check.h:69
World & world()
Definition check.h:58
Check(World &world)
Definition check.h:55
static Ref is_uniform(Defs defs)
Yields defs.front(), if all defs are Check::alpha-equivalent (infer = false) and nullptr otherwise.
Definition check.cpp:213
Base class for all Defs.
Definition def.h:223
Def * set(size_t i, const Def *def)
Successively set from left to right.
Definition def.cpp:246
const Def * op(size_t i) const
Definition def.h:269
bool has_dep(Dep d) const
Definition def.h:336
World & world() const
Definition def.cpp:415
const Def * type() const
Definition def.h:248
flags_t flags() const
Definition def.h:238
flags_t flags_
Definition def.h:588
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
Definition def.cpp:264
const T * isa_imm() const
Definition def.h:441
Def * reset(size_t i, const Def *def)
Successively reset from left to right.
Definition def.h:291
Dbg dbg() const
Definition def.h:466
This node is a hole in the IR that is inferred by its context later on.
Definition check.h:12
static bool eliminate(Vector< Ref * >)
Eliminate Infers that may have been resolved in the meantime by rebuilding.
Definition check.cpp:62
Infer * stub_(World &, Ref) override
Definition def.cpp:130
static bool should_eliminate(Ref def)
Definition check.h:31
Infer * stub(Ref type)
Definition check.h:38
static const Def * find(const Def *)
Union-Find to unify Infer nodes.
Definition check.cpp:29
static constexpr auto Node
Definition check.h:40
friend class World
Definition check.h:49
const Def * op() const
Definition check.h:22
Infer * reset(const Def *op)
Definition check.h:24
Infer * unset()
Definition check.h:25
Infer * set(const Def *op)
Definition check.h:23
Ref rebuild_(World &, Ref, Defs) const override
Definition def.cpp:89
Helper class to retrieve Infer::arg if present.
Definition def.h:86
CRTP-based Mixin to declare setters for Def::loc & Def::name using a covariant return type.
Definition def.h:186
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
This is a thin wrapper for absl::InlinedVector<T, N, / A> which in turn is a drop-in replacement for ...
Definition vector.h:16
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
@ Infer
Definition def.h:40
Definition cfg.h:11
View< const Def * > Defs
Definition def.h:61
u64 flags_t
Definition types.h:45
GIDMap< Def *, To > MutMap
Definition def.h:68