2017-11-29 00:17:44 +00:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015 - present 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.
|
|
|
|
|
*/
|
|
|
|
|
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <hsa/hsa.h>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <istream>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
namespace hip_impl {
|
2019-03-09 14:21:11 -05:00
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
std::string transmogrify_triple(const std::string& triple)
|
|
|
|
|
{
|
|
|
|
|
static constexpr const char old_prefix[]{"hcc-amdgcn--amdhsa-gfx"};
|
|
|
|
|
static constexpr const char new_prefix[]{"hcc-amdgcn-amd-amdhsa--gfx"};
|
|
|
|
|
|
|
|
|
|
if (triple.find(old_prefix) == 0) {
|
|
|
|
|
return new_prefix + triple.substr(sizeof(old_prefix) - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (triple.find(new_prefix) == 0) ? triple : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
std::string isa_name(std::string triple)
|
|
|
|
|
{
|
|
|
|
|
static constexpr const char offload_prefix[]{"hcc-"};
|
|
|
|
|
|
|
|
|
|
triple = transmogrify_triple(triple);
|
|
|
|
|
if (triple.empty()) return {};
|
|
|
|
|
|
|
|
|
|
triple.erase(0, sizeof(offload_prefix) - 1);
|
|
|
|
|
|
|
|
|
|
return triple;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
hsa_isa_t triple_to_hsa_isa(const std::string& triple) {
|
|
|
|
|
const std::string isa{isa_name(std::move(triple))};
|
|
|
|
|
|
|
|
|
|
if (isa.empty()) return hsa_isa_t({});
|
|
|
|
|
|
|
|
|
|
hsa_isa_t r{};
|
|
|
|
|
|
|
|
|
|
if(HSA_STATUS_SUCCESS != hsa_isa_from_name(isa.c_str(), &r)) {
|
|
|
|
|
r.handle = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
|
|
|
|
|
struct Bundled_code {
|
|
|
|
|
union Header {
|
|
|
|
|
struct {
|
|
|
|
|
std::uint64_t offset;
|
|
|
|
|
std::uint64_t bundle_sz;
|
|
|
|
|
std::uint64_t triple_sz;
|
|
|
|
|
};
|
|
|
|
|
char cbuf[sizeof(offset) + sizeof(bundle_sz) + sizeof(triple_sz)];
|
|
|
|
|
} header;
|
|
|
|
|
std::string triple;
|
|
|
|
|
std::vector<char> blob;
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-02 17:28:06 -04:00
|
|
|
#define magic_string_ "__CLANG_OFFLOAD_BUNDLE__"
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
class Bundled_code_header {
|
|
|
|
|
// DATA - STATICS
|
|
|
|
|
static constexpr auto magic_string_sz_ = sizeof(magic_string_) - 1;
|
|
|
|
|
|
|
|
|
|
// DATA
|
|
|
|
|
union Header_ {
|
|
|
|
|
struct {
|
|
|
|
|
char bundler_magic_string_[magic_string_sz_];
|
|
|
|
|
std::uint64_t bundle_cnt_;
|
|
|
|
|
};
|
|
|
|
|
char cbuf_[sizeof(bundler_magic_string_) + sizeof(bundle_cnt_)];
|
|
|
|
|
} header_;
|
|
|
|
|
std::vector<Bundled_code> bundles_;
|
|
|
|
|
|
|
|
|
|
// FRIENDS - MANIPULATORS
|
|
|
|
|
template <typename RandomAccessIterator>
|
|
|
|
|
friend inline bool read(RandomAccessIterator f, RandomAccessIterator l,
|
|
|
|
|
Bundled_code_header& x) {
|
|
|
|
|
if (f == l) return false;
|
|
|
|
|
|
|
|
|
|
std::copy_n(f, sizeof(x.header_.cbuf_), x.header_.cbuf_);
|
|
|
|
|
|
|
|
|
|
if (valid(x)) {
|
|
|
|
|
x.bundles_.resize(x.header_.bundle_cnt_);
|
|
|
|
|
|
|
|
|
|
auto it = f + sizeof(x.header_.cbuf_);
|
|
|
|
|
for (auto&& y : x.bundles_) {
|
|
|
|
|
std::copy_n(it, sizeof(y.header.cbuf), y.header.cbuf);
|
|
|
|
|
it += sizeof(y.header.cbuf);
|
|
|
|
|
|
|
|
|
|
y.triple.assign(it, it + y.header.triple_sz);
|
|
|
|
|
|
|
|
|
|
std::copy_n(f + y.header.offset, y.header.bundle_sz, std::back_inserter(y.blob));
|
|
|
|
|
|
|
|
|
|
it += y.header.triple_sz;
|
2018-10-18 16:53:03 -04:00
|
|
|
|
|
|
|
|
x.bundled_code_size = std::max(x.bundled_code_size,
|
|
|
|
|
y.header.offset + y.header.bundle_sz);
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
return true;
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
friend inline bool read(const std::vector<char>& blob, Bundled_code_header& x) {
|
|
|
|
|
return read(blob.cbegin(), blob.cend(), x);
|
|
|
|
|
}
|
|
|
|
|
friend inline bool read(std::istream& is, Bundled_code_header& x) {
|
|
|
|
|
return read(
|
|
|
|
|
std::vector<char>{std::istreambuf_iterator<char>{is}, std::istreambuf_iterator<char>{}},
|
|
|
|
|
x);
|
|
|
|
|
}
|
|
|
|
|
// FRIENDS - ACCESSORS
|
|
|
|
|
friend inline bool valid(const Bundled_code_header& x) {
|
2019-04-02 17:28:06 -04:00
|
|
|
const std::string ms = {magic_string_};
|
|
|
|
|
return std::equal(ms.begin(), ms.end(), x.header_.bundler_magic_string_);
|
2018-03-12 11:29:03 +05:30
|
|
|
}
|
2019-04-02 17:28:06 -04:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
friend inline const std::vector<Bundled_code>& bundles(const Bundled_code_header& x) {
|
|
|
|
|
return x.bundles_;
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
}
|
2018-03-12 11:29:03 +05:30
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CREATORS
|
|
|
|
|
Bundled_code_header() = default;
|
|
|
|
|
template <typename RandomAccessIterator>
|
|
|
|
|
Bundled_code_header(RandomAccessIterator f, RandomAccessIterator l);
|
|
|
|
|
explicit Bundled_code_header(const std::vector<char>& blob);
|
|
|
|
|
explicit Bundled_code_header(const void* maybe_blob);
|
|
|
|
|
Bundled_code_header(const Bundled_code_header&) = default;
|
|
|
|
|
Bundled_code_header(Bundled_code_header&&) = default;
|
|
|
|
|
~Bundled_code_header() = default;
|
|
|
|
|
|
|
|
|
|
// MANIPULATORS
|
|
|
|
|
Bundled_code_header& operator=(const Bundled_code_header&) = default;
|
|
|
|
|
Bundled_code_header& operator=(Bundled_code_header&&) = default;
|
2018-10-18 16:53:03 -04:00
|
|
|
|
|
|
|
|
size_t bundled_code_size = 0;
|
2018-03-12 11:29:03 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CREATORS
|
|
|
|
|
template <typename RandomAccessIterator>
|
|
|
|
|
Bundled_code_header::Bundled_code_header(RandomAccessIterator f, RandomAccessIterator l)
|
|
|
|
|
: Bundled_code_header{} {
|
|
|
|
|
read(f, l, *this);
|
|
|
|
|
}
|
|
|
|
|
} // Namespace hip_impl.
|