instruction.hpp 2.61 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_INSTRUCTION_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_INSTRUCTION_HPP

#include <migraph/literal.hpp>
#include <migraph/shape.hpp>
#include <migraph/instruction_ref.hpp>
Paul's avatar
Paul committed
7
#include <migraph/operation.hpp>
Paul's avatar
Paul committed
8
#include <migraph/erase.hpp>
Paul's avatar
Paul committed
9
#include <string>
Paul's avatar
Paul committed
10
#include <utility>
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
namespace migraph {
Paul's avatar
Paul committed
13

Paul's avatar
Paul committed
14
shape compute_shape(const operation& op, const std::vector<instruction_ref>& args);
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
17
struct instruction
{
Paul's avatar
Paul committed
18
19
    instruction() {}

Paul's avatar
Paul committed
20
    instruction(operation o, shape r, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
21

Paul's avatar
Paul committed
22
    instruction(literal l);
Paul's avatar
Paul committed
23

Paul's avatar
Paul committed
24
    void replace(const shape& r);
Paul's avatar
Paul committed
25

Paul's avatar
Paul committed
26
    void recompute_shape();
Paul's avatar
Paul committed
27

Paul's avatar
Paul committed
28
    void clear_arguments();
Paul's avatar
Paul committed
29

Paul's avatar
Paul committed
30
    friend bool operator==(const instruction& i, instruction_ref ref);
Paul's avatar
Paul committed
31

Paul's avatar
Paul committed
32
    bool valid(instruction_ref start) const;
Paul's avatar
Paul committed
33

Paul's avatar
Paul committed
34
    bool valid() const;
Paul's avatar
Paul committed
35

Paul's avatar
Paul committed
36
37
    shape get_shape() const;
    const literal& get_literal() const;
38

Paul's avatar
Paul committed
39
    const operation& get_operator() const;
Paul's avatar
Paul committed
40

Paul's avatar
Paul committed
41
    std::string name() const;
Paul's avatar
Paul committed
42

Paul's avatar
Paul committed
43
    const std::vector<instruction_ref>& inputs() const;
Paul's avatar
Paul committed
44

Paul's avatar
Paul committed
45
    const std::vector<instruction_ref>& outputs() const;
Paul's avatar
Paul committed
46

Paul's avatar
Paul committed
47
    friend bool operator==(const instruction& x, const instruction& y);
Paul's avatar
Paul committed
48

Paul's avatar
Paul committed
49
50
    friend bool operator!=(const instruction& x, const instruction& y);

Paul's avatar
Paul committed
51
    friend bool operator==(instruction_ref ref, const instruction& i);
Paul's avatar
Paul committed
52

Paul's avatar
Paul committed
53
    friend bool operator!=(const instruction& i, instruction_ref ref);
Paul's avatar
Paul committed
54

Paul's avatar
Paul committed
55
    friend bool operator!=(instruction_ref ref, const instruction& i);
Paul's avatar
Paul committed
56

Paul's avatar
Paul committed
57
    void add_output(instruction_ref ins);
Paul's avatar
Paul committed
58

Paul's avatar
Paul committed
59
    template <class T>
Paul's avatar
Paul committed
60
61
62
63
    void remove_output(const T& ins)
    {
        migraph::erase(output, ins);
    }
Paul's avatar
Paul committed
64

Paul's avatar
Paul committed
65
    static void backreference(instruction_ref ref);
Paul's avatar
Paul committed
66

Paul's avatar
Paul committed
67
    static void replace_argument(instruction_ref ins, instruction_ref old, instruction_ref new_ins);
Paul's avatar
Paul committed
68

Paul's avatar
Paul committed
69
    static void
Paul's avatar
Paul committed
70
    replace(instruction_ref ins, operation o, const shape& r, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
71

Paul's avatar
Paul committed
72
73
    static instruction_ref get_output_alias(instruction_ref ins);

Paul's avatar
Paul committed
74
    private:
Paul's avatar
Paul committed
75
    // internal
Paul's avatar
Paul committed
76
    void replace(operation o, const shape& r, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
77

Paul's avatar
Paul committed
78
    // internal
Paul's avatar
Paul committed
79
    void replace(std::vector<instruction_ref> args);
Paul's avatar
Paul committed
80

Paul's avatar
Paul committed
81
    // internal
Paul's avatar
Paul committed
82
    void replace_argument(instruction_ref old, instruction_ref new_ins);
Paul's avatar
Paul committed
83

Paul's avatar
Paul committed
84
    private:
Paul's avatar
Paul committed
85
    operation op;
Paul's avatar
Paul committed
86
    shape result;
Paul's avatar
Paul committed
87
    std::vector<instruction_ref> output;
Paul's avatar
Paul committed
88
    std::vector<instruction_ref> arguments;
Paul's avatar
Paul committed
89
    literal lit;
Paul's avatar
Paul committed
90
};
Paul's avatar
Paul committed
91
} // namespace migraph
Paul's avatar
Paul committed
92

Paul's avatar
Paul committed
93
94
95
namespace std {
template <>
struct hash<migraph::instruction_ref>
96
{
Paul's avatar
Paul committed
97
98
99
    using argument_type = migraph::instruction_ref;
    using result_type   = std::size_t;
    result_type operator()(const argument_type& x) const noexcept
100
    {
Paul's avatar
Paul committed
101
102
103
        return std::hash<migraph::instruction*>{}(&*x);
    }
};
104
105
} // namespace std

Paul's avatar
Paul committed
106
#endif