19 m(Type, type, Judge::Meta) m(Univ, univ, Judge::Meta) m(UMax, umax, Judge::Meta) m(UInc, uinc, Judge::Meta) \
20 m(Pi, pi, Judge::Form) m(Lam, lam, Judge::Intro) m(App, app, Judge::Elim) \
21 m(Sigma, sigma, Judge::Form) m(Tuple, tuple, Judge::Intro) m(Extract, extract, Judge::Elim) m(Insert, insert, Judge::Intro | Judge::Elim) \
22 m(Arr, arr, Judge::Form) m(Pack, pack, Judge::Intro) \
23 m(Join, join, Judge::Form) m(Vel, vel, Judge::Intro) m(Test, test, Judge::Elim) m(Top, top, Judge::Intro) \
24 m(Meet, meet, Judge::Form) m(Ac, ac, Judge::Intro) m(Pick, pick, Judge::Elim) m(Bot, bot, Judge::Intro) \
25 m(Uniq, Uniq, Judge::Form) \
26 m(Nat, nat, Judge::Form) \
27 m(Idx, idx, Judge::Intro) m(Lit, lit, Judge::Intro) \
28 m(Axiom, axiom, Judge::Intro) \
29 m(Var, var, Judge::Intro) \
30 m(Global, global, Judge::Intro) \
31 m(Proxy, proxy, Judge::Intro) \
32 m(Hole, hole, Judge::Hole)
84#define CODE(node, name, _) node,
89#define CODE(node, name, _) +size_t(1)
97enum class Dep :
unsigned {
120template<>
struct fe::is_bit_enum<
mim::
Dep> : std::true_type {};
121template<>
struct fe::is_bit_enum<
mim::
Judge> : std::true_type {};
127#define MIM_PROJ(NAME, CONST) \
128 nat_t num_##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_projs(); } \
129 nat_t num_t##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_tprojs(); } \
130 const Def* NAME(nat_t a, nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(a, i); } \
131 const Def* NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(i); } \
132 const Def* t##NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->tproj(i); } \
133 template<nat_t A = std::dynamic_extent, class F> auto NAME##s(F f) CONST noexcept { \
134 return ((const Def*)NAME())->projs<A, F>(f); \
136 template<class F> auto t##NAME##s(F f) CONST noexcept { return ((const Def*)NAME())->tprojs<F>(f); } \
137 template<nat_t A = std::dynamic_extent> auto NAME##s() CONST noexcept { return ((const Def*)NAME())->projs<A>(); } \
138 auto t##NAME##s() CONST noexcept { return ((const Def*)NAME())->tprojs(); } \
139 template<class F> auto NAME##s(nat_t a, F f) CONST noexcept { return ((const Def*)NAME())->projs<F>(a, f); } \
140 auto NAME##s(nat_t a) CONST noexcept { return ((const Def*)NAME())->projs(a); }
143template<
class P,
class D = Def>
class Setters {
145 P* super() {
return static_cast<P*
>(
this); }
146 const P* super()
const {
return static_cast<const P*
>(
this); }
150 template<
bool Ow = false>
const P*
set(Loc l )
const { super()->D::template
set<Ow>(l);
return super(); }
151 template<
bool Ow = false> P*
set(Loc l ) { super()->D::template
set<Ow>(l);
return super(); }
152 template<
bool Ow = false>
const P*
set( Sym s )
const { super()->D::template
set<Ow>(s);
return super(); }
153 template<
bool Ow = false> P*
set( Sym s ) { super()->D::template
set<Ow>(s);
return super(); }
154 template<
bool Ow = false>
const P*
set( std::string s)
const { super()->D::template
set<Ow>(std::move(s));
return super(); }
155 template<
bool Ow = false> P*
set( std::string s) { super()->D::template
set<Ow>(std::move(s));
return super(); }
156 template<
bool Ow = false>
const P*
set(Loc l, Sym s )
const { super()->D::template
set<Ow>(l, s);
return super(); }
157 template<
bool Ow = false> P*
set(Loc l, Sym s ) { super()->D::template
set<Ow>(l, s);
return super(); }
158 template<
bool Ow = false>
const P*
set(Loc l, std::string s)
const { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
159 template<
bool Ow = false> P*
set(Loc l, std::string s) { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
160 template<
bool Ow = false>
const P*
set(
Dbg d )
const { super()->D::template
set<Ow>(d);
return super(); }
161 template<
bool Ow = false> P*
set(
Dbg d ) { super()->D::template
set<Ow>(d);
return super(); }
198class Def :
public fe::RuntimeCast<Def> {
200 Def& operator=(
const Def&) =
delete;
201 Def(
const Def&) =
delete;
217 constexpr u32 gid() const noexcept {
return gid_; }
218 constexpr u32 tid() const noexcept {
return tid_; }
219 constexpr u32 mark() const noexcept {
return mark_; }
220 constexpr size_t hash() const noexcept {
return hash_; }
221 constexpr Node node() const noexcept {
return node_; }
242 const Def*
type() const noexcept {
return type_; }
254 assert(a.has_value());
261 template<
size_t N = std::dynamic_extent>
constexpr auto ops() const noexcept {
264 const Def*
op(
size_t i)
const noexcept {
return ops()[i]; }
265 constexpr size_t num_ops() const noexcept {
return num_ops_; }
299 const Def*
dep(
size_t i) const noexcept {
return deps()[i]; }
315 bool has_dep(
unsigned u)
const {
return dep_ & u; }
345 template<nat_t A = std::dynamic_extent,
class F>
auto projs(F f)
const {
346 using R = std::decay_t<
decltype(f(
this))>;
347 if constexpr (A == std::dynamic_extent) {
351 std::array<R, A> array;
352 for (
nat_t i = 0; i != A; ++i) array[i] = f(
proj(A, i));
360 using R = std::decay_t<
decltype(f(
this))>;
363 template<nat_t A = std::dynamic_extent>
auto projs()
const {
364 return projs<A>([](
const Def* def) {
return def; });
367 return tprojs([](
const Def* def) {
return def; });
370 return projs(a, [](
const Def* def) {
return def; });
430 template<
class T = Def,
bool invert = false> T*
isa_mut()
const {
431 if constexpr (std::is_same<T, Def>::value)
432 return mut_ ^ invert ?
const_cast<Def*
>(
this) :
nullptr;
434 return mut_ ^ invert ?
const_cast<Def*
>(
this)->
template isa<T>() :
nullptr;
438 template<
class T = Def,
bool invert = false> T*
as_mut()
const {
439 assert(mut_ ^ invert);
440 if constexpr (std::is_same<T, Def>::value)
441 return const_cast<Def*
>(
this);
443 return const_cast<Def*
>(
this)->
template as<T>();
459 template<
bool Ow = false>
const Def*
set(Loc l)
const {
if (Ow || !
dbg_.loc())
dbg_.set(l);
return this; }
460 template<
bool Ow = false> Def*
set(Loc l) {
if (Ow || !
dbg_.loc())
dbg_.set(l);
return this; }
461 template<
bool Ow = false>
const Def*
set(Sym s)
const {
if (Ow || !
dbg_.sym())
dbg_.set(s);
return this; }
462 template<
bool Ow = false> Def*
set(Sym s) {
if (Ow || !
dbg_.sym())
dbg_.set(s);
return this; }
463 template<
bool Ow = false>
const Def*
set( std::string s)
const {
set(
sym(std::move(s)));
return this; }
464 template<
bool Ow = false> Def*
set( std::string s) {
set(
sym(std::move(s)));
return this; }
465 template<
bool Ow = false>
const Def*
set(Loc l, Sym s )
const {
set(l);
set(s);
return this; }
466 template<
bool Ow = false> Def*
set(Loc l, Sym s ) {
set(l);
set(s);
return this; }
467 template<
bool Ow = false>
const Def*
set(Loc l, std::string s)
const {
set(l);
set(
sym(std::move(s)));
return this; }
468 template<
bool Ow = false> Def*
set(Loc l, std::string s) {
set(l);
set(
sym(std::move(s)));
return this; }
469 template<
bool Ow = false>
const Def*
set(
Dbg d)
const {
set(d.loc(), d.sym());
return this; }
470 template<
bool Ow = false> Def*
set(
Dbg d) {
set(d.loc(), d.sym());
return this; }
482 const Def*
debug_suffix(std::string)
const {
return this; }
514 virtual const Def*
check(
size_t,
const Def* def) {
return def; }
522 void dump(
int max)
const;
523 void write(
int max)
const;
524 void write(
int max,
const char* file)
const;
525 std::ostream&
stream(std::ostream&,
int max)
const;
532 void dot(std::ostream& os, uint32_t max = 0xFFFFFF,
bool types =
false)
const;
534 void dot(
const char* file =
nullptr, uint32_t max = 0xFFFFFF,
bool types =
false)
const;
535 void dot(
const std::string& file, uint32_t max = 0xFFFFFF,
bool types =
false)
const {
536 return dot(file.c_str(), max, types);
544 Sym
sym(
const char*)
const;
545 Sym
sym(std::string_view)
const;
546 Sym
sym(std::string)
const;
550 virtual Def*
stub_(
World&,
const Def*) { fe::unreachable(); }
555 Def*
unset(
size_t i);
556 const Def** ops_ptr()
const {
557 return reinterpret_cast<const Def**
>(
reinterpret_cast<char*
>(
const_cast<Def*
>(
this + 1)));
559 bool equal(
const Def* other)
const;
567 mutable World* world_;
587 mutable u32 tid_ = 0;
590 template<
class D,
size_t N>
friend class Sets;
593 friend std::ostream&
operator<<(std::ostream&,
const Def*);
598using DefDef = std::tuple<const Def*, const Def*>;
602 if constexpr (
sizeof(size_t) == 8)
603 return hash((
u64(std::get<0>(pair)->gid()) << 32_u64) |
u64(std::get<1>(pair)->gid()));
613template<
class To>
using DefDefMap = absl::flat_hash_map<DefDef, To, DefDefHash, DefDefEq>;
614using DefDefSet = absl::flat_hash_set<DefDef, DefDefHash, DefDefEq>;
638class Univ :
public Def,
public Setters<Univ> {
652class UMax :
public Def,
public Setters<UMax> {
665class UInc :
public Def,
public Setters<UInc> {
687class Type :
public Def,
public Setters<Type> {
718 template<
class T = flags_t> T
get()
const {
719 static_assert(
sizeof(T) <= 8);
730 template<
class T = nat_t>
static std::optional<T>
isa(
const Def* def) {
732 if (
auto lit = def->isa<Lit>())
return lit->get<T>();
735 template<
class T = nat_t>
static T
as(
const Def* def) {
return def->as<Lit>()->
get<T>(); }
774 static const Def*
isa(
const Def* def);
775 static const Def*
as(
const Def* def) {
780 static std::optional<nat_t>
isa_lit(
const Def* def);
783 assert(res.has_value());
805class Proxy :
public Def,
public Setters<Proxy> {
830class Global :
public Def,
public Setters<Global> {
840 const Def*
init()
const {
return op(0); }
bool is_set() const
Yields true if empty or the last op is set.
size_t num_deps() const noexcept
const Def * set(Dbg d) const
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.
constexpr Node node() const noexcept
Def * set(size_t i, const Def *)
Successively set from left to right.
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
const Var * has_var() const
As above if this is a mutable.
Defs deps() const noexcept
const T * isa_imm(R(T::*f)() const) const
bool has_dep(Dep d) const
bool is_elim() const noexcept
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
nat_t as_lit_arity() const
bool has_dep(unsigned u) const
World & world() const noexcept
virtual const Def * check()
const Def * refine(size_t i, const Def *new_op) const
void dot(const std::string &file, uint32_t max=0xFFFFFF, bool types=false) const
std::string_view node_name() const
auto projs(nat_t a, F f) const
bool is_intro() const noexcept
constexpr auto ops() const noexcept
Vars local_vars() const
Vars reachable by following immutable deps().
constexpr flags_t flags() const noexcept
virtual Def * stub_(World &, const Def *)
const Def * set(std::string s) const
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
constexpr u32 mark() const noexcept
Used internally by free_vars().
constexpr u32 tid() const noexcept
Trie id - only used in Trie.
auto projs(nat_t a) const
friend std::ostream & operator<<(std::ostream &, const Def *)
This will stream def as an operand.
const Def * debug_prefix(std::string) const
const Def * op(size_t i) const noexcept
bool is_immutabilizable()
virtual const Def * rebuild_(World &w, const Def *type, Defs ops) const =0
DefVec reduce(const Def *arg) const
Rewrites Def::ops by substituting this mutable's Var with arg.
const Def * var(nat_t a, nat_t i) noexcept
void transfer_external(Def *to)
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
const Def * proj(nat_t i) const
As above but takes Def::num_projs as arity.
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
bool is_open() const
Has free_vars()?
constexpr size_t hash() const noexcept
virtual const Def * immutabilize()
Tries to make an immutable from a mutable.
bool is_form() const noexcept
virtual const Def * check(size_t, const Def *def)
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
const Def * debug_suffix(std::string) const
const Def * set(Sym s) const
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
const Def * dep(size_t i) const noexcept
const Def * rebuild(World &w, const Def *type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
const Def * set(Loc l, Sym s) const
bool is_meta() const noexcept
const Def * set(Loc l) const
void write(int max) const
Def * set(Loc l, std::string s)
Def * stub(const Def *type)
nat_t num_projs() const
Yields Def::as_lit_arity(), if it is in fact a Lit, or 1 otherwise.
std::optional< nat_t > isa_lit_arity() const
std::ostream & stream(std::ostream &, int max) const
friend void swap(World &, World &) noexcept
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
const Def * arity() const
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
std::string unique_name() const
name + "_" + Def::gid
const Def * set(Loc l, std::string s) const
const T * isa_imm() const
Muts users()
Set of mutables where this mutable is locally referenced.
bool is_closed() const
Has no free_vars()?
const Def * rebuild(const Def *type, Defs ops) const
Def * reset(size_t i, const Def *def)
Successively reset from left to right.
Def * stub(World &w, const Def *type)
u32 judge() const noexcept
const Def * tproj(nat_t i) const
As above but takes Def::num_tprojs.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
constexpr size_t num_ops() const noexcept
void set(const Def *init)
Global * stub(const Def *type)
const Def * alloced_type() const
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr auto Node
Global * stub_(World &, const Def *) override
This node is a hole in the IR that is inferred by its context later on.
static constexpr auto Node
static nat_t as_lit(const Def *def)
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr nat_t size2bitwidth(nat_t n)
static constexpr nat_t bitwidth2size(nat_t n)
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static std::optional< nat_t > isa_lit(const Def *def)
static const Def * as(const Def *def)
static constexpr auto Node
static std::optional< T > isa(const Def *def)
const Def * rebuild_(World &, const Def *, Defs) const override
static T as(const Def *def)
static constexpr auto Node
const Def * rebuild_(World &, const Def *, Defs) const override
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr auto Node
u32 pass() const
IPass::index within PassMan.
CRTP-based Mixin to declare setters for Def::loc & Def::name using a covariant return type.
const P * set(Sym s) const
const P * set(Loc l, Sym s) const
const P * set(Loc l) const
const P * set(Dbg d) const
const P * set(Loc l, std::string s) const
P * set(Loc l, std::string s)
const P * set(std::string s) const
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr auto Node
const Def * level() const
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr auto Node
static constexpr auto Node
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr auto Node
const Def * rebuild_(World &, const Def *, Defs) const override
const Def * rebuild_(World &, const Def *, Defs) const override
static constexpr auto Node
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
The World represents the whole program and manages creation of MimIR nodes (Defs).
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
DefMap< const Def * > Def2Def
Vector< const Def * > DefVec
absl::flat_hash_map< DefDef, To, DefDefHash, DefDefEq > DefDefMap
D bitcast(const S &src)
A bitcast from src of type S to D.
Dep
Tracks a dependency to certain Defs transitively through the Def::deps() up to but excliding mutables...
constexpr size_t hash_combine(size_t seed, T v) noexcept
std::tuple< const Def *, const Def * > DefDef
absl::flat_hash_set< K, GIDHash< K >, GIDEq< K > > GIDSet
GIDSet< const Var * > VarSet
GIDMap< const Var *, To > VarMap
GIDMap< const Def *, To > DefMap
absl::flat_hash_map< K, V, GIDHash< K >, GIDEq< K > > GIDMap
GIDSet< const Def * > DefSet
consteval size_t hash_begin() noexcept
static constexpr size_t Num_Nodes
const Def *(*)(const Def *, const Def *, const Def *) NormalizeFn
constexpr size_t hash(size_t h) noexcept
Sets< const Var >::Set Vars
absl::flat_hash_set< DefDef, DefDefHash, DefDefEq > DefDefSet
@ Intro
Term Introduction like λ(x: Nat): Nat = x.
@ Meta
Meta rules for Universe and Type levels.
@ Form
Type Formation like T -> T.
@ Elim
Term Elimination like f a.
Sort
TODO remove or fix this.
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >
GIDMap< Def *, To > MutMap
VarMap< const Var * > Var2Var
bool operator()(DefDef p1, DefDef p2) const
size_t operator()(DefDef pair) const