aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/stream.h
blob: 7029b6c4e7849e5633309ff469008007e77ecb26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
204
205
206
207
208
209
210
211
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This file is part of the sirit project.
 * Copyright (c) 2019 sirit
 * This software may be used and distributed according to the terms of the
 * 3-Clause BSD License
 */

#pragma once

#include <bit>
#include <cassert>
#include <concepts>
#include <cstddef>
#include <functional>
#include <string_view>
#include <unordered_map>
#include <variant>
#include <vector>
#include <utility>

#ifndef __cpp_lib_bit_cast
#include <cstring>
#endif

#include <spirv/unified1/spirv.hpp>

#include "common_types.h"

namespace Sirit {

class Declarations;

struct OpId {
    OpId(spv::Op opcode_) : opcode{opcode_} {}
    OpId(spv::Op opcode_, Id result_type_) : opcode{opcode_}, result_type{result_type_} {
        assert(result_type.value != 0);
    }

    spv::Op opcode{};
    Id result_type{};
};

struct EndOp {};

inline size_t WordsInString(std::string_view string) {
    return string.size() / sizeof(u32) + 1;
}

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) {
        return offset < size ? static_cast<u32>(string[offset]) : 0u;
    };

    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;
    }
}

class Stream {
    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);
    }

    u32 LocalAddress() const noexcept {
        return static_cast<u32>(words.size());
    }

    u32 Value(u32 index) const noexcept {
        return words[index];
    }

    void SetValue(u32 index, u32 value) noexcept {
        words[index] = value;
    }

    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) {
#ifdef __cpp_lib_bit_cast
        return *this << std::bit_cast<u32>(value);
#else
        static_assert(sizeof(float) == sizeof(u32));
        u32 int_value;
        std::memcpy(&int_value, &value, sizeof(int_value));
        return *this << int_value;
#endif
    }

    Stream& operator<<(double value) {
#ifdef __cpp_lib_bit_cast
        return *this << std::bit_cast<u64>(value);
#else
        static_assert(sizeof(double) == sizeof(u64));
        u64 int_value;
        std::memcpy(&int_value, &value, sizeof(int_value));
        return *this << int_value;
#endif
    }

    Stream& operator<<(bool value) {
        return *this << static_cast<u32>(value ? 1 : 0);
    }

    Stream& operator<<(Id value) {
        assert(value.value != 0);
        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;
    }

    Stream& operator<<(const char* string) {
        return *this << std::string_view{string};
    }

    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 {
public:
    explicit Declarations(u32* bound) : stream{bound} {}

    void Reserve(size_t num_words) {
        return stream.Reserve(num_words);
    }

    std::span<const u32> Words() const noexcept {
        return stream.Words();
    }

    template <typename T>
    Declarations& operator<<(const T& value) {
        stream << value;
        return *this;
    }

    // Declarations without an id don't exist
    Declarations& operator<<(spv::Op) = delete;

    Declarations& operator<<(OpId op) {
        id_index = op.result_type.value != 0 ? 2 : 1;
        stream << op;
        return *this;
    }

    Id operator<<(EndOp) {
        const auto begin = stream.words.data();
        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};
    }

private:
    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;
};

} // namespace Sirit