5#include <fe/worklist.h>
46 auto [nis_nps, meta, shapes, in_tys, comb_init, map_out, maps_all, is_all] = mr->uncurry_args<8>();
48 auto [nis, nps] = nis_nps->projs<2>();
50 if (
Lit::isa(nis) != 1 ||
Lit::isa(nps) != 0 || meta->proj(5, 2) != meta->proj(5, 3))
return {};
51 auto [So, Sr, sched] = shapes->projs<3>();
52 if (Sr != So)
return {};
53 auto id_lam = map_out->isa_mut<
Lam>();
54 if (!id_lam || !id_lam->is_set() || id_lam->body() != id_lam->var())
return {};
55 auto [comb, init, post] = comb_init->
projs<3>();
58 auto sole = [](
const Def* d,
u64 n,
u64 i) {
return d->proj(n, i)->proj(1, 0); };
59 return PureRead{sole(is_all, 2, 0), sole(maps_all, 2, 0), sole(in_tys, 6, 0), sole(in_tys, 6, 1),
65 auto& w = arr->
world();
66 return w.app(w.app(w.annex<
tensor::get>(), {T, r, s}), {index, arr});
71 auto& w = arr->
world();
72 return w.app(w.app(w.annex<
tensor::set>(), {T, r, s}), {index, arr, x});
83 auto charge = [&](
this auto&& charge,
const Def* d) ->
void {
86 else if (d->isa<
Tuple>() || d->isa<
Pack>())
87 for (
auto op : d->ops())
91 auto wl = fe::BFSWorklist<DefSet>();
92 wl.push(world.
roots());
96 auto transparent = def->isa<
Tuple>() || def->isa<
Pack>();
97 for (
auto op : def->ops())
99 if (!transparent) charge(op);
102 if (def->type()) wl.push(def->type());
static auto isa(const Def *def)
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.
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
World & world() const noexcept
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
static const Def * isa_ret_arg(const Def *d)
Yields the y of lm (x, ret) = ret y - the argument d's body hands to its Lam::ret_var.
static std::optional< T > isa(const Def *def)
A (possibly paramterized) Tuple.
Data constructor for a Sigma.
The World represents the whole program and manages creation of MimIR nodes (Defs).
auto roots() const
annexes() + externals().muts() in this order.
bool is_identity_post(const Def *post)
Is post the (rebuilt) CPS identity tensor.id, i.e.
const Def * op_set(const Def *T, const Def *r, const Def *s, const Def *arr, const Def *index, const Def *x)
std::optional< PureRead > is_pure_read(const Def *value)
If value is a pure re-indexed read — a copy-combiner map_reduce without reduction loops that writes i...
bool is_copy_comb(const Def *comb)
Recognizes the (rebuilt) tensor_copy combiner (acc, ys) ↦ ys#0: the result is exactly the single inpu...
const Def * op_get(const Def *T, const Def *r, const Def *s, const Def *arr, const Def *index)
DefMap< u64 > count_consumers(const World &world, Pred pred)
Counts the consumers of every def of world matched by pred.
A pure re-indexed read: the source tensor, the access map into it (over the read's output coordinates...
GIDMap< const Def *, To > DefMap