spirv: make IdResultType and IdRef weak aliases of IdResult

Previously they were strong aliases, but as these types are used quite
intermittendly it resulted in a lot of toRef() calls. Removing them
improves readability a bit.
This commit is contained in:
Robin Voetter 2022-11-28 19:14:16 +01:00
parent 0c2526b18e
commit 700dee34d5
No known key found for this signature in database
GPG Key ID: E755662F227CB468
6 changed files with 73 additions and 95 deletions

View File

@ -227,7 +227,7 @@ pub const DeclGen = struct {
/// keep track of the previous block.
fn beginSpvBlock(self: *DeclGen, label_id: IdResult) !void {
try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label_id });
self.current_block_label_id = label_id.toRef();
self.current_block_label_id = label_id;
}
/// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need
@ -340,7 +340,7 @@ pub const DeclGen = struct {
};
const decl = self.module.declPtr(fn_decl_index);
self.module.markDeclAlive(decl);
return self.ids.get(fn_decl_index).?.toRef();
return self.ids.get(fn_decl_index).?;
}
const target = self.getTarget();
@ -350,7 +350,7 @@ pub const DeclGen = struct {
if (val.isUndef()) {
try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_type_id, .id_result = result_id });
return result_id.toRef();
return result_id;
}
switch (ty.zigTypeTag()) {
@ -538,7 +538,7 @@ pub const DeclGen = struct {
else => return self.todo("constant generation of type {s}: {}", .{ @tagName(ty.zigTypeTag()), ty.fmtDebug() }),
}
return result_id.toRef();
return result_id;
}
/// Turn a Zig type into a SPIR-V Type, and return its type result-id.
@ -766,7 +766,7 @@ pub const DeclGen = struct {
.id_result_type = try self.resolveTypeId(decl.ty.fnReturnType()),
.id_result = result_id,
.function_control = .{}, // TODO: We can set inline here if the type requires it.
.function_type = prototype_id.toRef(),
.function_type = prototype_id,
});
const params = decl.ty.fnParamLen();
@ -780,7 +780,7 @@ pub const DeclGen = struct {
.id_result_type = param_type_id,
.id_result = arg_result_id,
});
self.args.appendAssumeCapacity(arg_result_id.toRef());
self.args.appendAssumeCapacity(arg_result_id);
}
// TODO: This could probably be done in a better way...
@ -791,7 +791,7 @@ pub const DeclGen = struct {
try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{
.id_result = root_block_id,
});
self.current_block_label_id = root_block_id.toRef();
self.current_block_label_id = root_block_id;
const main_body = self.air.getMainBody();
try self.genBody(main_body);
@ -804,7 +804,7 @@ pub const DeclGen = struct {
defer self.module.gpa.free(fqn);
try self.spv.sections.debug_names.emit(self.gpa, .OpName, .{
.target = result_id.toRef(),
.target = result_id,
.name = fqn,
});
} else {
@ -928,7 +928,7 @@ pub const DeclGen = struct {
.operand_1 = lhs_id,
.operand_2 = rhs_id,
});
return result_id.toRef();
return result_id;
}
fn airShift(self: *DeclGen, inst: Air.Inst.Index, comptime opcode: Opcode) !?IdRef {
@ -951,9 +951,9 @@ pub const DeclGen = struct {
.id_result_type = result_type_id,
.id_result = result_id,
.base = lhs_id,
.shift = shift_id.toRef(),
.shift = shift_id,
});
return result_id.toRef();
return result_id;
}
fn maskStrangeInt(self: *DeclGen, ty_id: IdResultType, int_id: IdRef, bits: u16) !IdRef {
@ -976,9 +976,9 @@ pub const DeclGen = struct {
.id_result_type = ty_id,
.id_result = result_id,
.operand_1 = int_id,
.operand_2 = mask_id.toRef(),
.operand_2 = mask_id,
});
return result_id.toRef();
return result_id;
}
fn airArithOp(
@ -1046,7 +1046,7 @@ pub const DeclGen = struct {
// TODO: Trap on overflow? Probably going to be annoying.
// TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap.
return result_id.toRef();
return result_id;
}
fn airOverflowArithOp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1089,7 +1089,7 @@ pub const DeclGen = struct {
.operand_1 = lhs,
.operand_2 = rhs,
});
break :blk result_id.toRef();
break :blk result_id;
};
// Now convert the SPIR-V flavor result into a Zig-flavor result.
@ -1111,7 +1111,7 @@ pub const DeclGen = struct {
.id_result = result_id,
.unsigned_value = overflow,
});
break :blk result_id.toRef();
break :blk result_id;
};
// TODO: If copying this function for borrow, make sure to convert -1 to 1 as appropriate.
@ -1127,7 +1127,7 @@ pub const DeclGen = struct {
casted_overflow,
},
});
return result_id.toRef();
return result_id;
}
fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1163,7 +1163,7 @@ pub const DeclGen = struct {
self.func.body.writeOperand(spec.LiteralInteger, unsigned);
}
}
return result_id.toRef();
return result_id;
}
fn airCmp(self: *DeclGen, inst: Air.Inst.Index, comptime fop: Opcode, comptime sop: Opcode, comptime uop: Opcode) !?IdRef {
@ -1215,7 +1215,7 @@ pub const DeclGen = struct {
else => unreachable,
}
return result_id.toRef();
return result_id;
}
fn bitcast(self: *DeclGen, target_type_id: IdResultType, value_id: IdRef) !IdRef {
@ -1225,7 +1225,7 @@ pub const DeclGen = struct {
.id_result = result_id,
.operand = value_id,
});
return result_id.toRef();
return result_id;
}
fn airBitcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1258,7 +1258,7 @@ pub const DeclGen = struct {
.unsigned_value = operand_id,
}),
}
return result_id.toRef();
return result_id;
}
fn airNot(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1272,7 +1272,7 @@ pub const DeclGen = struct {
.id_result = result_id,
.operand = operand_id,
});
return result_id.toRef();
return result_id;
}
fn extractField(self: *DeclGen, result_ty: IdResultType, object: IdRef, field: u32) !IdRef {
@ -1283,7 +1283,7 @@ pub const DeclGen = struct {
.composite = object,
.indexes = &.{field},
});
return result_id.toRef();
return result_id;
}
fn airSliceField(self: *DeclGen, inst: Air.Inst.Index, field: u32) !?IdRef {
@ -1314,7 +1314,7 @@ pub const DeclGen = struct {
.composite = slice,
.indexes = &.{0},
});
break :blk result_id.toRef();
break :blk result_id;
};
const result_id = self.spv.allocId();
@ -1324,7 +1324,7 @@ pub const DeclGen = struct {
.base = slice_ptr,
.indexes = &.{index},
});
return result_id.toRef();
return result_id;
}
fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1347,7 +1347,7 @@ pub const DeclGen = struct {
.composite = slice,
.indexes = &.{0},
});
break :blk result_id.toRef();
break :blk result_id;
};
const elem_ptr = blk: {
@ -1358,7 +1358,7 @@ pub const DeclGen = struct {
.base = slice_ptr,
.indexes = &.{index},
});
break :blk result_id.toRef();
break :blk result_id;
};
const result_id = self.spv.allocId();
@ -1367,7 +1367,7 @@ pub const DeclGen = struct {
.id_result = result_id,
.pointer = elem_ptr,
});
return result_id.toRef();
return result_id;
}
fn airPtrElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1392,7 +1392,7 @@ pub const DeclGen = struct {
.base = base_ptr,
.indexes = &.{rhs},
});
return result_id.toRef();
return result_id;
}
fn airStructFieldVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
@ -1418,7 +1418,7 @@ pub const DeclGen = struct {
.composite = object,
.indexes = &.{field_index},
});
return result_id.toRef();
return result_id;
}
fn structFieldPtr(
@ -1446,9 +1446,9 @@ pub const DeclGen = struct {
.id_result_type = result_type_id,
.id_result = result_id,
.base = object_ptr,
.indexes = &.{field_index_id.toRef()},
.indexes = &.{field_index_id},
});
return result_id.toRef();
return result_id;
},
},
else => unreachable, // TODO
@ -1483,7 +1483,7 @@ pub const DeclGen = struct {
.id_result = result_id,
.storage_class = storage_class,
});
return result_id.toRef();
return result_id;
}
fn airArg(self: *DeclGen) IdRef {
@ -1503,7 +1503,7 @@ pub const DeclGen = struct {
var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.gpa, 4);
try self.blocks.putNoClobber(self.gpa, inst, .{
.label_id = label_id.toRef(),
.label_id = label_id,
.incoming_blocks = &incoming_blocks,
});
defer {
@ -1538,7 +1538,7 @@ pub const DeclGen = struct {
self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming.break_value_id, incoming.src_label_id });
}
return result_id.toRef();
return result_id;
}
fn airBr(self: *DeclGen, inst: Air.Inst.Index) !void {
@ -1571,8 +1571,8 @@ pub const DeclGen = struct {
try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{
.condition = condition_id,
.true_label = then_label_id.toRef(),
.false_label = else_label_id.toRef(),
.true_label = then_label_id,
.false_label = else_label_id,
});
try self.beginSpvBlock(then_label_id);
@ -1610,7 +1610,7 @@ pub const DeclGen = struct {
.memory_access = access,
});
return result_id.toRef();
return result_id;
}
fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void {
@ -1620,13 +1620,13 @@ pub const DeclGen = struct {
const loop_label_id = self.spv.allocId();
// Jump to the loop entry point
try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id.toRef() });
try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id });
// TODO: Look into OpLoopMerge.
try self.beginSpvBlock(loop_label_id);
try self.genBody(body);
try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id.toRef() });
try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id });
}
fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void {
@ -1658,7 +1658,7 @@ pub const DeclGen = struct {
.id_result = result_id,
.pointer = ptr,
});
try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = result_id.toRef() });
try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = result_id });
}
fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void {
@ -1730,7 +1730,7 @@ pub const DeclGen = struct {
// Emit the instruction before generating the blocks.
try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions);
self.func.body.writeOperand(IdRef, cond);
self.func.body.writeOperand(IdRef, default.toRef());
self.func.body.writeOperand(IdRef, default);
// Emit each of the cases
{
@ -1965,6 +1965,6 @@ pub const DeclGen = struct {
return null;
}
return result_id.toRef();
return result_id;
}
};

View File

@ -135,7 +135,7 @@ const AsmValue = union(enum) {
return switch (self) {
.just_declared, .unresolved_forward_reference => unreachable,
.value => |result| result,
.ty => |ref| spv.typeResultId(ref).toRef(),
.ty => |ref| spv.typeResultId(ref),
};
}
};
@ -486,7 +486,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
section.instructions.items[first_word] |= @as(u32, @intCast(u16, actual_word_count)) << 16 | @enumToInt(self.inst.opcode);
if (maybe_result_id) |result| {
return AsmValue{ .value = result.toRef() };
return AsmValue{ .value = result };
}
return null;
}

View File

@ -196,7 +196,7 @@ pub fn resolveSourceFileName(self: *Module, decl: *ZigDecl) !IdRef {
const result = try self.source_file_names.getOrPut(self.gpa, path);
if (!result.found_existing) {
const file_result_id = self.allocId();
result.value_ptr.* = file_result_id.toRef();
result.value_ptr.* = file_result_id;
try self.sections.debug_strings.emit(self.gpa, .OpString, .{
.id_result = file_result_id,
.string = path,
@ -205,7 +205,7 @@ pub fn resolveSourceFileName(self: *Module, decl: *ZigDecl) !IdRef {
try self.sections.debug_strings.emit(self.gpa, .OpSource, .{
.source_language = .Unknown, // TODO: Register Zig source language.
.version = 0, // TODO: Zig version as u32?
.file = file_result_id.toRef(),
.file = file_result_id,
.source = null, // TODO: Store actual source also?
});
}
@ -239,7 +239,7 @@ pub fn typeResultId(self: Module, type_ref: Type.Ref) IdResultType {
/// Get the result-id of a particular type as IdRef, by Type.Ref. Asserts type_ref is valid.
pub fn typeRefId(self: Module, type_ref: Type.Ref) IdRef {
return self.type_cache.values()[@enumToInt(type_ref)].toRef();
return self.type_cache.values()[@enumToInt(type_ref)];
}
/// Unconditionally emit a spir-v type into the appropriate section.
@ -250,7 +250,7 @@ pub fn typeRefId(self: Module, type_ref: Type.Ref) IdRef {
/// be emitted at this point.
pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
const result_id = self.allocId();
const ref_id = result_id.toRef();
const ref_id = result_id;
const types = &self.sections.types_globals_constants;
const debug_names = &self.sections.debug_names;
const annotations = &self.sections.annotations;
@ -260,14 +260,14 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
.void => {
try types.emit(self.gpa, .OpTypeVoid, result_id_operand);
try debug_names.emit(self.gpa, .OpName, .{
.target = result_id.toRef(),
.target = result_id,
.name = "void",
});
},
.bool => {
try types.emit(self.gpa, .OpTypeBool, result_id_operand);
try debug_names.emit(self.gpa, .OpName, .{
.target = result_id.toRef(),
.target = result_id,
.name = "bool",
});
},
@ -302,7 +302,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
defer self.gpa.free(name);
try debug_names.emit(self.gpa, .OpName, .{
.target = result_id.toRef(),
.target = result_id,
.name = name,
});
},
@ -316,25 +316,25 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
const name = try std.fmt.allocPrint(self.gpa, "f{}", .{bits});
defer self.gpa.free(name);
try debug_names.emit(self.gpa, .OpName, .{
.target = result_id.toRef(),
.target = result_id,
.name = name,
});
},
.vector => try types.emit(self.gpa, .OpTypeVector, .{
.id_result = result_id,
.component_type = self.typeResultId(ty.childType()).toRef(),
.component_type = self.typeResultId(ty.childType()),
.component_count = ty.payload(.vector).component_count,
}),
.matrix => try types.emit(self.gpa, .OpTypeMatrix, .{
.id_result = result_id,
.column_type = self.typeResultId(ty.childType()).toRef(),
.column_type = self.typeResultId(ty.childType()),
.column_count = ty.payload(.matrix).column_count,
}),
.image => {
const info = ty.payload(.image);
try types.emit(self.gpa, .OpTypeImage, .{
.id_result = result_id,
.sampled_type = self.typeResultId(ty.childType()).toRef(),
.sampled_type = self.typeResultId(ty.childType()),
.dim = info.dim,
.depth = @enumToInt(info.depth),
.arrayed = @boolToInt(info.arrayed),
@ -347,7 +347,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
.sampler => try types.emit(self.gpa, .OpTypeSampler, result_id_operand),
.sampled_image => try types.emit(self.gpa, .OpTypeSampledImage, .{
.id_result = result_id,
.image_type = self.typeResultId(ty.childType()).toRef(),
.image_type = self.typeResultId(ty.childType()),
}),
.array => {
const info = ty.payload(.array);
@ -365,8 +365,8 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
try types.emit(self.gpa, .OpTypeArray, .{
.id_result = result_id,
.element_type = self.typeResultId(ty.childType()).toRef(),
.length = length_id.toRef(),
.element_type = self.typeResultId(ty.childType()),
.length = length_id,
});
if (info.array_stride != 0) {
try annotations.decorate(self.gpa, ref_id, .{ .ArrayStride = .{ .array_stride = info.array_stride } });
@ -376,7 +376,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
const info = ty.payload(.runtime_array);
try types.emit(self.gpa, .OpTypeRuntimeArray, .{
.id_result = result_id,
.element_type = self.typeResultId(ty.childType()).toRef(),
.element_type = self.typeResultId(ty.childType()),
});
if (info.array_stride != 0) {
try annotations.decorate(self.gpa, ref_id, .{ .ArrayStride = .{ .array_stride = info.array_stride } });
@ -387,7 +387,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
try types.emitRaw(self.gpa, .OpTypeStruct, 1 + info.members.len);
types.writeOperand(IdResult, result_id);
for (info.members) |member| {
types.writeOperand(IdRef, self.typeResultId(member.ty).toRef());
types.writeOperand(IdRef, self.typeResultId(member.ty));
}
try self.decorateStruct(ref_id, info);
},
@ -400,7 +400,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
try types.emit(self.gpa, .OpTypePointer, .{
.id_result = result_id,
.storage_class = info.storage_class,
.type = self.typeResultId(ty.childType()).toRef(),
.type = self.typeResultId(ty.childType()),
});
if (info.array_stride != 0) {
try annotations.decorate(self.gpa, ref_id, .{ .ArrayStride = .{ .array_stride = info.array_stride } });
@ -416,9 +416,9 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
const info = ty.payload(.function);
try types.emitRaw(self.gpa, .OpTypeFunction, 2 + info.parameters.len);
types.writeOperand(IdResult, result_id);
types.writeOperand(IdRef, self.typeResultId(info.return_type).toRef());
types.writeOperand(IdRef, self.typeResultId(info.return_type));
for (info.parameters) |parameter_type| {
types.writeOperand(IdRef, self.typeResultId(parameter_type).toRef());
types.writeOperand(IdRef, self.typeResultId(parameter_type));
}
},
.event => try types.emit(self.gpa, .OpTypeEvent, result_id_operand),
@ -433,7 +433,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
.named_barrier => try types.emit(self.gpa, .OpTypeNamedBarrier, result_id_operand),
}
return result_id.toResultType();
return result_id;
}
fn decorateStruct(self: *Module, target: IdRef, info: *const Type.Payload.Struct) !void {

View File

@ -122,7 +122,7 @@ fn writeOperands(section: *Section, comptime Operands: type, operands: Operands)
pub fn writeOperand(section: *Section, comptime Operand: type, operand: Operand) void {
switch (Operand) {
spec.IdResultType, spec.IdResult, spec.IdRef => section.writeWord(operand.id),
spec.IdResult => section.writeWord(operand.id),
spec.LiteralInteger => section.writeWord(operand),
@ -258,9 +258,7 @@ fn operandsSize(comptime Operands: type, operands: Operands) usize {
fn operandSize(comptime Operand: type, operand: Operand) usize {
return switch (Operand) {
spec.IdResultType,
spec.IdResult,
spec.IdRef,
spec.LiteralInteger,
spec.LiteralExtInstInteger,
=> 1,
@ -382,7 +380,9 @@ test "SPIR-V Section emit() - string" {
}, section.instructions.items);
}
test "SPIR-V Section emit()- extended mask" {
test "SPIR-V Section emit() - extended mask" {
if (@import("builtin").zig_backend == .stage1) return error.SkipZigTest;
var section = Section{};
defer section.deinit(std.testing.allocator);

View File

@ -3,22 +3,11 @@
const Version = @import("std").builtin.Version;
pub const Word = u32;
pub const IdResultType = struct {
id: Word,
pub fn toRef(self: IdResultType) IdRef {
return .{ .id = self.id };
}
};
pub const IdResult = struct {
id: Word,
pub fn toRef(self: IdResult) IdRef {
return .{ .id = self.id };
}
pub fn toResultType(self: IdResult) IdResultType {
return .{ .id = self.id };
}
};
pub const IdRef = struct { id: Word };
pub const IdResultType = IdResult;
pub const IdRef = IdResult;
pub const IdMemorySemantics = IdRef;
pub const IdScope = IdRef;

View File

@ -80,22 +80,11 @@ fn render(writer: anytype, allocator: Allocator, registry: g.CoreRegistry) !void
\\const Version = @import("std").builtin.Version;
\\
\\pub const Word = u32;
\\pub const IdResultType = struct{
\\ id: Word,
\\ pub fn toRef(self: IdResultType) IdRef {
\\ return .{.id = self.id};
\\ }
\\};
\\pub const IdResult = struct{
\\ id: Word,
\\ pub fn toRef(self: IdResult) IdRef {
\\ return .{.id = self.id};
\\ }
\\ pub fn toResultType(self: IdResult) IdResultType {
\\ return .{.id = self.id};
\\ }
\\};
\\pub const IdRef = struct{ id: Word };
\\pub const IdResultType = IdResult;
\\pub const IdRef = IdResult;
\\
\\pub const IdMemorySemantics = IdRef;
\\pub const IdScope = IdRef;