MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
tuple.cpp
Go to the documentation of this file.
1#include "mim/tuple.h"
2
3#include <cassert>
4
5#include "mim/world.h"
6
7// TODO this code needs to be rewritten
8
9namespace mim {
10
11namespace {
12bool should_flatten(const Def* def) {
13 auto type = (def->is_term() ? def->type() : def);
14 if (type->isa<Sigma>()) return true;
15 if (auto arr = type->isa<Arr>()) {
16 if (auto a = arr->isa_lit_arity(); a && *a > def->world().flags().scalarize_threshold) return false;
17 return true;
18 }
19 return false;
20}
21
22bool mut_val_or_typ(const Def* def) {
23 auto typ = def->is_term() ? def->type() : def;
24 return typ->isa_mut();
25}
26
27const Def* unflatten(Defs defs, const Def* type, size_t& j, bool flatten_muts) {
28 if (!defs.empty() && defs[0]->type() == type) return defs[j++];
29 if (auto a = type->isa_lit_arity();
30 flatten_muts == mut_val_or_typ(type) && a && *a != 1 && a <= type->world().flags().scalarize_threshold) {
31 auto& world = type->world();
32 auto ops = DefVec(*a, [&](size_t i) { return unflatten(defs, type->proj(*a, i), j, flatten_muts); });
33 return world.tuple(type, ops);
34 }
35
36 return defs[j++];
37}
38} // namespace
39
40const Def* Pack::shape() const {
41 if (auto arr = type()->isa<Arr>()) return arr->shape();
42 if (type() == world().sigma()) return world().lit_nat_0();
43 return world().lit_nat_1();
44}
45
46bool is_unit(const Def* def) { return def->type() == def->world().sigma(); }
47
48std::string tuple2str(const Def* def) {
49 if (def == nullptr) return {};
50
51 auto array = def->projs(Lit::as(def->arity()), [](auto op) { return Lit::as(op); });
52 return std::string(array.begin(), array.end());
53}
54
55size_t flatten(DefVec& ops, const Def* def, bool flatten_muts) {
56 if (auto a = def->isa_lit_arity(); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
57 auto n = 0;
58 for (size_t i = 0; i != *a; ++i) n += flatten(ops, def->proj(*a, i), flatten_muts);
59 return n;
60 } else {
61 ops.emplace_back(def);
62 return 1;
63 }
64}
65
66const Def* flatten(const Def* def) {
67 if (!should_flatten(def)) return def;
68 DefVec ops;
69 flatten(ops, def);
70 return def->is_term() ? def->world().tuple(def->type(), ops) : def->world().sigma(ops);
71}
72
73const Def* unflatten(Defs defs, const Def* type, bool flatten_muts) {
74 size_t j = 0;
75 auto def = unflatten(defs, type, j, flatten_muts);
76 assert(j == defs.size());
77 return def;
78}
79
80const Def* unflatten(const Def* def, const Def* type) { return unflatten(def->projs(Lit::as(def->arity())), type); }
81
82DefVec merge(const Def* def, Defs defs) {
83 return DefVec(defs.size() + 1, [&](auto i) { return i == 0 ? def : defs[i - 1]; });
84}
85
87 DefVec result(a.size() + b.size());
88 auto [_, o] = std::ranges::copy(a, result.begin());
89 std::ranges::copy(b, o);
90 return result;
91}
92
93const Def* merge_sigma(const Def* def, Defs defs) {
94 if (auto sigma = def->isa_imm<Sigma>()) return def->world().sigma(merge(sigma->ops(), defs));
95 return def->world().sigma(merge(def, defs));
96}
97
98const Def* merge_tuple(const Def* def, Defs defs) {
99 auto& w = def->world();
100 if (auto sigma = def->type()->isa_imm<Sigma>()) {
101 auto a = sigma->num_ops();
102 auto tuple = DefVec(a, [&](auto i) { return w.extract(def, a, i); });
103 return w.tuple(merge(tuple, defs));
104 }
105
106 return def->world().tuple(merge(def, defs));
107}
108
110 auto& world = t->world();
111 if (auto sigma = t->isa<Sigma>()) return world.tuple(sigma->ops());
112 if (auto arr = t->isa<Arr>()) return world.pack(arr->shape(), arr->body());
113 return t;
114}
115
116} // namespace mim
A (possibly paramterized) Array.
Definition tuple.h:66
Base class for all Defs.
Definition def.h:223
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.
Definition def.cpp:518
World & world() const
Definition def.cpp:415
bool is_term() const
Yields true if this:T and T:(Type 0).
Definition def.cpp:470
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
Definition def.h:367
const Def * type() const
Definition def.h:248
Ref arity() const
Definition def.cpp:481
std::optional< nat_t > isa_lit_arity() const
Definition def.cpp:488
const T * isa_imm() const
Definition def.h:441
static T as(Ref def)
Definition def.h:768
const Def * shape() const
Definition tuple.cpp:40
Helper class to retrieve Infer::arg if present.
Definition def.h:86
A dependent tuple type.
Definition tuple.h:9
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
Ref tuple(Defs ops)
Definition world.cpp:238
const Lit * lit_nat_0()
Definition world.h:389
Ref sigma(Defs ops)
Definition world.cpp:230
const Lit * lit_nat_1()
Definition world.h:390
Definition cfg.h:11
View< const Def * > Defs
Definition def.h:61
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Definition tuple.cpp:66
Vector< const Def * > DefVec
Definition def.h:62
bool is_unit(const Def *)
Definition tuple.cpp:46
const Def * merge_sigma(const Def *def, Defs defs)
Definition tuple.cpp:93
std::string tuple2str(const Def *)
Definition tuple.cpp:48
DefVec merge(Defs, Defs)
Definition tuple.cpp:86
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a Pack / Tuple, given the original type.
Definition tuple.cpp:80
Ref tuple_of_types(Ref t)
Definition tuple.cpp:109
const Def * merge_tuple(const Def *def, Defs defs)
Definition tuple.cpp:98