target.hpp 4.9 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

Paul's avatar
Paul committed
48
#ifdef DOXYGEN
Paul's avatar
Paul committed
49

Paul's avatar
Paul committed
50
/// An interface for a compilation target
Paul's avatar
Paul committed
51
52
53
54
55
56
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
57
     *
Paul's avatar
Paul committed
58
     * @param ctx This is the target-dependent context that is created by `get_context`
59
     * @param options Compiling options passed in by the user
Paul's avatar
Paul committed
60
61
     * @return The passes to be ran
     */
62
    std::vector<pass> get_passes(context& ctx, const compile_options& options) const;
Paul's avatar
Paul committed
63
64
65
66
67
    /**
     * @brief Construct a context for the target.
     * @return The context to be used during compilation and execution.
     */
    context get_context() const;
68
    /**
varunsh's avatar
varunsh committed
69
70
71
72
     * @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
73
     */
varunsh's avatar
varunsh committed
74
    supported_segments target_is_supported(T&, const_module_ref mod, support_metric metric) const;
Shucai Xiao's avatar
Shucai Xiao committed
75
76
    /**
     * @brief copy an argument to the current target.
Shucai Xiao's avatar
Shucai Xiao committed
77
     *
Shucai Xiao's avatar
Shucai Xiao committed
78
79
80
81
82
83
     * @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
84
     *
Shucai Xiao's avatar
Shucai Xiao committed
85
86
87
88
89
90
     * @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
91
     *
Shucai Xiao's avatar
Shucai Xiao committed
92
93
94
95
     * @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
96
97
98
};

#else
Paul's avatar
Paul committed
99

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

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

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

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

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

Paul's avatar
Paul committed
146
147
#endif

Paul's avatar
Paul committed
148
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
149
} // namespace migraphx
Paul's avatar
Paul committed
150
151

#endif