Clean up triple_to_hsa_isa naming (#403)

Этот коммит содержится в:
Aaron Enye Shi
2018-04-16 21:17:55 -04:00
коммит произвёл Maneesh Gupta
родитель 1a281240d6
Коммит 5a80c16cd2
+46 -30
Просмотреть файл
@@ -10,42 +10,58 @@
using namespace std;
hsa_isa_t hip_impl::triple_to_hsa_isa(const std::string& triple) {
static constexpr const char OffloadKind[] = "hcc-";
hsa_isa_t Isa = {0};
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"};
// Check that Triple larger than hcc- and the prefix matches hcc-
if ((triple.size() < sizeof(OffloadKind) - 1) || !std::equal(triple.c_str(), triple.c_str() + sizeof(OffloadKind) - 1, OffloadKind)) {
return Isa;
if (triple.find(old_prefix) == 0) {
return new_prefix + triple.substr(sizeof(old_prefix) - 1);
}
std::string validatedTriple = triple.substr(sizeof(OffloadKind) - 1);
static constexpr const char newPrefix[] = "amdgcn-amd-amdhsa--gfx";
return (triple.find(new_prefix) == 0) ? triple : "";
}
// Check if the target triple matches the new prefix
if ((validatedTriple.size() >= sizeof(newPrefix) - 1) && std::equal(validatedTriple.c_str(), validatedTriple.c_str() + sizeof(newPrefix) - 1, newPrefix)) {
if (HSA_STATUS_SUCCESS != hsa_isa_from_name(validatedTriple.c_str(), &Isa)) {
// If new prefix fails, try older prefix incase of older ROCR
// Supports backwards compatibility with old naming
Isa = {};
auto it = std::find_if(
validatedTriple.cbegin(),
validatedTriple.cend(),
[](char x) { return std::isdigit(x); });
if (std::equal(validatedTriple.cbegin(), it, newPrefix)) {
std::string tmp = "AMD:AMDGPU";
while (it != validatedTriple.cend()) {
tmp.push_back(':');
tmp.push_back(*it++);
}
if (HSA_STATUS_SUCCESS != hsa_isa_from_name(tmp.c_str(), &Isa)) {
// If it also fails, then unsupported target triple
Isa.handle = 0;
}
}
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);
static hsa_isa_t tmp{};
static const bool is_old_rocr{
hsa_isa_from_name(triple.c_str(), &tmp) != HSA_STATUS_SUCCESS};
if (is_old_rocr) {
auto tmp{triple.substr(triple.rfind('x') + 1)};
triple.replace(0, std::string::npos, "AMD:AMDGPU");
for (auto&& x : tmp) {
triple.push_back(':');
triple.push_back(x);
}
}
return Isa;
return triple;
}
hsa_isa_t hip_impl::triple_to_hsa_isa(const std::string& triple) {
const auto 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;
}
// DATA - STATICS