Skip to content

Commit 7057bff

Browse files
authored
Merge pull request #19337 from Snektron/spirv-globals
spirv: rework generic global
2 parents c52a2c3 + 2f9e37a commit 7057bff

23 files changed

+14997
-3357
lines changed

src/codegen/spirv.zig

Lines changed: 432 additions & 259 deletions
Large diffs are not rendered by default.

src/codegen/spirv/Assembler.zig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ inst: struct {
194194
/// This map maps results to their tracked values.
195195
value_map: AsmValueMap = .{},
196196

197+
/// This set is used to quickly transform from an opcode name to the
198+
/// index in its instruction set. The index of the key is the
199+
/// index in `spec.InstructionSet.core.instructions()`.
200+
instruction_map: std.StringArrayHashMapUnmanaged(void) = .{},
201+
197202
/// Free the resources owned by this assembler.
198203
pub fn deinit(self: *Assembler) void {
199204
for (self.errors.items) |err| {
@@ -204,9 +209,20 @@ pub fn deinit(self: *Assembler) void {
204209
self.inst.operands.deinit(self.gpa);
205210
self.inst.string_bytes.deinit(self.gpa);
206211
self.value_map.deinit(self.gpa);
212+
self.instruction_map.deinit(self.gpa);
207213
}
208214

209215
pub fn assemble(self: *Assembler) Error!void {
216+
// Populate the opcode map if it isn't already
217+
if (self.instruction_map.count() == 0) {
218+
const instructions = spec.InstructionSet.core.instructions();
219+
try self.instruction_map.ensureUnusedCapacity(self.gpa, @intCast(instructions.len));
220+
for (spec.InstructionSet.core.instructions(), 0..) |inst, i| {
221+
const entry = try self.instruction_map.getOrPut(self.gpa, inst.name);
222+
assert(entry.index == i);
223+
}
224+
}
225+
210226
try self.tokenize();
211227
while (!self.testToken(.eof)) {
212228
try self.parseInstruction();
@@ -475,12 +491,14 @@ fn parseInstruction(self: *Assembler) !void {
475491
}
476492

477493
const opcode_text = self.tokenText(opcode_tok);
478-
@setEvalBranchQuota(10000);
479-
self.inst.opcode = std.meta.stringToEnum(Opcode, opcode_text) orelse {
494+
const index = self.instruction_map.getIndex(opcode_text) orelse {
480495
return self.fail(opcode_tok.start, "invalid opcode '{s}'", .{opcode_text});
481496
};
482497

483-
const expected_operands = self.inst.opcode.operands();
498+
const inst = spec.InstructionSet.core.instructions()[index];
499+
self.inst.opcode = @enumFromInt(inst.opcode);
500+
501+
const expected_operands = inst.operands;
484502
// This is a loop because the result-id is not always the first operand.
485503
const requires_lhs_result = for (expected_operands) |op| {
486504
if (op.kind == .IdResult) break true;

src/codegen/spirv/Cache.zig

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ const Tag = enum {
134134
/// data is (bool) type
135135
bool_false,
136136

137-
const SimpleType = enum { void, bool };
137+
const SimpleType = enum {
138+
void,
139+
bool,
140+
};
138141

139142
const VectorType = Key.VectorType;
140143
const ArrayType = Key.ArrayType;
@@ -287,11 +290,12 @@ pub const Key = union(enum) {
287290
pub const PointerType = struct {
288291
storage_class: StorageClass,
289292
child_type: Ref,
293+
/// Ref to a .fwd_ptr_type.
290294
fwd: Ref,
291295
// TODO: Decorations:
292296
// - Alignment
293-
// - ArrayStride,
294-
// - MaxByteOffset,
297+
// - ArrayStride
298+
// - MaxByteOffset
295299
};
296300

297301
pub const ForwardPointerType = struct {
@@ -728,6 +732,9 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
728732
// },
729733
.ptr_type => |ptr| Item{
730734
.tag = .type_ptr_simple,
735+
// For this variant we need to steal the ID of the forward-declaration, instead
736+
// of allocating one manually. This will make sure that we get a single result-id
737+
// any possibly forward declared pointer type.
731738
.result_id = self.resultId(ptr.fwd),
732739
.data = try self.addExtra(spv, Tag.SimplePointerType{
733740
.storage_class = ptr.storage_class,
@@ -896,24 +903,6 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
896903
},
897904
};
898905
},
899-
// .type_ptr_generic => .{
900-
// .ptr_type = .{
901-
// .storage_class = .Generic,
902-
// .child_type = @enumFromInt(data),
903-
// },
904-
// },
905-
// .type_ptr_crosswgp => .{
906-
// .ptr_type = .{
907-
// .storage_class = .CrossWorkgroup,
908-
// .child_type = @enumFromInt(data),
909-
// },
910-
// },
911-
// .type_ptr_function => .{
912-
// .ptr_type = .{
913-
// .storage_class = .Function,
914-
// .child_type = @enumFromInt(data),
915-
// },
916-
// },
917906
.type_ptr_simple => {
918907
const payload = self.extraData(Tag.SimplePointerType, data);
919908
return .{

0 commit comments

Comments
 (0)