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 @@...@@ -1,4 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const Allocator = std.mem.Allocator;
3
2const spec = @import("spirv/spec.zig");4const spec = @import("spirv/spec.zig");
3const Module = @import("../Module.zig");5const Module = @import("../Module.zig");
4const Decl = Module.Decl;6const Decl = Module.Decl;
...@@ -10,14 +12,35 @@ pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []c...@@ -10,14 +12,35 @@ pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []c
10}12}
1113
12pub const SPIRVModule = struct {14pub const SPIRVModule = struct {
13 // TODO: Also use a free list.
14 next_id: u32 = 0,15 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
16 pub fn allocId(self: *SPIRVModule) u32 {28 pub fn allocId(self: *SPIRVModule) u32 {
29 if (self.free_id_list.popOrNull()) |id| return id;
30
17 defer self.next_id += 1;31 defer self.next_id += 1;
18 return self.next_id;32 return self.next_id;
19 }33 }
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
21 pub fn idBound(self: *SPIRVModule) u32 {44 pub fn idBound(self: *SPIRVModule) u32 {
22 return self.next_id;45 return self.next_id;
23 }46 }
src/link/SpirV.zig+9-4
...@@ -38,7 +38,7 @@ pub const FnData = struct {...@@ -38,7 +38,7 @@ pub const FnData = struct {
38base: link.File,38base: link.File,
3939
40// TODO: Does this file need to support multiple independent modules?40// TODO: Does this file need to support multiple independent modules?
41spirv_module: codegen.SPIRVModule = .{},41spirv_module: codegen.SPIRVModule,
4242
43pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {43pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {
44 const spirv = try gpa.create(SpirV);44 const spirv = try gpa.create(SpirV);
...@@ -49,6 +49,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {...@@ -49,6 +49,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {
49 .file = null,49 .file = null,
50 .allocator = gpa,50 .allocator = gpa,
51 },51 },
52 .spirv_module = codegen.SPIRVModule.init(gpa),
52 };53 };
5354
54 // TODO: Figure out where to put all of these55 // 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...@@ -87,6 +88,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
87}88}
8889
89pub fn deinit(self: *SpirV) void {90pub fn deinit(self: *SpirV) void {
91 self.spirv_module.deinit();
90}92}
9193
92pub fn updateDecl(self: *SpirV, module: *Module, decl: *Module.Decl) !void {94pub fn updateDecl(self: *SpirV, module: *Module, decl: *Module.Decl) !void {
...@@ -116,9 +118,12 @@ pub fn updateDeclExports(...@@ -116,9 +118,12 @@ pub fn updateDeclExports(
116) !void {}118) !void {}
117119
118pub fn freeDecl(self: *SpirV, decl: *Module.Decl) void {120pub 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);
120 decl.fn_link.spirv = undefined;124 decl.fn_link.spirv = undefined;
121}125}
126
122pub fn flush(self: *SpirV, comp: *Compilation) !void {127pub fn flush(self: *SpirV, comp: *Compilation) !void {
123 if (build_options.have_llvm and self.base.options.use_lld) {128 if (build_options.have_llvm and self.base.options.use_lld) {
124 return error.LLD_LinkingIsTODO_ForSpirV; // TODO: LLD Doesn't support SpirV at all.129 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 {...@@ -137,8 +142,8 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void {
137 var binary = std.ArrayList(u32).init(self.base.allocator);142 var binary = std.ArrayList(u32).init(self.base.allocator);
138 defer binary.deinit();143 defer binary.deinit();
139144
140 // Note: The order of adding functions to the final binary145 // Note: The order of adding sections to the final binary
141 // follows the SPIR-V logical moduel format!146 // follows the SPIR-V logical module format!
142147
143 try binary.appendSlice(&[_]u32{148 try binary.appendSlice(&[_]u32{
144 spec.magic_number,149 spec.magic_number,