program.cpp 7.79 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraph/program.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/instruction.hpp>
Paul's avatar
Paul committed
4
#include <iostream>
Paul's avatar
Paul committed
5
#include <sstream>
Paul's avatar
Paul committed
6
7
#include <algorithm>

Paul's avatar
Paul committed
8
namespace migraph {
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
11
12
13
struct program_impl
{
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
Paul's avatar
Paul committed
14
    context ctx;
Paul's avatar
Paul committed
15
16
};

Paul's avatar
Paul committed
17
const operation& get_operation(instruction_ref ins) { return ins->op; }
Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
program::program() : impl(std::make_unique<program_impl>()) {}
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
program::program(program&&) noexcept = default;
Paul's avatar
Paul committed
22
23
program& program::operator=(program&&) noexcept = default;
program::~program() noexcept                    = default;
Paul's avatar
Paul committed
24

Paul's avatar
Paul committed
25
instruction_ref program::add_instruction(operation op, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
26
27
28
{
    return insert_instruction(impl->instructions.end(), std::move(op), std::move(args));
}
Paul's avatar
Paul committed
29
30
instruction_ref
program::insert_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
31
{
Paul's avatar
Paul committed
32
33
34
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
35
    assert(not starts_with(op.name(), "@"));
Paul's avatar
Paul committed
36
37
    // TODO: Use move
    shape r     = compute_shape(op, args);
Paul's avatar
Paul committed
38
    auto result = impl->instructions.insert(ins, {op, r, args});
Paul's avatar
Paul committed
39
    backreference(result);
Paul's avatar
Paul committed
40
    assert(result->arguments == args);
Paul's avatar
Paul committed
41
    return result;
Paul's avatar
Paul committed
42
43
}

Paul's avatar
Paul committed
44
45
46
47
48
49
instruction_ref
program::replace_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args)
{
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
50
    assert(not starts_with(op.name(), "@"));
Paul's avatar
Paul committed
51

Paul's avatar
Paul committed
52
    shape r = compute_shape(op, args);
Paul's avatar
Paul committed
53
54
55
56
57
    ins->replace(op, r, args);
    backreference(ins);
    return ins;
}

Paul's avatar
Paul committed
58
instruction_ref program::remove_instruction(instruction_ref ins)
Paul's avatar
Paul committed
59
60
61
62
63
64
65
{
    assert(has_instruction(ins));
    assert(ins->output.empty());
    ins->clear_arguments();
    return impl->instructions.erase(ins);
}

66
67
instruction_ref program::remove_instructions(instruction_ref first, instruction_ref last)
{
Paul's avatar
Paul committed
68
69
    if(first == last)
        return first;
Paul's avatar
Paul committed
70
    // TODO: Check every element
71
    assert(has_instruction(first));
Paul's avatar
Paul committed
72
73
    std::for_each(first, last, [&](instruction& ins) { ins.clear_arguments(); });
    assert(std::all_of(first, last, [&](instruction& ins) { return ins.output.empty(); }));
74
75
76
77
78
79
80
81
82
    return impl->instructions.erase(first, last);
}

instruction_ref program::move_instruction(instruction_ref src, instruction_ref dst)
{
    impl->instructions.splice(dst, impl->instructions, src);
    return src;
}

Paul's avatar
Paul committed
83
instruction_ref program::add_literal(literal l)
Paul's avatar
Paul committed
84
{
Paul's avatar
Paul committed
85
86
87
88
89
90
91
92
    impl->instructions.emplace_front(std::move(l));
    return impl->instructions.begin();
}

instruction_ref program::add_outline(shape s)
{
    impl->instructions.push_front({builtin::outline{s}, s, {}});
    return impl->instructions.begin();
Paul's avatar
Paul committed
93
94
}

Paul's avatar
Paul committed
95
instruction_ref program::add_parameter(std::string name, shape s)
Paul's avatar
Paul committed
96
{
Paul's avatar
Paul committed
97
98
99
100
101
102
103
    impl->instructions.push_front({builtin::param{std::move(name)}, s, {}});
    return impl->instructions.begin();
}

shape program::get_parameter_shape(std::string name)
{
    auto ins = std::find_if(
Paul's avatar
Paul committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
            if(x.op.name() == "@param")
            {
                return any_cast<builtin::param>(x.op).parameter == name;
            }
            else
            {
                return false;
            }
        });
    if(ins != this->end())
        return ins->result;
    else
        return {};
Paul's avatar
Paul committed
118
119
}

