2018-08-26 00:16:37 +01:00
|
|
|
/* This file is part of the sirit project.
|
2019-07-14 22:48:59 +01:00
|
|
|
* Copyright (c) 2019 sirit
|
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* 3-Clause BSD License
|
2018-08-26 00:16:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
#include <bit>
|
|
|
|
#include <concepts>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <functional>
|
2019-03-16 04:15:45 +00:00
|
|
|
#include <string_view>
|
2020-08-01 05:50:01 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <variant>
|
2018-08-26 00:16:37 +01:00
|
|
|
#include <vector>
|
2020-08-01 05:50:01 +01:00
|
|
|
|
2020-08-10 08:55:04 +01:00
|
|
|
#ifndef __cpp_lib_bit_cast
|
|
|
|
#include <cstring>
|
|
|
|
#endif
|
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
#include <spirv/unified1/spirv.hpp>
|
|
|
|
|
2019-03-11 06:26:21 +00:00
|
|
|
#include "common_types.h"
|
2018-08-26 00:16:37 +01:00
|
|
|
|
|
|
|
namespace Sirit {
|
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
class Declarations;
|
|
|
|
|
|
|
|
struct OpId {
|
2020-08-10 08:55:04 +01:00
|
|
|
spv::Op opcode{};
|
|
|
|
Id result_type{};
|
2020-08-01 05:50:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EndOp {};
|
|
|
|
|
|
|
|
constexpr size_t WordsInString(std::string_view string) {
|
|
|
|
return string.size() / sizeof(u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void InsertStringView(std::vector<u32>& words, size_t& insert_index,
|
|
|
|
std::string_view string) {
|
|
|
|
const size_t size = string.size();
|
|
|
|
const auto read = [string, size](size_t offset) {
|
2020-08-10 08:55:04 +01:00
|
|
|
return offset < size ? static_cast<u32>(string[offset]) : 0u;
|
2020-08-01 05:50:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i += sizeof(u32)) {
|
|
|
|
words[insert_index++] = read(i) | read(i + 1) << 8 | read(i + 2) << 16 | read(i + 3) << 24;
|
|
|
|
}
|
|
|
|
if (size % sizeof(u32) == 0) {
|
|
|
|
words[insert_index++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:16:37 +01:00
|
|
|
class Stream {
|
2020-08-01 05:50:01 +01:00
|
|
|
friend Declarations;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Stream(u32* bound_) : bound{bound_} {}
|
|
|
|
|
|
|
|
void Reserve(size_t num_words) {
|
|
|
|
if (insert_index + num_words <= words.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
words.resize(insert_index + num_words);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::span<const u32> Words() const noexcept {
|
|
|
|
return std::span(words.data(), insert_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(spv::Op op) {
|
|
|
|
op_index = insert_index;
|
|
|
|
words[insert_index++] = static_cast<u32>(op);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(OpId op) {
|
|
|
|
op_index = insert_index;
|
|
|
|
words[insert_index++] = static_cast<u32>(op.opcode);
|
|
|
|
if (op.result_type.value != 0) {
|
|
|
|
words[insert_index++] = op.result_type.value;
|
|
|
|
}
|
|
|
|
words[insert_index++] = ++*bound;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Id operator<<(EndOp) {
|
|
|
|
const size_t num_words = insert_index - op_index;
|
|
|
|
words[op_index] |= static_cast<u32>(num_words) << 16;
|
|
|
|
return Id{*bound};
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(u32 value) {
|
|
|
|
words[insert_index++] = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(s32 value) {
|
|
|
|
return *this << static_cast<u32>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(u64 value) {
|
|
|
|
return *this << static_cast<u32>(value) << static_cast<u32>(value >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(s64 value) {
|
|
|
|
return *this << static_cast<u64>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(float value) {
|
2020-08-10 08:55:04 +01:00
|
|
|
#ifdef __cpp_lib_bit_cast
|
2020-08-01 05:50:01 +01:00
|
|
|
return *this << std::bit_cast<u32>(value);
|
2020-08-10 08:55:04 +01:00
|
|
|
#else
|
|
|
|
static_assert(sizeof(float) == sizeof(u32));
|
|
|
|
u32 int_value;
|
|
|
|
std::memcpy(&int_value, &value, sizeof(int_value));
|
|
|
|
return *this << int_value;
|
|
|
|
#endif
|
2020-08-01 05:50:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(double value) {
|
2020-08-10 08:55:04 +01:00
|
|
|
#ifdef __cpp_lib_bit_cast
|
2020-08-01 05:50:01 +01:00
|
|
|
return *this << std::bit_cast<u64>(value);
|
2020-08-10 08:55:04 +01:00
|
|
|
#else
|
|
|
|
static_assert(sizeof(double) == sizeof(u64));
|
|
|
|
u64 int_value;
|
|
|
|
std::memcpy(&int_value, &value, sizeof(int_value));
|
|
|
|
return *this << int_value;
|
|
|
|
#endif
|
2020-08-01 05:50:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(bool value) {
|
|
|
|
return *this << static_cast<u32>(value ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(Id value) {
|
|
|
|
return *this << value.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(const Literal& literal) {
|
|
|
|
std::visit([this](auto value) { *this << value; }, literal);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream& operator<<(std::string_view string) {
|
|
|
|
InsertStringView(words, insert_index, string);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-10 08:55:04 +01:00
|
|
|
Stream& operator<<(const char* string) {
|
|
|
|
return *this << std::string_view{string};
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
template <typename T>
|
|
|
|
requires std::is_enum_v<T> Stream& operator<<(T value) {
|
|
|
|
static_assert(sizeof(T) == sizeof(u32));
|
|
|
|
return *this << static_cast<u32>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Stream& operator<<(std::optional<T> value) {
|
|
|
|
if (value) {
|
|
|
|
*this << *value;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Stream& operator<<(std::span<const T> values) {
|
|
|
|
for (const auto& value : values) {
|
|
|
|
*this << value;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
u32* bound = nullptr;
|
|
|
|
std::vector<u32> words;
|
|
|
|
size_t insert_index = 0;
|
|
|
|
size_t op_index = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Declarations {
|
2019-03-11 06:26:21 +00:00
|
|
|
public:
|
2020-08-01 05:50:01 +01:00
|
|
|
explicit Declarations(u32* bound) : stream{bound} {}
|
|
|
|
|
|
|
|
void Reserve(size_t num_words) {
|
|
|
|
return stream.Reserve(num_words);
|
|
|
|
}
|
2018-10-03 04:32:45 +01:00
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
std::span<const u32> Words() const noexcept {
|
|
|
|
return stream.Words();
|
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
template <typename T>
|
|
|
|
Declarations& operator<<(const T& value) {
|
|
|
|
stream << value;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
// Declarations without an id don't exist
|
|
|
|
Declarations& operator<<(spv::Op) = delete;
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
Declarations& operator<<(OpId op) {
|
|
|
|
id_index = op.result_type.value != 0 ? 2 : 1;
|
|
|
|
stream << op;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2020-08-01 05:50:01 +01:00
|
|
|
Id operator<<(EndOp) {
|
2020-08-10 08:55:04 +01:00
|
|
|
const auto begin = stream.words.data();
|
2020-08-01 05:50:01 +01:00
|
|
|
std::vector<u32> declarations(begin + stream.op_index, begin + stream.insert_index);
|
|
|
|
|
|
|
|
// Normalize result id for lookups
|
|
|
|
const u32 id = std::exchange(declarations[id_index], 0);
|
|
|
|
|
|
|
|
const auto [entry, inserted] = existing_declarations.emplace(declarations, id);
|
|
|
|
if (inserted) {
|
|
|
|
return stream << EndOp{};
|
|
|
|
}
|
|
|
|
// If the declaration already exists, undo the operation
|
|
|
|
stream.insert_index = stream.op_index;
|
|
|
|
--*stream.bound;
|
|
|
|
|
|
|
|
return Id{entry->second};
|
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2019-03-11 06:26:21 +00:00
|
|
|
private:
|
2020-08-01 05:50:01 +01:00
|
|
|
struct HashVector {
|
|
|
|
size_t operator()(const std::vector<u32>& vector) const noexcept {
|
|
|
|
size_t hash = std::hash<size_t>{}(vector.size());
|
|
|
|
for (const u32 value : vector) {
|
|
|
|
hash ^= std::hash<u32>{}(value);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Stream stream;
|
|
|
|
std::unordered_map<std::vector<u32>, u32, HashVector> existing_declarations;
|
|
|
|
size_t id_index = 0;
|
2018-08-26 00:16:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Sirit
|