instruction.cpp 6.17 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
32
33
34
35
36
void instruction::replace(const operation& o)
{
    op = std::move(o);
    recompute_shape();
}

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

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

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

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

Paul's avatar
Paul committed
89
90
91
92
93
94
shape instruction::get_shape() const { return result; }
const literal& instruction::get_literal() const
{
    assert(op.name() == "@literal");
    return lit;
}
Paul's avatar
Paul committed
95

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

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

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

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

Paul's avatar
Paul committed
104
105
bool operator==(const instruction& x, const instruction& y)
{
106
    if(std::tie(x.result, x.op, x.arguments) != std::tie(y.result, y.op, y.arguments))
Paul's avatar
Paul committed
107
108
109
110
111
        return false;
    if(x.name() == "@literal")
        return x.lit == y.lit;
    return true;
}
Paul's avatar
Paul committed
112

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

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

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

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

Paul's avatar
Paul committed
121
122
123
124
125
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
126

Paul's avatar
Paul committed
127
128
129
130
131
void instruction::backreference(instruction_ref ref)
{
    for(auto&& arg : ref->inputs())
        arg->add_output(ref);
}
Paul's avatar
Paul committed
132

Paul's avatar
Paul committed
133
134
135
136
137
138
139
140
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
141

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

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

Paul's avatar
Paul committed
158
159
160
161
162
void instruction::replace(std::vector<instruction_ref> args)
{
    clear_arguments();
    arguments = std::move(args);
}
Paul's avatar
Paul committed
163

Paul's avatar
Paul committed
164
165
void instruction::replace_argument(instruction_ref old, instruction_ref new_ins)
{
Paul's avatar
Paul committed
166
    assert(std::any_of(arguments.begin(), arguments.end(), [&](auto i) { return i == old; }));
Paul's avatar
Paul committed
167
168
169
    std::replace(arguments.begin(), arguments.end(), old, new_ins);
    old->remove_output(*this);
}
Paul's avatar
Paul committed
170

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

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

Paul's avatar
Paul committed
208
209
void instruction::finalize(context& ctx)
{
Paul's avatar
Paul committed
210
    if(has_finalize(this->op))
Paul's avatar
Paul committed
211
212
213
        this->op.finalize(ctx, this->get_shape(), to_shapes(this->inputs()));
}

Paul's avatar
Paul committed
214
instruction_ref instruction::get_output_alias(instruction_ref ins, bool shallow)
Paul's avatar
Paul committed
215
{
Paul's avatar
Paul committed
216
    auto i = ins->get_operator().output_alias(to_shapes(ins->inputs()));
Paul's avatar
Paul committed
217
218
    if(i < 0)
        return ins;
Paul's avatar
Paul committed
219
    if(shallow)
Paul's avatar
Paul committed
220
        return ins->inputs().at(i);
Paul's avatar
Paul committed
221
222
223
    return get_output_alias(ins->inputs().at(i));
}

Paul's avatar
Paul committed
224
225
226
227
228
229
230
231
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
232
233
shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
{
Paul's avatar
Paul committed
234
    return op.compute_shape(to_shapes(args));
Paul's avatar
Paul committed
235
236
}

Paul's avatar
Paul committed
237
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
238
} // namespace migraphx