program.hpp 3.96 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_PROGRAM_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_PROGRAM_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <list>
Paul's avatar
Paul committed
5
#include <unordered_map>
Paul's avatar
Paul committed
6
7
8
9
10
11
#include <migraphx/operation.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/builtin.hpp>
#include <migraphx/instruction_ref.hpp>
#include <migraphx/target.hpp>
#include <migraphx/tracer.hpp>
Paul's avatar
Paul committed
12
#include <migraphx/env.hpp>
Paul's avatar
Paul committed
13
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
14
#include <algorithm>
Paul's avatar
Paul committed
15
#include <iostream>
Paul's avatar
Paul committed
16

Paul's avatar
Paul committed
17
namespace migraphx {
Paul's avatar
Paul committed
18
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
19

Paul's avatar
Paul committed
20
21
22
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_COMPILE)
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_EVAL)

Paul's avatar
Paul committed
23
24
struct program_impl;

Paul's avatar
Paul committed
25
26
const operation& get_operation(instruction_ref ins);

mei-ye's avatar
mei-ye committed
27
/**
Paul's avatar
Paul committed
28
29
 * @brief Stores the instruction stream
 */
Paul's avatar
Paul committed
30
31
struct program
{
Paul's avatar
Paul committed
32
    program();
33
34

    // move constructor
Paul's avatar
Paul committed
35
    program(program&&) noexcept;
36
37
38
39
40

    // copy constructor
    program(const program&) noexcept;

    // move assignment operator
Paul's avatar
Paul committed
41
    program& operator=(program&&) noexcept;
42
43
44
45

    // copy assignment operator
    program& operator=(const program&) noexcept;

Paul's avatar
Paul committed
46
    ~program() noexcept;
Paul's avatar
Paul committed
47

mei-ye's avatar
mei-ye committed
48
    using parameter_map = std::unordered_map<std::string, argument>;
Paul's avatar
Paul committed
49

Paul's avatar
Paul committed
50
    template <class... Ts>
Paul's avatar
Paul committed
51
    instruction_ref add_instruction(operation op, Ts... args)
Paul's avatar
Paul committed
52
    {
Paul's avatar
Paul committed
53
        return add_instruction(op, {args...});
Paul's avatar
Paul committed
54
    }
Paul's avatar
Paul committed
55
    instruction_ref add_instruction(const operation& op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
56

Paul's avatar
Paul committed
57
58
59
60
61
    template <class... Ts>
    instruction_ref insert_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return insert_instruction(ins, op, {args...});
    }
Paul's avatar
Paul committed
62
    instruction_ref
Paul's avatar
Paul committed
63
    insert_instruction(instruction_ref ins, const operation& op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
64

Paul's avatar
Paul committed
65
66
67
68
69
    template <class... Ts>
    instruction_ref replace_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return replace_instruction(ins, op, {args...});
    }
Paul's avatar
Paul committed
70
71
72
    instruction_ref replace_instruction(instruction_ref ins,
                                        const operation& op,
                                        std::vector<instruction_ref> args);
Paul's avatar
Paul committed
73

Paul's avatar
Paul committed
74
    instruction_ref replace_instruction(instruction_ref ins, instruction_ref rep);
Paul's avatar
Paul committed
75

Paul's avatar
Paul committed
76
    instruction_ref remove_instruction(instruction_ref ins);
77
    instruction_ref remove_instructions(instruction_ref first, instruction_ref last);
Paul's avatar
Paul committed
78

79
    instruction_ref move_instruction(instruction_ref src, instruction_ref dst);
Paul's avatar
Paul committed
80

Paul's avatar
Paul committed
81
    template <class... Ts>
Paul's avatar
Paul committed
82
    instruction_ref add_literal(Ts&&... xs)
Paul's avatar
Paul committed
83
    {
Paul's avatar
Paul committed
84
        return add_literal(literal{std::forward<Ts>(xs)...});
Paul's avatar
Paul committed
85
86
    }

Paul's avatar
Paul committed
87
    instruction_ref add_literal(literal l);
Paul's avatar
Paul committed
88

Paul's avatar
Paul committed
89
    instruction_ref add_outline(const shape& s);
Paul's avatar
Paul committed
90

Paul's avatar
Paul committed
91
    instruction_ref add_parameter(std::string name, shape s);
Paul's avatar
Paul committed
92

Paul's avatar
Paul committed
93
    shape get_parameter_shape(std::string name) const;
Paul's avatar
Paul committed
94

mei-ye's avatar
mei-ye committed
95
96
    instruction_ref get_parameter(std::string name) const;

Paul's avatar
Paul committed
97
98
    std::unordered_map<std::string, shape> get_parameter_shapes() const;

Paul's avatar
Paul committed
99
    argument eval(parameter_map params) const;
Paul's avatar
Paul committed
100

Paul's avatar
Paul committed
101
    bool has_instruction(instruction_ref ins) const;
Paul's avatar
Paul committed
102

Paul's avatar
Paul committed
103
    std::size_t size() const;
Paul's avatar
Paul committed
104
105
    instruction_ref begin() const;
    instruction_ref end() const;
106

Paul's avatar
Paul committed
107
108
    shape get_shape() const;

Paul's avatar
Paul committed
109
110
    context& get_context() const;

Paul's avatar
Paul committed
111
112
    instruction_ref validate() const;

mei-ye's avatar
mei-ye committed
113
    void compile(const target& t, tracer trace = tracer{});
Paul's avatar
Paul committed
114

Paul's avatar
Paul committed
115
    void finalize();
Paul's avatar
Paul committed
116

Paul's avatar
Paul committed
117
118
    void perf_report(std::ostream& os, std::size_t n, parameter_map params) const;

Paul's avatar
Paul committed
119
120
121
    void debug_print() const;
    void debug_print(instruction_ref ins) const;
    void debug_print(const std::vector<instruction_ref>& inss) const;
122
    void print_graph(std::ostream& os) const;
Paul's avatar
Paul committed
123

Paul's avatar
Paul committed
124
125
    void dry_run(parameter_map params) const;

Paul's avatar
Paul committed
126
127
    void annotate(std::ostream& os, std::function<void(instruction_ref)> a) const;

Paul's avatar
Paul committed
128
129
    friend std::ostream& operator<<(std::ostream& os, const program& p);
    friend bool operator==(const program& x, const program& y);
Paul's avatar
Paul committed
130
    friend bool operator!=(const program& x, const program& y) { return !(x == y); }
Paul's avatar
Paul committed
131

132
133
134
    private:
    void copy(const program& prog);

Paul's avatar
Paul committed
135
    private:
Paul's avatar
Paul committed
136
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
137
};
Paul's avatar
Paul committed
138

Paul's avatar
Paul committed
139
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
140
} // namespace migraphx
Paul's avatar
Paul committed
141
142

#endif