write_literals.cpp 1.26 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#include <migraphx/gpu/write_literals.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/env.hpp>
Paul's avatar
Paul committed
6

Paul's avatar
Paul committed
7
namespace migraphx {
Paul's avatar
Paul committed
8
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
9
namespace gpu {
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_COPY_LITERALS)
12

Paul's avatar
Paul committed
13
void write_literals::apply(program& p) const
Paul's avatar
Paul committed
14
{
Paul's avatar
Paul committed
15
    assert(ctx != nullptr);
16
    std::size_t n = 0;
Paul's avatar
Paul committed
17
18
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
19
        if(ins->name() == "@literal")
Paul's avatar
Paul committed
20
        {
Paul's avatar
Paul committed
21
            if(enabled(MIGRAPHX_COPY_LITERALS{}))
22
            {
Paul's avatar
Paul committed
23
24
                literal l  = ins->get_literal();
                auto pre   = p.add_literal(l);
25
                auto alloc = p.insert_instruction(std::next(pre), hip_allocate{l.get_shape()});
26
                p.replace_instruction(ins, hip_copy_to_gpu{}, pre, alloc);
Paul's avatar
Paul committed
27
            }
28
29
            else
            {
30
31
32
33
34
                std::string id = "@literal:" + std::to_string(n);
                argument a     = to_gpu(ins->get_literal().get_argument());
                ctx->get_current_device().preallocations[id] = a;
                p.replace_instruction(ins, hip_load_memory{a.get_shape(), id});
                n++;
35
            }
Paul's avatar
Paul committed
36
37
38
        }
    }
}
39

Paul's avatar
Paul committed
40
} // namespace gpu
Paul's avatar
Paul committed
41
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
42
} // namespace migraphx