#pragma once #include #include #include #include #include #include #include #include namespace hip_impl { hsa_isa_t triple_to_hsa_isa(const std::string& triple); struct Bundled_code { union { struct { std::uint64_t offset; std::uint64_t bundle_sz; std::uint64_t triple_sz; }; std::uint8_t cbuf[ sizeof(offset) + sizeof(bundle_sz) + sizeof(triple_sz)]; }; std::string triple; std::vector blob; }; class Bundled_code_header { // DATA - STATICS static constexpr const char magic_string_[] = "__CLANG_OFFLOAD_BUNDLE__"; static constexpr auto magic_string_sz_ = sizeof(magic_string_) - 1; // DATA union { struct { std::uint8_t bundler_magic_string_[magic_string_sz_]; std::uint64_t bundle_cnt_; }; std::uint8_t cbuf_[ sizeof(bundler_magic_string_) + sizeof(bundle_cnt_)]; }; std::vector bundles_; // FRIENDS - MANIPULATORS template friend inline bool read( RandomAccessIterator f, RandomAccessIterator l, Bundled_code_header& x) { std::copy_n(f, sizeof(x.cbuf_), x.cbuf_); if (valid(x)) { x.bundles_.resize(x.bundle_cnt_); auto it = f + sizeof(x.cbuf_); for (auto&& y : x.bundles_) { std::copy_n(it, sizeof(y.cbuf), y.cbuf); it += sizeof(y.cbuf); y.triple.insert(y.triple.cend(), it, it + y.triple_sz); std::copy_n( f + y.offset, y.bundle_sz, std::back_inserter(y.blob)); it += y.triple_sz; } return true; } return false; } friend inline bool read(const std::vector& 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{ std::istreambuf_iterator{is}, std::istreambuf_iterator{}}, x); } // FRIENDS - ACCESSORS friend inline bool valid(const Bundled_code_header& x) { return std::equal( x.bundler_magic_string_, x.bundler_magic_string_ + magic_string_sz_, x.magic_string_); } friend inline const std::vector& bundles(const Bundled_code_header& x) { return x.bundles_; } public: // CREATORS Bundled_code_header() = default; template Bundled_code_header(RandomAccessIterator f, RandomAccessIterator l); explicit Bundled_code_header(const std::vector& 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; }; // CREATORS template Bundled_code_header::Bundled_code_header(I f, I l) : Bundled_code_header{} { read(f, l, *this); } } // Namespace hip_impl.