authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-11-28 19:14:16+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:51:51+02:00
log700dee34d5c7bdedc678032689c761e3a417fa03
tree3ddf95feb67fe929ab3f9ebd1fc716699af226f5
parent0c2526b18ec21d8155b070022d3b3b9069303744
signaturelock-open Commit is signed but in an unrecognized format.

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.

6 files changed, 73 insertions(+), 95 deletions(-)

src/codegen/spirv.zig+43-43
...@@ -227,7 +227,7 @@ pub const DeclGen = struct {...@@ -227,7 +227,7 @@ pub const DeclGen = struct {
227 /// keep track of the previous block.227 /// keep track of the previous block.
228 fn beginSpvBlock(self: *DeclGen, label_id: IdResult) !void {228 fn beginSpvBlock(self: *DeclGen, label_id: IdResult) !void {
229 try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label_id });229 try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label_id });
230 self.current_block_label_id = label_id.toRef();230 self.current_block_label_id = label_id;
231 }231 }
232232
233 /// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need233 /// 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 {...@@ -340,7 +340,7 @@ pub const DeclGen = struct {
340 };340 };
341 const decl = self.module.declPtr(fn_decl_index);341 const decl = self.module.declPtr(fn_decl_index);
342 self.module.markDeclAlive(decl);342 self.module.markDeclAlive(decl);
343 return self.ids.get(fn_decl_index).?.toRef();343 return self.ids.get(fn_decl_index).?;
344 }344 }
345345
346 const target = self.getTarget();346 const target = self.getTarget();
...@@ -350,7 +350,7 @@ pub const DeclGen = struct {...@@ -350,7 +350,7 @@ pub const DeclGen = struct {
350350
351 if (val.isUndef()) {351 if (val.isUndef()) {
352 try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_type_id, .id_result = result_id });352 try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_type_id, .id_result = result_id });
353 return result_id.toRef();353 return result_id;
354 }354 }
355355
356 switch (ty.zigTypeTag()) {356 switch (ty.zigTypeTag()) {
...@@ -538,7 +538,7 @@ pub const DeclGen = struct {...@@ -538,7 +538,7 @@ pub const DeclGen = struct {
538 else => return self.todo("constant generation of type {s}: {}", .{ @tagName(ty.zigTypeTag()), ty.fmtDebug() }),538 else => return self.todo("constant generation of type {s}: {}", .{ @tagName(ty.zigTypeTag()), ty.fmtDebug() }),
539 }539 }
540540
541 return result_id.toRef();541 return result_id;
542 }542 }
543543
544 /// Turn a Zig type into a SPIR-V Type, and return its type result-id.544 /// Turn a Zig type into a SPIR-V Type, and return its type result-id.
...@@ -766,7 +766,7 @@ pub const DeclGen = struct {...@@ -766,7 +766,7 @@ pub const DeclGen = struct {
766 .id_result_type = try self.resolveTypeId(decl.ty.fnReturnType()),766 .id_result_type = try self.resolveTypeId(decl.ty.fnReturnType()),
767 .id_result = result_id,767 .id_result = result_id,
768 .function_control = .{}, // TODO: We can set inline here if the type requires it.768 .function_control = .{}, // TODO: We can set inline here if the type requires it.
769 .function_type = prototype_id.toRef(),769 .function_type = prototype_id,
770 });770 });
771771
772 const params = decl.ty.fnParamLen();772 const params = decl.ty.fnParamLen();
...@@ -780,7 +780,7 @@ pub const DeclGen = struct {...@@ -780,7 +780,7 @@ pub const DeclGen = struct {
780 .id_result_type = param_type_id,780 .id_result_type = param_type_id,
781 .id_result = arg_result_id,781 .id_result = arg_result_id,
782 });782 });
783 self.args.appendAssumeCapacity(arg_result_id.toRef());783 self.args.appendAssumeCapacity(arg_result_id);
784 }784 }
785785
786 // TODO: This could probably be done in a better way...786 // TODO: This could probably be done in a better way...
...@@ -791,7 +791,7 @@ pub const DeclGen = struct {...@@ -791,7 +791,7 @@ pub const DeclGen = struct {
791 try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{791 try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{
792 .id_result = root_block_id,792 .id_result = root_block_id,
793 });793 });
794 self.current_block_label_id = root_block_id.toRef();794 self.current_block_label_id = root_block_id;
795795
796 const main_body = self.air.getMainBody();796 const main_body = self.air.getMainBody();
797 try self.genBody(main_body);797 try self.genBody(main_body);
...@@ -804,7 +804,7 @@ pub const DeclGen = struct {...@@ -804,7 +804,7 @@ pub const DeclGen = struct {
804 defer self.module.gpa.free(fqn);804 defer self.module.gpa.free(fqn);
805805
806 try self.spv.sections.debug_names.emit(self.gpa, .OpName, .{806 try self.spv.sections.debug_names.emit(self.gpa, .OpName, .{
807 .target = result_id.toRef(),807 .target = result_id,
808 .name = fqn,808 .name = fqn,
809 });809 });
810 } else {810 } else {
...@@ -928,7 +928,7 @@ pub const DeclGen = struct {...@@ -928,7 +928,7 @@ pub const DeclGen = struct {
928 .operand_1 = lhs_id,928 .operand_1 = lhs_id,
929 .operand_2 = rhs_id,929 .operand_2 = rhs_id,
930 });930 });
931 return result_id.toRef();931 return result_id;
932 }932 }
933933
934 fn airShift(self: *DeclGen, inst: Air.Inst.Index, comptime opcode: Opcode) !?IdRef {934 fn airShift(self: *DeclGen, inst: Air.Inst.Index, comptime opcode: Opcode) !?IdRef {
...@@ -951,9 +951,9 @@ pub const DeclGen = struct {...@@ -951,9 +951,9 @@ pub const DeclGen = struct {
951 .id_result_type = result_type_id,951 .id_result_type = result_type_id,
952 .id_result = result_id,952 .id_result = result_id,
953 .base = lhs_id,953 .base = lhs_id,
954 .shift = shift_id.toRef(),954 .shift = shift_id,
955 });955 });
956 return result_id.toRef();956 return result_id;
957 }957 }
958958
959 fn maskStrangeInt(self: *DeclGen, ty_id: IdResultType, int_id: IdRef, bits: u16) !IdRef {959 fn maskStrangeInt(self: *DeclGen, ty_id: IdResultType, int_id: IdRef, bits: u16) !IdRef {
...@@ -976,9 +976,9 @@ pub const DeclGen = struct {...@@ -976,9 +976,9 @@ pub const DeclGen = struct {
976 .id_result_type = ty_id,976 .id_result_type = ty_id,
977 .id_result = result_id,977 .id_result = result_id,
978 .operand_1 = int_id,978 .operand_1 = int_id,
979 .operand_2 = mask_id.toRef(),979 .operand_2 = mask_id,
980 });980 });
981 return result_id.toRef();981 return result_id;
982 }982 }
983983
984 fn airArithOp(984 fn airArithOp(
...@@ -1046,7 +1046,7 @@ pub const DeclGen = struct {...@@ -1046,7 +1046,7 @@ pub const DeclGen = struct {
1046 // TODO: Trap on overflow? Probably going to be annoying.1046 // TODO: Trap on overflow? Probably going to be annoying.
1047 // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap.1047 // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap.
10481048
1049 return result_id.toRef();1049 return result_id;
1050 }1050 }
10511051
1052 fn airOverflowArithOp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1052 fn airOverflowArithOp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1089,7 +1089,7 @@ pub const DeclGen = struct {...@@ -1089,7 +1089,7 @@ pub const DeclGen = struct {
1089 .operand_1 = lhs,1089 .operand_1 = lhs,
1090 .operand_2 = rhs,1090 .operand_2 = rhs,
1091 });1091 });
1092 break :blk result_id.toRef();1092 break :blk result_id;
1093 };1093 };
10941094
1095 // Now convert the SPIR-V flavor result into a Zig-flavor result.1095 // Now convert the SPIR-V flavor result into a Zig-flavor result.
...@@ -1111,7 +1111,7 @@ pub const DeclGen = struct {...@@ -1111,7 +1111,7 @@ pub const DeclGen = struct {
1111 .id_result = result_id,1111 .id_result = result_id,
1112 .unsigned_value = overflow,1112 .unsigned_value = overflow,
1113 });1113 });
1114 break :blk result_id.toRef();1114 break :blk result_id;
1115 };1115 };
11161116
1117 // TODO: If copying this function for borrow, make sure to convert -1 to 1 as appropriate.1117 // TODO: If copying this function for borrow, make sure to convert -1 to 1 as appropriate.
...@@ -1127,7 +1127,7 @@ pub const DeclGen = struct {...@@ -1127,7 +1127,7 @@ pub const DeclGen = struct {
1127 casted_overflow,1127 casted_overflow,
1128 },1128 },
1129 });1129 });
1130 return result_id.toRef();1130 return result_id;
1131 }1131 }
11321132
1133 fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1133 fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1163,7 +1163,7 @@ pub const DeclGen = struct {...@@ -1163,7 +1163,7 @@ pub const DeclGen = struct {
1163 self.func.body.writeOperand(spec.LiteralInteger, unsigned);1163 self.func.body.writeOperand(spec.LiteralInteger, unsigned);
1164 }1164 }
1165 }1165 }
1166 return result_id.toRef();1166 return result_id;
1167 }1167 }
11681168
1169 fn airCmp(self: *DeclGen, inst: Air.Inst.Index, comptime fop: Opcode, comptime sop: Opcode, comptime uop: Opcode) !?IdRef {1169 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 {...@@ -1215,7 +1215,7 @@ pub const DeclGen = struct {
1215 else => unreachable,1215 else => unreachable,
1216 }1216 }
12171217
1218 return result_id.toRef();1218 return result_id;
1219 }1219 }
12201220
1221 fn bitcast(self: *DeclGen, target_type_id: IdResultType, value_id: IdRef) !IdRef {1221 fn bitcast(self: *DeclGen, target_type_id: IdResultType, value_id: IdRef) !IdRef {
...@@ -1225,7 +1225,7 @@ pub const DeclGen = struct {...@@ -1225,7 +1225,7 @@ pub const DeclGen = struct {
1225 .id_result = result_id,1225 .id_result = result_id,
1226 .operand = value_id,1226 .operand = value_id,
1227 });1227 });
1228 return result_id.toRef();1228 return result_id;
1229 }1229 }
12301230
1231 fn airBitcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1231 fn airBitcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1258,7 +1258,7 @@ pub const DeclGen = struct {...@@ -1258,7 +1258,7 @@ pub const DeclGen = struct {
1258 .unsigned_value = operand_id,1258 .unsigned_value = operand_id,
1259 }),1259 }),
1260 }1260 }
1261 return result_id.toRef();1261 return result_id;
1262 }1262 }
12631263
1264 fn airNot(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1264 fn airNot(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1272,7 +1272,7 @@ pub const DeclGen = struct {...@@ -1272,7 +1272,7 @@ pub const DeclGen = struct {
1272 .id_result = result_id,1272 .id_result = result_id,
1273 .operand = operand_id,1273 .operand = operand_id,
1274 });1274 });
1275 return result_id.toRef();1275 return result_id;
1276 }1276 }
12771277
1278 fn extractField(self: *DeclGen, result_ty: IdResultType, object: IdRef, field: u32) !IdRef {1278 fn extractField(self: *DeclGen, result_ty: IdResultType, object: IdRef, field: u32) !IdRef {
...@@ -1283,7 +1283,7 @@ pub const DeclGen = struct {...@@ -1283,7 +1283,7 @@ pub const DeclGen = struct {
1283 .composite = object,1283 .composite = object,
1284 .indexes = &.{field},1284 .indexes = &.{field},
1285 });1285 });
1286 return result_id.toRef();1286 return result_id;
1287 }1287 }
12881288
1289 fn airSliceField(self: *DeclGen, inst: Air.Inst.Index, field: u32) !?IdRef {1289 fn airSliceField(self: *DeclGen, inst: Air.Inst.Index, field: u32) !?IdRef {
...@@ -1314,7 +1314,7 @@ pub const DeclGen = struct {...@@ -1314,7 +1314,7 @@ pub const DeclGen = struct {
1314 .composite = slice,1314 .composite = slice,
1315 .indexes = &.{0},1315 .indexes = &.{0},
1316 });1316 });
1317 break :blk result_id.toRef();1317 break :blk result_id;
1318 };1318 };
13191319
1320 const result_id = self.spv.allocId();1320 const result_id = self.spv.allocId();
...@@ -1324,7 +1324,7 @@ pub const DeclGen = struct {...@@ -1324,7 +1324,7 @@ pub const DeclGen = struct {
1324 .base = slice_ptr,1324 .base = slice_ptr,
1325 .indexes = &.{index},1325 .indexes = &.{index},
1326 });1326 });
1327 return result_id.toRef();1327 return result_id;
1328 }1328 }
13291329
1330 fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1330 fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1347,7 +1347,7 @@ pub const DeclGen = struct {...@@ -1347,7 +1347,7 @@ pub const DeclGen = struct {
1347 .composite = slice,1347 .composite = slice,
1348 .indexes = &.{0},1348 .indexes = &.{0},
1349 });1349 });
1350 break :blk result_id.toRef();1350 break :blk result_id;
1351 };1351 };
13521352
1353 const elem_ptr = blk: {1353 const elem_ptr = blk: {
...@@ -1358,7 +1358,7 @@ pub const DeclGen = struct {...@@ -1358,7 +1358,7 @@ pub const DeclGen = struct {
1358 .base = slice_ptr,1358 .base = slice_ptr,
1359 .indexes = &.{index},1359 .indexes = &.{index},
1360 });1360 });
1361 break :blk result_id.toRef();1361 break :blk result_id;
1362 };1362 };
13631363
1364 const result_id = self.spv.allocId();1364 const result_id = self.spv.allocId();
...@@ -1367,7 +1367,7 @@ pub const DeclGen = struct {...@@ -1367,7 +1367,7 @@ pub const DeclGen = struct {
1367 .id_result = result_id,1367 .id_result = result_id,
1368 .pointer = elem_ptr,1368 .pointer = elem_ptr,
1369 });1369 });
1370 return result_id.toRef();1370 return result_id;
1371 }1371 }
13721372
1373 fn airPtrElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1373 fn airPtrElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1392,7 +1392,7 @@ pub const DeclGen = struct {...@@ -1392,7 +1392,7 @@ pub const DeclGen = struct {
1392 .base = base_ptr,1392 .base = base_ptr,
1393 .indexes = &.{rhs},1393 .indexes = &.{rhs},
1394 });1394 });
1395 return result_id.toRef();1395 return result_id;
1396 }1396 }
13971397
1398 fn airStructFieldVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {1398 fn airStructFieldVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
...@@ -1418,7 +1418,7 @@ pub const DeclGen = struct {...@@ -1418,7 +1418,7 @@ pub const DeclGen = struct {
1418 .composite = object,1418 .composite = object,
1419 .indexes = &.{field_index},1419 .indexes = &.{field_index},
1420 });1420 });
1421 return result_id.toRef();1421 return result_id;
1422 }1422 }
14231423
1424 fn structFieldPtr(1424 fn structFieldPtr(
...@@ -1446,9 +1446,9 @@ pub const DeclGen = struct {...@@ -1446,9 +1446,9 @@ pub const DeclGen = struct {
1446 .id_result_type = result_type_id,1446 .id_result_type = result_type_id,
1447 .id_result = result_id,1447 .id_result = result_id,
1448 .base = object_ptr,1448 .base = object_ptr,
1449 .indexes = &.{field_index_id.toRef()},1449 .indexes = &.{field_index_id},
1450 });1450 });
1451 return result_id.toRef();1451 return result_id;
1452 },1452 },
1453 },1453 },
1454 else => unreachable, // TODO1454 else => unreachable, // TODO
...@@ -1483,7 +1483,7 @@ pub const DeclGen = struct {...@@ -1483,7 +1483,7 @@ pub const DeclGen = struct {
1483 .id_result = result_id,1483 .id_result = result_id,
1484 .storage_class = storage_class,1484 .storage_class = storage_class,
1485 });1485 });
1486 return result_id.toRef();1486 return result_id;
1487 }1487 }
14881488
1489 fn airArg(self: *DeclGen) IdRef {1489 fn airArg(self: *DeclGen) IdRef {
...@@ -1503,7 +1503,7 @@ pub const DeclGen = struct {...@@ -1503,7 +1503,7 @@ pub const DeclGen = struct {
1503 var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.gpa, 4);1503 var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.gpa, 4);
15041504
1505 try self.blocks.putNoClobber(self.gpa, inst, .{1505 try self.blocks.putNoClobber(self.gpa, inst, .{
1506 .label_id = label_id.toRef(),1506 .label_id = label_id,
1507 .incoming_blocks = &incoming_blocks,1507 .incoming_blocks = &incoming_blocks,
1508 });1508 });
1509 defer {1509 defer {
...@@ -1538,7 +1538,7 @@ pub const DeclGen = struct {...@@ -1538,7 +1538,7 @@ pub const DeclGen = struct {
1538 self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming.break_value_id, incoming.src_label_id });1538 self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming.break_value_id, incoming.src_label_id });
1539 }1539 }
15401540
1541 return result_id.toRef();1541 return result_id;
1542 }1542 }
15431543
1544 fn airBr(self: *DeclGen, inst: Air.Inst.Index) !void {1544 fn airBr(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -1571,8 +1571,8 @@ pub const DeclGen = struct {...@@ -1571,8 +1571,8 @@ pub const DeclGen = struct {
15711571
1572 try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{1572 try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{
1573 .condition = condition_id,1573 .condition = condition_id,
1574 .true_label = then_label_id.toRef(),1574 .true_label = then_label_id,
1575 .false_label = else_label_id.toRef(),1575 .false_label = else_label_id,
1576 });1576 });
15771577
1578 try self.beginSpvBlock(then_label_id);1578 try self.beginSpvBlock(then_label_id);
...@@ -1610,7 +1610,7 @@ pub const DeclGen = struct {...@@ -1610,7 +1610,7 @@ pub const DeclGen = struct {
1610 .memory_access = access,1610 .memory_access = access,
1611 });1611 });
16121612
1613 return result_id.toRef();1613 return result_id;
1614 }1614 }
16151615
1616 fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void {1616 fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -1620,13 +1620,13 @@ pub const DeclGen = struct {...@@ -1620,13 +1620,13 @@ pub const DeclGen = struct {
1620 const loop_label_id = self.spv.allocId();1620 const loop_label_id = self.spv.allocId();
16211621
1622 // Jump to the loop entry point1622 // Jump to the loop entry point
1623 try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id.toRef() });1623 try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id });
16241624
1625 // TODO: Look into OpLoopMerge.1625 // TODO: Look into OpLoopMerge.
1626 try self.beginSpvBlock(loop_label_id);1626 try self.beginSpvBlock(loop_label_id);
1627 try self.genBody(body);1627 try self.genBody(body);
16281628
1629 try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id.toRef() });1629 try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id });
1630 }1630 }
16311631
1632 fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void {1632 fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -1658,7 +1658,7 @@ pub const DeclGen = struct {...@@ -1658,7 +1658,7 @@ pub const DeclGen = struct {
1658 .id_result = result_id,1658 .id_result = result_id,
1659 .pointer = ptr,1659 .pointer = ptr,
1660 });1660 });
1661 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = result_id.toRef() });1661 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = result_id });
1662 }1662 }
16631663
1664 fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void {1664 fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void {
...@@ -1730,7 +1730,7 @@ pub const DeclGen = struct {...@@ -1730,7 +1730,7 @@ pub const DeclGen = struct {
1730 // Emit the instruction before generating the blocks.1730 // Emit the instruction before generating the blocks.
1731 try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions);1731 try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions);
1732 self.func.body.writeOperand(IdRef, cond);1732 self.func.body.writeOperand(IdRef, cond);
1733 self.func.body.writeOperand(IdRef, default.toRef());1733 self.func.body.writeOperand(IdRef, default);
17341734
1735 // Emit each of the cases1735 // Emit each of the cases
1736 {1736 {
...@@ -1965,6 +1965,6 @@ pub const DeclGen = struct {...@@ -1965,6 +1965,6 @@ pub const DeclGen = struct {
1965 return null;1965 return null;
1966 }1966 }
19671967
1968 return result_id.toRef();1968 return result_id;
1969 }1969 }
1970};1970};
src/codegen/spirv/Assembler.zig+2-2
...@@ -135,7 +135,7 @@ const AsmValue = union(enum) {...@@ -135,7 +135,7 @@ const AsmValue = union(enum) {
135 return switch (self) {135 return switch (self) {
136 .just_declared, .unresolved_forward_reference => unreachable,136 .just_declared, .unresolved_forward_reference => unreachable,
137 .value => |result| result,137 .value => |result| result,
138 .ty => |ref| spv.typeResultId(ref).toRef(),138 .ty => |ref| spv.typeResultId(ref),
139 };139 };
140 }140 }
141};141};
...@@ -486,7 +486,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {...@@ -486,7 +486,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
486 section.instructions.items[first_word] |= @as(u32, @intCast(u16, actual_word_count)) << 16 | @enumToInt(self.inst.opcode);486 section.instructions.items[first_word] |= @as(u32, @intCast(u16, actual_word_count)) << 16 | @enumToInt(self.inst.opcode);
487487
488 if (maybe_result_id) |result| {488 if (maybe_result_id) |result| {
489 return AsmValue{ .value = result.toRef() };489 return AsmValue{ .value = result };
490 }490 }
491 return null;491 return null;
492}492}
src/codegen/spirv/Module.zig+20-20
...@@ -196,7 +196,7 @@ pub fn resolveSourceFileName(self: *Module, decl: *ZigDecl) !IdRef {...@@ -196,7 +196,7 @@ pub fn resolveSourceFileName(self: *Module, decl: *ZigDecl) !IdRef {
196 const result = try self.source_file_names.getOrPut(self.gpa, path);196 const result = try self.source_file_names.getOrPut(self.gpa, path);
197 if (!result.found_existing) {197 if (!result.found_existing) {
198 const file_result_id = self.allocId();198 const file_result_id = self.allocId();
199 result.value_ptr.* = file_result_id.toRef();199 result.value_ptr.* = file_result_id;
200 try self.sections.debug_strings.emit(self.gpa, .OpString, .{200 try self.sections.debug_strings.emit(self.gpa, .OpString, .{
201 .id_result = file_result_id,201 .id_result = file_result_id,
202 .string = path,202 .string = path,
...@@ -205,7 +205,7 @@ pub fn resolveSourceFileName(self: *Module, decl: *ZigDecl) !IdRef {...@@ -205,7 +205,7 @@ pub fn resolveSourceFileName(self: *Module, decl: *ZigDecl) !IdRef {
205 try self.sections.debug_strings.emit(self.gpa, .OpSource, .{205 try self.sections.debug_strings.emit(self.gpa, .OpSource, .{
206 .source_language = .Unknown, // TODO: Register Zig source language.206 .source_language = .Unknown, // TODO: Register Zig source language.
207 .version = 0, // TODO: Zig version as u32?207 .version = 0, // TODO: Zig version as u32?
208 .file = file_result_id.toRef(),208 .file = file_result_id,
209 .source = null, // TODO: Store actual source also?209 .source = null, // TODO: Store actual source also?
210 });210 });
211 }211 }
...@@ -239,7 +239,7 @@ pub fn typeResultId(self: Module, type_ref: Type.Ref) IdResultType {...@@ -239,7 +239,7 @@ pub fn typeResultId(self: Module, type_ref: Type.Ref) IdResultType {
239239
240/// Get the result-id of a particular type as IdRef, by Type.Ref. Asserts type_ref is valid.240/// Get the result-id of a particular type as IdRef, by Type.Ref. Asserts type_ref is valid.
241pub fn typeRefId(self: Module, type_ref: Type.Ref) IdRef {241pub fn typeRefId(self: Module, type_ref: Type.Ref) IdRef {
242 return self.type_cache.values()[@enumToInt(type_ref)].toRef();242 return self.type_cache.values()[@enumToInt(type_ref)];
243}243}
244244
245/// Unconditionally emit a spir-v type into the appropriate section.245/// Unconditionally emit a spir-v type into the appropriate section.
...@@ -250,7 +250,7 @@ pub fn typeRefId(self: Module, type_ref: Type.Ref) IdRef {...@@ -250,7 +250,7 @@ pub fn typeRefId(self: Module, type_ref: Type.Ref) IdRef {
250/// be emitted at this point.250/// be emitted at this point.
251pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {251pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
252 const result_id = self.allocId();252 const result_id = self.allocId();
253 const ref_id = result_id.toRef();253 const ref_id = result_id;
254 const types = &self.sections.types_globals_constants;254 const types = &self.sections.types_globals_constants;
255 const debug_names = &self.sections.debug_names;255 const debug_names = &self.sections.debug_names;
256 const annotations = &self.sections.annotations;256 const annotations = &self.sections.annotations;
...@@ -260,14 +260,14 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -260,14 +260,14 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
260 .void => {260 .void => {
261 try types.emit(self.gpa, .OpTypeVoid, result_id_operand);261 try types.emit(self.gpa, .OpTypeVoid, result_id_operand);
262 try debug_names.emit(self.gpa, .OpName, .{262 try debug_names.emit(self.gpa, .OpName, .{
263 .target = result_id.toRef(),263 .target = result_id,
264 .name = "void",264 .name = "void",
265 });265 });
266 },266 },
267 .bool => {267 .bool => {
268 try types.emit(self.gpa, .OpTypeBool, result_id_operand);268 try types.emit(self.gpa, .OpTypeBool, result_id_operand);
269 try debug_names.emit(self.gpa, .OpName, .{269 try debug_names.emit(self.gpa, .OpName, .{
270 .target = result_id.toRef(),270 .target = result_id,
271 .name = "bool",271 .name = "bool",
272 });272 });
273 },273 },
...@@ -302,7 +302,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -302,7 +302,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
302 defer self.gpa.free(name);302 defer self.gpa.free(name);
303303
304 try debug_names.emit(self.gpa, .OpName, .{304 try debug_names.emit(self.gpa, .OpName, .{
305 .target = result_id.toRef(),305 .target = result_id,
306 .name = name,306 .name = name,
307 });307 });
308 },308 },
...@@ -316,25 +316,25 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -316,25 +316,25 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
316 const name = try std.fmt.allocPrint(self.gpa, "f{}", .{bits});316 const name = try std.fmt.allocPrint(self.gpa, "f{}", .{bits});
317 defer self.gpa.free(name);317 defer self.gpa.free(name);
318 try debug_names.emit(self.gpa, .OpName, .{318 try debug_names.emit(self.gpa, .OpName, .{
319 .target = result_id.toRef(),319 .target = result_id,
320 .name = name,320 .name = name,
321 });321 });
322 },322 },
323 .vector => try types.emit(self.gpa, .OpTypeVector, .{323 .vector => try types.emit(self.gpa, .OpTypeVector, .{
324 .id_result = result_id,324 .id_result = result_id,
325 .component_type = self.typeResultId(ty.childType()).toRef(),325 .component_type = self.typeResultId(ty.childType()),
326 .component_count = ty.payload(.vector).component_count,326 .component_count = ty.payload(.vector).component_count,
327 }),327 }),
328 .matrix => try types.emit(self.gpa, .OpTypeMatrix, .{328 .matrix => try types.emit(self.gpa, .OpTypeMatrix, .{
329 .id_result = result_id,329 .id_result = result_id,
330 .column_type = self.typeResultId(ty.childType()).toRef(),330 .column_type = self.typeResultId(ty.childType()),
331 .column_count = ty.payload(.matrix).column_count,331 .column_count = ty.payload(.matrix).column_count,
332 }),332 }),
333 .image => {333 .image => {
334 const info = ty.payload(.image);334 const info = ty.payload(.image);
335 try types.emit(self.gpa, .OpTypeImage, .{335 try types.emit(self.gpa, .OpTypeImage, .{
336 .id_result = result_id,336 .id_result = result_id,
337 .sampled_type = self.typeResultId(ty.childType()).toRef(),337 .sampled_type = self.typeResultId(ty.childType()),
338 .dim = info.dim,338 .dim = info.dim,
339 .depth = @enumToInt(info.depth),339 .depth = @enumToInt(info.depth),
340 .arrayed = @boolToInt(info.arrayed),340 .arrayed = @boolToInt(info.arrayed),
...@@ -347,7 +347,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -347,7 +347,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
347 .sampler => try types.emit(self.gpa, .OpTypeSampler, result_id_operand),347 .sampler => try types.emit(self.gpa, .OpTypeSampler, result_id_operand),
348 .sampled_image => try types.emit(self.gpa, .OpTypeSampledImage, .{348 .sampled_image => try types.emit(self.gpa, .OpTypeSampledImage, .{
349 .id_result = result_id,349 .id_result = result_id,
350 .image_type = self.typeResultId(ty.childType()).toRef(),350 .image_type = self.typeResultId(ty.childType()),
351 }),351 }),
352 .array => {352 .array => {
353 const info = ty.payload(.array);353 const info = ty.payload(.array);
...@@ -365,8 +365,8 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -365,8 +365,8 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
365365
366 try types.emit(self.gpa, .OpTypeArray, .{366 try types.emit(self.gpa, .OpTypeArray, .{
367 .id_result = result_id,367 .id_result = result_id,
368 .element_type = self.typeResultId(ty.childType()).toRef(),368 .element_type = self.typeResultId(ty.childType()),
369 .length = length_id.toRef(),369 .length = length_id,
370 });370 });
371 if (info.array_stride != 0) {371 if (info.array_stride != 0) {
372 try annotations.decorate(self.gpa, ref_id, .{ .ArrayStride = .{ .array_stride = info.array_stride } });372 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 {...@@ -376,7 +376,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
376 const info = ty.payload(.runtime_array);376 const info = ty.payload(.runtime_array);
377 try types.emit(self.gpa, .OpTypeRuntimeArray, .{377 try types.emit(self.gpa, .OpTypeRuntimeArray, .{
378 .id_result = result_id,378 .id_result = result_id,
379 .element_type = self.typeResultId(ty.childType()).toRef(),379 .element_type = self.typeResultId(ty.childType()),
380 });380 });
381 if (info.array_stride != 0) {381 if (info.array_stride != 0) {
382 try annotations.decorate(self.gpa, ref_id, .{ .ArrayStride = .{ .array_stride = info.array_stride } });382 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 {...@@ -387,7 +387,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
387 try types.emitRaw(self.gpa, .OpTypeStruct, 1 + info.members.len);387 try types.emitRaw(self.gpa, .OpTypeStruct, 1 + info.members.len);
388 types.writeOperand(IdResult, result_id);388 types.writeOperand(IdResult, result_id);
389 for (info.members) |member| {389 for (info.members) |member| {
390 types.writeOperand(IdRef, self.typeResultId(member.ty).toRef());390 types.writeOperand(IdRef, self.typeResultId(member.ty));
391 }391 }
392 try self.decorateStruct(ref_id, info);392 try self.decorateStruct(ref_id, info);
393 },393 },
...@@ -400,7 +400,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -400,7 +400,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
400 try types.emit(self.gpa, .OpTypePointer, .{400 try types.emit(self.gpa, .OpTypePointer, .{
401 .id_result = result_id,401 .id_result = result_id,
402 .storage_class = info.storage_class,402 .storage_class = info.storage_class,
403 .type = self.typeResultId(ty.childType()).toRef(),403 .type = self.typeResultId(ty.childType()),
404 });404 });
405 if (info.array_stride != 0) {405 if (info.array_stride != 0) {
406 try annotations.decorate(self.gpa, ref_id, .{ .ArrayStride = .{ .array_stride = info.array_stride } });406 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 {...@@ -416,9 +416,9 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
416 const info = ty.payload(.function);416 const info = ty.payload(.function);
417 try types.emitRaw(self.gpa, .OpTypeFunction, 2 + info.parameters.len);417 try types.emitRaw(self.gpa, .OpTypeFunction, 2 + info.parameters.len);
418 types.writeOperand(IdResult, result_id);418 types.writeOperand(IdResult, result_id);
419 types.writeOperand(IdRef, self.typeResultId(info.return_type).toRef());419 types.writeOperand(IdRef, self.typeResultId(info.return_type));
420 for (info.parameters) |parameter_type| {420 for (info.parameters) |parameter_type| {
421 types.writeOperand(IdRef, self.typeResultId(parameter_type).toRef());421 types.writeOperand(IdRef, self.typeResultId(parameter_type));
422 }422 }
423 },423 },
424 .event => try types.emit(self.gpa, .OpTypeEvent, result_id_operand),424 .event => try types.emit(self.gpa, .OpTypeEvent, result_id_operand),
...@@ -433,7 +433,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {...@@ -433,7 +433,7 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
433 .named_barrier => try types.emit(self.gpa, .OpTypeNamedBarrier, result_id_operand),433 .named_barrier => try types.emit(self.gpa, .OpTypeNamedBarrier, result_id_operand),
434 }434 }
435435
436 return result_id.toResultType();436 return result_id;
437}437}
438438
439fn decorateStruct(self: *Module, target: IdRef, info: *const Type.Payload.Struct) !void {439fn decorateStruct(self: *Module, target: IdRef, info: *const Type.Payload.Struct) !void {
src/codegen/spirv/Section.zig+4-4
...@@ -122,7 +122,7 @@ fn writeOperands(section: *Section, comptime Operands: type, operands: Operands)...@@ -122,7 +122,7 @@ fn writeOperands(section: *Section, comptime Operands: type, operands: Operands)
122122
123pub fn writeOperand(section: *Section, comptime Operand: type, operand: Operand) void {123pub fn writeOperand(section: *Section, comptime Operand: type, operand: Operand) void {
124 switch (Operand) {124 switch (Operand) {
125 spec.IdResultType, spec.IdResult, spec.IdRef => section.writeWord(operand.id),125 spec.IdResult => section.writeWord(operand.id),
126126
127 spec.LiteralInteger => section.writeWord(operand),127 spec.LiteralInteger => section.writeWord(operand),
128128
...@@ -258,9 +258,7 @@ fn operandsSize(comptime Operands: type, operands: Operands) usize {...@@ -258,9 +258,7 @@ fn operandsSize(comptime Operands: type, operands: Operands) usize {
258258
259fn operandSize(comptime Operand: type, operand: Operand) usize {259fn operandSize(comptime Operand: type, operand: Operand) usize {
260 return switch (Operand) {260 return switch (Operand) {
261 spec.IdResultType,
262 spec.IdResult,261 spec.IdResult,
263 spec.IdRef,
264 spec.LiteralInteger,262 spec.LiteralInteger,
265 spec.LiteralExtInstInteger,263 spec.LiteralExtInstInteger,
266 => 1,264 => 1,
...@@ -382,7 +380,9 @@ test "SPIR-V Section emit() - string" {...@@ -382,7 +380,9 @@ test "SPIR-V Section emit() - string" {
382 }, section.instructions.items);380 }, section.instructions.items);
383}381}
384382
385test "SPIR-V Section emit()- extended mask" {383test "SPIR-V Section emit() - extended mask" {
384 if (@import("builtin").zig_backend == .stage1) return error.SkipZigTest;
385
386 var section = Section{};386 var section = Section{};
387 defer section.deinit(std.testing.allocator);387 defer section.deinit(std.testing.allocator);
388388
src/codegen/spirv/spec.zig+2-13
...@@ -3,22 +3,11 @@...@@ -3,22 +3,11 @@
3const Version = @import("std").builtin.Version;3const Version = @import("std").builtin.Version;
44
5pub const Word = u32;5pub const Word = u32;
6pub const IdResultType = struct {
7 id: Word,
8 pub fn toRef(self: IdResultType) IdRef {
9 return .{ .id = self.id };
10 }
11};
12pub const IdResult = struct {6pub const IdResult = struct {
13 id: Word,7 id: Word,
14 pub fn toRef(self: IdResult) IdRef {
15 return .{ .id = self.id };
16 }
17 pub fn toResultType(self: IdResult) IdResultType {
18 return .{ .id = self.id };
19 }
20};8};
21pub const IdRef = struct { id: Word };9pub const IdResultType = IdResult;
10pub const IdRef = IdResult;
2211
23pub const IdMemorySemantics = IdRef;12pub const IdMemorySemantics = IdRef;
24pub const IdScope = IdRef;13pub const IdScope = IdRef;
tools/gen_spirv_spec.zig+2-13
...@@ -80,22 +80,11 @@ fn render(writer: anytype, allocator: Allocator, registry: g.CoreRegistry) !void...@@ -80,22 +80,11 @@ fn render(writer: anytype, allocator: Allocator, registry: g.CoreRegistry) !void
80 \\const Version = @import("std").builtin.Version;80 \\const Version = @import("std").builtin.Version;
81 \\81 \\
82 \\pub const Word = u32;82 \\pub const Word = u32;
83 \\pub const IdResultType = struct{
84 \\ id: Word,
85 \\ pub fn toRef(self: IdResultType) IdRef {
86 \\ return .{.id = self.id};
87 \\ }
88 \\};
89 \\pub const IdResult = struct{83 \\pub const IdResult = struct{
90 \\ id: Word,84 \\ id: Word,
91 \\ pub fn toRef(self: IdResult) IdRef {
92 \\ return .{.id = self.id};
93 \\ }
94 \\ pub fn toResultType(self: IdResult) IdResultType {
95 \\ return .{.id = self.id};
96 \\ }
97 \\};85 \\};
98 \\pub const IdRef = struct{ id: Word };86 \\pub const IdResultType = IdResult;
87 \\pub const IdRef = IdResult;
99 \\88 \\
100 \\pub const IdMemorySemantics = IdRef;89 \\pub const IdMemorySemantics = IdRef;
101 \\pub const IdScope = IdRef;90 \\pub const IdScope = IdRef;