"examples/vscode:/vscode.git/clone" did not exist on "fb6b101c49ca79143f0ad3054aedb0adc34b9847"
pass_manager.cpp 4.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#include <migraphx/program.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/target.hpp>
#include <migraphx/env.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/time.hpp>
#include <migraphx/iterator_for.hpp>
#include <iostream>
#include <sstream>
#include <algorithm>
13
#include <unordered_map>
14
15
16
17
18
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

19
20
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_PASSES);

umangyadav's avatar
umangyadav committed
21
void validate_pass(module& mod, const pass& p, const tracer& trace)
22
23
24
{
    (void)mod;
    (void)p;
umangyadav's avatar
umangyadav committed
25
    (void)trace;
26
#ifndef NDEBUG
27
    trace("Validate...");
28
29
30
31
32
33
34
    auto invalid = mod.validate();
    if(invalid != mod.end())
    {
        auto index = std::distance(mod.begin(), invalid);
        MIGRAPHX_THROW(p.name() + " pass produces invalid program at instruction " +
                       std::to_string(index) + ": " + invalid->name());
    }
35
    trace();
36
37
#endif
}
38
39

void run_pass(program& prog, const pass& p, tracer& trace)
40
{
41
    auto t_start = std::chrono::high_resolution_clock::now();
42
    p.apply(prog);
umangyadav's avatar
umangyadav committed
43
44
45
46
47
48
49
50
51
52
    auto t_end             = std::chrono::high_resolution_clock::now();
    double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end - t_start).count();
    trace(p.name(),
          "Pass: ",
          p.name(),
          "\n",
          prog,
          "Elapsed Wall Time (ms): ",
          elapsed_time_ms,
          "\n");
53
54
}

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
struct module_pm : module_pass_manager
{
    module* mod;
    program* prog;
    tracer* t;

    module_pm(module* pmod = nullptr, program* pprog = nullptr, tracer* pt = nullptr)
        : mod(pmod), prog(pprog), t(pt)
    {
    }

    template <class... Ts>
    void trace(Ts&&... xs) const
    {
        assert(t);
        (*t)(xs...);
    }

    virtual module& get_module() override
    {
        assert(mod);
        return *mod;
    }
    virtual module* create_module(const std::string& name) override
    {
        assert(prog);
        return prog->create_module(name);
    }
    virtual void run_pass(const pass& p) override
    {
        assert(mod);
        assert(mod->validate() == mod->end());
87
        auto t_start = std::chrono::high_resolution_clock::now();
88
        p.apply(*this);
umangyadav's avatar
umangyadav committed
89
90
91
92
93
94
95
96
97
98
99
        auto t_end             = std::chrono::high_resolution_clock::now();
        double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end - t_start).count();
        trace(p.name(),
              "Module: ",
              mod->name(),
              ", Pass: ",
              p.name(),
              "\n",
              *mod,
              "Elapsed Wall Time (ms): ",
              elapsed_time_ms);
100
        validate_pass(*mod, p, *t);
101
102
103
104
105
    }
};

module& get_module(module_pass_manager& mpm) { return mpm.get_module(); }

106
void run_passes(module& mod, const std::vector<pass>& passes, tracer trace)
107
{
umangyadav's avatar
umangyadav committed
108
    if(enabled(MIGRAPHX_TRACE_PASSES{}) and not trace.enabled())
109
        trace = tracer{std::cout};
110
    for(const auto& p : passes)
111
    {
112
        module_pm{&mod, nullptr, &trace}.run_pass(p);
113
114
    }
}
115

116
117
void run_passes(program& prog, const std::vector<pass>& passes, tracer trace)
{
118
    if(enabled(MIGRAPHX_TRACE_PASSES{}) and not trace.enabled())
119
        trace = tracer{std::cout};
umangyadav's avatar
umangyadav committed
120

121
    std::unordered_map<std::string, tracer> module_tracer_map;
122
123
124
125
    for(const auto& p : passes)
    {
        auto mods = prog.get_modules();
        for(const auto& mod : reverse(mods))
126
        {
umangyadav's avatar
umangyadav committed
127
128
129
130
131
132
            // Set tracer for module passes, if tracer is set to output to file stream then set name
            // of the dump directory. For file dumps, tracer object internally sets the counter for
            // the individual passes' file dumps.
            if(module_tracer_map.find(mod->name()) == module_tracer_map.end())
            {
                module_tracer_map[mod->name()] =
umangyadav's avatar
umangyadav committed
133
134
                    // cppcheck-suppress stlFindInsert
                    trace.fs_enabled() ? tracer{trace.dump_dir + "/" + mod->name()} : trace; 
135
            }
136
137
            if(mod->bypass())
                continue;
138
            module_pm{mod, &prog, &module_tracer_map[mod->name()]}.run_pass(p);
139
        }
140
        run_pass(prog, p, trace);
141
142
143
144
145
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx