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.
This commit is contained in:
Robin Voetter 2024-03-31 19:05:54 +02:00
parent 3942083806
commit 42c7e752e1
No known key found for this signature in database
2 changed files with 21 additions and 9 deletions

View File

@ -5440,7 +5440,7 @@ const DeclGen = struct {
}; };
// First, pre-allocate the labels for the cases. // First, pre-allocate the labels for the cases.
const first_case_label = self.spv.allocIds(num_cases); const case_labels = self.spv.allocIds(num_cases);
// We always need the default case - if zig has none, we will generate unreachable there. // We always need the default case - if zig has none, we will generate unreachable there.
const default = self.spv.allocId(); const default = self.spv.allocId();
@ -5471,7 +5471,7 @@ const DeclGen = struct {
const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len];
extra_index = case.end + case.data.items_len + case_body.len; extra_index = case.end + case.data.items_len + case_body.len;
const label: IdRef = @enumFromInt(@intFromEnum(first_case_label) + case_i); const label = case_labels.at(case_i);
for (items) |item| { for (items) |item| {
const value = (try self.air.value(item, mod)) orelse unreachable; const value = (try self.air.value(item, mod)) orelse unreachable;
@ -5511,7 +5511,7 @@ const DeclGen = struct {
const case_body: []const Air.Inst.Index = @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]); const case_body: []const Air.Inst.Index = @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]);
extra_index = case.end + case.data.items_len + case_body.len; extra_index = case.end + case.data.items_len + case_body.len;
const label: IdResult = @enumFromInt(@intFromEnum(first_case_label) + case_i); const label = case_labels.at(case_i);
try self.beginSpvBlock(label); try self.beginSpvBlock(label);

View File

@ -197,14 +197,26 @@ pub fn deinit(self: *Module) void {
self.* = undefined; self.* = undefined;
} }
pub fn allocId(self: *Module) spec.IdResult { pub const IdRange = struct {
defer self.next_result_id += 1; base: u32,
return @enumFromInt(self.next_result_id); len: u32,
pub fn at(range: IdRange, i: usize) IdResult {
assert(i < range.len);
return @enumFromInt(range.base + i);
}
};
pub fn allocIds(self: *Module, n: u32) IdRange {
defer self.next_result_id += n;
return .{
.base = self.next_result_id,
.len = n,
};
} }
pub fn allocIds(self: *Module, n: u32) spec.IdResult { pub fn allocId(self: *Module) IdResult {
defer self.next_result_id += n; return self.allocIds(1).at(0);
return @enumFromInt(self.next_result_id);
} }
pub fn idBound(self: Module) Word { pub fn idBound(self: Module) Word {