program.hpp 2.43 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_PROGRAM_HPP
#define MIGRAPH_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
#include <migraph/operation.hpp>
#include <migraph/literal.hpp>
#include <migraph/builtin.hpp>
#include <migraph/instruction_ref.hpp>
#include <migraph/target.hpp>
Paul's avatar
Paul committed
11
#include <algorithm>
Paul's avatar
Paul committed
12
#include <iostream>
Paul's avatar
Paul committed
13

Paul's avatar
Paul committed
14
namespace migraph {
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
17
struct program_impl;

Paul's avatar
Paul committed
18
19
const operation& get_operation(instruction_ref ins);

Paul's avatar
Paul committed
20
21
22
/**
 * @brief Stores the instruction stream
 */
Paul's avatar
Paul committed
23
24
struct program
{
Paul's avatar
Paul committed
25
26
    program();
    program(program&&) noexcept;
Paul's avatar
Paul committed
27
28
    program& operator=(program&&) noexcept;
    ~program() noexcept;
Paul's avatar
Paul committed
29

Paul's avatar
Paul committed
30
31
    using parameter_map = std::unordered_map<std::string, argument>;

Paul's avatar
Paul committed
32
    template <class... Ts>
Paul's avatar
Paul committed
33
    instruction_ref add_instruction(operation op, Ts... args)
Paul's avatar
Paul committed
34
    {
Paul's avatar
Paul committed
35
        return add_instruction(op, {args...});
Paul's avatar
Paul committed
36
    }
Paul's avatar
Paul committed
37
    instruction_ref add_instruction(operation op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
38

Paul's avatar
Paul committed
39
40
41
42
43
    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
44
45
    instruction_ref
    insert_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
46

Paul's avatar
Paul committed
47
48
49
50
51
52
53
54
    template <class... Ts>
    instruction_ref replace_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return replace_instruction(ins, op, {args...});
    }
    instruction_ref
    replace_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args);

Paul's avatar
Paul committed
55
    instruction_ref remove_instruction(instruction_ref ins);
56
57
58
    instruction_ref remove_instructions(instruction_ref first, instruction_ref last);
    
    instruction_ref move_instruction(instruction_ref src, instruction_ref dst);
Paul's avatar
Paul committed
59

Paul's avatar
Paul committed
60
    template <class... Ts>
Paul's avatar
Paul committed
61
    instruction_ref add_literal(Ts&&... xs)
Paul's avatar
Paul committed
62
    {
Paul's avatar
Paul committed
63
        return add_literal(literal{std::forward<Ts>(xs)...});
Paul's avatar
Paul committed
64
65
    }

Paul's avatar
Paul committed
66
    instruction_ref add_literal(literal l);
Paul's avatar
Paul committed
67

Paul's avatar
Paul committed
68
    instruction_ref add_outline(shape s);
Paul's avatar
Paul committed
69

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

Paul's avatar
Paul committed
72
73
    shape get_parameter_shape(std::string name);

Paul's avatar
Paul committed
74
    argument eval(parameter_map params) const;
Paul's avatar
Paul committed
75

Paul's avatar
Paul committed
76
    friend std::ostream& operator<<(std::ostream& os, const program& p);
Paul's avatar
Paul committed
77

Paul's avatar
Paul committed
78
    bool has_instruction(instruction_ref ins) const;
Paul's avatar
Paul committed
79

80
81
82
    instruction_ref begin();
    instruction_ref end();

Paul's avatar
Paul committed
83
84
    instruction_ref validate() const;

Paul's avatar
Paul committed
85
86
    void compile(const target& t);

Paul's avatar
Paul committed
87
    private:
Paul's avatar
Paul committed
88
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
89
90
};

Paul's avatar
Paul committed
91
} // namespace migraph
Paul's avatar
Paul committed
92
93

#endif