pass_manager.cpp 5.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

41
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_PASSES);
42
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TIME_PASSES);
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void validate_pass(module& mod, const pass& p, tracer trace)
{
    (void)mod;
    (void)p;
    (void)trace;
#ifndef NDEBUG
    trace("Validate ...");
    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());
    }
    trace();
#endif
}
void run_pass(program& prog, const pass& p, tracer trace)
{
    trace("Pass: ", p.name());
    p.apply(prog);
    trace(prog);
}

Paul's avatar
Paul committed
68
69
70
std::size_t get_size(const program& p)
{
    std::size_t n = 0;
Paul's avatar
Format  
Paul committed
71
72
73
74
75
    for(auto* mod : p.get_modules())
    {
        for(const auto& ins : *mod)
        {
            if(ins.name() != "@literal")
Paul's avatar
Paul committed
76
77
78
79
80
81
82
83
84
85
                continue;
            n += ins.get_literal().get_shape().bytes();
        }
    }
    return n;
}

std::string pretty_size(std::size_t n)
{
    const std::vector<std::string> keys = {"", "K", "M", "G", "T"};
Paul's avatar
Format  
Paul committed
86
87
    double d                            = n;
    std::size_t i                       = 0;
Paul's avatar
Paul committed
88
89
90
91
92
93
94
95
    while(d > 1024 and i < keys.size())
    {
        d /= 1024;
        i++;
    }
    return std::to_string(d) + keys[std::max<std::ptrdiff_t>(0, i - 1)];
}

96
97
struct module_pm : module_pass_manager
{
98
99
100
101
    module* mod           = nullptr;
    tracer* t             = nullptr;
    module* common_parent = nullptr;
    program* prog         = nullptr;
102

103
    module_pm(module* pmod = nullptr, tracer* pt = nullptr) : mod(pmod), t(pt) {}
104
105
106
107
108
109
110
111
112
113
114
115
116

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

    virtual module& get_module() override
    {
        assert(mod);
        return *mod;
    }
Charlie Lin's avatar
Charlie Lin committed
117

118
119
120
121
122
    virtual module* create_module(const std::string& name) override
    {
        assert(prog);
        return prog->create_module(name);
    }
Charlie Lin's avatar
Charlie Lin committed
123

124
    virtual module* get_common_parent() override { return common_parent; }
Charlie Lin's avatar
Charlie Lin committed
125
126
127
128
129
130
131

    virtual module* get_root_module() override
    {
        assert(prog);
        return prog->get_main_module();
    }

132
133
134
135
    virtual void run_pass(const pass& p) override
    {
        assert(mod);
        assert(mod->validate() == mod->end());
136
137
138
139
140
141
142
143
144
145
        if(enabled(MIGRAPHX_TIME_PASSES{}))
        {
            using milliseconds = std::chrono::duration<double, std::milli>;
            auto ms            = time<milliseconds>([&] { p.apply(*this); });
            std::cout << p.name() << ": " << ms << "ms\n";
        }
        else
        {
            p.apply(*this);
        }
146
147
148
149
150
151
152
        trace(*mod);
        validate_pass(*mod, p, *t);
    }
};

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

153
void run_passes(module& mod, const std::vector<pass>& passes, tracer trace)
154
{
155
156
    if(enabled(MIGRAPHX_TRACE_PASSES{}))
        trace = tracer{std::cout};
157
    for(const auto& p : passes)
158
    {
159
        module_pm{&mod, &trace}.run_pass(p);
160
161
    }
}
162

163
164
void run_passes(program& prog, const std::vector<pass>& passes, tracer trace)
{
165
166
    if(enabled(MIGRAPHX_TRACE_PASSES{}))
        trace = tracer{std::cout};
167
    std::unordered_set<module_ref> visited;
168
169
    for(const auto& p : passes)
    {
Paul's avatar
Paul committed
170
        std::cout << p.name() << ": " << pretty_size(get_size(prog)) << std::endl;
171
        auto mods = prog.get_modules();
172
173
        auto tree = prog.get_module_tree();
        visited.clear();
174
        for(const auto& mod : reverse(mods))
175
        {
176
177
            if(mod->bypass())
                continue;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
            if(not visited.insert(mod).second)
                continue;
            module_pm mpm{mod, &trace};
            mpm.prog      = &prog;
            auto parents  = range(tree.equal_range(mod));
            auto nparents = distance(parents);
            if(nparents == 0)
                mpm.common_parent = nullptr;
            else if(nparents == 1)
                mpm.common_parent = parents.begin()->second;
            else
                // Just set common parent to main module when there is muliple parents for now
                // TODO: Compute the common parent
                mpm.common_parent = prog.get_main_module();
            mpm.run_pass(p);
193
        }
194
        run_pass(prog, p, trace);
Paul's avatar
Paul committed
195
        std::cout << p.name() << ": " << pretty_size(get_size(prog)) << std::endl;
196
197
198
199
200
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx