target.hpp 5.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_TARGET_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_TARGET_HPP
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
#include <cassert>
Paul's avatar
Paul committed
28
29
30
31
32
#include <string>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
Paul's avatar
Paul committed
33
#include <vector>
Paul's avatar
Paul committed
34
35
#include <migraphx/context.hpp>
#include <migraphx/pass.hpp>
Paul's avatar
Paul committed
36
#include <migraphx/config.hpp>
37
#include <migraphx/compile_options.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
38
39
#include <migraphx/argument.hpp>
#include <migraphx/rank.hpp>
varunsh's avatar
varunsh committed
40
#include <migraphx/module_ref.hpp>
41
42
#include <migraphx/support_metric.hpp>
#include <migraphx/instruction_ref.hpp>
varunsh's avatar
varunsh committed
43
#include <migraphx/supported_segments.hpp>
Paul's avatar
Paul committed
44

Paul's avatar
Paul committed
45
namespace migraphx {
Paul's avatar
Paul committed
46
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
47

48
49
struct value;

Paul's avatar
Paul committed
50
#ifdef DOXYGEN
Paul's avatar
Paul committed
51

Paul's avatar
Paul committed
52
/// An interface for a compilation target
Paul's avatar
Paul committed
53
54
55
56
57
58
struct target
{
    /// A unique name used to identify the target
    std::string name() const;
    /**
     * @brief The transformation pass to be run during compilation.
Paul's avatar
Paul committed
59
     *
Paul's avatar
Paul committed
60
     * @param ctx This is the target-dependent context that is created by `get_context`
61
     * @param options Compiling options passed in by the user
Paul's avatar
Paul committed
62
63
     * @return The passes to be ran
     */
64
    std::vector<pass> get_passes(context& ctx, const compile_options& options) const;
Paul's avatar
Paul committed
65
66
67
68
69
    /**
     * @brief Construct a context for the target.
     * @return The context to be used during compilation and execution.
     */
    context get_context() const;
70
    /**
varunsh's avatar
varunsh committed
71
72
73
74
     * @brief Get the ranges of instructions that are supported on a target
     * @param module Module to check for supported instructions
     * @param metric Used to define how the quality of the support should be measured
     * @return the supported segments of the graph
75
     */
varunsh's avatar
varunsh committed
76
    supported_segments target_is_supported(T&, const_module_ref mod, support_metric metric) const;
Shucai Xiao's avatar
Shucai Xiao committed
77
78
    /**
     * @brief copy an argument to the current target.
Shucai Xiao's avatar
Shucai Xiao committed
79
     *
Shucai Xiao's avatar
Shucai Xiao committed
80
81
82
83
84
85
     * @param arg Input argument to be copied to the target
     * @return Argument in the target.
     */
    argument copy_to(const argument& arg) const;
    /**
     * @brief copy an argument from the current target.
Shucai Xiao's avatar
Shucai Xiao committed
86
     *
Shucai Xiao's avatar
Shucai Xiao committed
87
88
89
90
91
92
     * @param arg Input argument to be copied from the target
     * @return Argument in the host.
     */
    argument copy_from(const argument& arg) const;
    /**
     * @brief Allocate an argument based on the input shape
Shucai Xiao's avatar
Shucai Xiao committed
93
     *
Shucai Xiao's avatar
Shucai Xiao committed
94
95
96
97
     * @param s Shape of the argument to be allocated in the target
     * @return Allocated argument in the target.
     */
    argument allocate(const shape& s) const;
Paul's avatar
Paul committed
98
99
100
};

#else
Paul's avatar
Paul committed
101

Shucai Xiao's avatar
Shucai Xiao committed
102
template <class T>
Paul's avatar
Paul committed
103
argument target_allocate(T& x, const shape&)
Shucai Xiao's avatar
Shucai Xiao committed
104
105
106
107
108
109
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

template <class T>
Paul's avatar
Paul committed
110
argument copy_to_target(T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
111
{
Shucai Xiao's avatar
Shucai Xiao committed
112
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
113
114
115
}

template <class T>
Paul's avatar
Paul committed
116
argument copy_from_target(T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
117
{
Shucai Xiao's avatar
Shucai Xiao committed
118
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
119
120
}

121
template <class T>
varunsh's avatar
varunsh committed
122
supported_segments target_find_supported(T&, const_module_ref, support_metric)
123
{
varunsh's avatar
varunsh committed
124
    return {};
125
126
}

Paul's avatar
Paul committed
127
<%
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 interface('target',
           virtual('name', returns = 'std::string', const = True),
           virtual('get_passes',
                   ctx     = 'context&',
                   options = 'const compile_options&',
                   returns = 'std::vector<pass>',
                   const   = True),
           virtual('get_context', returns = 'context', const = True),
           virtual('find_supported',
                   returns = 'supported_segments',
                   mod     = 'const_module_ref',
                   m       = 'support_metric',
                   const   = True,
                   default = 'target_find_supported'),
           virtual('copy_to',
                   returns = 'argument',
                   input   = 'const argument&',
                   const   = True,
                   default = 'copy_to_target'),
           virtual('copy_from',
                   returns = 'argument',
                   input   = 'const argument&',
                   const   = True,
                   default = 'copy_from_target'),
           virtual('allocate',
                   s       = 'const shape&',
                   returns = 'argument',
                   const   = True,
                   default = 'target_allocate')) %>
Paul's avatar
Paul committed
157

Paul's avatar
Paul committed
158
159
#endif

160
161
162
void migraphx_to_value(value& v, const target& t);
void migraphx_from_value(const value& v, target& t);

Paul's avatar
Paul committed
163
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
164
} // namespace migraphx
Paul's avatar
Paul committed
165
166

#endif