instruction.hpp 2.71 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>
9
#include <migraph/config.hpp>
Paul's avatar
Paul committed
10
#include <string>
Paul's avatar
Paul committed
11
#include <utility>
Paul's avatar
Paul committed
12

13
14
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
15

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

Paul's avatar
Paul committed
18
19
struct instruction
{
Paul's avatar
Paul committed
20
21
    instruction() {}

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

Paul's avatar
Paul committed
24
    instruction(literal l);
Paul's avatar
Paul committed
25

Paul's avatar
Paul committed
26
    void replace(const shape& r);
Paul's avatar
Paul committed
27

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

Paul's avatar
Paul committed
30
    void clear_arguments();
Paul's avatar
Paul committed
31

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

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

Paul's avatar
Paul committed
36
    bool valid() const;
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
39
    shape get_shape() const;
    const literal& get_literal() const;
40

Paul's avatar
Paul committed
41
    const operation& get_operator() const;
Paul's avatar
Paul committed
42

Paul's avatar
Paul committed
43
    std::string name() const;
Paul's avatar
Paul committed
44

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

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

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

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

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

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

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

Paul's avatar
Paul committed
59
    void add_output(instruction_ref ins);
Paul's avatar
Paul committed
60

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

Paul's avatar
Paul committed
67
    static void backreference(instruction_ref ref);
Paul's avatar
Paul committed
68

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

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

Paul's avatar
Paul committed
74
75
    static instruction_ref get_output_alias(instruction_ref ins);

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

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

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

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

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

108
109
} // namespace std

Paul's avatar
Paul committed
110
#endif