authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-03-31 19:05:54+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-04-06 13:37:37+02:00
log42c7e752e1eae7663068e9c52ad77f7383d977e9
treea99443bbee26923de3fb0097b77b91e573232d33
parent39420838061a9049fbc889212836a9d4d2ab9af4
signaturebadge-check Signed by SSH key SHA256:ZS52FNyUv2WUXvO4njmVaFVO46RHojFuOrxRc4LuKzg

spirv: id range helper

This allows us to more sanely allocate a continuous range of result-ids, and avoids a bunch of nasty casting code in a few places. Its currently not used very often, but will be useful in the future.

2 files changed, 21 insertions(+), 9 deletions(-)

src/codegen/spirv.zig+3-3
......@@ -5440,7 +5440,7 @@ const DeclGen = struct {
54405440 };
54415441
54425442 // First, pre-allocate the labels for the cases.
5443 const first_case_label = self.spv.allocIds(num_cases);
5443 const case_labels = self.spv.allocIds(num_cases);
54445444 // We always need the default case - if zig has none, we will generate unreachable there.
54455445 const default = self.spv.allocId();
54465446
......@@ -5471,7 +5471,7 @@ const DeclGen = struct {
54715471 const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len];
54725472 extra_index = case.end + case.data.items_len + case_body.len;
54735473
5474 const label: IdRef = @enumFromInt(@intFromEnum(first_case_label) + case_i);
5474 const label = case_labels.at(case_i);
54755475
54765476 for (items) |item| {
54775477 const value = (try self.air.value(item, mod)) orelse unreachable;
......@@ -5511,7 +5511,7 @@ const DeclGen = struct {
55115511 const case_body: []const Air.Inst.Index = @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]);
55125512 extra_index = case.end + case.data.items_len + case_body.len;
55135513
5514 const label: IdResult = @enumFromInt(@intFromEnum(first_case_label) + case_i);
5514 const label = case_labels.at(case_i);
55155515
55165516 try self.beginSpvBlock(label);
55175517
src/codegen/spirv/Module.zig+18-6
......@@ -197,14 +197,26 @@ pub fn deinit(self: *Module) void {
197197 self.* = undefined;
198198}
199199
200pub fn allocId(self: *Module) spec.IdResult {
201 defer self.next_result_id += 1;
202 return @enumFromInt(self.next_result_id);
203}
200pub const IdRange = struct {
201 base: u32,
202 len: u32,
203
204 pub fn at(range: IdRange, i: usize) IdResult {
205 assert(i < range.len);
206 return @enumFromInt(range.base + i);
207 }
208};
204209
205pub fn allocIds(self: *Module, n: u32) spec.IdResult {
210pub fn allocIds(self: *Module, n: u32) IdRange {
206211 defer self.next_result_id += n;
207 return @enumFromInt(self.next_result_id);
212 return .{
213 .base = self.next_result_id,
214 .len = n,
215 };
216}
217
218pub fn allocId(self: *Module) IdResult {
219 return self.allocIds(1).at(0);
208220}
209221
210222pub fn idBound(self: Module) Word {