compile_kernel.cpp 8.77 KB
Newer Older
arai713's avatar
arai713 committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.

4
#include <rtc/hip.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
5
#include <rtc/compile_kernel.hpp>
6
#ifdef HIPRTC_FOR_CODEGEN_TESTS
7
#include <hip/hiprtc.h>
Mirza Halilcevic's avatar
Mirza Halilcevic committed
8
#include <rtc/manage_ptr.hpp>
9
#endif
Paul Fultz II's avatar
Paul Fultz II committed
10
#include <rtc/tmp_dir.hpp>
11
#include <algorithm>
Paul Fultz II's avatar
Paul Fultz II committed
12
#include <cassert>
13
#include <deque>
14
15
16
17
#include <fstream>
#include <iostream>
#include <numeric>
#include <stdexcept>
Paul Fultz II's avatar
Paul Fultz II committed
18
19
20

namespace rtc {

Mirza Halilcevic's avatar
Mirza Halilcevic committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
bool EndsWith(const std::string& value, const std::string& suffix)
{
    if(suffix.size() > value.size())
        return false;
    else
        return std::equal(suffix.rbegin(), suffix.rend(), value.rbegin());
}

std::vector<std::string> SplitString(const std::string& s, char delim)
{
    std::vector<std::string> elems;
    std::stringstream ss(s + delim);
    std::string item;
    while(std::getline(ss, item, delim))
    {
        elems.push_back(item);
    }
    return elems;
}

Paul Fultz II's avatar
Paul Fultz II committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
template <class T>
T generic_read_file(const std::string& filename, size_t offset = 0, size_t nbytes = 0)
{
    std::ifstream is(filename, std::ios::binary | std::ios::ate);
    if(nbytes == 0)
    {
        // if there is a non-zero offset and nbytes is not set,
        // calculate size of remaining bytes to read
        nbytes = is.tellg();
        if(offset > nbytes)
            throw std::runtime_error("offset is larger than file size");
        nbytes -= offset;
    }
    if(nbytes < 1)
        throw std::runtime_error("Invalid size for: " + filename);
    is.seekg(offset, std::ios::beg);

    T buffer(nbytes, 0);
    if(not is.read(&buffer[0], nbytes))
        throw std::runtime_error("Error reading file: " + filename);
    return buffer;
}

std::vector<char> read_buffer(const std::string& filename, size_t offset = 0, size_t nbytes = 0)
{
    return generic_read_file<std::vector<char>>(filename, offset, nbytes);
}

std::string read_string(const std::string& filename)
{
    return generic_read_file<std::string>(filename);
}

void write_buffer(const std::string& filename, const char* buffer, std::size_t size)
{
    std::ofstream os(filename);
    os.write(buffer, size);
}
void write_buffer(const std::string& filename, const std::vector<char>& buffer)
{
    write_buffer(filename, buffer.data(), buffer.size());
}
void write_string(const std::string& filename, const std::string_view& buffer)
{
    write_buffer(filename, buffer.data(), buffer.size());
}

std::string compiler() { return "/opt/rocm/llvm/bin/clang++ -x hip --cuda-device-only"; }
arai713's avatar
arai713 committed
89
90
// TODO: undo after extracting the codeobj
// std::string compiler() { return "/opt/rocm/llvm/bin/clang++ -x hip"; }
Paul Fultz II's avatar
Paul Fultz II committed
91

92
kernel clang_compile_kernel(const std::vector<src_file>& srcs, compile_options options)
Paul Fultz II's avatar
Paul Fultz II committed
93
94
95
96
97
98
99
100
101
102
{
    assert(not srcs.empty());
    tmp_dir td{"compile"};
    options.flags += " -I. -O3";
    options.flags += " -std=c++17";
    options.flags += " --offload-arch=" + get_device_name();
    std::string out;

    for(const auto& src : srcs)
    {
103
104
105
        fs::path full_path   = td.path / src.path;
        fs::path parent_path = full_path.parent_path();
        fs::create_directories(parent_path);
Paul Fultz II's avatar
Paul Fultz II committed
106
107
108
109
110
111
112
113
114
115
116
117
118
        write_string(full_path.string(), src.content);
        if(src.path.extension().string() == ".cpp")
        {
            options.flags += " -c " + src.path.filename().string();
            if(out.empty())
                out = src.path.stem().string() + ".o";
        }
    }

    options.flags += " -o " + out;
    td.execute(compiler() + options.flags);

    auto out_path = td.path / out;
119
    if(not fs::exists(out_path))
Paul Fultz II's avatar
Paul Fultz II committed
120
121
122
123
        throw std::runtime_error("Output file missing: " + out);

    auto obj = read_buffer(out_path.string());

arai713's avatar
arai713 committed
124
125
126
127
128
129
    std::ofstream ofh("obj.o", std::ios::binary);
    for(auto i : obj)
        ofh << i;
    ofh.close();
    // int s = std::system(("/usr/bin/cp " + out_path.string() + " codeobj.bin").c_str());
    // assert(s == 0);
Paul Fultz II's avatar
Paul Fultz II committed
130
131
132
    return kernel{obj.data(), options.kernel_name};
}

133
134
#ifdef HIPRTC_FOR_CODEGEN_TESTS

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

Mirza Halilcevic's avatar
Mirza Halilcevic committed
140
void hiprtc_check_error(hiprtcResult err, const std::string& msg = "")
141
142
143
144
145
{
    if(err != HIPRTC_SUCCESS)
        throw std::runtime_error(hiprtc_error(err, msg));
}

Mirza Halilcevic's avatar
Mirza Halilcevic committed
146
struct hiprtc_src_file
147
{
Mirza Halilcevic's avatar
Mirza Halilcevic committed
148
149
150
151
    hiprtc_src_file() = default;
    hiprtc_src_file(const src_file& s) : path(s.path.string()), content(s.content) {}
    std::string path;
    std::string content;
152
153
};

Mirza Halilcevic's avatar
Mirza Halilcevic committed
154
155
void hiprtc_program_destroy(hiprtcProgram prog) { hiprtcDestroyProgram(&prog); }
using hiprtc_program_ptr = RTC_MANAGE_PTR(hiprtcProgram, hiprtc_program_destroy);
156
157
158
159
160
161
162

template <class... Ts>
hiprtc_program_ptr hiprtc_program_create(Ts... xs)
{
    hiprtcProgram prog = nullptr;
    auto result        = hiprtcCreateProgram(&prog, xs...);
    hiprtc_program_ptr p{prog};
Mirza Halilcevic's avatar
Mirza Halilcevic committed
163
    hiprtc_check_error(result, "Create program failed.");
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    return p;
}

struct hiprtc_program
{
    struct string_array
    {
        std::deque<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 = "";

    hiprtc_program(const std::string& src, const std::string& name = "main.cpp")
        : cpp_src(src), cpp_name(name)
    {
        create_program();
    }

    hiprtc_program(std::vector<src_file> srcs)
    {
        for(auto&& src : srcs)
        {
Mirza Halilcevic's avatar
Mirza Halilcevic committed
204
            if(EndsWith(src.path, ".cpp"))
205
206
207
208
209
210
            {
                cpp_src  = std::move(src.content);
                cpp_name = std::move(src.path);
            }
            else
            {
211
                headers.push_back(std::move(src.content));
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
                include_names.push_back(std::move(src.path));
            }
        }
        create_program();
    }

    void create_program()
    {
        assert(not cpp_src.empty());
        assert(not cpp_name.empty());
        assert(headers.size() == include_names.size());
        prog = hiprtc_program_create(cpp_src.c_str(),
                                     cpp_name.c_str(),
                                     headers.size(),
                                     headers.data(),
                                     include_names.data());
    }

    void compile(const std::vector<std::string>& options, bool quiet = false) const
    {
        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(); });
        auto result   = hiprtcCompileProgram(prog.get(), c_options.size(), c_options.data());
        auto prog_log = log();
        if(not prog_log.empty() and not quiet)
        {
            std::cerr << prog_log << std::endl;
        }
        if(result != HIPRTC_SUCCESS)
            throw std::runtime_error("Compilation failed.");
    }

    std::string log() const
    {
        std::size_t n = 0;
Mirza Halilcevic's avatar
Mirza Halilcevic committed
250
        hiprtc_check_error(hiprtcGetProgramLogSize(prog.get(), &n));
251
252
253
        if(n == 0)
            return {};
        std::string buffer(n, '\0');
Mirza Halilcevic's avatar
Mirza Halilcevic committed
254
        hiprtc_check_error(hiprtcGetProgramLog(prog.get(), buffer.data()));
255
256
257
258
259
260
261
        assert(buffer.back() != 0);
        return buffer;
    }

    std::vector<char> get_code_obj() const
    {
        std::size_t n = 0;
Mirza Halilcevic's avatar
Mirza Halilcevic committed
262
        hiprtc_check_error(hiprtcGetCodeSize(prog.get(), &n));
263
        std::vector<char> buffer(n);
Mirza Halilcevic's avatar
Mirza Halilcevic committed
264
        hiprtc_check_error(hiprtcGetCode(prog.get(), buffer.data()));
265
266
267
268
        return buffer;
    }
};

Mirza Halilcevic's avatar
Mirza Halilcevic committed
269
270
std::vector<std::vector<char>> compile_hip_src_with_hiprtc(const std::vector<src_file>& srcs,
                                                           const compile_options& options)
271
{
Mirza Halilcevic's avatar
Mirza Halilcevic committed
272
    hiprtc_program prog(srcs);
Mirza Halilcevic's avatar
Mirza Halilcevic committed
273
    auto flags = SplitString(options.flags, ' ');
Mirza Halilcevic's avatar
Mirza Halilcevic committed
274
    prog.compile(flags);
275
276
277
    return {prog.get_code_obj()};
}

Mirza Halilcevic's avatar
Mirza Halilcevic committed
278
static kernel hiprtc_compile_kernel(const std::vector<src_file>& srcs, compile_options options)
279
{
Mirza Halilcevic's avatar
Mirza Halilcevic committed
280
281
282
283
    options.flags += " -I. -O3";
    options.flags += " -std=c++17";
    options.flags += " --offload-arch=" + get_device_name();
    auto cos = compile_hip_src_with_hiprtc(srcs, options);
284
285
286
287
288
289
    if(cos.size() != 1)
        std::runtime_error("No code object");
    auto& obj = cos.front();
    return kernel{obj.data(), options.kernel_name};
}

290
291
#endif

Mirza Halilcevic's avatar
Mirza Halilcevic committed
292
kernel compile_kernel(const std::vector<src_file>& srcs, compile_options options)
293
{
294
295
296
297
298
#ifdef HIPRTC_FOR_CODEGEN_TESTS
    return hiprtc_compile_kernel(srcs, options);
#else
    return clang_compile_kernel(srcs, options);
#endif
299
300
}

Paul Fultz II's avatar
Paul Fultz II committed
301
} // namespace rtc