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/tuple.h"
6#include "mim/world.h"
7
8// TODO this code needs to be rewritten
9
10namespace mim {
11
12namespace {
13bool should_flatten(const Def* def) {
14 auto type = (def->is_term() ? def->type() : def);
15 if (type->isa<Sigma>()) return true;
16 if (auto arr = type->isa<Arr>()) {
17 if (auto a = Lit::isa(arr->arity()); a && *a > def->world().flags().scalarize_threshold) return false;
18 return true;
19 }
20 return false;
21}
22
23bool mut_val_or_typ(const Def* def) {
24 auto typ = def->is_term() ? def->type() : def;
25 return typ->isa_mut();
26}
27
28const Def* unflatten(Defs defs, const Def* type, size_t& j, bool flatten_muts) {
29 if (!defs.empty() && defs[0]->type() == type) return defs[j++];
30 if (auto a = Lit::isa(type->arity());
31 flatten_muts == mut_val_or_typ(type) && a && *a != 1 && a <= type->world().flags().scalarize_threshold) {
32 auto& world = type->world();
33 auto ops = DefVec(*a, [&](size_t i) { return unflatten(defs, type->proj(*a, i), j, flatten_muts); });
34 return world.tuple(type, ops);
35 }
36
37 return defs[j++];
38}
39} // namespace
40
41const Def* Sigma::arity() const {
42 auto n = num_ops();
43 if (n != 1 || isa_mut()) return world().lit_nat(n);
44 return op(0)->arity();
45}
46
47const Def* Pack::arity() const {
48 if (auto arr = type()->isa<Arr>()) return arr->arity();
49 if (type() == world().sigma()) return world().lit_nat_0();
50 return world().lit_nat_1();
51}
52
53Select::Select(const Def* def) {
54 if (def) {
55 if ((extract_ = def->isa<Extract>())) {
56 pair_ = extract_->tuple();
57 cond_ = extract_->index();
58 if (!Lit::isa(cond_)) {
59 if (auto a = Lit::isa(pair_->arity()); a && a == 2) {
60 ff_ = pair_->proj(2, 0);
61 tt_ = pair_->proj(2, 1);
62 }
63 }
64 }
65 }
66}
67
69 : Select(def->isa<App>() ? def->as<App>()->callee() : nullptr) {
70 if ((app_ = def->isa<App>())) {
71 callee_ = app_->callee();
72 arg_ = app_->arg();
73 }
74}
75
76bool is_unit(const Def* def) { return def->type() == def->world().sigma(); }
77
78std::string tuple2str(const Def* def) {
79 if (def == nullptr) return {};
80
81 auto& w = def->world();
82 auto res = std::string();
83 if (auto n = Lit::isa(def->arity())) {
84 for (size_t i = 0; i != *n; ++i) {
85 auto elem = def->proj(*n, i);
86 if (elem->type() == w.type_i8()) {
87 if (auto l = Lit::isa<char>(elem)) {
88 res.push_back(*l);
89 continue;
90 }
91 }
92 return {};
93 }
94 }
95 return res;
96}
97
98size_t flatten(DefVec& ops, const Def* def, bool flatten_muts) {
99 if (auto a = Lit::isa(def->arity()); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
100 auto n = 0;
101 for (size_t i = 0; i != *a; ++i)
102 n += flatten(ops, def->proj(*a, i), flatten_muts);
103 return n;
104 } else {
105 ops.emplace_back(def);
106 return 1;
107 }
108}
109
110const Def* flatten(const Def* def) {
111 if (!should_flatten(def)) return def;
112 DefVec ops;
113 flatten(ops, def);
114 return def->is_intro() ? def->world().tuple(def->type(), ops) : def->world().sigma(ops);
115}
116
117const Def* unflatten(Defs defs, const Def* type, bool flatten_muts) {
118 size_t j = 0;
119 auto def = unflatten(defs, type, j, flatten_muts);
120 assert(j == defs.size());
121 return def;
122}
123
124const Def* unflatten(const Def* def, const Def* type) { return unflatten(def->projs(Lit::as(def->arity())), type); }
125
126/*
127 * merge & cat
128 */
129
131 auto res = DefVec();
132 res.reserve(a.size() + b.size());
133 res.insert(res.end(), a.begin(), a.end());
134 res.insert(res.end(), b.begin(), b.end());
135 return res;
136}
137
138DefVec cat(nat_t n, nat_t m, const Def* a, const Def* b) {
139 auto defs = DefVec();
140 defs.reserve(n + m);
141 for (size_t i = 0, e = n; i != e; ++i)
142 defs.emplace_back(a->proj(e, i));
143 for (size_t i = 0, e = m; i != e; ++i)
144 defs.emplace_back(b->proj(e, i));
145
146 return defs;
147}
148
149const Def* cat_tuple(nat_t n, nat_t m, const Def* a, const Def* b) { return a->world().tuple(cat(n, m, a, b)); }
150const Def* cat_sigma(nat_t n, nat_t m, const Def* a, const Def* b) { return a->world().sigma(cat(n, m, a, b)); }
151
152const Def* cat_tuple(World& world, Defs a, Defs b) { return world.tuple(cat(a, b)); }
153const Def* cat_sigma(World& world, Defs a, Defs b) { return world.sigma(cat(a, b)); }
154
155const Def* tuple_of_types(const Def* t) {
156 auto& world = t->world();
157 if (auto sigma = t->isa<Sigma>()) return world.tuple(sigma->ops());
158 if (auto arr = t->isa<Arr>()) return world.pack(arr->arity(), arr->body());
159 return t;
160}
161
162} // namespace mim
A (possibly paramterized) Array.
Definition tuple.h:117
const Def * callee() const
Definition tuple.h:288
Branch(const Def *)
Definition tuple.cpp:68
Base class for all Defs.
Definition def.h:251
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:585
World & world() const noexcept
Definition def.cpp:436
bool is_intro() const noexcept
Definition def.h:284
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:485
const Def * op(size_t i) const noexcept
Definition def.h:308
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:390
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:444
virtual const Def * arity() const
Definition def.cpp:550
constexpr size_t num_ops() const noexcept
Definition def.h:309
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:206
static std::optional< T > isa(const Def *def)
Definition def.h:824
static T as(const Def *def)
Definition def.h:830
const Def * arity() const final
Definition tuple.cpp:47
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Definition def.cpp:24
Select(const Def *)
Definition tuple.cpp:53
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Definition def.cpp:24
A dependent tuple type.
Definition tuple.h:20
const Def * arity() const final
Definition tuple.cpp:41
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:31
const Def * sigma(Defs ops)
Definition world.cpp:281
const Def * tuple(Defs ops)
Definition world.cpp:291
const Lit * lit_nat_0()
Definition world.h:456
const Lit * lit_nat(nat_t a)
Definition world.h:455
const Lit * lit_nat_1()
Definition world.h:457
Definition ast.h:14
View< const Def * > Defs
Definition def.h:76
u64 nat_t
Definition types.h:43
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Definition tuple.cpp:110
Vector< const Def * > DefVec
Definition def.h:77
const Def * cat_tuple(nat_t n, nat_t m, const Def *a, const Def *b)
Definition tuple.cpp:149
bool is_unit(const Def *)
Definition tuple.cpp:76
std::string tuple2str(const Def *)
Definition tuple.cpp:78
const Def * cat_sigma(nat_t n, nat_t m, const Def *a, const Def *b)
Definition tuple.cpp:150
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a Pack / Tuple, given the original type.
Definition tuple.cpp:124
const Def * tuple_of_types(const Def *t)
Definition tuple.cpp:155
DefVec cat(Defs, Defs)
Definition tuple.cpp:130