target.hpp 4.8 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>
40
41
#include <migraphx/support_metric.hpp>
#include <migraphx/instruction_ref.hpp>
Paul's avatar
Paul committed
42

Paul's avatar
Paul committed
43
namespace migraphx {
Paul's avatar
Paul committed
44
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
45

Paul's avatar
Paul committed
46
#ifdef DOXYGEN
Paul's avatar
Paul committed
47

Paul's avatar
Paul committed
48
/// An interface for a compilation target
Paul's avatar
Paul committed
49
50
51
52
53
54
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
55
     *
Paul's avatar
Paul committed
56
     * @param ctx This is the target-dependent context that is created by `get_context`
57
     * @param options Compiling options passed in by the user
Paul's avatar
Paul committed
58
59
     * @return The passes to be ran
     */
60
    std::vector<pass> get_passes(context& ctx, const compile_options& options) const;
Paul's avatar
Paul committed
61
62
63
64
65
    /**
     * @brief Construct a context for the target.
     * @return The context to be used during compilation and execution.
     */
    context get_context() const;
66
67
68
69
70
71
72
    /**
     * @brief Check how well an instruction is supported on a target with the given metric
     * @param ins Instruction to check if it's supported
     * @param metric Used to define how the return value should be interpreted
     * @return The value based on the chosen metric. Negative numbers mean unsupported
     */
    float is_supported(T&, instruction_ref ins, support_metric m) const;
Shucai Xiao's avatar
Shucai Xiao committed
73
74
    /**
     * @brief copy an argument to the current target.
Shucai Xiao's avatar
Shucai Xiao committed
75
     *
Shucai Xiao's avatar
Shucai Xiao committed
76
77
78
79
80
81
     * @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
82
     *
Shucai Xiao's avatar
Shucai Xiao committed
83
84
85
86
87
88
     * @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
89
     *
Shucai Xiao's avatar
Shucai Xiao committed
90
91
92
93
     * @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
94
95
96
};

#else
Paul's avatar
Paul committed
97

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

template <class T>
Paul's avatar
Paul committed
106
argument copy_to_target(T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
107
{
Shucai Xiao's avatar
Shucai Xiao committed
108
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
109
110
111
}

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

117
118
119
120
121
122
template <class T>
float target_is_supported(T&, instruction_ref, support_metric)
{
    return 0;
}

Paul's avatar
Paul committed
123
124
<%
interface('target',
Shucai Xiao's avatar
Shucai Xiao committed
125
     virtual('name', returns='std::string', const=True),
126
     virtual('get_passes', ctx='context&', options='const compile_options&', returns='std::vector<pass>', const=True),
Shucai Xiao's avatar
Shucai Xiao committed
127
     virtual('get_context', returns='context', const=True),
128
     virtual('is_supported', returns='float', ins='instruction_ref', m='support_metric', const=True, default='target_is_supported'),
Shucai Xiao's avatar
Shucai Xiao committed
129
130
131
132
133
134
135
136
137
138
139
140
     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
141
142
143
)
%>

Paul's avatar
Paul committed
144
145
#endif

Paul's avatar
Paul committed
146
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
147
} // namespace migraphx
Paul's avatar
Paul committed
148
149

#endif