compile_hip.cpp 2.56 KB
Newer Older
Paul Fultz II's avatar
Paul Fultz II committed
1
2
#include <migraphx/gpu/compile_hip.hpp>
#include <migraphx/errors.hpp>
3
4
5
#include <migraphx/stringutils.hpp>
#include <migraphx/compile_src.hpp>
#include <migraphx/process.hpp>
6
#include <migraphx/env.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cassert>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

bool is_hcc_compiler()
{
    static const auto result = ends_with(MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER), "hcc");
    return result;
}

bool is_hip_clang_compiler()
{
    static const auto result = ends_with(MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER), "clang++");
    return result;
}

25
26
27
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_DEBUG);
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_OPTIMIZE);

Paul Fultz II's avatar
Paul Fultz II committed
28
29
30
std::vector<std::vector<char>>
compile_hip_src(const std::vector<src_file>& srcs, std::string params, const std::string& arch)
{
31
    assert(not srcs.empty());
Paul Fultz II's avatar
Paul Fultz II committed
32
33
34
    if(not is_hcc_compiler() and not is_hip_clang_compiler())
        MIGRAPHX_THROW("Unknown hip compiler: " +
                       std::string(MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER)));
35

Paul Fultz II's avatar
Paul Fultz II committed
36
37
38
39
40
41
42
43
44
45
46
47
    if(params.find("-std=") == std::string::npos)
        params += " --std=c++17";
    params += " -fno-gpu-rdc";
    params += " -c";
    if(is_hcc_compiler())
    {
        params += " -amdgpu-target=" + arch;
    }
    else if(is_hip_clang_compiler())
    {
        params += " --cuda-gpu-arch=" + arch;
        params += " --cuda-device-only";
48
        params += " -O" + string_value_of(MIGRAPHX_GPU_OPTIMIZE{}, "3") + " ";
Paul Fultz II's avatar
Paul Fultz II committed
49
50
    }

51
52
53
    if(enabled(MIGRAPHX_GPU_DEBUG{}))
        params += " -DMIGRAPHX_DEBUG";

54
    params += " -Wno-unused-command-line-argument -Wno-cuda-compat ";
Paul Fultz II's avatar
Paul Fultz II committed
55
56
    params += MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER_FLAGS);

57
58
59
    src_compiler compiler;
    compiler.flags    = params;
    compiler.compiler = MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER);
Paul Fultz II's avatar
Paul Fultz II committed
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    if(is_hcc_compiler())
        compiler.process = [&](const fs::path& obj_path) -> fs::path {
            process{MIGRAPHX_STRINGIZE(MIGRAPHX_EXTRACT_KERNEL) + std::string{" -i "} +
                    obj_path.string()}
                .cwd(obj_path.parent_path());
            for(const auto& entry : fs::directory_iterator{obj_path.parent_path()})
            {
                const auto& hsaco_path = entry.path();
                if(not fs::is_regular_file(hsaco_path))
                    continue;
                if(hsaco_path.extension() != ".hsaco")
                    continue;
                return hsaco_path;
            }
            MIGRAPHX_THROW("Missing hsaco");
        };
Paul Fultz II's avatar
Paul Fultz II committed
77

78
    return {compiler.compile(srcs)};
Paul Fultz II's avatar
Paul Fultz II committed
79
80
81
82
83
}

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx