MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
phase.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <ranges>
5
6#include <mim/driver.h>
7#include <mim/nest.h>
8#include <mim/phase.h>
9#include <mim/world.h>
10
12
13namespace mim::plug::mach {
14
15/// Registers the Phase @p P for the annex @p A, constructed with the Target of the one loaded backend.
16template<class A, class P>
17void hook(Flags2Phases& phases) {
18 fe::assert_emplace(phases, Annex::base<A>(), [](World& world) {
19 return std::make_unique<P>(world, Annex::base<A>(), Target::of(world.driver()));
20 });
21}
22
23/// The Node%s of @p scc in a deterministic order.
24inline fe::Vector<const Nest::Node*> sorted(const Nest::Node::SCC& scc) {
25 auto res = fe::Vector<const Nest::Node*>(scc.begin(), scc.end());
26 std::ranges::sort(res, {}, &Nest::Node::gid);
27 return res;
28}
29
30/// The closed `Cn`s with a body that get a convention of their own instead of the ABI.
31/// Such a function is neither external nor recursive, and only ever called or jumped to.
32/// Recursion also counts through other closed functions, so it takes the Nest of all of them.
33inline LamSet known(World& world) {
34 struct Unknowns : Analysis {
35 Unknowns(World& world)
36 : Analysis(world, "unknowns") {
37 find_unknowns();
38 }
39 void unknown(Lam* lam) final { lams.emplace(lam); }
40 LamSet lams;
41 } unknowns(world);
42 auto funs = fe::Vector<Def*>();
43 world.for_each<Lam>(true, [&](Lam* lam) {
44 if (Lam::isa_mut_cn(lam)) funs.emplace_back(lam);
45 });
46 auto nest = Nest(world, funs, false, Nest::Place::Late);
47 auto res = LamSet();
48 for (auto fun : funs)
49 if (auto lam = fun->as<Lam>(); !unknowns.lams.contains(lam) && !nest[lam]->is_recursive()) res.emplace(lam);
50 return res;
51}
52
53/// Visits the closed `Cn`s with a body of a scheduled World for one Target, each before the ones that call or jump to
54/// it.
55class FunPhase : public Phase {
56public:
58 : Phase(world, annex)
59 , target_(target) {}
60
61 const Target& target() const { return target_; }
62 /// The arguments `-X <backend>:<arg>` of the Target's backend plugin.
63 const fe::Vector<std::string>& target_args() { return driver().args(target_.name()); }
64 /// The functions with a convention of their own, see mach::known.
65 const LamSet& known() const { return known_; }
66
67protected:
68 virtual void visit(Lam*) = 0;
69 void start() override {
70 known_ = mach::known(world());
72 true,
73 [this](Lam* lam) {
74 if (Lam::isa_mut_cn(lam)) visit(lam);
75 },
76 true);
77 }
78
79private:
80 const Target& target_;
81 LamSet known_;
82};
83
84} // namespace mim::plug::mach
Traverses the current World using Rewriter infrastructure while staying in the same world.
Definition phase.h:132
const fe::Vector< std::string > & args(std::string_view plugin) const
Yields an empty fe::Vector if plugin has none.
Definition driver.cpp:218
A function.
Definition lam.h:91
static Lam * isa_mut_cn(const Def *d)
Definition lam.h:128
GIDSet< const Node * > SCC
Strongly Connected Component.
Definition nest.h:127
uint32_t gid() const
Stable id for deterministic hashing/ordering: the underlying mut's gid, or 0 for the (unique) virtual...
Definition nest.h:49
Nest(Def *root, Place)
Definition nest.cpp:9
flags_t annex() const
Definition phase.h:64
Phase(World &world, std::string name)
Definition phase.h:29
Driver & driver()
Definition phase.h:61
World & world()
Definition phase.h:60
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
Driver & driver()
Definition world.h:107
void for_each(bool elide_empty, std::function< void(Def *)>, bool schedule=false)
Definition world.cpp:1184
void start() override
Actual entry.
Definition phase.h:69
FunPhase(World &world, flags_t annex, const Target &target)
Definition phase.h:57
const LamSet & known() const
The functions with a convention of their own, see mach::known.
Definition phase.h:65
const Target & target() const
Definition phase.h:61
const fe::Vector< std::string > & target_args()
The arguments -X <backend>:<arg> of the Target's backend plugin.
Definition phase.h:63
virtual void visit(Lam *)=0
The machine facts of a native backend that the phases of mach need.
Definition target.h:45
static const Target & of(Driver &driver)
The Target of the one loaded backend; every backend's Target is a mach::Target.
Definition target.h:55
The mach Plugin
Definition asm.h:15
fe::Vector< const Nest::Node * > sorted(const Nest::Node::SCC &scc)
The Nodes of scc in a deterministic order.
Definition phase.h:24
LamSet known(World &world)
The closed Cns with a body that get a convention of their own instead of the ABI.
Definition phase.h:33
void hook(Flags2Phases &phases)
Registers the Phase P for the annex A, constructed with the Target of the one loaded backend.
Definition phase.h:17
GIDSet< Lam * > LamSet
Definition lam.h:211
u64 flags_t
Definition types.h:39
ankerl::unordered_dense::map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition plugin.h:31
static consteval flags_t base()
Definition plugin.h:278