Paul's avatar
Paul committed
120
121
122
std::unordered_map<std::string, shape> program::get_parameter_shapes() const
{
    std::unordered_map<std::string, shape> result;
Paul's avatar
Paul committed
123
    for(auto&& ins : impl->instructions)
Paul's avatar
Paul committed
124
125
126
    {
        if(ins.op.name() == "@param")
        {
Paul's avatar
Paul committed
127
            auto&& name  = any_cast<builtin::param>(ins.op).parameter;
Paul's avatar
Paul committed
128
129
130
131
132
133
            result[name] = ins.result;
        }
    }
    return result;
}

Paul's avatar
Paul committed
134
bool program::has_instruction(instruction_ref ins) const
Paul's avatar
Paul committed
135
{
Paul's avatar
Paul committed
136
137
138
139
    return std::find_if(
               impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
                   return std::addressof(*ins) == std::addressof(x);
               }) != impl->instructions.end();
Paul's avatar
Paul committed
140
141
}

Paul's avatar
Paul committed
142
143
instruction_ref program::begin() { return impl->instructions.begin(); }
instruction_ref program::end() { return impl->instructions.end(); }
144

Paul's avatar
Paul committed
145
shape program::get_shape() const { return impl->instructions.back().result; }
Paul's avatar
Paul committed
146

Paul's avatar
Paul committed
147
148
instruction_ref program::validate() const
{
Paul's avatar
Paul committed
149
150
    return std::find_if(impl->instructions.begin(),
                        impl->instructions.end(),
Paul's avatar
Paul committed
151
                        [&](const instruction& i) { return !i.valid(impl->instructions.begin()); });
Paul's avatar
Paul committed
152
153
}

Paul's avatar
Paul committed
154
155
void program::compile(const target& t)
{
Paul's avatar
Paul committed
156
    assert(this->validate() == impl->instructions.end());
Paul's avatar
Paul committed
157
    this->impl->ctx = t.get_context();
Paul's avatar
Paul committed
158
    for(auto&& p : t.get_passes(this->impl->ctx))
Paul's avatar
Paul committed
159
160
161
    {
        p.apply(*this);
#ifndef NDEBUG
Paul's avatar
Paul committed
162
        auto invalid = this->validate();
Paul's avatar
Paul committed
163
164
        if(invalid != impl->instructions.end())
        {
Paul's avatar
Paul committed
165
            auto index = std::distance(impl->instructions.begin(), invalid);
Paul's avatar
Paul committed
166
167
            MIGRAPH_THROW(p.name() + " pass produces invalid program at instruction " +
                          std::to_string(index));
Paul's avatar
Paul committed
168
        }
Paul's avatar
Paul committed
169
170
#endif
    }
Paul's avatar
Paul committed
171
    auto invalid = this->validate();
Paul's avatar
Paul committed
172
173
    if(invalid != impl->instructions.end())
    {
Paul's avatar
Paul committed
174
175
176
        auto index = std::distance(impl->instructions.begin(), invalid);
        MIGRAPH_THROW("Invalid program from compilation at instruction " + std::to_string(index));
    }
Paul's avatar
Paul committed
177
178
}

