eliminate_layout.cpp 4.09 KB
Newer Older
Khalique Ahmed's avatar
Khalique Ahmed committed
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
#include "migraphx/instruction_ref.hpp"
#include <cstdio>
Khalique Ahmed's avatar
Khalique Ahmed committed
26
27
28
29
30
31
32
33
34
35
36
#include <migraphx/eliminate_layout.hpp>
#include <migraphx/module.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/permutation.hpp>
#include <migraphx/functional.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/pass_manager.hpp>
37
38
#include <unordered_set>
#include <vector>
Khalique Ahmed's avatar
Khalique Ahmed committed
39
40
41
42

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

Khalique Ahmed's avatar
Khalique Ahmed committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// template <class Predicate>
// std::vector<instruction_ref> find_lasts(const module& m, Predicate pred)
// {
//     std::vector<instruction_ref> result;
//     fix([&](auto self, auto ins) {
//         if(pred(ins))
//         {
//             result.push_back(ins);
//             return;
//         }
//         for(auto input : ins->inputs())
//             self(input);
//     })(std::prev(m.end()));
//     return result;
// }
Khalique Ahmed's avatar
Khalique Ahmed committed
58

Khalique Ahmed's avatar
Khalique Ahmed committed
59
60
61
62
63
64
65
66
// std::unordered_set<instruction_ref> preserve_output_layout(module& m)
// {
//     std::unordered_set<instruction_ref> result;
//     std::vector<instruction_ref> outputs =
//         find_lasts(m, [](auto ins) { return ins->get_shape().lens().size() == 4; });
//     for(auto output : outputs)
//     {
//         auto permutation = find_permutation(output->get_shape());
Khalique Ahmed's avatar
Khalique Ahmed committed
67

Khalique Ahmed's avatar
Khalique Ahmed committed
68
69
//         auto layout_ins = m.insert_instruction(
//             std::next(output), make_op("layout", {{"permutation", permutation}}), output);
Khalique Ahmed's avatar
Khalique Ahmed committed
70

Khalique Ahmed's avatar
Khalique Ahmed committed
71
72
73
74
//         auto output1 = m.insert_instruction(
//             layout_ins, make_op("allocate", {{"shape", to_value(layout_ins->get_shape())}}));
//         std::vector<instruction_ref> refs = layout_ins->inputs();
//         refs.push_back(output1);
Khalique Ahmed's avatar
Khalique Ahmed committed
75

Khalique Ahmed's avatar
Khalique Ahmed committed
76
77
78
79
80
//         auto layout = m.replace_instruction(
//             layout_ins,
//             make_op("gpu::precompile_op", {{"op", to_value(layout_ins->get_operator())}}),
//             refs,
//             layout_ins->module_inputs());
Khalique Ahmed's avatar
Khalique Ahmed committed
81

Khalique Ahmed's avatar
Khalique Ahmed committed
82
//         result.insert(layout);
83
//     }
Khalique Ahmed's avatar
Khalique Ahmed committed
84
//     return result;
85
// }
Khalique Ahmed's avatar
Khalique Ahmed committed
86

Khalique Ahmed's avatar
Khalique Ahmed committed
87
void remove_layout(module& m)
Khalique Ahmed's avatar
Khalique Ahmed committed
88
89
90
91
92
{
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "gpu::precompile_op")
            continue;
93

Khalique Ahmed's avatar
Khalique Ahmed committed
94
95
        auto precompile_op = ins->get_operator();
        auto val           = precompile_op.to_value();
96

Khalique Ahmed's avatar
Khalique Ahmed committed
97
98
99
100
101
102
103
104
        if(val["op"].at("name").to<std::string>() != "layout")
        {
            continue;
        }
        if(ins->get_shape() != ins->inputs().front()->get_shape())
        {
            continue;
        }
Khalique Ahmed's avatar
Khalique Ahmed committed
105
106
        // if(contains(output_layouts, ins))
        //     continue;
107

Khalique Ahmed's avatar
Khalique Ahmed committed
108
109
110
        m.replace_instruction(ins, ins->inputs().front());
    }
}
Khalique Ahmed's avatar
Khalique Ahmed committed
111
112
113

void eliminate_layout::apply(module_pass_manager& mpm) const
{
Khalique Ahmed's avatar
Khalique Ahmed committed
114
115
    // std::unordered_set<instruction_ref> output_layouts =
    // preserve_output_layout(mpm.get_module());
Khalique Ahmed's avatar
Khalique Ahmed committed
116
    remove_layout(mpm.get_module());
Khalique Ahmed's avatar
Khalique Ahmed committed
117
118
119
120
121
    mpm.run_pass(dead_code_elimination{});
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx