compile_hip.cpp 12.3 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 Fultz II's avatar
Paul Fultz II committed
24
25
#include <migraphx/gpu/compile_hip.hpp>
#include <migraphx/errors.hpp>
26
#include <migraphx/stringutils.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
27
#include <migraphx/ranges.hpp>
28
#include <migraphx/env.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
29
#include <cassert>
30
31
#include <iostream>

Umang Yadav's avatar
Umang Yadav committed
32
#ifdef MIGRAPHX_USE_HIPRTC
33
34
#include <hip/hiprtc.h>
#include <migraphx/manage_ptr.hpp>
35
36
37
38
39
40
41
#include <migraphx/value.hpp>
#include <migraphx/tmp_dir.hpp>
#include <migraphx/dynamic_loader.hpp>
#include <migraphx/process.hpp>
#include <migraphx/msgpack.hpp>
#include <migraphx/serialize.hpp>
#include <migraphx/file_buffer.hpp>
42
43
44
45
#else
#include <migraphx/compile_src.hpp>
#include <migraphx/process.hpp>
#endif
Paul Fultz II's avatar
Paul Fultz II committed
46
47
48
49
50

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

51
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_DEBUG);
52
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_DEBUG_SYM);
53
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_OPTIMIZE);
54
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_DUMP_ASM);
55
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_DUMP_SRC);
56

Umang Yadav's avatar
Umang Yadav committed
57
#ifdef MIGRAPHX_USE_HIPRTC
58
59
60
61
62
63
64
65
66
67
68
69

std::string hiprtc_error(hiprtcResult err, const std::string& msg)
{
    return "hiprtc: " + (hiprtcGetErrorString(err) + (": " + msg));
}

void hiprtc_check_error(hiprtcResult err, const std::string& msg, const std::string& ctx)
{
    if(err != HIPRTC_SUCCESS)
        throw make_exception(ctx, hiprtc_error(err, msg));
}

70
// NOLINTNEXTLINE
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#define MIGRAPHX_HIPRTC(...) \
    hiprtc_check_error(__VA_ARGS__, #__VA_ARGS__, MIGRAPHX_MAKE_SOURCE_CTX())

#define MIGRAPHX_HIPRTC_THROW(error, msg) MIGRAPHX_THROW(hiprtc_error(error, msg))

// Workaround hiprtc's broken API
void hiprtc_program_destroy(hiprtcProgram prog) { hiprtcDestroyProgram(&prog); }
using hiprtc_program_ptr = MIGRAPHX_MANAGE_PTR(hiprtcProgram, hiprtc_program_destroy);

template <class... Ts>
hiprtc_program_ptr hiprtc_program_create(Ts... xs)
{
    hiprtcProgram prog = nullptr;
    auto result        = hiprtcCreateProgram(&prog, xs...);
    hiprtc_program_ptr p{prog};
    if(result != HIPRTC_SUCCESS)
        MIGRAPHX_HIPRTC_THROW(result, "Create program failed.");
    return p;
}

struct hiprtc_program
{
    struct string_array
    {
        std::vector<std::string> strings{};
        std::vector<const char*> c_strs{};

        string_array() {}
        string_array(const string_array&) = delete;

        std::size_t size() const { return strings.size(); }

        const char** data() { return c_strs.data(); }

        void push_back(std::string s)
        {
            strings.push_back(std::move(s));
            c_strs.push_back(strings.back().c_str());
        }
    };

    hiprtc_program_ptr prog = nullptr;
    string_array headers{};
    string_array include_names{};
    std::string cpp_src  = "";
    std::string cpp_name = "";

118
119
120
121
122
123
    hiprtc_program(const std::string& src, const std::string& name = "main.cpp")
        : cpp_src(src), cpp_name(name)
    {
        create_program();
    }

124
    hiprtc_program(std::vector<hiprtc_src_file> srcs)
125
126
127
    {
        for(auto&& src : srcs)
        {
128
            if(ends_with(src.path, ".cpp"))
129
            {
130
131
                cpp_src  = std::move(src.content);
                cpp_name = std::move(src.path);
132
133
134
            }
            else
            {
135
136
                headers.push_back(std::move(src.content));
                include_names.push_back(std::move(src.path));
137
138
            }
        }
139
140
141
142
143
144
145
146
        create_program();
    }

    void create_program()
    {
        assert(not cpp_src.empty());
        assert(not cpp_name.empty());
        assert(headers.size() == include_names.size());
147
148
149
150
151
152
153
        prog = hiprtc_program_create(cpp_src.c_str(),
                                     cpp_name.c_str(),
                                     headers.size(),
                                     headers.data(),
                                     include_names.data());
    }

154
    void compile(const std::vector<std::string>& options, bool quiet = false) const
155
156
157
158
159
160
161
162
    {
        if(enabled(MIGRAPHX_TRACE_HIPRTC{}))
            std::cout << "hiprtc " << join_strings(options, " ") << " " << cpp_name << std::endl;
        std::vector<const char*> c_options;
        std::transform(options.begin(),
                       options.end(),
                       std::back_inserter(c_options),
                       [](const std::string& s) { return s.c_str(); });
Umang Yadav's avatar
Umang Yadav committed
163
164
        auto result   = hiprtcCompileProgram(prog.get(), c_options.size(), c_options.data());
        auto prog_log = log();
165
        if(not prog_log.empty() and not quiet)
Umang Yadav's avatar
Umang Yadav committed
166
167
168
        {
            std::cerr << prog_log << std::endl;
        }
169
170
171
172
        if(result != HIPRTC_SUCCESS)
            MIGRAPHX_HIPRTC_THROW(result, "Compilation failed.");
    }

Umang Yadav's avatar
Umang Yadav committed
173
    std::string log() const
174
175
176
    {
        std::size_t n = 0;
        MIGRAPHX_HIPRTC(hiprtcGetProgramLogSize(prog.get(), &n));
Umang Yadav's avatar
Umang Yadav committed
177
        if(n == 0)
178
            return {};
Umang Yadav's avatar
Umang Yadav committed
179
        std::string buffer(n, '\0');
180
        MIGRAPHX_HIPRTC(hiprtcGetProgramLog(prog.get(), buffer.data()));
Umang Yadav's avatar
Umang Yadav committed
181
182
        assert(buffer.back() != 0);
        return buffer;
183
184
    }

Umang Yadav's avatar
Umang Yadav committed
185
    std::vector<char> get_code_obj() const
186
187
188
189
190
191
192
193
194
    {
        std::size_t n = 0;
        MIGRAPHX_HIPRTC(hiprtcGetCodeSize(prog.get(), &n));
        std::vector<char> buffer(n);
        MIGRAPHX_HIPRTC(hiprtcGetCode(prog.get(), buffer.data()));
        return buffer;
    }
};

195
196
197
std::vector<std::vector<char>> compile_hip_src_with_hiprtc(std::vector<hiprtc_src_file> srcs,
                                                           std::string params,
                                                           const std::string& arch)
198
{
199
    hiprtc_program prog(std::move(srcs));
200
    auto options = split_string(params, ' ');
Umang Yadav's avatar
Umang Yadav committed
201
202
203
204
205
206
207
    options.push_back("-DMIGRAPHX_USE_HIPRTC=1");
    // remove following three compilation flags for HIPRTC once fixes from hipRTC are available in
    if(enabled(MIGRAPHX_ENABLE_HIPRTC_WORKAROUNDS{}))
    {
        options.push_back("-DMIGRAPHX_HAS_DPP=0");
        options.push_back("-DMIGRAPHX_ENABLE_HIPRTC_WORKAROUNDS=1");
        options.push_back("-Wno-reserved-identifier");
208
        options.push_back("-Wno-unused-parameter");
Umang Yadav's avatar
Umang Yadav committed
209
210
211
212
        options.push_back("-Wno-gnu-line-marker");
        options.push_back("-Wno-old-style-cast");
    }

213
214
215
216
217
218
219
    if(enabled(MIGRAPHX_GPU_DEBUG{}))
        options.push_back("-DMIGRAPHX_DEBUG");
    if(std::none_of(options.begin(), options.end(), [](const std::string& s) {
           return starts_with(s, "--std=") or starts_with(s, "-std=");
       }))
        options.push_back("-std=c++17");
    options.push_back("-fno-gpu-rdc");
Umang Yadav's avatar
Umang Yadav committed
220
    options.push_back("-O" + string_value_of(MIGRAPHX_GPU_OPTIMIZE{}, "3"));
221
    options.push_back("-Wno-cuda-compat");
Umang Yadav's avatar
Umang Yadav committed
222
    options.push_back("--offload-arch=" + arch);
223
224
225
226
    prog.compile(options);
    return {prog.get_code_obj()};
}

227
228
229
230
231
232
233
234
235
236
237
238
239
240
bool hip_has_flags(const std::vector<std::string>& flags)
{
    hiprtc_program prog{" "};
    try
    {
        prog.compile(flags, true);
        return true;
    }
    catch(...)
    {
        return false;
    }
}

241
242
243
244
std::vector<std::vector<char>>
compile_hip_src(const std::vector<src_file>& srcs, std::string params, const std::string& arch)
{
    std::vector<hiprtc_src_file> hsrcs{srcs.begin(), srcs.end()};
245
246
247
248
249
250
    if(enabled(MIGRAPHX_GPU_DUMP_SRC{}))
    {
        for(const auto& src : srcs)
        {
            if(src.path.extension() != ".cpp")
                continue;
251
            std::cout << std::string(src.content) << std::endl;
252
253
        }
    }
Artur Wojcik's avatar
Artur Wojcik committed
254
255
256
#ifdef _WIN32
    fs::path driver{"migraphx-hiprtc-driver.exe"};
#else
257
258
    auto p      = dynamic_loader::path(&compile_hip_src_with_hiprtc);
    auto driver = p.parent_path().parent_path() / "bin" / "migraphx-hiprtc-driver";
Artur Wojcik's avatar
Artur Wojcik committed
259
#endif
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

    if(fs::exists(driver))
    {
        value v;
        v["srcs"]   = to_value(hsrcs);
        v["params"] = to_value(params);
        v["arch"]   = to_value(arch);

        tmp_dir td{};
        auto out = td.path / "output";

        process(driver.string() + " " + out.string()).write([&](auto writer) {
            to_msgpack(v, writer);
        });
        if(fs::exists(out))
            return {read_buffer(out.string())};
    }
    return compile_hip_src_with_hiprtc(std::move(hsrcs), std::move(params), arch);
}

280
281
#else // MIGRAPHX_USE_HIPRTC

282
283
284
285
286
287
288
std::vector<std::vector<char>> compile_hip_src_with_hiprtc(std::vector<hiprtc_src_file>, // NOLINT
                                                           std::string,                  // NOLINT
                                                           const std::string&)
{
    MIGRAPHX_THROW("Not using hiprtc");
}

Paul Fultz II's avatar
Paul Fultz II committed
289
290
bool is_hip_clang_compiler()
{
Artur Wojcik's avatar
Artur Wojcik committed
291
292
    static const auto result =
        fs::path{MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER)}.stem() == "clang++";
Paul Fultz II's avatar
Paul Fultz II committed
293
294
295
    return result;
}

Artur Wojcik's avatar
Artur Wojcik committed
296
297
#ifdef MIGRAPHX_HIP_COMPILER_LAUNCHER

298
299
300
301
302
303
bool has_compiler_launcher()
{
    static const auto result = fs::exists(MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER_LAUNCHER));
    return result;
}

Artur Wojcik's avatar
Artur Wojcik committed
304
305
#endif

306
307
308
309
310
311
312
src_compiler assemble(src_compiler compiler)
{
    compiler.out_ext = ".S";
    compiler.flags   = replace_string(compiler.flags, " -c", " -S");
    return compiler;
}

Paul Fultz II's avatar
Paul Fultz II committed
313
314
315
std::vector<std::vector<char>>
compile_hip_src(const std::vector<src_file>& srcs, std::string params, const std::string& arch)
{
316
    assert(not srcs.empty());
Umang Yadav's avatar
Umang Yadav committed
317
    if(not is_hip_clang_compiler())
Artur Wojcik's avatar
Artur Wojcik committed
318
        MIGRAPHX_THROW("Unknown hip compiler: " MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER));
319

Paul Fultz II's avatar
Paul Fultz II committed
320
321
322
    if(params.find("-std=") == std::string::npos)
        params += " --std=c++17";
    params += " -fno-gpu-rdc";
323
324
    if(enabled(MIGRAPHX_GPU_DEBUG_SYM{}))
        params += " -g";
Paul Fultz II's avatar
Paul Fultz II committed
325
    params += " -c";
Umang Yadav's avatar
Umang Yadav committed
326
327
328
    params += " --offload-arch=" + arch;
    params += " --cuda-device-only";
    params += " -O" + string_value_of(MIGRAPHX_GPU_OPTIMIZE{}, "3") + " ";
Paul Fultz II's avatar
Paul Fultz II committed
329

330
331
332
    if(enabled(MIGRAPHX_GPU_DEBUG{}))
        params += " -DMIGRAPHX_DEBUG";

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

336
337
338
    src_compiler compiler;
    compiler.flags    = params;
    compiler.compiler = MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER);
339
340
341
342
#ifdef MIGRAPHX_HIP_COMPILER_LAUNCHER
    if(has_compiler_launcher())
        compiler.launcher = MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER_LAUNCHER);
#endif
343
344
345
346
347
348
    if(enabled(MIGRAPHX_GPU_DUMP_SRC{}))
    {
        for(const auto& src : srcs)
        {
            if(src.path.extension() != ".cpp")
                continue;
349
            std::cout << std::string(src.content) << std::endl;
350
351
352
        }
    }

353
354
355
356
357
358
    if(enabled(MIGRAPHX_GPU_DUMP_ASM{}))
    {

        std::cout << assemble(compiler).compile(srcs).data() << std::endl;
    }

359
    return {compiler.compile(srcs)};
Paul Fultz II's avatar
Paul Fultz II committed
360
361
}

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
bool hip_has_flags(const std::vector<std::string>& flags)
{
    src_compiler compiler;
    compiler.compiler = MIGRAPHX_STRINGIZE(MIGRAPHX_HIP_COMPILER);
    compiler.flags =
        join_strings(flags, " ") + " -x hip -c --offload-arch=gfx900 --cuda-device-only";

    std::string src;
    src_file input;
    input.path    = "main.cpp";
    input.content = std::make_pair(src.data(), src.data() + src.size());

    try
    {
        compiler.compile({input});
        return true;
    }
    catch(...)
    {
        return false;
    }
}

Umang Yadav's avatar
Umang Yadav committed
385
386
#endif // MIGRAPHX_USE_HIPRTC

Shucai Xiao's avatar
Shucai Xiao committed
387
388
389
390
391
392
393
std::string enum_params(std::size_t count, std::string param)
{
    std::vector<std::string> items(count);
    transform(range(count), items.begin(), [&](auto i) { return param + std::to_string(i); });
    return join_strings(items, ",");
}

Paul Fultz II's avatar
Paul Fultz II committed
394
395
396
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx