MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
pipelinebuilder.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/pass/optimize.h"
4#include "mim/pass/pass.h"
5#include "mim/phase/phase.h"
6#include "mim/plugin.h"
7#include "mim/world.h"
8
9namespace mim {
10
12public:
14 : pipe(std::make_unique<Pipeline>(world))
15 , world_(world) {}
16
17 World& world() { return world_; }
18
19 // Adds a pass and remembers it associated with the given def.
20 template<class P, class... Args> void add_pass(const Def* def, Args&&... args) {
21 auto pass = (Pass*)man->add<P>(std::forward<Args>(args)...);
22 def2pass(def, pass);
23 }
24 // TODO: add remembered entry
25 template<class P, class... Args> void add_phase(Args&&... args) {
26 assert(!man && "cannot add phase while in pass phase");
27 pipe->add<P>(std::forward<Args>(args)...);
28 }
29
30 void begin_pass_phase();
31 void end_pass_phase();
32
33 void def2pass(const Def*, Pass* p);
34 Pass* pass(const Def*);
35
36 void run_pipeline();
37
38private:
39 absl::btree_map<const Def*, Pass*, GIDLt<const Def*>> def2pass_;
40 std::unique_ptr<PassMan> man;
41 std::unique_ptr<Pipeline> pipe;
42 World& world_;
43};
44
45/// @name Register Pass/Phase
46///@{
47template<class A, class P, class... CArgs> void register_pass(Passes& passes, CArgs&&... args) {
49 [... args = std::forward<CArgs>(args)](World&, PipelineBuilder& builder, const Def* app) {
50 builder.add_pass<P>(app, args...);
51 });
52}
53
54template<class A, class P, class... CArgs> void register_phase(Passes& passes, CArgs&&... args) {
56 [... args = std::forward<CArgs>(args)](World&, PipelineBuilder& builder, const Def*) {
57 builder.add_phase<P>(args...);
58 });
59}
60
61template<class A, class P, class Q> void register_pass_with_arg(Passes& passes) {
62 assert_emplace(passes, flags_t(Annex::Base<A>), [](World& world, PipelineBuilder& builder, const Def* app) {
63 auto pass_arg = (Q*)(builder.pass(app->as<App>()->arg()));
64 world.DLOG("register using arg: {} of type {} for gid {}", pass_arg, typeid(Q).name(),
65 app->as<App>()->arg()->gid());
66 builder.add_pass<P>(app, pass_arg);
67 });
68}
69///@}
70
71} // namespace mim
const Def * arg() const
Definition lam.h:227
Base class for all Defs.
Definition def.h:223
u32 gid() const
Definition def.h:239
All Passes that want to be registered in the PassMan must implement this interface.
Definition pass.h:22
void def2pass(const Def *, Pass *p)
Pass * pass(const Def *)
PipelineBuilder(World &world)
void add_phase(Args &&... args)
void add_pass(const Def *def, Args &&... args)
Organizes several Phases as a pipeline.
Definition phase.h:113
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
Definition cfg.h:11
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:102
u64 flags_t
Definition types.h:45
absl::flat_hash_map< flags_t, std::function< void(World &, PipelineBuilder &, const Def *)> > Passes
axiom ↦ (pipeline part) × (axiom application) → () The function should inspect Application to const...
Definition plugin.h:22
void register_pass(Passes &passes, CArgs &&... args)
void register_pass_with_arg(Passes &passes)
void register_phase(Passes &passes, CArgs &&... args)
Definition span.h:104
static constexpr flags_t Base
Definition plugin.h:118