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& w = def->world();
52 auto res = std::string();
53 if (auto n = Lit::isa(def->arity())) {
54 for (size_t i = 0; i != *n; ++i) {
55 auto elem = def->proj(*n, i);
56 if (elem->type() == w.type_i8()) {
57 if (auto l = Lit::isa<char>(elem)) {
58 res.push_back(*l);
59 continue;
60 }
61 }
62 return {};
63 }
64 }
65 return res;
66}
67
68size_t flatten(DefVec& ops, const Def* def, bool flatten_muts) {
69 if (auto a = def->isa_lit_arity(); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
70 auto n = 0;
71 for (size_t i = 0; i != *a; ++i) n += flatten(ops, def->proj(*a, i), flatten_muts);
72 return n;
73 } else {
74 ops.emplace_back(def);
75 return 1;
76 }
77}
78
79const Def* flatten(const Def* def) {
80 if (!should_flatten(def)) return def;
81 DefVec ops;
82 flatten(ops, def);
83 return def->is_intro() ? def->world().tuple(def->type(), ops) : def->world().sigma(ops);
84}
85
86const Def* unflatten(Defs defs, const Def* type, bool flatten_muts) {
87 size_t j = 0;
88 auto def = unflatten(defs, type, j, flatten_muts);
89 assert(j == defs.size());
90 return def;
91}
92
93const Def* unflatten(const Def* def, const Def* type) { return unflatten(def->projs(Lit::as(def->arity())), type); }
94
95DefVec merge(const Def* def, Defs defs) {
96 return DefVec(defs.size() + 1, [&](size_t i) { return i == 0 ? def : defs[i - 1]; });
97}
98
100 DefVec result(a.size() + b.size());
101 auto [_, o] = std::ranges::copy(a, result.begin());
102 std::ranges::copy(b, o);
103 return result;
104}
105
106const Def* merge_sigma(const Def* def, Defs defs) {
107 if (auto sigma = def->isa_imm<Sigma>()) return def->world().sigma(merge(sigma->ops(), defs));
108 return def->world().sigma(merge(def, defs));
109}
110
111const Def* merge_tuple(const Def* def, Defs defs) {
112 auto& w = def->world();
113 if (auto sigma = def->type()->isa_imm<Sigma>()) {
114 auto a = sigma->num_ops();
115 auto tuple = DefVec(a, [&](auto i) { return w.extract(def, a, i); });
116 return w.tuple(merge(tuple, defs));
117 }
118
119 return def->world().tuple(merge(def, defs));
120}
121
122const Def* tuple_of_types(const Def* t) {
123 auto& world = t->world();
124 if (auto sigma = t->isa<Sigma>()) return world.tuple(sigma->ops());
125 if (auto arr = t->isa<Arr>()) return world.pack(arr->shape(), arr->body());
126 return t;
127}
128
129} // namespace mim
A (possibly paramterized) Array.
Definition tuple.h:74
Base class for all Defs.
Definition def.h:198
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:486
World & world() const noexcept
Definition def.cpp:371
bool is_intro() const noexcept
Definition def.h:231
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:340
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:242
std::optional< nat_t > isa_lit_arity() const
Definition def.cpp:456
const Def * arity() const
Definition def.cpp:449
const T * isa_imm() const
Definition def.h:419
static std::optional< T > isa(const Def *def)
Definition def.h:719
static T as(const Def *def)
Definition def.h:724
const Def * shape() const
Definition tuple.cpp:40
A dependent tuple type.
Definition tuple.h:9
const Def * sigma(Defs ops)
Definition world.cpp:258
const Def * tuple(Defs ops)
Definition world.cpp:266
const Lit * lit_nat_0()
Definition world.h:385
const Lit * lit_nat_1()
Definition world.h:386
Definition ast.h:14
View< const Def * > Defs
Definition def.h:49
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Definition tuple.cpp:79
Vector< const Def * > DefVec
Definition def.h:50
bool is_unit(const Def *)
Definition tuple.cpp:46
const Def * merge_sigma(const Def *def, Defs defs)
Definition tuple.cpp:106
std::string tuple2str(const Def *)
Definition tuple.cpp:48
DefVec merge(Defs, Defs)
Definition tuple.cpp:99
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a Pack / Tuple, given the original type.
Definition tuple.cpp:93
const Def * tuple_of_types(const Def *t)
Definition tuple.cpp:122
const Def * merge_tuple(const Def *def, Defs defs)
Definition tuple.cpp:111