authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-01-19 14:28:48+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-01-19 15:28:17+01:00
log1055344673a87af39f2288bae069ec9403e6086d
treee6702f9451c380fe53e7e90279be856620b8cd69
parent801732aebd3092c539b754170032455139c7418c

SPIR-V: Use free list for result id generation


2 files changed, 33 insertions(+), 5 deletions(-)

src/codegen/spirv.zig+24-1
......@@ -1,4 +1,6 @@
11const std = @import("std");
2const Allocator = std.mem.Allocator;
3
24const spec = @import("spirv/spec.zig");
35const Module = @import("../Module.zig");
46const Decl = Module.Decl;
......@@ -10,14 +12,35 @@ pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []c
1012}
1113
1214pub const SPIRVModule = struct {
13 // TODO: Also use a free list.
1415 next_id: u32 = 0,
16 free_id_list: std.ArrayList(u32),
17
18 pub fn init(allocator: *Allocator) SPIRVModule {
19 return .{
20 .free_id_list = std.ArrayList(u32).init(allocator),
21 };
22 }
23
24 pub fn deinit(self: *SPIRVModule) void {
25 self.free_id_list.deinit();
26 }
1527
1628 pub fn allocId(self: *SPIRVModule) u32 {
29 if (self.free_id_list.popOrNull()) |id| return id;
30
1731 defer self.next_id += 1;
1832 return self.next_id;
1933 }
2034
35 pub fn freeId(self: *SPIRVModule, id: u32) void {
36 if (id + 1 == self.next_id) {
37 self.next_id -= 1;
38 } else {
39 // If no more memory to append the id to the free list, just ignore it.
40 self.free_id_list.append(id) catch {};
41 }
42 }
43
2144 pub fn idBound(self: *SPIRVModule) u32 {
2245 return self.next_id;
2346 }
src/link/SpirV.zig+9-4
......@@ -38,7 +38,7 @@ pub const FnData = struct {
3838base: link.File,
3939
4040// TODO: Does this file need to support multiple independent modules?
41spirv_module: codegen.SPIRVModule = .{},
41spirv_module: codegen.SPIRVModule,
4242
4343pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {
4444 const spirv = try gpa.create(SpirV);
......@@ -49,6 +49,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {
4949 .file = null,
5050 .allocator = gpa,
5151 },
52 .spirv_module = codegen.SPIRVModule.init(gpa),
5253 };
5354
5455 // TODO: Figure out where to put all of these
......@@ -87,6 +88,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
8788}
8889
8990pub fn deinit(self: *SpirV) void {
91 self.spirv_module.deinit();
9092}
9193
9294pub fn updateDecl(self: *SpirV, module: *Module, decl: *Module.Decl) !void {
......@@ -116,9 +118,12 @@ pub fn updateDeclExports(
116118) !void {}
117119
118120pub fn freeDecl(self: *SpirV, decl: *Module.Decl) void {
119 decl.fn_link.spirv.code.deinit(self.base.allocator);
121 var fn_data = decl.fn_link.spirv;
122 fn_data.code.deinit(self.base.allocator);
123 if (fn_data.id) |id| self.spirv_module.freeId(id);
120124 decl.fn_link.spirv = undefined;
121125}
126
122127pub fn flush(self: *SpirV, comp: *Compilation) !void {
123128 if (build_options.have_llvm and self.base.options.use_lld) {
124129 return error.LLD_LinkingIsTODO_ForSpirV; // TODO: LLD Doesn't support SpirV at all.
......@@ -137,8 +142,8 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void {
137142 var binary = std.ArrayList(u32).init(self.base.allocator);
138143 defer binary.deinit();
139144
140 // Note: The order of adding functions to the final binary
141 // follows the SPIR-V logical moduel format!
145 // Note: The order of adding sections to the final binary
146 // follows the SPIR-V logical module format!
142147
143148 try binary.appendSlice(&[_]u32{
144149 spec.magic_number,