179
argument program::eval(std::unordered_map<std::string, argument> params) const
Paul's avatar
Paul committed
180
{
Paul's avatar
Paul committed
181
    assert(this->validate() == impl->instructions.end());
Paul's avatar
Paul committed
182
183
    std::unordered_map<const instruction*, argument> results;
    argument result;
Paul's avatar
Paul committed
184
    for(auto& ins : impl->instructions)
Paul's avatar
Paul committed
185
    {
Paul's avatar
Paul committed
186
        if(ins.op.name() == "@literal")
Paul's avatar
Paul committed
187
188
189
        {
            result = ins.lit.get_argument();
        }
Paul's avatar
Paul committed
190
        else if(ins.op.name() == "@param")
Paul's avatar
Paul committed
191
        {
Paul's avatar
Paul committed
192
            result = params.at(any_cast<builtin::param>(ins.op).parameter);
Paul's avatar
Paul committed
193
        }
Paul's avatar
Paul committed
194
195
196
197
        else if(ins.op.name() == "@outline")
        {
            result = argument{ins.result, nullptr};
        }
Paul's avatar
Paul committed
198
199
200
        else
        {
            std::vector<argument> values(ins.arguments.size());
Paul's avatar
Paul committed
201
202
203
            std::transform(ins.arguments.begin(),
                           ins.arguments.end(),
                           values.begin(),
Paul's avatar
Paul committed
204
                           [&](instruction_ref i) { return results.at(std::addressof(*i)); });
Paul's avatar
Paul committed
205
            result = ins.op.compute(this->impl->ctx, ins.result, values);
Paul's avatar
Paul committed
206
207
208
        }
        results.emplace(std::addressof(ins), result);
    }
209
    return result;
Paul's avatar
Paul committed
210
211
}

Paul's avatar
Paul committed
212
bool operator==(const program& x, const program& y) { return to_string(x) == to_string(y); }
Paul's avatar
Paul committed
213

Paul's avatar
Paul committed
214
std::ostream& operator<<(std::ostream& os, const program& p)
Paul's avatar
Paul committed
215
216
217
218
{
    std::unordered_map<const instruction*, std::string> names;
    int count = 0;

Paul's avatar
Paul committed
219
    for(auto& ins : p.impl->instructions)
Paul's avatar
Paul committed
220
221
    {
        std::string var_name = "@" + std::to_string(count);
Paul's avatar
Paul committed
222
        if(ins.op.name() == "@param")
Paul's avatar
Paul committed
223
        {
Paul's avatar
Paul committed
224
            var_name = any_cast<builtin::param>(ins.op).parameter;
Paul's avatar
Paul committed
225
226
        }

Paul's avatar
Paul committed
227
        os << var_name << " = ";
Paul's avatar
Paul committed
228

Paul's avatar
Paul committed
229
        os << ins.op;
Paul's avatar
Paul committed
230
231
232

        if(ins.op.name() == "@literal")
        {
Paul's avatar
Paul committed
233
            if(ins.lit.get_shape().elements() > 10)
Paul's avatar
Paul committed
234
                os << "{ ... }";
Paul's avatar
Paul committed
235
            else
Paul's avatar
Paul committed
236
                os << "{" << ins.lit << "}";
Paul's avatar
Paul committed
237
238
239
240
241
        }

        if(!ins.arguments.empty())
        {
            char delim = '(';
Paul's avatar
Paul committed
242
            for(auto&& arg : ins.arguments)
Paul's avatar
Paul committed
243
            {
Paul's avatar
Paul committed
244
245
                assert(p.has_instruction(arg) && "Instruction not found");
                os << delim << names.at(std::addressof(*arg));
Paul's avatar
Paul committed
246
247
                delim = ',';
            }
Paul's avatar
Paul committed
248
            os << ")";
Paul's avatar
Paul committed
249
250
        }

Paul's avatar
Paul committed
251
        os << " -> " << ins.result;
Paul's avatar
Paul committed
252

Paul's avatar
Paul committed
253
        os << std::endl;
Paul's avatar
Paul committed
254
255

        names.emplace(std::addressof(ins), var_name);
Paul's avatar
Paul committed
256
        count++;
Paul's avatar
Paul committed
257
    }
Paul's avatar
Paul committed
258
    return os;
Paul's avatar
Paul committed
259
260
}

Paul's avatar
Paul committed
261
} // namespace migraph