Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request ziglang#19337 from Snektron/spirv-globals
Browse files Browse the repository at this point in the history
spirv: rework generic global
  • Loading branch information
Snektron authored Mar 19, 2024
2 parents c52a2c3 + 2f9e37a commit 7057bff
Show file tree
Hide file tree
Showing 23 changed files with 14,997 additions and 3,357 deletions.
691 changes: 432 additions & 259 deletions src/codegen/spirv.zig

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions src/codegen/spirv/Assembler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ inst: struct {
/// This map maps results to their tracked values.
value_map: AsmValueMap = .{},

/// This set is used to quickly transform from an opcode name to the
/// index in its instruction set. The index of the key is the
/// index in `spec.InstructionSet.core.instructions()`.
instruction_map: std.StringArrayHashMapUnmanaged(void) = .{},

/// Free the resources owned by this assembler.
pub fn deinit(self: *Assembler) void {
for (self.errors.items) |err| {
Expand All @@ -204,9 +209,20 @@ pub fn deinit(self: *Assembler) void {
self.inst.operands.deinit(self.gpa);
self.inst.string_bytes.deinit(self.gpa);
self.value_map.deinit(self.gpa);
self.instruction_map.deinit(self.gpa);
}

pub fn assemble(self: *Assembler) Error!void {
// Populate the opcode map if it isn't already
if (self.instruction_map.count() == 0) {
const instructions = spec.InstructionSet.core.instructions();
try self.instruction_map.ensureUnusedCapacity(self.gpa, @intCast(instructions.len));
for (spec.InstructionSet.core.instructions(), 0..) |inst, i| {
const entry = try self.instruction_map.getOrPut(self.gpa, inst.name);
assert(entry.index == i);
}
}

try self.tokenize();
while (!self.testToken(.eof)) {
try self.parseInstruction();
Expand Down Expand Up @@ -475,12 +491,14 @@ fn parseInstruction(self: *Assembler) !void {
}

const opcode_text = self.tokenText(opcode_tok);
@setEvalBranchQuota(10000);
self.inst.opcode = std.meta.stringToEnum(Opcode, opcode_text) orelse {
const index = self.instruction_map.getIndex(opcode_text) orelse {
return self.fail(opcode_tok.start, "invalid opcode '{s}'", .{opcode_text});
};

const expected_operands = self.inst.opcode.operands();
const inst = spec.InstructionSet.core.instructions()[index];
self.inst.opcode = @enumFromInt(inst.opcode);

const expected_operands = inst.operands;
// This is a loop because the result-id is not always the first operand.
const requires_lhs_result = for (expected_operands) |op| {
if (op.kind == .IdResult) break true;
Expand Down
31 changes: 10 additions & 21 deletions src/codegen/spirv/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ const Tag = enum {
/// data is (bool) type
bool_false,

const SimpleType = enum { void, bool };
const SimpleType = enum {
void,
bool,
};

const VectorType = Key.VectorType;
const ArrayType = Key.ArrayType;
Expand Down Expand Up @@ -287,11 +290,12 @@ pub const Key = union(enum) {
pub const PointerType = struct {
storage_class: StorageClass,
child_type: Ref,
/// Ref to a .fwd_ptr_type.
fwd: Ref,
// TODO: Decorations:
// - Alignment
// - ArrayStride,
// - MaxByteOffset,
// - ArrayStride
// - MaxByteOffset
};

pub const ForwardPointerType = struct {
Expand Down Expand Up @@ -728,6 +732,9 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
// },
.ptr_type => |ptr| Item{
.tag = .type_ptr_simple,
// For this variant we need to steal the ID of the forward-declaration, instead
// of allocating one manually. This will make sure that we get a single result-id
// any possibly forward declared pointer type.
.result_id = self.resultId(ptr.fwd),
.data = try self.addExtra(spv, Tag.SimplePointerType{
.storage_class = ptr.storage_class,
Expand Down Expand Up @@ -896,24 +903,6 @@ pub fn lookup(self: *const Self, ref: Ref) Key {
},
};
},
// .type_ptr_generic => .{
// .ptr_type = .{
// .storage_class = .Generic,
// .child_type = @enumFromInt(data),
// },
// },
// .type_ptr_crosswgp => .{
// .ptr_type = .{
// .storage_class = .CrossWorkgroup,
// .child_type = @enumFromInt(data),
// },
// },
// .type_ptr_function => .{
// .ptr_type = .{
// .storage_class = .Function,
// .child_type = @enumFromInt(data),
// },
// },
.type_ptr_simple => {
const payload = self.extraData(Tag.SimplePointerType, data);
return .{
Expand Down
Loading

0 comments on commit 7057bff

Please sign in to comment.