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
namespace migraph { inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

107
108
} // namespace std

Paul's avatar
Paul committed
109
#endif