MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
ll.h
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4#include <format>
5#include <iomanip>
6#include <optional>
7#include <ostream>
8#include <ranges>
9#include <string>
10
11#include <absl/container/btree_set.h>
12
13#include <mim/plug/clos/clos.h>
14#include <mim/plug/math/math.h>
15#include <mim/plug/mem/mem.h>
16#include <mim/plug/vec/vec.h>
17
18#include "mim/be/emitter.h"
19#include "mim/util/print.h"
20
21#include "mim/plug/core/core.h"
23
24// Lessons learned:
25// * **Always** follow all ops - even if you actually want to ignore one.
26// Otherwise, you might end up with an incorrect schedule.
27// This was the case for an Extract of type Mem.
28// While we want to ignore the value obtained from that, since there is no Mem value in LLVM,
29// we still want to **first** recursively emit code for its operands and **then** ignore the Extract itself.
30// * i1 has a different meaning in LLVM then in Mim:
31// * Mim: {0, 1} = i1
32// * LLVM: {0, -1} = i1
33// This is a problem when, e.g., using an index of type i1 as LLVM thinks like this:
34// getelementptr ..., i1 1 == getelementptr .., i1 -1
35namespace mim {
36
37class World;
38
39namespace ll {
40
41namespace clos = mim::plug::clos;
42namespace core = mim::plug::core;
43namespace math = mim::plug::math;
44namespace mem = mim::plug::mem;
45namespace vec = mim::plug::vec;
46
47namespace {
48inline const char* math_suffix(const Def* type) {
49 if (auto w = math::isa_f(type)) {
50 switch (*w) {
51 case 32: return "f";
52 case 64: return "";
53 }
54 }
55 error("unsupported floating point type '{}'", type);
56}
57
58inline const char* llvm_suffix(const Def* type) {
59 if (auto w = math::isa_f(type)) {
60 switch (*w) {
61 case 16: return ".f16";
62 case 32: return ".f32";
63 case 64: return ".f64";
64 }
65 }
66 error("unsupported floating point type '{}'", type);
67}
68
69// [%mem.M 0, T] => T
70// TODO there may be more instances where we have to deal with this trickery
71inline const Def* isa_mem_sigma_2(const Def* type) {
72 if (auto sigma = type->isa<Sigma>())
73 if (sigma->num_ops() == 2 && Axm::isa<mem::M>(sigma->op(0))) return sigma->op(1);
74 return {};
75}
76} // namespace
77
78void emit(World&, std::ostream&);
79
80int compile(World&, std::string name);
81int compile(World&, std::string ll, std::string out);
82int compile_and_run(World&, std::string name, std::string args = {});
83
84struct BB {
85 BB() = default;
86 BB(const BB&) = delete;
87 BB(BB&& other) noexcept = default;
88 BB& operator=(BB other) noexcept { return swap(*this, other), *this; }
89
90 std::deque<std::ostringstream>& head() { return parts[0]; }
91 std::deque<std::ostringstream>& body() { return parts[1]; }
92 std::deque<std::ostringstream>& tail() { return parts[2]; }
93
94 template<class... Args>
95 inline std::string assign(std::string_view name, const char* s, Args&&... args) {
96 print(print(body().emplace_back(), "{} = ", name), s, std::forward<Args>(args)...);
97 return std::string(name);
98 }
99
100 template<class... Args>
101 inline void tail(const char* s, Args&&... args) {
102 print(tail().emplace_back(), s, std::forward<Args>(args)...);
103 }
104
105 friend inline void swap(BB& a, BB& b) noexcept {
106 using std::swap;
107 swap(a.phis, b.phis);
108 swap(a.parts, b.parts);
109 }
110
112 std::array<std::deque<std::ostringstream>, 3> parts;
113};
114
115class Emitter : public mim::Emitter<std::string, std::string, BB, Emitter> {
116public:
118
119 Emitter(World& world, std::ostream& ostream)
120 : Super(world, "llvm_emitter", ostream) {}
121
122 bool is_valid(std::string_view s) { return !s.empty(); }
123 void start() override;
124 void emit_imported(Lam*);
125 virtual void emit_epilogue(Lam*);
126 std::string emit_bb(BB&, const Def*);
127 virtual std::string prepare();
128 void finalize();
129
130 virtual inline std::optional<std::string> isa_targetspecific_intrinsic(BB&, const Def*) { return std::nullopt; }
131 inline std::string as_targetspecific_intrinsic(BB& bb, const Def* def) {
132 if (auto res = isa_targetspecific_intrinsic(bb, def)) return res.value();
133 error("target-specific intrinsic detected but not handled in LLVM backend: {} : {}", def, def->type());
134 }
135
136 template<class... Args>
137 void declare(const char* s, Args&&... args) {
138 std::ostringstream decl;
139 print(decl << "declare ", s, std::forward<Args>(args)...);
140 decls_.emplace(decl.str());
141 }
142
143private:
144 std::string id(const Def*, bool force_bb = false) const;
145 std::string convert(const Def*, bool simd = true);
146 std::string convert_ret_pi(const Pi*);
147
148 absl::btree_set<std::string> decls_;
149 std::ostringstream type_decls_;
150 std::ostringstream vars_decls_;
151 std::ostringstream func_decls_;
152 std::ostringstream func_impls_;
153 LamMap<const Def*> simd_phi_;
154};
155
156/*
157 * convert
158 */
159
160inline static std::optional<std::pair<nat_t, const Def*>> is_simd(const Def* type) {
161 if (auto arr = type->isa<Arr>()) {
162 if (auto l = Lit::isa(arr->arity())) {
163 if (arr->body()->isa<Nat>() || Idx::isa(arr->body()) || Axm::isa<math::F>(arr->body()))
164 return std::pair{*l, arr->body()};
165 }
166 }
167 return {};
168}
169
170inline static std::optional<std::pair<nat_t, const Def*>> is_simd_aggregate(const std::vector<const Def*> types) {
171 if (std::ranges::all_of(types, [&](auto i) { return i == types[0]; })) {
172 if (types[0]->isa<Nat>() || Idx::isa(types[0]) || Axm::isa<math::F>(types[0]))
173 return std::pair{types.size(), types[0]};
174 }
175
176 return {};
177}
178
179inline static const Def* find_common_simd_src(const App* app) {
180 const Def* common_src = nullptr;
181 for (auto arg : app->args()) {
182 if (Axm::isa<mem::M>(arg->type())) continue;
183 auto extract = arg->isa<Extract>();
184 if (!extract || !is_simd(extract->tuple()->type())) return nullptr;
185 if (!common_src)
186 common_src = extract->tuple();
187 else if (common_src != extract->tuple())
188 return nullptr;
189 }
190 return common_src;
191}
192
193inline std::string Emitter::id(const Def* def, bool force_bb /*= false*/) const {
194 if (auto global = def->isa<Global>()) return "@" + global->unique_name();
195
196 if (auto lam = def->isa_mut<Lam>(); lam && !force_bb) {
197 if (lam->type()->ret_pi()) {
198 if (lam->is_external() || !lam->is_set())
199 return std::string("@") + lam->sym().str(); // TODO or use is_internal or sth like that?
200 return std::string("@") + lam->unique_name();
201 }
202 }
203
204 return std::string("%") + def->unique_name();
205}
206
207inline std::string Emitter::convert(const Def* type, bool simd) {
208 if (auto i = types_.find(type); i != types_.end()) return i->second;
209
210 assert(!Axm::isa<mem::M>(type));
211 std::ostringstream s;
212 std::string name;
213
214 if (type->isa<Nat>()) {
215 return types_[type] = "i64";
216 } else if (auto size = Idx::isa(type)) {
217 return types_[type] = "i" + std::to_string(*Idx::size2bitwidth(size));
218 } else if (auto w = math::isa_f(type)) {
219 switch (*w) {
220 case 16: return types_[type] = "half";
221 case 32: return types_[type] = "float";
222 case 64: return types_[type] = "double";
223 default: fe::unreachable();
224 }
225 } else if (auto ptr = Axm::isa<mem::Ptr>(type)) {
226 auto [pointee, addr_space] = ptr->args<2>();
227 // TODO addr_space
228 print(s, "{}*", convert(pointee, false));
229 } else if (auto arr = type->isa<Arr>()) {
230 if (auto se = is_simd(arr); se && simd) {
231 auto [size, elem] = *se;
232 print(s, "<{} x {}>", size, convert(elem));
233 } else {
234 u64 size = 0;
235 if (auto arity = Lit::isa(arr->arity())) size = *arity;
236 print(s, "[{} x {}]", size, convert(arr->body(), false));
237 }
238 } else if (auto pi = type->isa<Pi>()) {
239 assert(Pi::isa_returning(pi) && "should never have to convert type of BB");
240 print(s, "{} (", convert_ret_pi(pi->ret_pi()));
241
242 if (auto t = isa_mem_sigma_2(pi->dom()))
243 s << convert(t);
244 else {
245 auto doms = pi->doms();
246 for (auto sep = ""; auto dom : doms.view().rsubspan(1)) {
247 if (Axm::isa<mem::M>(dom)) continue;
248 s << sep << convert(dom);
249 sep = ", ";
250 }
251 }
252 s << ")*";
253 } else if (auto t = isa_mem_sigma_2(type)) {
254 return convert(t);
255 } else if (auto sigma = type->isa<Sigma>()) {
256 if (sigma->isa_mut()) {
257 name = id(sigma);
258 types_[sigma] = name;
259 print(s, "{} = type", name);
260 }
261
262 print(s, "{{");
263 for (auto sep = ""; auto t : sigma->ops()) {
264 if (Axm::isa<mem::M>(t)) continue;
265 s << sep << convert(t);
266 sep = ", ";
267 }
268 print(s, "}}");
269 } else {
270 fe::unreachable();
271 }
272
273 if (name.empty()) return types_[type] = s.str();
274
275 assert(!s.str().empty());
276 type_decls_ << s.str() << '\n';
277 return types_[type] = name;
278}
279
280inline std::string Emitter::convert_ret_pi(const Pi* pi) {
281 auto dom = mem::strip_mem_ty(pi->dom());
282 if (dom == world().sigma()) return "void";
283 return convert(dom);
284}
285
286/*
287 * emit
288 */
289
290inline void Emitter::start() {
291 Super::start();
292
293 ostream() << type_decls_.str() << '\n';
294 for (auto&& decl : decls_)
295 ostream() << decl << '\n';
296 ostream() << func_decls_.str() << '\n';
297 ostream() << vars_decls_.str() << '\n';
298 ostream() << func_impls_.str() << '\n';
299}
300
301inline void Emitter::emit_imported(Lam* lam) {
302 // TODO merge with declare method
303 print(func_decls_, "declare {} {}(", convert_ret_pi(lam->type()->ret_pi()), id(lam));
304
305 auto doms = lam->doms();
306 for (auto sep = ""; auto dom : doms.view().rsubspan(1)) {
307 if (Axm::isa<mem::M>(dom)) continue;
308 print(func_decls_, "{}{}", sep, convert(dom));
309 sep = ", ";
310 }
311
312 print(func_decls_, ")\n");
313}
314
315inline std::string Emitter::prepare() {
316 auto internal = root()->is_external() ? "" : "internal ";
317 auto ret_t = convert_ret_pi(root()->type()->ret_pi());
318 print(func_impls_, "define {} {} {}(", internal, ret_t, id(root()));
319
320 auto vars = root()->vars();
321 for (auto sep = ""; auto var : vars.view().rsubspan(1)) {
322 if (Axm::isa<mem::M>(var->type())) continue;
323 if (auto arr = var->type()->isa<Arr>())
324 if (is_simd(arr->body())) convert(arr->body()); // pre-add input vector to cache
325 auto name = id(var);
326 locals_[var] = name;
327 print(func_impls_, "{}{} {}", sep, convert(var->type()), name);
328 sep = ", ";
329 }
330
331 print(func_impls_, ") {{\n");
332 return root()->unique_name();
333}
334
335inline void Emitter::finalize() {
336 for (auto& [lam, bb] : lam2bb_) {
337 for (const auto& [phi, args] : bb.phis) {
338 print(bb.head().emplace_back(), "{} = phi {} ", id(phi), convert(phi->type()));
339 for (auto sep = ""; const auto& [arg, pred] : args) {
340 print(bb.head().back(), "{}[ {}, {} ]", sep, arg, pred);
341 sep = ", ";
342 }
343 }
344 }
345
346 for (auto mut : Scheduler::schedule(nest())) {
347 if (auto lam = mut->isa_mut<Lam>()) {
348 assert(lam2bb_.contains(lam));
349 auto& bb = lam2bb_[lam];
350 print(func_impls_, "{}:\n", lam->unique_name());
351
352 ++tab;
353 for (const auto& part : bb.parts)
354 for (const auto& line : part)
355 tab.print(func_impls_, "{}\n", line.str());
356 --tab;
357 func_impls_ << std::endl;
358 }
359 }
360
361 print(func_impls_, "}}\n\n");
362}
363
364/*
365 Block type return
366BB:
367 Cn [M, a, A] → 2 phi
368 Cn «2;A» → 2 phi
369 Cn [M, «2;A»] → 1 phi
370Ret:
371 Cn[M,A,A] → 1 phi
372 Cn «2;A» → 1 phi
373 Cn[M, «2;A»] → 1 phi
374
375Fun:
376 Cn[A, A, Cn R] → 2 args + ret
377 Cn[«2; A», Cn R] → 1 args + ret
378 Cn[M, A, A, Cn R] → 2 args + ret
379 Cn[M, «2; A», Cn R] → 1 args + ret
380*/
381inline void Emitter::emit_epilogue(Lam* lam) {
382 auto app = lam->body()->as<App>();
383 auto& bb = lam2bb_[lam];
384 if (app->callee() == root()->ret_var()) { // return
385 std::vector<std::string> values;
386 std::vector<const Def*> types;
387 for (auto arg : app->args()) {
388 if (auto val = emit_unsafe(arg); !val.empty()) {
389 values.emplace_back(val);
390 types.emplace_back(arg->type());
391 }
392 }
393
394 switch (values.size()) {
395 case 0: return bb.tail("ret void");
396 case 1: return bb.tail("ret {} {}", convert(types[0]), values[0]);
397 default: {
398 std::string type;
399 std::string prev;
400
401 if (auto se = is_simd_aggregate(types)) {
402 auto common_src = find_common_simd_src(app);
403 if (common_src) {
404 auto v_src = emit(common_src);
405 auto t = convert(common_src->type());
406 return bb.tail("ret {} {}", t, v_src);
407 }
408 auto [size, elem] = *se;
409 auto val_t = convert(elem);
410
411 type = std::format("<{} x {}>", size, val_t);
412 for (auto val : values) {
413 if (prev.empty())
414 prev = "<";
415 else
416 prev += ", ";
417 prev += std::format("{} {}", val_t, val);
418 }
419 prev += ">";
420 } else {
421 prev = "undef";
422 type = convert(world().sigma(types));
423 for (size_t i = 0, n = values.size(); i != n; ++i) {
424 auto v_elem = values[i];
425 auto t_elem = convert(types[i]);
426 auto namei = "%ret_val." + std::to_string(i);
427 bb.tail("{} = insertvalue {} {}, {} {}, {}", namei, type, prev, t_elem, v_elem, i);
428 prev = namei;
429 }
430 }
431 bb.tail("ret {} {}", type, prev);
432 }
433 }
434
435 } else if (auto dispatch = Dispatch(app)) {
436 for (auto callee : dispatch.tuple()->projs([](const Def* def) { return def->isa_mut<Lam>(); })) {
437 size_t n = callee->num_tvars();
438 if (n == 1 && is_simd(callee->var(0)->type())) {
439 auto phi = callee->var(0);
440 auto arg = emit(app->arg(n, 0));
441 lam2bb_[callee].phis[phi].emplace_back(arg, id(lam, true));
442 locals_[phi] = id(phi);
443 } else {
444 for (size_t i = 0; i != n; ++i) {
445 if (auto arg = emit_unsafe(app->arg(n, i)); !arg.empty()) {
446 auto phi = callee->var(n, i);
447 assert(!Axm::isa<mem::M>(phi->type()));
448 lam2bb_[callee].phis[phi].emplace_back(arg, id(lam, true));
449 locals_[phi] = id(phi);
450 }
451 }
452 }
453 }
454
455 auto v_index = emit(dispatch.index());
456 size_t n = dispatch.num_targets();
457 auto bbs = absl::FixedArray<std::string>(n);
458 for (size_t i = 0; i != n; ++i)
459 bbs[i] = emit(dispatch.target(i));
460
461 if (auto branch = Branch(app)) return bb.tail("br i1 {}, label {}, label {}", v_index, bbs[1], bbs[0]);
462
463 auto t_index = convert(dispatch.index()->type());
464 bb.tail("switch {} {}, label {} [ ", t_index, v_index, bbs[0]);
465 for (size_t i = 1; i != n; ++i)
466 print(bb.tail().back(), "{} {}, label {} ", t_index, std::to_string(i), bbs[i]);
467 print(bb.tail().back(), "]");
468 } else if (app->callee()->isa<Bot>()) {
469 return bb.tail("ret ; bottom: unreachable");
470 } else if (auto callee = Lam::isa_mut_basicblock(app->callee())) { // ordinary jump
471
472 auto common_src = find_common_simd_src(app);
473 if (common_src) {
474 auto v_src = emit(common_src);
475 auto callee_var = callee->var();
476 if (simd_phi_.find(callee) == simd_phi_.end()) simd_phi_[callee] = callee_var;
477 auto key = simd_phi_[callee];
478 lam2bb_[callee].phis[key].emplace_back(v_src, id(lam, true));
479 locals_[key] = id(key);
480 for (auto var : callee->vars())
481 locals_[var] = id(key);
482 locals_[callee_var] = id(key);
483 } else {
484 size_t n = callee->num_tvars();
485 for (size_t i = 0; i != n; ++i) {
486 if (auto arg = emit_unsafe(app->arg(n, i)); !arg.empty()) {
487 auto phi = callee->var(n, i);
488 assert(!Axm::isa<mem::M>(phi->type()));
489 lam2bb_[callee].phis[phi].emplace_back(arg, id(lam, true));
490 locals_[phi] = id(phi);
491 }
492 }
493 }
494 return bb.tail("br label {}", id(callee));
495
496 } else if (auto longjmp = Axm::isa<clos::longjmp>(app)) {
497 declare("void @longjmp(i8*, i32) noreturn");
498
499 auto [mem, jbuf, tag] = app->args<3>();
500 emit_unsafe(mem);
501 auto v_jb = emit(jbuf);
502 auto v_tag = emit(tag);
503 bb.tail("call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
504 return bb.tail("unreachable");
505 } else if (Pi::isa_returning(app->callee_type())) { // function call
506 auto v_callee = emit(app->callee());
507
508 std::vector<std::string> args;
509 auto app_args = app->args();
510 for (auto arg : app_args.view().rsubspan(1))
511 if (auto v_arg = emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) + " " + v_arg);
512
513 if (app->args().back()->isa<Bot>()) {
514 // TODO: Perhaps it'd be better to simply η-wrap this prior to the BE...
515 assert(convert_ret_pi(app->callee_type()->ret_pi()) == "void");
516 bb.tail("call void {}({, })", v_callee, args);
517 return bb.tail("unreachable");
518 }
519
520 auto ret_lam = app->args().back()->as_mut<Lam>();
521 size_t num_vars = ret_lam->num_vars();
522 size_t n = 0;
523 DefVec values(num_vars);
524 DefVec types(num_vars);
525 for (auto var : ret_lam->vars()) {
526 if (Axm::isa<mem::M>(var->type())) continue;
527 values[n] = var;
528 types[n] = var->type();
529 ++n;
530 }
531
532 if (n == 0) {
533 bb.tail("call void {}({, })", v_callee, args);
534 } else {
535 auto name = "%" + app->unique_name() + "ret";
536 auto t_ret = convert_ret_pi(ret_lam->type());
537 bb.tail("{} = call {} {}({, })", name, t_ret, v_callee, args);
538 auto phi = ret_lam->var();
539 lam2bb_[ret_lam].phis[phi].emplace_back(name, id(lam, true));
540 locals_[phi] = id(phi);
541 }
542
543 return bb.tail("br label {}", id(ret_lam));
544 }
545}
546
547inline std::string Emitter::emit_bb(BB& bb, const Def* def) {
548 if (auto lam = def->isa<Lam>()) return id(lam);
549
550 auto name = id(def);
551 std::string op;
552
553 auto emit_tuple = [&](const Def* tuple) {
554 if (isa_mem_sigma_2(tuple->type())) {
555 emit_unsafe(tuple->proj(2, 0));
556 return emit(tuple->proj(2, 1));
557 }
558
559 if (tuple->is_closed()) {
560 bool is_array = tuple->type()->isa<Arr>();
561 auto simd_array = convert(tuple->type()).front() == '<'; // needed to respect pointer context
562 std::string s;
563 s += simd_array ? "<" : is_array ? "[" : "{";
564 auto sep = "";
565 for (size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
566 auto e = tuple->proj(n, i);
567 if (auto v_elem = emit_unsafe(e); !v_elem.empty()) {
568 auto t_elem = convert(e->type());
569 s += sep + t_elem + " " + v_elem;
570 sep = ", ";
571 }
572 }
573
574 return s += simd_array ? ">" : is_array ? "]" : "}";
575 }
576
577 std::string prev = "undef";
578 auto t = convert(tuple->type());
579 for (size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
580 auto e = tuple->proj(n, src);
581 if (auto elem = emit_unsafe(e); !elem.empty()) {
582 auto elem_t = convert(e->type());
583 // TODO: check dst vs src
584 auto namei = name + "." + std::to_string(dst);
585 if (t.front() == '<') // not using is_simd to respect the pointer context (Pointer Pointee case)
586 prev = bb.assign(namei, "insertelement {} {}, {} {}, {} {}", t, prev, elem_t, elem, elem_t, dst);
587 else
588 prev = bb.assign(namei, "insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
589 dst++;
590 }
591 }
592 return prev;
593 };
594
595 if (def->isa<Var>()) {
596 if (is_simd(def->type())) return id(def);
597 auto ts = def->type()->projs();
598 if (std::ranges::any_of(ts, [](auto t) { return Axm::isa<mem::M>(t); })) return {};
599 return emit_tuple(def);
600 }
601
602 auto emit_gep_index = [&](const Def* index) {
603 auto v_i = emit(index);
604 auto t_i = convert(index->type());
605
606 if (auto size = Idx::isa(index->type())) {
607 if (auto w = Idx::size2bitwidth(size); w && *w < 64) {
608 v_i = bb.assign(name + ".zext",
609 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
610 t_i, v_i, *w + 1);
611 t_i = "i" + std::to_string(*w + 1);
612 }
613 }
614
615 return std::pair(v_i, t_i);
616 };
617
618 if (auto lit = def->isa<Lit>()) {
619 if (lit->type()->isa<Nat>() || Idx::isa(lit->type())) {
620 return std::to_string(lit->get());
621 } else if (auto w = math::isa_f(lit->type())) {
622 std::stringstream s;
623 u64 hex;
624
625 switch (*w) {
626 case 16:
627 s << "0xH" << std::setfill('0') << std::setw(4) << std::right << std::hex << lit->get<u16>();
628 return s.str();
629 case 32: {
630 hex = std::bit_cast<u64>(f64(lit->get<f32>()));
631 break;
632 }
633 case 64: hex = lit->get<u64>(); break;
634 default: fe::unreachable();
635 }
636
637 s << "0x" << std::setfill('0') << std::setw(16) << std::right << std::hex << hex;
638 return s.str();
639 }
640 fe::unreachable();
641 } else if (def->isa<Bot>()) {
642 return "undef";
643 } else if (auto top = def->isa<Top>()) {
644 if (Axm::isa<mem::M>(top->type())) return {};
645 // bail out to error below
646 } else if (auto tuple = def->isa<Tuple>()) {
647 return emit_tuple(tuple);
648 } else if (auto pack = def->isa<Pack>()) {
649 if (auto lit = Lit::isa(pack->body()); lit && *lit == 0) return "zeroinitializer";
650 return emit_tuple(pack);
651 } else if (auto sel = Select(def)) {
652 auto t = convert(sel.extract()->type());
653 auto [elem_a, elem_b] = sel.pair()->projs<2>([&](auto e) { return emit_unsafe(e); });
654 auto cond_t = convert(sel.cond()->type());
655 auto cond = emit(sel.cond());
656 return bb.assign(name, "select {} {}, {} {}, {} {}", cond_t, cond, t, elem_b, t, elem_a);
657 } else if (auto extract = def->isa<Extract>()) {
658 auto tuple = extract->tuple();
659 auto index = extract->index();
660 auto v_tup = emit_unsafe(tuple);
661 if (is_simd(tuple->type()) && !Axm::isa<mem::M>(tuple->type())) return v_tup;
662
663 // this exact location is important: after emitting the tuple -> ordering of mem ops
664 // before emitting the index, as it might be a weird value for mem vars.
665 if (Axm::isa<mem::M>(extract->type())) return {};
666
667 auto t_tup = convert(tuple->type());
668 if (auto li = Lit::isa(index)) {
669 if (isa_mem_sigma_2(tuple->type())) return v_tup;
670 // Adjust index, if mem is present.
671 auto v_i = Axm::isa<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
672
673 return bb.assign(name, "extractvalue {} {}, {}", t_tup, v_tup, v_i);
674 }
675
676 auto t_elem = convert(extract->type());
677 auto [v_i, t_i] = emit_gep_index(index);
678
679 print(lam2bb_[root()].body().emplace_front(),
680 "{}.alloca = alloca {} ; copy to alloca to emulate extract with store + gep + load", name, t_tup);
681 print(bb.body().emplace_back(), "store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup, name);
682 print(bb.body().emplace_back(), "{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}", name, t_tup,
683 t_tup, name, t_i, v_i);
684 return bb.assign(name, "load {}, {}* {}.gep", t_elem, t_elem, name);
685 } else if (auto insert = def->isa<Insert>()) {
686 assert(!Axm::isa<mem::M>(insert->tuple()->proj(0)->type()));
687 auto t_tup = convert(insert->tuple()->type());
688 auto t_val = convert(insert->value()->type());
689 auto v_tup = emit(insert->tuple());
690 auto v_val = emit(insert->value());
691 if (auto idx = Lit::isa(insert->index())) {
692 auto v_idx = emit(insert->index());
693 if (is_simd(insert->tuple()->type()))
694
695 return bb.assign(name, "insertelement {} {}, {} {}, i32 {}", t_tup, v_tup, t_val, v_val, v_idx);
696 else
697
698 return bb.assign(name, " insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
699 } else {
700 if (is_simd(insert->tuple()->type())) {
701 auto v_i = emit(insert->index());
702 auto t_i = convert(insert->index()->type());
703 if (t_i != "i32") {
704 auto w_src = *Idx::size2bitwidth(Idx::isa(insert->index()->type()));
705 v_i = bb.assign(name + ".idx", "{} {} {} to i32", w_src < 32 ? "zext" : "trunc", t_i, v_i);
706 }
707 return bb.assign(name, "insertelement {} {}, {} {}, i32 {}", t_tup, v_tup, t_val, v_val, v_i);
708 }
709 auto t_elem = convert(insert->value()->type());
710 auto [v_i, t_i] = emit_gep_index(insert->index());
711 print(lam2bb_[root()].body().emplace_front(),
712 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load", name, t_tup);
713 print(bb.body().emplace_back(), "store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup, name);
714 print(bb.body().emplace_back(), "{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}", name,
715 t_tup, t_tup, name, t_i, v_i);
716 print(bb.body().emplace_back(), "store {} {}, {}* {}.gep", t_val, v_val, t_val, name);
717 return bb.assign(name, "load {}, {}* {}.alloca", t_tup, t_tup, name);
718 }
719 } else if (auto global = def->isa<Global>()) {
720 auto v_init = emit(global->init());
721 auto [pointee, addr_space] = Axm::as<mem::Ptr>(global->type())->args<2>();
722 print(vars_decls_, "{} = global {} {}\n", name, convert(pointee), v_init);
723 return globals_[global] = name;
724 } else if (auto nat = Axm::isa<core::nat>(def)) {
725 auto [a, b] = nat->args<2>([this](auto def) { return emit(def); });
726
727 switch (nat.id()) {
728 case core::nat::add: op = "add"; break;
729 case core::nat::sub: op = "sub"; break;
730 case core::nat::mul: op = "mul"; break;
731 }
732
733 return bb.assign(name, "{} nsw nuw i64 {}, {}", op, a, b);
734 } else if (auto ncmp = Axm::isa<core::ncmp>(def)) {
735 auto [a, b] = ncmp->args<2>([this](auto def) { return emit(def); });
736 op = "icmp ";
737
738 switch (ncmp.id()) {
739 // clang-format off
740 case core::ncmp::e: op += "eq" ; break;
741 case core::ncmp::ne: op += "ne" ; break;
742 case core::ncmp::g: op += "ugt"; break;
743 case core::ncmp::ge: op += "uge"; break;
744 case core::ncmp::l: op += "ult"; break;
745 case core::ncmp::le: op += "ule"; break;
746 // clang-format on
747 default: fe::unreachable();
748 }
749
750 return bb.assign(name, "{} i64 {}, {}", op, a, b);
751 } else if (auto idx = Axm::isa<core::idx>(def)) {
752 auto x = emit(idx->arg());
753 auto s = *Idx::size2bitwidth(Idx::isa(idx->type()));
754 auto t = convert(idx->type());
755 if (s < 64) return bb.assign(name, "trunc i64 {} to {}", x, t);
756 return x;
757 } else if (auto bit1 = Axm::isa<core::bit1>(def)) {
758 assert(bit1.id() == core::bit1::neg);
759 auto x = emit(bit1->arg());
760 auto t = convert(bit1->type());
761 return bb.assign(name, "xor {} -1, {}", t, x);
762 } else if (auto bit2 = Axm::isa<core::bit2>(def)) {
763 auto [a, b] = bit2->args<2>([this](auto def) { return emit(def); });
764 auto t = convert(bit2->type());
765
766 auto neg = [&](std::string_view x) { return bb.assign(name + ".neg", "xor {} -1, {}", t, x); };
767
768 switch (bit2.id()) {
769 // clang-format off
770 case core::bit2::and_: return bb.assign(name, "and {} {}, {}", t, a, b);
771 case core::bit2:: or_: return bb.assign(name, "or {} {}, {}", t, a, b);
772 case core::bit2::xor_: return bb.assign(name, "xor {} {}, {}", t, a, b);
773 case core::bit2::nand: return neg(bb.assign(name, "and {} {}, {}", t, a, b));
774 case core::bit2:: nor: return neg(bb.assign(name, "or {} {}, {}", t, a, b));
775 case core::bit2::nxor: return neg(bb.assign(name, "xor {} {}, {}", t, a, b));
776 case core::bit2:: iff: return bb.assign(name, "and {} {}, {}", neg(a), b);
777 case core::bit2::niff: return bb.assign(name, "or {} {}, {}", neg(a), b);
778 // clang-format on
779 default: fe::unreachable();
780 }
781 } else if (auto shr = Axm::isa<core::shr>(def)) {
782 auto [a, b] = shr->args<2>([this](auto def) { return emit(def); });
783 auto t = convert(shr->type());
784
785 switch (shr.id()) {
786 case core::shr::a: op = "ashr"; break;
787 case core::shr::l: op = "lshr"; break;
788 }
789
790 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
791 } else if (auto wrap = Axm::isa<core::wrap>(def)) {
792 auto [mode, ab] = wrap->uncurry_args<2>();
793 auto [a, b] = ab->projs<2>([this](auto def) { return emit(def); });
794 auto t = convert(wrap->type());
795 auto lmode = Lit::as(mode);
796
797 switch (wrap.id()) {
798 case core::wrap::add: op = "add"; break;
799 case core::wrap::sub: op = "sub"; break;
800 case core::wrap::mul: op = "mul"; break;
801 case core::wrap::shl: op = "shl"; break;
802 }
803
804 if (lmode & core::Mode::nuw) op += " nuw";
805 if (lmode & core::Mode::nsw) op += " nsw";
806
807 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
808 } else if (auto div = Axm::isa<core::div>(def)) {
809 auto [m, xy] = div->args<2>();
810 auto [x, y] = xy->projs<2>();
811 auto t = convert(x->type());
812 emit_unsafe(m);
813 auto a = emit(x);
814 auto b = emit(y);
815
816 switch (div.id()) {
817 case core::div::sdiv: op = "sdiv"; break;
818 case core::div::udiv: op = "udiv"; break;
819 case core::div::srem: op = "srem"; break;
820 case core::div::urem: op = "urem"; break;
821 }
822
823 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
824 } else if (auto icmp = Axm::isa<core::icmp>(def)) {
825 auto [a, b] = icmp->args<2>([this](auto def) { return emit(def); });
826 auto t = convert(icmp->arg(0)->type());
827 op = "icmp ";
828
829 switch (icmp.id()) {
830 // clang-format off
831 case core::icmp::e: op += "eq" ; break;
832 case core::icmp::ne: op += "ne" ; break;
833 case core::icmp::sg: op += "sgt"; break;
834 case core::icmp::sge: op += "sge"; break;
835 case core::icmp::sl: op += "slt"; break;
836 case core::icmp::sle: op += "sle"; break;
837 case core::icmp::ug: op += "ugt"; break;
838 case core::icmp::uge: op += "uge"; break;
839 case core::icmp::ul: op += "ult"; break;
840 case core::icmp::ule: op += "ule"; break;
841 // clang-format on
842 default: fe::unreachable();
843 }
844
845 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
846 } else if (auto extr = Axm::isa<core::extrema>(def)) {
847 auto [x, y] = extr->args<2>();
848 auto t = convert(x->type());
849 auto a = emit(x);
850 auto b = emit(y);
851 std::string f = "llvm.";
852 switch (extr.id()) {
853 case core::extrema::Sm: f += "smin."; break;
854 case core::extrema::SM: f += "smax."; break;
855 case core::extrema::sm: f += "umin."; break;
856 case core::extrema::sM: f += "umax."; break;
857 }
858 f += t;
859 declare("{} @{}({}, {})", t, f, t, t);
860 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
861 } else if (auto abs = Axm::isa<core::abs>(def)) {
862 auto [m, x] = abs->args<2>();
863 auto t = convert(x->type());
864 auto a = emit(x);
865 std::string f = "llvm.abs." + t;
866 declare("{} @{}({}, {})", t, f, t, "i1");
867 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, "i1", "1");
868 } else if (auto conv = Axm::isa<core::conv>(def)) {
869 auto v_src = emit(conv->arg());
870 auto t_src = convert(conv->arg()->type());
871 auto t_dst = convert(conv->type());
872
873 nat_t w_src = *Idx::size2bitwidth(Idx::isa(conv->arg()->type()));
874 nat_t w_dst = *Idx::size2bitwidth(Idx::isa(conv->type()));
875
876 if (w_src == w_dst) return v_src;
877
878 switch (conv.id()) {
879 case core::conv::s: op = w_src < w_dst ? "sext" : "trunc"; break;
880 case core::conv::u: op = w_src < w_dst ? "zext" : "trunc"; break;
881 }
882
883 return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
884 } else if (auto bitcast = Axm::isa<core::bitcast>(def)) {
885 auto dst_type_ptr = Axm::isa<mem::Ptr>(bitcast->type());
886 auto src_type_ptr = Axm::isa<mem::Ptr>(bitcast->arg()->type());
887 auto v_src = emit(bitcast->arg());
888 auto t_src = convert(bitcast->arg()->type());
889 auto t_dst = convert(bitcast->type());
890
891 if (auto lit = Lit::isa(bitcast->arg()); lit && *lit == 0) return "zeroinitializer";
892 // clang-format off
893 if (src_type_ptr && dst_type_ptr) return bb.assign(name, "bitcast {} {} to {}", t_src, v_src, t_dst);
894 if (src_type_ptr) return bb.assign(name, "ptrtoint {} {} to {}", t_src, v_src, t_dst);
895 if (dst_type_ptr) return bb.assign(name, "inttoptr {} {} to {}", t_src, v_src, t_dst);
896 // clang-format on
897
898 auto size2width = [&](const Def* type) {
899 if (type->isa<Nat>()) return 64_n;
900 if (auto size = Idx::isa(type)) return *Idx::size2bitwidth(size);
901 return 0_n;
902 };
903
904 auto src_size = size2width(bitcast->arg()->type());
905 auto dst_size = size2width(bitcast->type());
906
907 op = "bitcast";
908 if (src_size && dst_size) {
909 if (src_size == dst_size) return v_src;
910 op = (src_size < dst_size) ? "zext" : "trunc";
911 }
912 return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
913 } else if (auto lea = Axm::isa<mem::lea>(def)) {
914 auto [ptr, i] = lea->args<2>();
915 auto pointee = Axm::as<mem::Ptr>(ptr->type())->arg(0);
916 auto v_ptr = emit(ptr);
917 auto t_pointee = convert(pointee);
918 auto t_ptr = convert(ptr->type());
919 if (pointee->isa<Sigma>())
920 return bb.assign(name, "getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
921 Lit::as(i));
922
923 assert(pointee->isa<Arr>());
924 auto [v_i, t_i] = emit_gep_index(i);
925
926 return bb.assign(name, "getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
927 } else if (auto malloc = Axm::isa<mem::malloc>(def)) {
928 declare("i8* @malloc(i64)");
929
930 emit_unsafe(malloc->arg(0));
931 auto size = emit(malloc->arg(1));
932 auto ptr_t = convert(Axm::as<mem::Ptr>(def->proj(1)->type()));
933 bb.assign(name + "i8", "call i8* @malloc(i64 {})", size);
934 return bb.assign(name, "bitcast i8* {} to {}", name + "i8", ptr_t);
935 } else if (auto free = Axm::isa<mem::free>(def)) {
936 declare("void @free(i8*)");
937 emit_unsafe(free->arg(0));
938 auto ptr = emit(free->arg(1));
939 auto ptr_t = convert(Axm::as<mem::Ptr>(free->arg(1)->type()));
940
941 bb.assign(name + "i8", "bitcast {} {} to i8*", ptr_t, ptr);
942 bb.tail("call void @free(i8* {})", name + "i8");
943 return {};
944 } else if (auto mslot = Axm::isa<mem::mslot>(def)) {
945 auto [Ta, msi] = mslot->uncurry_args<2>();
946 auto [pointee, addr_space] = Ta->projs<2>();
947 auto [mem, _, __] = msi->projs<3>();
948 emit_unsafe(mslot->arg(0));
949 // TODO array with size
950 // auto v_size = emit(mslot->arg(1));
951 print(bb.body().emplace_back(), "{} = alloca {}", name, convert(pointee, false));
952 return name;
953 } else if (auto load = Axm::isa<mem::load>(def)) {
954 emit_unsafe(load->arg(0));
955 auto v_ptr = emit(load->arg(1));
956 auto t_ptr = convert(load->arg(1)->type());
957 auto t_pointee = convert(Axm::as<mem::Ptr>(load->arg(1)->type())->arg(0), false);
958 return bb.assign(name, "load {}, {} {}", t_pointee, t_ptr, v_ptr);
959 } else if (auto store = Axm::isa<mem::store>(def)) {
960 emit_unsafe(store->arg(0));
961 auto v_ptr = emit(store->arg(1));
962 auto v_val = emit(store->arg(2));
963 auto t_ptr = convert(store->arg(1)->type());
964 auto t_val = convert(store->arg(2)->type(), false);
965 print(bb.body().emplace_back(), "store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
966 return {};
967 } else if (auto q = Axm::isa<clos::alloc_jmpbuf>(def)) {
968 declare("i64 @jmpbuf_size()");
969
970 emit_unsafe(q->arg());
971 auto size = name + ".size";
972 bb.assign(size, "call i64 @jmpbuf_size()");
973 return bb.assign(name, "alloca i8, i64 {}", size);
974 } else if (auto setjmp = Axm::isa<clos::setjmp>(def)) {
975 declare("i32 @_setjmp(i8*) returns_twice");
976
977 auto [mem, jmpbuf] = setjmp->arg()->projs<2>();
978 emit_unsafe(mem);
979 auto v_jb = emit(jmpbuf);
980 return bb.assign(name, "call i32 @_setjmp(i8* {})", v_jb);
981 } else if (auto arith = Axm::isa<math::arith>(def)) {
982 auto [mode, ab] = arith->uncurry_args<2>();
983 auto [a, b] = ab->projs<2>([this](auto def) { return emit(def); });
984 auto t = convert(arith->type());
985 auto lmode = Lit::as(mode);
986
987 switch (arith.id()) {
988 case math::arith::add: op = "fadd"; break;
989 case math::arith::sub: op = "fsub"; break;
990 case math::arith::mul: op = "fmul"; break;
991 case math::arith::div: op = "fdiv"; break;
992 case math::arith::rem: op = "frem"; break;
993 }
994
995 if (lmode == math::Mode::fast)
996 op += " fast";
997 else {
998 // clang-format off
999 if (lmode & math::Mode::nnan ) op += " nnan";
1000 if (lmode & math::Mode::ninf ) op += " ninf";
1001 if (lmode & math::Mode::nsz ) op += " nsz";
1002 if (lmode & math::Mode::arcp ) op += " arcp";
1003 if (lmode & math::Mode::contract) op += " contract";
1004 if (lmode & math::Mode::afn ) op += " afn";
1005 if (lmode & math::Mode::reassoc ) op += " reassoc";
1006 // clang-format on
1007 }
1008
1009 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
1010 } else if (auto tri = Axm::isa<math::tri>(def)) {
1011 auto a = emit(tri->arg());
1012 auto t = convert(tri->type());
1013
1014 std::string f;
1015
1016 if (tri.id() == math::tri::sin) {
1017 f = std::string("llvm.sin") + llvm_suffix(tri->type());
1018 } else if (tri.id() == math::tri::cos) {
1019 f = std::string("llvm.cos") + llvm_suffix(tri->type());
1020 } else {
1021 if (tri.sub() & sub_t(math::tri::a)) f += "a";
1022
1023 switch (math::tri((tri.id() & 0x3) | Annex::base<math::tri>())) {
1024 case math::tri::sin: f += "sin"; break;
1025 case math::tri::cos: f += "cos"; break;
1026 case math::tri::tan: f += "tan"; break;
1027 case math::tri::ahFF: error("this axm is supposed to be unused");
1028 default: fe::unreachable();
1029 }
1030
1031 if (tri.sub() & sub_t(math::tri::h)) f += "h";
1032 f += math_suffix(tri->type());
1033 }
1034
1035 declare("{} @{}({})", t, f, t);
1036 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1037 } else if (auto extrema = Axm::isa<math::extrema>(def)) {
1038 auto [a, b] = extrema->args<2>([this](auto def) { return emit(def); });
1039 auto t = convert(extrema->type());
1040 std::string f = "llvm.";
1041 switch (extrema.id()) {
1042 case math::extrema::fmin: f += "minnum"; break;
1043 case math::extrema::fmax: f += "maxnum"; break;
1044 case math::extrema::ieee754min: f += "minimum"; break;
1045 case math::extrema::ieee754max: f += "maximum"; break;
1046 }
1047 f += llvm_suffix(extrema->type());
1048
1049 declare("{} @{}({}, {})", t, f, t, t);
1050 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
1051 } else if (auto pow = Axm::isa<math::pow>(def)) {
1052 auto [a, b] = pow->args<2>([this](auto def) { return emit(def); });
1053 auto t = convert(pow->type());
1054 std::string f = "llvm.pow";
1055 f += llvm_suffix(pow->type());
1056 declare("{} @{}({}, {})", t, f, t, t);
1057 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
1058 } else if (auto rt = Axm::isa<math::rt>(def)) {
1059 auto a = emit(rt->arg());
1060 auto t = convert(rt->type());
1061 std::string f;
1062 if (rt.id() == math::rt::sq)
1063 f = std::string("llvm.sqrt") + llvm_suffix(rt->type());
1064 else
1065 f = std::string("cbrt") += math_suffix(rt->type());
1066 declare("{} @{}({})", t, f, t);
1067 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1068 } else if (auto exp = Axm::isa<math::exp>(def)) {
1069 auto a = emit(exp->arg());
1070 auto t = convert(exp->type());
1071 std::string f = "llvm.";
1072 f += (exp.sub() & sub_t(math::exp::log)) ? "log" : "exp";
1073 f += (exp.sub() & sub_t(math::exp::bin)) ? "2" : (exp.sub() & sub_t(math::exp::dec)) ? "10" : "";
1074 f += llvm_suffix(exp->type());
1075 // TODO doesn't work for exp10"
1076 declare("{} @{}({})", t, f, t);
1077 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1078 } else if (auto er = Axm::isa<math::er>(def)) {
1079 auto a = emit(er->arg());
1080 auto t = convert(er->type());
1081 auto f = er.id() == math::er::f ? std::string("erf") : std::string("erfc");
1082 f += math_suffix(er->type());
1083 declare("{} @{}({})", t, f, t);
1084 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1085 } else if (auto gamma = Axm::isa<math::gamma>(def)) {
1086 auto a = emit(gamma->arg());
1087 auto t = convert(gamma->type());
1088 std::string f = gamma.id() == math::gamma::t ? "tgamma" : "lgamma";
1089 f += math_suffix(gamma->type());
1090 declare("{} @{}({})", t, f, t);
1091 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1092 } else if (auto cmp = Axm::isa<math::cmp>(def)) {
1093 auto [a, b] = cmp->args<2>([this](auto def) { return emit(def); });
1094 auto t = convert(cmp->arg(0)->type());
1095 op = "fcmp ";
1096
1097 switch (cmp.id()) {
1098 // clang-format off
1099 case math::cmp:: e: op += "oeq"; break;
1100 case math::cmp:: l: op += "olt"; break;
1101 case math::cmp:: le: op += "ole"; break;
1102 case math::cmp:: g: op += "ogt"; break;
1103 case math::cmp:: ge: op += "oge"; break;
1104 case math::cmp:: ne: op += "one"; break;
1105 case math::cmp:: o: op += "ord"; break;
1106 case math::cmp:: u: op += "uno"; break;
1107 case math::cmp:: ue: op += "ueq"; break;
1108 case math::cmp:: ul: op += "ult"; break;
1109 case math::cmp::ule: op += "ule"; break;
1110 case math::cmp:: ug: op += "ugt"; break;
1111 case math::cmp::uge: op += "uge"; break;
1112 case math::cmp::une: op += "une"; break;
1113 // clang-format on
1114 default: fe::unreachable();
1115 }
1116
1117 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
1118 } else if (auto conv = Axm::isa<math::conv>(def)) {
1119 auto v_src = emit(conv->arg());
1120 auto t_src = convert(conv->arg()->type());
1121 auto t_dst = convert(conv->type());
1122
1123 auto s_src = math::isa_f(conv->arg()->type());
1124 auto s_dst = math::isa_f(conv->type());
1125
1126 switch (conv.id()) {
1127 case math::conv::f2f: op = s_src < s_dst ? "fpext" : "fptrunc"; break;
1128 case math::conv::s2f: op = "sitofp"; break;
1129 case math::conv::u2f: op = "uitofp"; break;
1130 case math::conv::f2s: op = "fptosi"; break;
1131 case math::conv::f2u: op = "fptoui"; break;
1132 }
1133
1134 return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
1135 } else if (auto abs = Axm::isa<math::abs>(def)) {
1136 auto a = emit(abs->arg());
1137 auto t = convert(abs->type());
1138 std::string f = "llvm.fabs";
1139 f += llvm_suffix(abs->type());
1140 declare("{} @{}({})", t, f, t);
1141 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1142 } else if (auto round = Axm::isa<math::round>(def)) {
1143 auto a = emit(round->arg());
1144 auto t = convert(round->type());
1145 std::string f = "llvm.";
1146 switch (round.id()) {
1147 case math::round::f: f += "floor"; break;
1148 case math::round::c: f += "ceil"; break;
1149 case math::round::r: f += "round"; break;
1150 case math::round::t: f += "trunc"; break;
1151 }
1152 f += llvm_suffix(round->type());
1153 declare("{} @{}({})", t, f, t);
1154 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1155 } else if (auto zip = Axm::isa<vec::zip>(def)) {
1156 auto ni_n = zip->decurry()->decurry()->decurry()->arg();
1157 auto nat_ni = *Lit::isa(ni_n->proj(2, 0));
1158 auto f = zip->decurry()->arg();
1159 auto inputs = zip->arg();
1160 auto t_in = convert(inputs->proj(nat_ni, 0)->type());
1161 auto t_out = convert(def->type()); // <n x T>
1162
1163 std::string op;
1164 std::string prev;
1165
1166 if (auto nat_op = Axm::isa<core::nat, 1>(f)) {
1167 switch (nat_op.id()) {
1168 case core::nat::add: op = "add nuw nsw"; break;
1169 case core::nat::sub: op = "sub nuw nsw"; break;
1170 case core::nat::mul: op = "mul nuw nsw"; break;
1171 }
1172 } else if (auto arith_op = Axm::isa<math::arith, 1>(f)) {
1173 auto lmode = Lit::as(f->as<App>()->arg());
1174 switch (arith_op.id()) {
1175 case math::arith::add: op = "fadd"; break;
1176 case math::arith::sub: op = "fsub"; break;
1177 case math::arith::mul: op = "fmul"; break;
1178 case math::arith::div: op = "fdiv"; break;
1179 case math::arith::rem: op = "frem"; break;
1180 }
1181
1182 if (lmode == math::Mode::fast)
1183 op += " fast";
1184 else {
1185 if (lmode & math::Mode::nnan) op += " nnan";
1186 if (lmode & math::Mode::ninf) op += " ninf";
1187 if (lmode & math::Mode::nsz) op += " nsz";
1188 if (lmode & math::Mode::arcp) op += " arcp";
1189 if (lmode & math::Mode::contract) op += " contract";
1190 if (lmode & math::Mode::afn) op += " afn";
1191 if (lmode & math::Mode::reassoc) op += " reassoc";
1192 }
1193 } else if (auto ncmp_op = Axm::isa<core::ncmp, 1>(f)) {
1194 op = "icmp ";
1195 switch (ncmp_op.id()) {
1196 case core::ncmp::e: op += "eq"; break;
1197 case core::ncmp::ne: op += "ne"; break;
1198 case core::ncmp::g: op += "ugt"; break;
1199 case core::ncmp::ge: op += "uge"; break;
1200 case core::ncmp::l: op += "ult"; break;
1201 case core::ncmp::le: op += "ule"; break;
1202 default: fe::unreachable();
1203 }
1204 } else if (auto icmp_op = Axm::isa<core::icmp, 1>(f)) {
1205 op = "icmp ";
1206 switch (icmp_op.id()) {
1207 case core::icmp::e: op += "eq"; break;
1208 case core::icmp::ne: op += "ne"; break;
1209 case core::icmp::sg: op += "sgt"; break;
1210 case core::icmp::sge: op += "sge"; break;
1211 case core::icmp::sl: op += "slt"; break;
1212 case core::icmp::sle: op += "sle"; break;
1213 case core::icmp::ug: op += "ugt"; break;
1214 case core::icmp::uge: op += "uge"; break;
1215 case core::icmp::ul: op += "ult"; break;
1216 case core::icmp::ule: op += "ule"; break;
1217 default: fe::unreachable();
1218 }
1219 } else if (auto mcmp_op = Axm::isa<math::cmp, 1>(f)) {
1220 op = "fcmp ";
1221 switch (mcmp_op.id()) {
1222 case math::cmp::e: op += "oeq"; break;
1223 case math::cmp::l: op += "olt"; break;
1224 case math::cmp::le: op += "ole"; break;
1225 case math::cmp::g: op += "ogt"; break;
1226 case math::cmp::ge: op += "oge"; break;
1227 case math::cmp::ne: op += "one"; break;
1228 case math::cmp::o: op += "ord"; break;
1229 case math::cmp::u: op += "uno"; break;
1230 case math::cmp::ue: op += "ueq"; break;
1231 case math::cmp::ul: op += "ult"; break;
1232 case math::cmp::ule: op += "ule"; break;
1233 case math::cmp::ug: op += "ugt"; break;
1234 case math::cmp::uge: op += "uge"; break;
1235 case math::cmp::une: op += "une"; break;
1236 default: fe::unreachable();
1237 }
1238 } else {
1239 error("unhandled vec.zip operation: {}", f);
1240 }
1241
1242 auto v1 = emit(inputs->proj(nat_ni, 0));
1243 auto v2 = emit(inputs->proj(nat_ni, 1));
1244 prev = bb.assign(name, "{} {} {}, {}", op, t_in, v1, v2);
1245 return prev;
1246 } else if (auto res = isa_targetspecific_intrinsic(bb, def)) {
1247 return res.value();
1248 }
1249 error("unhandled def in LLVM backend: {} : {}", def, def->type());
1250}
1251
1252} // namespace ll
1253} // namespace mim
const Def * arg() const
Definition lam.h:285
A (possibly paramterized) Array.
Definition tuple.h:117
static auto isa(const Def *def)
Definition axm.h:107
static auto as(const Def *def)
Definition axm.h:130
Matches (ff, tt)#cond arg where cond is not a Literal.
Definition tuple.h:279
Lam * root() const
Definition phase.h:356
Base class for all Defs.
Definition def.h:252
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:593
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:498
const Def * op(size_t i) const noexcept
Definition def.h:309
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:430
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:391
nat_t num_vars() noexcept
Definition def.h:430
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
bool is_external() const noexcept
Definition def.h:479
auto vars(F f) noexcept
Definition def.h:430
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:584
Matches a dispatch through a jump table of the form: (target_0, target_1, ...)#index arg where index ...
Definition tuple.h:304
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:206
static constexpr nat_t size2bitwidth(nat_t n)
Definition def.h:910
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Definition def.cpp:616
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
Definition tuple.h:233
A function.
Definition lam.h:110
static Lam * isa_mut_basicblock(const Def *d)
Only for mutables.
Definition lam.h:145
const Pi * type() const
Definition lam.h:130
const Def * body() const
Definition lam.h:123
static std::optional< T > isa(const Def *def)
Definition def.h:843
static T as(const Def *def)
Definition def.h:849
const Nest & nest() const
Definition phase.h:373
A (possibly paramterized) Tuple.
Definition tuple.h:166
virtual void start()=0
Actual entry.
A dependent function type.
Definition lam.h:14
const Pi * ret_pi() const
Yields the last Pi::dom, if Pi::isa_basicblock.
Definition lam.cpp:13
static const Pi * isa_returning(const Def *d)
Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
Definition lam.h:49
static Schedule schedule(const Nest &)
Definition schedule.cpp:118
Matches (ff, tt)#cond - where cond is not a Literal.
Definition tuple.h:261
A dependent tuple type.
Definition tuple.h:20
World & world()
Definition pass.h:64
std::string_view name() const
Definition pass.h:67
Data constructor for a Sigma.
Definition tuple.h:68
A variable introduced by a binder (mutable).
Definition def.h:719
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:34
void emit_imported(Lam *)
Definition ll.h:301
Emitter(World &world, std::ostream &ostream)
Definition ll.h:119
virtual std::string prepare()
Definition ll.h:315
void finalize()
Definition ll.h:335
std::string as_targetspecific_intrinsic(BB &bb, const Def *def)
Definition ll.h:131
void start() override
Actual entry.
Definition ll.h:290
std::string emit_bb(BB &, const Def *)
Definition ll.h:547
virtual std::optional< std::string > isa_targetspecific_intrinsic(BB &, const Def *)
Definition ll.h:130
bool is_valid(std::string_view s)
Definition ll.h:122
mim::Emitter< std::string, std::string, BB, Emitter > Super
Definition ll.h:117
virtual void emit_epilogue(Lam *)
Definition ll.h:381
void declare(const char *s, Args &&... args)
Definition ll.h:137
Definition ll.h:39
static const Def * find_common_simd_src(const App *app)
Definition ll.h:179
static std::optional< std::pair< nat_t, const Def * > > is_simd_aggregate(const std::vector< const Def * > types)
Definition ll.h:170
static std::optional< std::pair< nat_t, const Def * > > is_simd(const Def *type)
Definition ll.h:160
int compile_and_run(World &, std::string name, std::string args={})
Definition ll.cpp:35
void emit(World &, std::ostream &)
Definition ll.cpp:13
int compile(World &, std::string name)
Definition ll.cpp:18
The clos Plugin
Definition clos.h:7
The core Plugin
Definition core.h:8
The math Plugin
Definition math.h:8
The mem Plugin
Definition mem.h:11
The vec Plugin
Definition ast.h:14
u64 nat_t
Definition types.h:44
Vector< const Def * > DefVec
Definition def.h:78
u8 sub_t
Definition types.h:49
D bitcast(const S &src)
A bitcast from src of type S to D.
Definition util.h:23
std::ostream & print(std::ostream &os, const char *s)
Base case.
Definition print.cpp:5
double f64
Definition types.h:42
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:125
float f32
Definition types.h:41
GIDMap< const Def *, To > DefMap
Definition def.h:74
TExt< true > Top
Definition lattice.h:172
GIDMap< Lam *, To > LamMap
Definition lam.h:219
TExt< false > Bot
Definition lattice.h:171
uint64_t u64
Definition types.h:35
@ Nat
Definition def.h:115
@ Pi
Definition def.h:115
@ Arr
Definition def.h:115
@ Sigma
Definition def.h:115
uint16_t u16
Definition types.h:35
static consteval flags_t base()
Definition plugin.h:119
std::deque< std::ostringstream > & tail()
Definition ll.h:92
void tail(const char *s, Args &&... args)
Definition ll.h:101
DefMap< std::deque< std::pair< std::string, std::string > > > phis
Definition ll.h:111
BB(const BB &)=delete
BB()=default
std::deque< std::ostringstream > & body()
Definition ll.h:91
BB(BB &&other) noexcept=default
friend void swap(BB &a, BB &b) noexcept
Definition ll.h:105
std::array< std::deque< std::ostringstream >, 3 > parts
Definition ll.h:112
std::deque< std::ostringstream > & head()
Definition ll.h:90
BB & operator=(BB other) noexcept
Definition ll.h:88
std::string assign(std::string_view name, const char *s, Args &&... args)
Definition ll.h:95