instruction.cpp 7.62 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
        {
25
26
27
            if(ins->name() == "@return")
                continue;

Paul's avatar
Paul committed
28
29
            assert(ins->name().front() != '@');
            ins->recompute_shape();
Paul's avatar
Paul committed
30
31
        }
    }
Paul's avatar
Paul committed
32
}
Paul's avatar
Paul committed
33

Paul's avatar
Paul committed
34
void instruction::replace(operation o)
Paul's avatar
Paul committed
35
{
36
37
    normalized = false;
    op         = std::move(o);
Paul's avatar
Paul committed
38
39
40
    recompute_shape();
}

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

Paul's avatar
Paul committed
43
44
45
void instruction::clear_arguments()
{
    for(auto&& arg : arguments)
Paul's avatar
Paul committed
46
    {
Paul's avatar
Paul committed
47
        arg->remove_output(*this);
Paul's avatar
Paul committed
48
    }
Paul's avatar
Paul committed
49
50
51
52
53
54
55
    arguments.clear();
}

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

Paul's avatar
Paul committed
57
58
59
60
61
62
63
64
65
66
67
68
69
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
70
    {
Paul's avatar
Paul committed
71
        computed = lit.get_shape();
Paul's avatar
Paul committed
72
    }
Paul's avatar
Paul committed
73
    else if(op.name() == "@param")
Paul's avatar
Paul committed
74
    {
Paul's avatar
Paul committed
75
        computed = result;
Paul's avatar
Paul committed
76
    }
77
78
79
80
    else if(op.name() == "@return")
    {
        computed = {};
    }
Paul's avatar
Paul committed
81
    else
Paul's avatar
Paul committed
82
    {
Paul's avatar
Paul committed
83
        try
Paul's avatar
Paul committed
84
        {
Paul's avatar
Paul committed
85
            computed = compute_shape(op, arguments);
Paul's avatar
Paul committed
86
        }
Paul's avatar
Paul committed
87
        catch(migraphx::exception&)
Paul's avatar
Paul committed
88
        {
Paul's avatar
Paul committed
89
            return false;
Paul's avatar
Paul committed
90
91
        }
    }
92

Paul's avatar
Paul committed
93
94
95
96
    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
97

Paul's avatar
Paul committed
98
99
100
101
102
103
shape instruction::get_shape() const { return result; }
const literal& instruction::get_literal() const
{
    assert(op.name() == "@literal");
    return lit;
}
Paul's avatar
Paul committed
104

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

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

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

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

Paul's avatar
Paul committed
113
114
bool operator==(const instruction& x, const instruction& y)
{
115
    if(std::tie(x.result, x.op, x.arguments) != std::tie(y.result, y.op, y.arguments))
Paul's avatar
Paul committed
116
117
118
119
120
        return false;
    if(x.name() == "@literal")
        return x.lit == y.lit;
    return true;
}
Paul's avatar
Paul committed
121

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

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

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

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

Paul's avatar
Paul committed
130
131
132
133
134
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
135

Paul's avatar
Paul committed
136
137
138
139
140
void instruction::backreference(instruction_ref ref)
{
    for(auto&& arg : ref->inputs())
        arg->add_output(ref);
}
Paul's avatar
Paul committed
141

Paul's avatar
Paul committed
142
143
144
145
146
147
148
149
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
150

Paul's avatar
Paul committed
151
152
153
154
155
156
157
158
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
159

Paul's avatar
Paul committed
160
161
void instruction::replace(operation o, const shape& r, std::vector<instruction_ref> args)
{
162
163
    normalized = false;
    op         = std::move(o);
Paul's avatar
Paul committed
164
165
166
    replace(r);
    replace(std::move(args));
}
Paul's avatar
Paul committed
167

Paul's avatar
Paul committed
168
169
170
171
172
void instruction::replace(std::vector<instruction_ref> args)
{
    clear_arguments();
    arguments = std::move(args);
}
Paul's avatar
Paul committed
173

Paul's avatar
Paul committed
174
175
void instruction::replace_argument(instruction_ref old, instruction_ref new_ins)
{
Paul's avatar
Paul committed
176
    assert(std::any_of(arguments.begin(), arguments.end(), [&](auto i) { return i == old; }));
Paul's avatar
Paul committed
177
178
179
    std::replace(arguments.begin(), arguments.end(), old, new_ins);
    old->remove_output(*this);
}
Paul's avatar
Paul committed
180

