Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
pipelinebuilder.h
Go to the documentation of this file.
1#pragma once
2
3#include "thorin/plugin.h"
4#include "thorin/world.h"
5
7#include "thorin/pass/pass.h"
9
10namespace thorin {
11
13public:
15 : pipe(std::make_unique<Pipeline>(world))
16 , world_(world) {}
17
18 World& world() { return world_; }
19
20 // Adds a pass and remembers it associated with the given def.
21 template<class P, class... Args> void add_pass(const Def* def, Args&&... args) {
22 auto pass = (Pass*)man->add<P>(std::forward<Args>(args)...);
23 def2pass(def, pass);
24 }
25 // TODO: add remembered entry
26 template<class P, class... Args> void add_phase(Args&&... args) {
27 assert(!man && "cannot add phase while in pass phase");
28 pipe->add<P>(std::forward<Args>(args)...);
29 }
30
31 void begin_pass_phase();
32 void end_pass_phase();
33
34 void def2pass(const Def*, Pass* p);
35 Pass* pass(const Def*);
36
37 void run_pipeline();
38
39private:
40 absl::btree_map<const Def*, Pass*, GIDLt<const Def*>> def2pass_;
41 std::unique_ptr<PassMan> man;
42 std::unique_ptr<Pipeline> pipe;
43 World& world_;
44};
45
46/// @name Register Pass/Phase
47///@{
48template<class A, class P, class... CArgs> void register_pass(Passes& passes, CArgs&&... args) {
50 [... args = std::forward<CArgs>(args)](World&, PipelineBuilder& builder, const Def* app) {
51 builder.add_pass<P>(app, args...);
52 });
53}
54
55template<class A, class P, class... CArgs> void register_phase(Passes& passes, CArgs&&... args) {
57 [... args = std::forward<CArgs>(args)](World&, PipelineBuilder& builder, const Def*) {
58 builder.add_phase<P>(args...);
59 });
60}
61
62template<class A, class P, class Q> void register_pass_with_arg(Passes& passes) {
63 assert_emplace(passes, flags_t(Annex::Base<A>), [](World& world, PipelineBuilder& builder, const Def* app) {
64 auto pass_arg = (Q*)(builder.pass(app->as<App>()->arg()));
65 world.DLOG("register using arg: {} of type {} for gid {}", pass_arg, typeid(Q).name(),
66 app->as<App>()->arg()->gid());
67 builder.add_pass<P>(app, pass_arg);
68 });
69}
70///@}
71
72} // namespace thorin
const Def * arg() const
Definition lam.h:215
Base class for all Defs.
Definition def.h:222
u32 gid() const
Definition def.h:238
All Passes that want to be registered in the PassMan must implement this interface.
Definition pass.h:22
void add_pass(const Def *def, Args &&... args)
PipelineBuilder(World &world)
Pass * pass(const Def *)
void add_phase(Args &&... args)
void def2pass(const Def *, Pass *p)
Organizes several Phases as a pipeline.
Definition phase.h:114
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Definition span.h:103
Definition cfg.h:11
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_with_arg(Passes &passes)
void register_pass(Passes &passes, CArgs &&... args)
void register_phase(Passes &passes, CArgs &&... args)
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:46
static constexpr flags_t Base
Definition plugin.h:131