authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-01-19 01:29:01+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-01-19 15:28:17+01:00
log71ac82ecb028605545b67eaa50b34f4e2494de44
tree1c59134020383b2aba81b91bed163c5279182acd
parent02c138fe7011346ebab5e4b24ba0f8575bb52173

SPIR-V: Make emitting binary more efficient


2 files changed, 67 insertions(+), 20 deletions(-)

src/codegen/spirv.zig+6
......@@ -19,4 +19,10 @@ pub const SPIRVModule = struct {
1919 pub fn genDecl(self: SPIRVModule, id: u32, code: *std.ArrayList(u32), decl: *Decl) !void {
2020
2121 }
22
23 pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []const u32) !void {
24 const word_count = @intCast(u32, args.len + 1);
25 try code.append((word_count << 16) | @enumToInt(instr));
26 try code.appendSlice(args);
27 }
2228};
src/link/SpirV.zig+61-20
......@@ -12,7 +12,23 @@ const trace = @import("../tracy.zig").trace;
1212const build_options = @import("build_options");
1313const spec = @import("../codegen/spirv/spec.zig");
1414
15//! SPIR-V Documentation: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html
15//! SPIR-V Spec documentation: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html
16//! According to above documentation, a SPIR-V module has the following logical layout:
17//! Header.
18//! OpCapability instructions.
19//! OpExtension instructions.
20//! OpExtInstImport instructions.
21//! A single OpMemoryModel instruction.
22//! All entry points, declared with OpEntryPoint instructions.
23//! All execution-mode declarators; OpExecutionMode and OpExecutionModeId instructions.
24//! Debug instructions:
25//! - First, OpString, OpSourceExtension, OpSource, OpSourceContinued (no forward references).
26//! - OpName and OpMemberName instructions.
27//! - OpModuleProcessed instructions.
28//! All annotation (decoration) instructions.
29//! All type declaration instructions, constant instructions, global variable declarations, (preferrably) OpUndef instructions.
30//! All function declarations without a body (extern functions presumably).
31//! All regular functions.
1632
1733pub const FnData = struct {
1834 id: ?u32 = null,
......@@ -103,7 +119,6 @@ pub fn freeDecl(self: *SpirV, decl: *Module.Decl) void {
103119 decl.fn_link.spirv.code.deinit(self.base.allocator);
104120 decl.fn_link.spirv = undefined;
105121}
106
107122pub fn flush(self: *SpirV, comp: *Compilation) !void {
108123 if (build_options.have_llvm and self.base.options.use_lld) {
109124 return error.LLD_LinkingIsTODO_ForSpirV; // TODO: LLD Doesn't support SpirV at all.
......@@ -118,34 +133,60 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void {
118133
119134 const module = self.base.options.module.?;
120135
121 const file = self.base.file.?;
122 var bw = std.io.bufferedWriter(file.writer());
123 const writer = bw.writer();
136 var binary = std.ArrayList(u32).init(self.base.allocator);
137 defer binary.deinit();
124138
125139 // Header
126 // SPIR-V files support both little and big endian words. The actual format is disambiguated by
127 // the magic number. This backend uses little endian.
128 try writer.writeIntLittle(u32, spec.magic_number);
129 try writer.writeIntLittle(u32, (spec.version.major << 16) | (spec.version.minor) << 8);
130 try writer.writeIntLittle(u32, 0); // TODO: Register Zig compiler magic number.
131 try writer.writeIntLittle(u32, self.spirv_module.idBound());
132 try writer.writeIntLittle(u32, 0); // Schema.
133
134 // Declarations
140 {
141 const header = [_]u32{
142 spec.magic_number,
143 (spec.version.major << 16) | (spec.version.minor << 8),
144 0, // TODO: Register Zig compiler magic number.
145 self.spirv_module.idBound(),
146 0, // Schema (currently reserved for future use in the SPIR-V spec).
147 };
148 try binary.appendSlice(&header);
149 }
150
151 // Collect list of buffers to write.
152 // SPIR-V files support both little and big endian words. The actual format is
153 // disambiguated by the magic number, and so theoretically we don't need to worry
154 // about endian-ness when writing the final binary.
155 var all_buffers = std.ArrayList(std.os.iovec_const).init(self.base.allocator);
156 defer all_buffers.deinit();
157
158 // Pre-allocate enough for the binary info + all functions
159 try all_buffers.ensureCapacity(module.decl_table.count() + 1);
160
161 all_buffers.appendAssumeCapacity(wordsToIovConst(binary.items));
162
163 // Functions
135164 for (module.decl_table.items()) |entry| {
136165 const decl = entry.value;
137166 switch (decl.typed_value) {
138167 .most_recent => |tvm| {
139168 const fn_data = &decl.fn_link.spirv;
140
141 // TODO: This could probably be more efficient.
142 for (fn_data.code.items) |word| {
143 try writer.writeIntLittle(u32, word);
144 }
169 all_buffers.appendAssumeCapacity(wordsToIovConst(fn_data.code.items));
145170 },
146171 .never_succeeded => continue,
147172 }
148173 }
149174
150 try bw.flush();
175 var file_size: u64 = 0;
176 for (all_buffers.items) |iov| {
177 file_size += iov.iov_len;
178 }
179
180 const file = self.base.file.?;
181 try file.seekTo(0);
182 try file.setEndPos(file_size);
183 try file.pwritevAll(all_buffers.items, 0);
184}
185
186fn wordsToIovConst(words: []const u32) std.os.iovec_const {
187 const bytes = std.mem.sliceAsBytes(words);
188 return .{
189 .iov_base = bytes.ptr,
190 .iov_len = bytes.len,
191 };
151192}