Paul's avatar
Paul committed
181
182
183
184
185
186
bool instruction::can_eval() const
{
    if(op.name() == "@literal")
    {
        return true;
    }
Paul's avatar
Paul committed
187
    else if(is_context_free(op))
Paul's avatar
Paul committed
188
    {
Paul's avatar
Paul committed
189
190
        return std::all_of(
            this->inputs().begin(), this->inputs().end(), [](auto arg) { return arg->can_eval(); });
Paul's avatar
Paul committed
191
192
193
194
195
196
197
    }
    else
    {
        return false;
    }
}

Paul's avatar
Paul committed
198
argument instruction::eval(bool check_eval) const
Paul's avatar
Paul committed
199
200
201
202
203
{
    if(op.name() == "@literal")
    {
        return this->get_literal().get_argument();
    }
Paul's avatar
Paul committed
204
    if(is_context_free(op))
Paul's avatar
Paul committed
205
    {
Paul's avatar
Paul committed
206
        if(check_eval and not this->can_eval())
Paul's avatar
Paul committed
207
            return {};
Paul's avatar
Paul committed
208
        std::vector<argument> args;
Paul's avatar
Paul committed
209
210
211
        std::transform(this->inputs().begin(),
                       this->inputs().end(),
                       std::back_inserter(args),
Paul's avatar
Paul committed
212
                       [](auto arg) { return arg->eval(false); });
213
        return normalized_operator().compute(result, args);
Paul's avatar
Paul committed
214
215
216
217
    }
    return {};
}

Paul's avatar
Paul committed
218
219
void instruction::finalize(context& ctx)
{
Paul's avatar
Paul committed
220
    if(has_finalize(this->op))
Paul's avatar
Paul committed
221
222
223
        this->op.finalize(ctx, this->get_shape(), to_shapes(this->inputs()));
}

Paul Fultz II's avatar
Paul Fultz II committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
static void debug_name(std::ostream& os, const instruction& ins)
{
    if(ins.name() == "@literal")
    {
        os << "@literal";
        if(ins.get_literal().get_shape().elements() > 10)
            os << "{ ... }";
        else
            os << "{" << ins.get_literal() << "}";
    }
    else
    {
        os << ins.get_operator();
    }
}

void instruction::debug_print() const
{
    debug_name(std::cout, *this);
    std::string delim = "(";
    for(auto arg : this->inputs())
    {
        std::cout << delim;
        debug_name(std::cout, *arg);
        delim = ", ";
    }
    if(not this->inputs().empty())
        std::cout << ")";
    std::cout << " -> " << this->get_shape() << std::endl;
}

Paul's avatar
Paul committed
255
instruction_ref instruction::get_output_alias(instruction_ref ins, bool shallow)
Paul's avatar
Paul committed
256
{
Paul's avatar
Paul committed
257
    auto i = ins->get_operator().output_alias(to_shapes(ins->inputs()));
Paul's avatar
Paul committed
258
259
    if(i < 0)
        return ins;
Paul's avatar
Paul committed
260
    if(shallow)
Paul's avatar
Paul committed
261
        return ins->inputs().at(i);
Paul's avatar
Paul committed
262
263
264
    return get_output_alias(ins->inputs().at(i));
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
void instruction::set_normalized(bool value) { normalized = value; }

bool instruction::is_normalized() const { return normalized; }

bool instruction::need_normalization() const
{
    return this->get_operator().need_normalization() and not normalized;
}

operation instruction::normalized_operator() const
{
    operation o = this->get_operator();
    if(this->need_normalization())
    {
        auto lens = this->inputs().front()->get_shape().lens();
        if(!normalize_attributes(o, lens))
            return this->get_operator();
    }
    return o;
}

Paul's avatar
Paul committed
286
287
288
289
290
291
292
293
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
294
295
shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
{
Paul's avatar
Paul committed
296
    return op.compute_shape(to_shapes(args));
Paul's avatar
Paul committed
297
298
}

Paul's avatar
Paul committed
299
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
300
} // namespace migraphx