"ts/webui/src/static/style/table.scss" did not exist on "f23f8a0688557e3ca3cf8bbf8e7669eab9912434"
instruction.cpp 6.08 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraphx/instruction.hpp>
#include <migraphx/builtin.hpp>
#include <migraphx/erase.hpp>
Paul's avatar
Paul committed
4

Paul's avatar
Paul committed
5
namespace migraphx {
Paul's avatar
Paul committed
6
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
9
10
11
instruction::instruction(operation o, shape r, std::vector<instruction_ref> args)
    : op(std::move(o)), result(std::move(r)), arguments(std::move(args))
{
}
Paul's avatar
Paul committed
12

Paul's avatar
Paul committed
13
14
15
16
instruction::instruction(literal l)
    : op(builtin::literal{}), result(l.get_shape()), lit(std::move(l))
{
}
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
19
20
void instruction::replace(const shape& r)
{
    if(r != result)
Paul's avatar
Paul committed
21
    {
Paul's avatar
Paul committed
22
23
        result = r;
        for(auto&& ins : output)
Paul's avatar
Paul committed
24
        {
Paul's avatar
Paul committed
25
26
            assert(ins->name().front() != '@');
            ins->recompute_shape();
Paul's avatar
Paul committed
27
28
        }
    }
Paul's avatar
Paul committed
29
}
Paul's avatar
Paul committed
30

Paul's avatar
Paul committed
31
void instruction::recompute_shape() { replace(compute_shape(op, arguments)); }
Paul's avatar
Paul committed
32

Paul's avatar
Paul committed
33
34
35
void instruction::clear_arguments()
{
    for(auto&& arg : arguments)
Paul's avatar
Paul committed
36
    {
Paul's avatar
Paul committed
37
        arg->remove_output(*this);
Paul's avatar
Paul committed
38
    }
Paul's avatar
Paul committed
39
40
41
42
43
44
45
    arguments.clear();
}

bool operator==(const instruction& i, instruction_ref ref)
{
    return std::addressof(i) == std::addressof(*ref);
}
Paul's avatar
Paul committed
46

Paul's avatar
Paul committed
47
48
49
50
51
52
53
54
55
56
57
58
59
bool instruction::valid(instruction_ref start) const
{
    return valid() && std::all_of(arguments.begin(), arguments.end(), [&](instruction_ref i) {
               auto self = std::find(i->outputs().begin(), i->outputs().end(), *this);
               return self != i->outputs().end() &&
                      std::distance(start, i) < std::distance(start, *self);
           });
}

bool instruction::valid() const
{
    shape computed;
    if(op.name() == "@literal")
Paul's avatar
Paul committed
60
    {
Paul's avatar
Paul committed
61
        computed = lit.get_shape();
Paul's avatar
Paul committed
62
    }
Paul's avatar
Paul committed
63
    else if(op.name() == "@param")
Paul's avatar
Paul committed
64
    {
Paul's avatar
Paul committed
65
        computed = result;
Paul's avatar
Paul committed
66
    }
Paul's avatar
Paul committed
67
    else
Paul's avatar
Paul committed
68
    {
Paul's avatar
Paul committed
69
        try
Paul's avatar
Paul committed
70
        {
Paul's avatar
Paul committed
71
            computed = compute_shape(op, arguments);
Paul's avatar
Paul committed
72
        }
Paul's avatar
Paul committed
73
        catch(migraphx::exception&)
Paul's avatar
Paul committed
74
        {
Paul's avatar
Paul committed
75
            return false;
Paul's avatar
Paul committed
76
77
        }
    }
Paul's avatar
Paul committed
78
79
80
81
    return result == computed && std::all_of(output.begin(), output.end(), [&](instruction_ref i) {
               return std::find(i->inputs().begin(), i->inputs().end(), *this) != i->inputs().end();
           });
}
Paul's avatar
Paul committed
82

Paul's avatar
Paul committed
83
84
85
86
87
88
shape instruction::get_shape() const { return result; }
const literal& instruction::get_literal() const
{
    assert(op.name() == "@literal");
    return lit;
}
Paul's avatar
Paul committed
89

Paul's avatar
Paul committed
90
const operation& instruction::get_operator() const { return op; }
Paul's avatar
Paul committed
91

Paul's avatar
Paul committed
92
std::string instruction::name() const { return op.name(); }
Paul's avatar
Paul committed
93

Paul's avatar
Paul committed
94
const std::vector<instruction_ref>& instruction::inputs() const { return arguments; }
Paul's avatar
Paul committed
95

Paul's avatar
Paul committed
96
const std::vector<instruction_ref>& instruction::outputs() const { return output; }
Paul's avatar
Paul committed
97

Paul's avatar
Paul committed
98
99
bool operator==(const instruction& x, const instruction& y)
{
100
    if(std::tie(x.result, x.op, x.arguments) != std::tie(y.result, y.op, y.arguments))
Paul's avatar
Paul committed
101
102
103
104
105
        return false;
    if(x.name() == "@literal")
        return x.lit == y.lit;
    return true;
}
Paul's avatar
Paul committed
106

Paul's avatar
Paul committed
107
108
bool operator!=(const instruction& x, const instruction& y) { return !(x == y); }

Paul's avatar
Paul committed
109
bool operator==(instruction_ref ref, const instruction& i) { return i == ref; }
Paul's avatar
Paul committed
110

Paul's avatar
Paul committed
111
bool operator!=(const instruction& i, instruction_ref ref) { return !(i == ref); }
Paul's avatar
Paul committed
112

Paul's avatar
Paul committed
113
bool operator!=(instruction_ref ref, const instruction& i) { return !(i == ref); }
Paul's avatar
Paul committed
114

Paul's avatar
Paul committed
115
116
117
118
119
void instruction::add_output(instruction_ref ins)
{
    if(std::find(output.begin(), output.end(), ins) == output.end())
        output.push_back(ins);
}
Paul's avatar
Paul committed
120

Paul's avatar
Paul committed
121
122
123
124
125
void instruction::backreference(instruction_ref ref)
{
    for(auto&& arg : ref->inputs())
        arg->add_output(ref);
}
Paul's avatar
Paul committed
126

Paul's avatar
Paul committed
127
128
129
130
131
132
133
134
void instruction::replace_argument(instruction_ref ins,
                                   instruction_ref old,
                                   instruction_ref new_ins)
{
    ins->replace_argument(old, new_ins);
    backreference(ins);
    ins->recompute_shape();
}
Paul's avatar
Paul committed
135

Paul's avatar
Paul committed
136
137
138
139
140
141
142
143
void instruction::replace(instruction_ref ins,
                          operation o,
                          const shape& r,
                          std::vector<instruction_ref> args)
{
    ins->replace(std::move(o), r, std::move(args));
    backreference(ins);
}
Paul's avatar
Paul committed
144

Paul's avatar
Paul committed
145
146
147
148
149
150
void instruction::replace(operation o, const shape& r, std::vector<instruction_ref> args)
{
    op = std::move(o);
    replace(r);
    replace(std::move(args));
}
Paul's avatar
Paul committed
151

Paul's avatar
Paul committed
152
153
154
155
156
void instruction::replace(std::vector<instruction_ref> args)
{
    clear_arguments();
    arguments = std::move(args);
}
Paul's avatar
Paul committed
157

Paul's avatar
Paul committed
158
159
void instruction::replace_argument(instruction_ref old, instruction_ref new_ins)
{
Paul's avatar
Paul committed
160
    assert(std::any_of(arguments.begin(), arguments.end(), [&](auto i) { return i == old; }));
Paul's avatar
Paul committed
161
162
163
    std::replace(arguments.begin(), arguments.end(), old, new_ins);
    old->remove_output(*this);
}
Paul's avatar
Paul committed
164

Paul's avatar
Paul committed
165
166
167
168
169
170
bool instruction::can_eval() const
{
    if(op.name() == "@literal")
    {
        return true;
    }
Paul's avatar
Paul committed
171
    else if(is_context_free(op))
Paul's avatar
Paul committed
172
    {
Paul's avatar
Paul committed
173
174
        return std::all_of(
            this->inputs().begin(), this->inputs().end(), [](auto arg) { return arg->can_eval(); });
Paul's avatar
Paul committed
175
176
177
178
179
180
181
    }
    else
    {
        return false;
    }
}

Paul's avatar
Paul committed
182
argument instruction::eval(bool check_eval) const
Paul's avatar
Paul committed
183
184
185
186
187
{
    if(op.name() == "@literal")
    {
        return this->get_literal().get_argument();
    }
Paul's avatar
Paul committed
188
    if(is_context_free(op))
Paul's avatar
Paul committed
189
    {
Paul's avatar
Paul committed
190
        if(check_eval and not this->can_eval())
Paul's avatar
Paul committed
191
            return {};
Paul's avatar
Paul committed
192
        std::vector<argument> args;
Paul's avatar
Paul committed
193
194
195
        std::transform(this->inputs().begin(),
                       this->inputs().end(),
                       std::back_inserter(args),
Paul's avatar
Paul committed
196
                       [](auto arg) { return arg->eval(false); });
Paul's avatar
Paul committed
197
198
199
200
201
        return op.compute(result, args);
    }
    return {};
}

Paul's avatar
Paul committed
202
203
void instruction::finalize(context& ctx)
{
Paul's avatar
Paul committed
204
    if(has_finalize(this->op))
Paul's avatar
Paul committed
205
206
207
        this->op.finalize(ctx, this->get_shape(), to_shapes(this->inputs()));
}

Paul's avatar
Paul committed
208
instruction_ref instruction::get_output_alias(instruction_ref ins, bool shallow)
Paul's avatar
Paul committed
209
{
Paul's avatar
Paul committed
210
    auto i = ins->get_operator().output_alias(to_shapes(ins->inputs()));
Paul's avatar
Paul committed
211
212
    if(i < 0)
        return ins;
Paul's avatar
Paul committed
213
    if(shallow)
Paul's avatar
Paul committed
214
        return ins->inputs().at(i);
Paul's avatar
Paul committed
215
216
217
    return get_output_alias(ins->inputs().at(i));
}

Paul's avatar
Paul committed
218
219
220
221
222
223
224
225
std::vector<shape> to_shapes(const std::vector<instruction_ref>& args)
{
    std::vector<shape> shapes(args.size());
    std::transform(
        args.begin(), args.end(), shapes.begin(), [](instruction_ref i) { return i->get_shape(); });
    return shapes;
}

Paul's avatar
Paul committed
226
227
shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
{
Paul's avatar
Paul committed
228
    return op.compute_shape(to_shapes(args));
Paul's avatar
Paul committed
229
230
}

Paul's avatar
Paul committed
231
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
232
} // namespace migraphx