"docs/source/Tuner/MetisTuner.rst" did not exist on "b7366b685afdde156e551f8ba5008857f789e368"
write_literals.cpp 2.33 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
26
27
#include <migraphx/gpu/write_literals.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/instruction.hpp>
28
#include <migraphx/program.hpp>
Paul's avatar
Paul committed
29
#include <migraphx/env.hpp>
Paul's avatar
Paul committed
30

Paul's avatar
Paul committed
31
namespace migraphx {
Paul's avatar
Paul committed
32
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
33
namespace gpu {
Paul's avatar
Paul committed
34

Paul's avatar
Paul committed
35
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_COPY_LITERALS)
36

37
void write_literals::apply(module& m) const
Paul's avatar
Paul committed
38
{
Paul's avatar
Paul committed
39
    assert(ctx != nullptr);
40
    std::size_t n = 0;
41
    for(auto ins : iterator_for(m))
Paul's avatar
Paul committed
42
    {
Paul's avatar
Paul committed
43
        if(ins->name() == "@literal")
Paul's avatar
Paul committed
44
        {
Paul's avatar
Paul committed
45
            if(enabled(MIGRAPHX_COPY_LITERALS{}))
46
            {
Paul's avatar
Paul committed
47
                literal l  = ins->get_literal();
48
49
50
                auto pre   = m.add_literal(l);
                auto alloc = m.insert_instruction(std::next(pre), hip_allocate{l.get_shape()});
                m.replace_instruction(ins, hip_copy_to_gpu{}, pre, alloc);
Paul's avatar
Paul committed
51
            }
52
53
            else
            {
54
55
                std::string id = m.name() + ":@literal:" + std::to_string(n);
                m.replace_instruction(ins, hip_copy_literal{ins->get_literal(), id});
56
                n++;
57
            }
Paul's avatar
Paul committed
58
59
60
        }
    }
}
61

Paul's avatar
Paul committed
62
} // namespace gpu
Paul's avatar
Paul committed
63
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
64
} // namespace migraphx