10using namespace std::literals;
21 , node_(unsigned(node))
30 , num_ops_(ops.size())
32 std::ranges::copy(
ops, ops_ptr());
35 if (
auto var = isa<Var>()) {
60 :
Def(nullptr, n, type, ops, flags) {}
74 muts_.fv_consumers =
Muts();
75 std::fill_n(ops_ptr(),
num_ops,
nullptr);
80 :
Def(Node, world.type(),
Defs{}, 0) {}
82UMax::UMax(World& world,
Defs ops)
83 : Def(Node, world.univ(), ops, 0) {}
100Ref Insert ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.insert(o[0], o[1], o[2]); }
103Ref Pack ::rebuild_(
World& w,
Ref t,
Defs o)
const {
return w.pack(t->arity(), o[0]); }
104Ref Pi ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.pi(o[0], o[1], is_implicit()); }
106Ref Proxy ::rebuild_(
World& w,
Ref t,
Defs o)
const {
return w.proxy(t, o, pass(), tag()); }
110Ref Test ::rebuild_(
World& w,
Ref ,
Defs o)
const {
return w.test(o[0], o[1], o[2], o[3]); }
115Ref Vel ::rebuild_(
World& w,
Ref t,
Defs o)
const {
return w.vel(t, o[0])->set(dbg()); }
118 if (&w != &world())
return w.axiom(normalizer(), curry(), trip(), t, plugin(), tag(), sub())->set(dbg());
135Pi* Pi ::stub_(
World& w,
Ref t) {
return w.mut_pi (t, is_implicit()); }
184 if (
auto n =
Lit::isa(
shape()); n && *n < w.flags().scalarize_threshold)
185 return w.sigma(
DefVec(*n, [&](
size_t i) {
return reduce(w.lit_idx(*n, i)); }));
194 if (
auto n =
Lit::isa(
shape()); n && *n < w.flags().scalarize_threshold)
195 return w.tuple(
DefVec(*n, [&](
size_t i) {
return reduce(w.lit_idx(*n, i)); }));
205 if (
auto mut =
isa_mut())
return mut->reduce(arg);
211 auto& cache =
world().move_.cache;
212 if (
auto i = cache.find({this, arg}); i != cache.end())
return i->second;
216 for (
size_t i = 0, e = res.size(); i != e; ++i) res[i] = rw.rewrite(
op(i));
218 return cache[{
this, arg}] = res;
224 if (
auto mut =
isa_mut(); mut &&
has_var())
return mut->reduce(arg)[i];
238void Def::finalize() {
243 const auto& p =
op->uses_.emplace(
this, i);
244 assert_unused(p.second);
259 assert(def && !
op(i) && curr_op_ == i);
261 curr_op_ = (curr_op_ + 1) %
num_ops();
264 const auto& p = def->uses_.emplace(
this, i);
265 assert_unused(p.second);
286 for (
size_t i = 0, e =
num_ops(); i != e; ++i) {
290 assert(std::all_of(ops_ptr() + i + 1, ops_ptr() +
num_ops(), [](
auto op) {
return !
op; }));
299 assert(ops_ptr()[i] && ops_ptr()[i]->uses_.contains(
Use(
this, i)));
300 ops_ptr()[i]->uses_.erase(
Use(
this, i));
301 ops_ptr()[i] =
nullptr;
306 if (
num_ops() == 0)
return true;
307 bool result =
ops().back();
308 assert((!result || std::ranges::all_of(
ops().rsubspan(1), [](
auto op) {
return op; }))
309 &&
"the last operand is set but others in front of it aren't");
323 if (
auto mut =
isa_mut())
return mut->free_vars();
336 for (
bool todo =
true; todo;) {
350 if (valid_ || mark_ == run)
return vars_.free;
353 auto fvs0 = vars_.free;
360 local_mut->muts_.fv_consumers =
world().
muts().insert(local_mut->muts_.fv_consumers,
this);
361 fvs =
world().
vars().merge(fvs, local_mut->free_vars(todo, run));
368 return vars_.free = fvs;
371void Def::validate() {
377 if (!local_mut->valid_) local_mut->validate();
381void Def::invalidate() {
386 muts_.fv_consumers.
clear();
392#ifdef MIM_ENABLE_CHECKS
412 if (isa<Univ>())
return *world_;
419 if (
auto t = isa<Type>())
return world().
type(
world().uinc(t->level()));
429#define CODE(op, abbr) \
430 case Node::op: return #abbr;
433 default: fe::unreachable();
438 if (isa<Type>() || isa<Univ>())
return Defs();
440 return Defs(ops_ptr() - 1, (
is_set() ? num_ops_ : 0) + 1);
451 if (var_)
return var_;
454 if (w.is_frozen())
return nullptr;
455 if (
auto lam = isa<Lam >())
return w.var(lam ->dom(), lam);
456 if (
auto pi = isa<Pi >())
return w.var(pi ->dom(), pi);
457 if (
auto sig = isa<Sigma>())
return w.var(sig, sig);
458 if (
auto arr = isa<Arr >())
return w.var(w.type_idx(arr ->shape()), arr );
459 if (
auto pack = isa<Pack >())
return w.var(w.type_idx(pack->shape()), pack);
460 if (isa<Bound >())
return w.var(
this,
this);
461 if (isa<Infer >())
return nullptr;
462 if (isa<Global>())
return nullptr;
467 if (
auto t =
type()) {
468 if (
auto u = t->type()) {
470 if (
auto level =
Lit::isa(
type->level()))
return *level == 0;
478 if (
auto sigma = isa<Sigma>())
return world().
lit_nat(sigma->num_ops());
479 if (
auto arr = isa<Arr >())
return arr->shape();
480 if (
auto t =
type())
return t->arity();
485 if (
auto sigma = isa<Sigma>())
return sigma->
num_ops();
486 if (
auto arr = isa<Arr >())
return Lit::isa(arr->shape());
487 if (
auto t =
type())
return t->isa_lit_arity();
493bool Def::equal(
const Def* other)
const {
494 if (isa<Univ>() || this->
isa_mut() || other->
isa_mut())
return this == other;
499 for (
size_t i = 0, e =
num_ops(); result && i != e; ++i) result &= this->
op(i) == other->
op(i);
515 static constexpr int Search_In_Uses_Threshold = 8;
519 if (
auto arr = isa<Arr>()) {
520 if (arr->arity()->isa<
Top>())
return arr->body();
521 return arr->reduce(w.lit_idx(a, i));
522 }
else if (
auto pack = isa<Pack>()) {
523 if (pack->arity()->isa<
Top>())
return pack->body();
524 assert(!w.is_frozen() &&
"TODO");
525 return pack->reduce(w.lit_idx(a, i));
529 if (!
type())
return this;
533 if (isa<Tuple>() || isa<Sigma>())
return op(i);
535 if (w.is_frozen() ||
uses().size() < Search_In_Uses_Threshold) {
536 for (
auto u :
uses()) {
537 if (
auto ex = u->isa<
Extract>(); ex && ex->
tuple() ==
this) {
538 if (
auto index =
Lit::isa(ex->index()); index && *index == i)
return ex;
542 if (w.is_frozen())
return nullptr;
545 return w.extract(
this, a, i);
553 if (
auto app = def->isa<
App>()) {
554 if (app->callee()->isa<
Idx>())
return app->arg();
562 if (
auto l =
Lit::isa(size))
return l;
567 if (size->isa<
Top>())
return 64;
A (possibly paramterized) Array.
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Ref reduce(Ref arg) const
static bool alpha(Ref d1, Ref d2)
bool is_set() const
Yields true if empty or the last op is set.
const Uses & uses() const
Ref var()
Not necessarily a Var: E.g., if the return type is [], this will yield ().
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
std::string_view node_name() const
Defs extended_ops() const
Vars local_vars() const
Vars reachable by following immutable extended_ops().
Ref unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
Def * set(size_t i, Ref)
Successively set from left to right.
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
DefVec reduce(Ref arg) const
Rewrites Def::ops by substituting this mutable's Var with arg.
bool is_term() const
Yields true if this:T and T:(Type 0).
const Def * debug_prefix(std::string) const
bool is_immutabilizable()
Def * reset(size_t i, Ref def)
Successively reset from left to right.
bool is_open() const
Has free_vars()?
Muts local_muts() const
Mutables reachable by following immutable extended_ops().
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.
Ref 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.
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
std::string unique_name() const
name + "_" + Def::gid
Ref refine(size_t i, Ref new_op) const
bool is_closed() const
Has no free_vars()?
Vars free_vars() const
Compute a global solution, i.e., by transitively following mutables as well.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Global * stub_(World &, Ref) override
A built-in constant of type Nat -> *.
static constexpr nat_t size2bitwidth(nat_t n)
static std::optional< nat_t > isa_lit(Ref def)
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.
Ref reduce(Ref 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.
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).
void make_internal(Def *def)
Flags & flags()
Retrive compile Flags.
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)
Vector< const Def * > DefVec
uint64_t scalarize_threshold
size_t hash_combine(size_t seed, T v)
PooledSet< const Var * > Vars