8using namespace std::literals;
19 , node_(unsigned(node))
28 , num_ops_(ops.size())
30 std::ranges::copy(
ops, ops_ptr());
33 if (
auto var = isa<Var>()) {
58 :
Def(nullptr, n, type, ops, flags) {}
72 muts_.fv_consumers =
Muts();
73 std::fill_n(ops_ptr(),
num_ops,
nullptr);
78 :
Def(Node, world.type(),
Defs{}, 0) {}
80UMax::UMax(World& world,
Defs ops)
81 : Def(Node, world.univ(), ops, 0) {}
97Ref Extract ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.extract(o[0], o[1]); }
98Ref Insert ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.insert(o[0], o[1], o[2]); }
101Ref Pack ::rebuild_(
World& w,
Ref t,
Defs o)
const {
return w.pack(t->arity(), o[0]); }
102Ref Pi ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.pi(o[0], o[1], is_implicit()); }
104Ref Proxy ::rebuild_(
World& w,
Ref t,
Defs o)
const {
return w.proxy(t, o, pass(), tag()); }
108Ref Test ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.test(o[0], o[1], o[2], o[3]); }
113Ref Vel ::rebuild_(
World& w,
Ref t,
Defs o)
const {
return w.vel(t, o[0])->set(dbg()); }
116 if (&w != &world())
return w.axiom(normalizer(), curry(), trip(), t, plugin(), tag(), sub())->set(dbg());
133Pi* Pi ::stub_(
World& w,
Ref t) {
return w.mut_pi (t, is_implicit()); }
176 if (
auto n =
Lit::isa(
shape()); n && *n < w.flags().scalarize_threshold)
177 return w.sigma(
DefVec(*n, [&](
size_t i) {
return reduce(w.lit_idx(*n, i)); }));
186 if (
auto n =
Lit::isa(
shape()); n && *n < w.flags().scalarize_threshold)
187 return w.tuple(
DefVec(*n, [&](
size_t i) {
return reduce(w.lit_idx(*n, i)); }));
207 if (
auto mut =
isa_mut())
return mut->reduce(arg);
212 auto& cache =
world().move_.cache;
213 if (
auto i = cache.find({this, arg}); i != cache.end())
return i->second;
215 return cache[{
this, arg}] =
rewrite(
this, arg);
228void Def::finalize() {
233 const auto& p =
op->uses_.emplace(
this, i);
234 assert_unused(p.second);
248 assert(def && !
op(i) && curr_op_ == i);
250 curr_op_ = (curr_op_ + 1) %
num_ops();
253 const auto& p = def->uses_.emplace(
this, i);
254 assert_unused(p.second);
269 for (
size_t i = 0, e =
num_ops(); i != e; ++i) {
273 assert(std::all_of(ops_ptr() + i + 1, ops_ptr() +
num_ops(), [](
auto op) {
return !
op; }));
282 assert(
op(i) &&
op(i)->uses_.contains(
Use(
this, i)));
283 op(i)->uses_.erase(
Use(
this, i));
284 ops_ptr()[i] =
nullptr;
310 if (
num_ops() == 0)
return true;
311 bool result =
ops().back();
312 assert((!result || std::ranges::all_of(
ops().rsubspan(1), [](
auto op) {
return op; }))
313 &&
"the last operand is set but others in front of it aren't");
327 if (
auto mut =
isa_mut())
return mut->free_vars();
341 for (
bool todo =
true; todo;) {
355 if (valid_ || mark_ == run)
return vars_.free;
358 auto fvs0 = vars_.free;
365 local_mut->muts_.fv_consumers =
world().
insert(local_mut->muts_.fv_consumers,
this);
366 fvs =
world().
merge(fvs, local_mut->free_vars(todo, run));
373 return vars_.free = fvs;
376void Def::validate() {
382 if (!local_mut->valid_) local_mut->validate();
386void Def::invalidate() {
391 muts_.fv_consumers.
clear();
397#ifdef MIM_ENABLE_CHECKS
416 if (isa<Univ>())
return *world_;
423 if (
auto t = isa<Type>())
return world().
type(
world().uinc(t->level()));
433#define CODE(op, abbr) \
434 case Node::op: return #abbr;
437 default: fe::unreachable();
442 if (isa<Type>() || isa<Univ>())
return Defs();
444 return Defs(ops_ptr() - 1, (
is_set() ? num_ops_ : 0) + 1);
455 if (var_)
return var_;
458 if (w.is_frozen())
return nullptr;
459 if (
auto lam = isa<Lam >())
return w.var(lam ->dom(), lam);
460 if (
auto pi = isa<Pi >())
return w.var(pi ->dom(), pi);
461 if (
auto sig = isa<Sigma>())
return w.var(sig, sig);
462 if (
auto arr = isa<Arr >())
return w.var(w.type_idx(arr ->shape()), arr );
463 if (
auto pack = isa<Pack >())
return w.var(w.type_idx(pack->shape()), pack);
464 if (isa<Bound >())
return w.var(
this,
this);
465 if (isa<Infer >())
return nullptr;
466 if (isa<Global>())
return nullptr;
471 if (
auto t =
type()) {
472 if (
auto u = t->type()) {
474 if (
auto level =
Lit::isa(
type->level()))
return *level == 0;
482 if (
auto sigma = isa<Sigma>())
return world().
lit_nat(sigma->num_ops());
483 if (
auto arr = isa<Arr >())
return arr->shape();
484 if (
auto t =
type())
return t->arity();
489 if (
auto sigma = isa<Sigma>())
return sigma->
num_ops();
490 if (
auto arr = isa<Arr >())
return Lit::isa(arr->shape());
491 if (
auto t =
type())
return t->isa_lit_arity();
497bool Def::equal(
const Def* other)
const {
498 if (isa<Univ>() || this->
isa_mut() || other->
isa_mut())
return this == other;
503 for (
size_t i = 0, e =
num_ops(); result && i != e; ++i) result &= this->
op(i) == other->
op(i);
519 static constexpr int Search_In_Uses_Threshold = 8;
522 if (!
type())
return this;
528 if (isa<Tuple>() || isa<Sigma>()) {
530 }
else if (
auto arr = isa<Arr>()) {
531 if (arr->arity()->isa<
Top>())
return arr->body();
532 return arr->reduce(w.lit_idx(a, i));
533 }
else if (
auto pack = isa<Pack>()) {
534 if (pack->arity()->isa<
Top>())
return pack->body();
535 assert(!w.is_frozen() &&
"TODO");
536 return pack->reduce(w.lit_idx(a, i));
539 if (w.is_frozen() ||
uses().size() < Search_In_Uses_Threshold) {
540 for (
auto u :
uses()) {
541 if (
auto ex = u->isa<
Extract>(); ex && ex->
tuple() ==
this) {
542 if (
auto index =
Lit::isa(ex->index()); index && *index == i)
return ex;
546 if (w.is_frozen())
return nullptr;
549 return w.extract(
this, a, i);
557 if (
auto app = def->isa<
App>()) {
558 if (app->callee()->isa<
Idx>())
return app->arg();
565 if (
size->isa<
Top>())
return 64;
A (possibly paramterized) Array.
const Def * immutabilize() override
Tries to make an immutable from a mutable.
const Def * shape() const
const Def * reduce(const Def *arg) const
static bool alpha(Ref d1, Ref d2)
Are d1 and d2 α-equivalent?
bool is_set() const
Yields true if empty or the last op is set.
Def * set(size_t i, const Def *def)
Successively set from left to right.
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.
const Uses & uses() const
Ref var()
Not necessarily a Var: E.g., if the return type is [], this will yield ().
const Def * op(size_t i) const
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
const Def * refine(size_t i, const Def *new_op) const
Def * set_type(const Def *)
std::string_view node_name() const
Defs extended_ops() const
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
bool is_term() const
Yields true if this:T and T:(Type 0).
const Def * debug_prefix(std::string) const
DefVec reduce(const Def *arg) const
Rewrites Def::ops by substituting this mutable's Var with arg.
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
bool is_open() const
Has free_vars()?
void update()
Resolves Infers of this Def's type.
const Def * debug_suffix(std::string) const
std::optional< nat_t > isa_lit_arity() const
Ref rebuild(World &w, Ref type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
std::string unique_name() const
name + "_" + Def::gid
bool is_closed() const
Has no free_vars()?
Def * reset(size_t i, const Def *def)
Successively reset from left to right.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
const Def * alloced_type() const
Global * stub_(World &, Ref) override
A built-in constant of type Nat -> *.
static Ref size(Ref def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static constexpr nat_t size2bitwidth(nat_t n)
This node is a hole in the IR that is inferred by its context later on.
static std::optional< T > isa(Ref def)
A (possibly paramterized) Tuple.
const Def * shape() const
const Def * reduce(const Def *arg) const
const Def * immutabilize() override
Tries to make an immutable from a mutable.
A dependent function type.
const Pi * immutabilize() override
Tries to make an immutable from a mutable.
constexpr bool contains(const T &elem) const
constexpr bool empty() const noexcept
constexpr void clear() noexcept
Helper class to retrieve Infer::arg if present.
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Ref rebuild_(World &, Ref, Defs) const override
This is a thin wrapper for std::span<T, N> with the following additional features:
Specific Bound depending on Up.
Ref rebuild_(World &, Ref, Defs) const override
TBound * stub_(World &, Ref) override
Extremum. Either Top (Up) or Bottom.
Ref rebuild_(World &, Ref, Defs) const override
TExt * stub_(World &, Ref) override
static constexpr size_t Type
The World represents the whole program and manages creation of MimIR nodes (Defs).
Vars vars(const Var *var)
void make_internal(Def *def)
Ref insert(Ref d, Ref i, Ref val)
Flags & flags()
Retrive compile Flags.
Vars merge(Vars a, Vars b)
Sym sym(std::string_view)
const Pi * pi(Ref dom, Ref codom, bool implicit=false)
void make_external(Def *def)
const Lit * lit_nat(nat_t a)
const Type * type(Ref level)
Vars erase(Vars vars, const Var *var)
Vector< const Def * > DefVec
uint64_t scalarize_threshold
hash_t murmur3(hash_t h, uint32_t key)
PooledSet< const Var * > Vars
DefVec rewrite(Def *mut, Ref arg)
hash_t murmur3_finalize(hash_t h, hash_t len)
hash_t murmur3_rest(hash_t h, uint8_t key)