authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-12-11 14:08:51+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:51:52+02:00
log0c53fea42cea8543733612c7c63a6982ca6e59e6
treed63537923ecf406c1895d7fcd2686f1afdeb1bd1
parent12a3a8b100b7af4110cdc30de4def7d8f8d8c148
signaturelock-open Commit is signed but in an unrecognized format.

spirv: improve genConstant usage

This little wrapper function allocates a result-id for us, so that we don't have to do that.

1 files changed, 28 insertions(+), 37 deletions(-)

src/codegen/spirv.zig+28-37
......@@ -238,9 +238,7 @@ pub const DeclGen = struct {
238238 return try self.resolveDecl(fn_decl_index);
239239 }
240240
241 const result_id = self.spv.allocId();
242 try self.genConstant(result_id, ty, val, .direct);
243 return result_id;
241 return try self.genConstant(ty, val, .direct);
244242 }
245243 const index = Air.refToIndex(inst).?;
246244 return self.inst_results.get(index).?; // Assertion means instruction does not dominate usage.
......@@ -396,9 +394,15 @@ pub const DeclGen = struct {
396394 return result_id;
397395 }
398396
397 fn genConstant(self: *DeclGen, ty: Type, val: Value, repr: Repr) Error!IdRef {
398 const result_id = self.spv.allocId();
399 try self.genConstantForId(result_id, ty, val, repr);
400 return result_id;
401 }
402
399403 /// Generate a constant representing `val`.
400404 /// TODO: Deduplication?
401 fn genConstant(self: *DeclGen, result_id: IdRef, ty: Type, val: Value, repr: Repr) Error!void {
405 fn genConstantForId(self: *DeclGen, result_id: IdRef, ty: Type, val: Value, repr: Repr) Error!void {
402406 const target = self.getTarget();
403407 const section = &self.spv.sections.types_globals_constants;
404408 const result_ty_ref = try self.resolveType(ty, repr);
......@@ -449,8 +453,7 @@ pub const DeclGen = struct {
449453 const constituents = try self.spv.gpa.alloc(IdRef, len);
450454 defer self.spv.gpa.free(constituents);
451455 for (elem_vals[0..len], 0..) |elem_val, i| {
452 constituents[i] = self.spv.allocId();
453 try self.genConstant(constituents[i], elem_ty, elem_val, repr);
456 constituents[i] = try self.genConstant(elem_ty, elem_val, repr);
454457 }
455458 try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{
456459 .id_result_type = result_ty_id,
......@@ -466,14 +469,12 @@ pub const DeclGen = struct {
466469 const constituents = try self.spv.gpa.alloc(IdRef, total_len);
467470 defer self.spv.gpa.free(constituents);
468471
469 const elem_val_id = self.spv.allocId();
470 try self.genConstant(elem_val_id, elem_ty, elem_val, repr);
472 const elem_val_id = try self.genConstant(elem_ty, elem_val, repr);
471473 for (constituents[0..len]) |*elem| {
472474 elem.* = elem_val_id;
473475 }
474476 if (ty.sentinel()) |sentinel| {
475 constituents[len] = self.spv.allocId();
476 try self.genConstant(constituents[len], elem_ty, sentinel, repr);
477 constituents[len] = try self.genConstant(elem_ty, sentinel, repr);
477478 }
478479 try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{
479480 .id_result_type = result_ty_id,
......@@ -517,8 +518,7 @@ pub const DeclGen = struct {
517518 const elem_refs = try self.gpa.alloc(IdRef, vector_len);
518519 defer self.gpa.free(elem_refs);
519520 for (elem_refs, 0..) |*elem, i| {
520 elem.* = self.spv.allocId();
521 try self.genConstant(elem.*, elem_ty, elem_vals[i], repr);
521 elem.* = try self.genConstant(elem_ty, elem_vals[i], repr);
522522 }
523523 try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{
524524 .id_result_type = result_ty_id,
......@@ -539,17 +539,15 @@ pub const DeclGen = struct {
539539 const constituents = try self.spv.gpa.alloc(IdRef, tuple.types.len);
540540 errdefer self.spv.gpa.free(constituents);
541541
542 var member_index: usize = 0;
542 var member_i: usize = 0;
543543 for (tuple.types, 0..) |field_ty, i| {
544544 const field_val = tuple.values[i];
545545 if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBits()) continue;
546 const member_id = self.spv.allocId();
547 try self.genConstant(member_id, field_ty, field_val, repr);
548 constituents[member_index] = member_id;
549 member_index += 1;
546 constituents[member_i] = try self.genConstant(field_ty, field_val, repr);
547 member_i += 1;
550548 }
551549
552 break :blk constituents[0..member_index];
550 break :blk constituents[0..member_i];
553551 } else blk: {
554552 const struct_ty = ty.castTag(.@"struct").?.data;
555553
......@@ -560,16 +558,14 @@ pub const DeclGen = struct {
560558 const field_vals = val.castTag(.aggregate).?.data;
561559 const constituents = try self.spv.gpa.alloc(IdRef, struct_ty.fields.count());
562560 errdefer self.spv.gpa.free(constituents);
563 var member_index: usize = 0;
561 var member_i: usize = 0;
564562 for (struct_ty.fields.values(), 0..) |field, i| {
565563 if (field.is_comptime or !field.ty.hasRuntimeBits()) continue;
566 const member_id = self.spv.allocId();
567 try self.genConstant(member_id, field.ty, field_vals[i], repr);
568 constituents[member_index] = member_id;
569 member_index += 1;
564 constituents[member_i] = try self.genConstant(field.ty, field_vals[i], repr);
565 member_i += 1;
570566 }
571567
572 break :blk constituents[0..member_index];
568 break :blk constituents[0..member_i];
573569 };
574570 defer self.spv.gpa.free(constituents);
575571
......@@ -580,20 +576,14 @@ pub const DeclGen = struct {
580576 });
581577 },
582578 .Pointer => switch (val.tag()) {
583 .decl_ref => {
584 const decl_index = val.castTag(.decl_ref).?.data;
585 const decl_result_id = self.spv.allocId();
586 try self.genDeclRef(decl_result_id, decl_index);
587 try self.variable(.global, result_id, result_ty_ref, decl_result_id);
588 },
579 .decl_ref_mut => try self.genDeclRef(result_ty_ref, result_id, val.castTag(.decl_ref_mut).?.data.decl_index),
580 .decl_ref => try self.genDeclRef(result_ty_ref, result_id, val.castTag(.decl_ref).?.data),
589581 .slice => {
590582 const slice = val.castTag(.slice).?.data;
591583 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
592584
593 const ptr_id = self.spv.allocId();
594 try self.genConstant(ptr_id, ty.slicePtrFieldType(&buf), slice.ptr, .indirect);
595 const len_id = self.spv.allocId();
596 try self.genConstant(len_id, Type.usize, slice.len, .indirect);
585 const ptr_id = try self.genConstant(ty.slicePtrFieldType(&buf), slice.ptr, .indirect);
586 const len_id = try self.genConstant(Type.usize, slice.len, .indirect);
597587
598588 const constituents = [_]IdRef{ ptr_id, len_id };
599589 try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{
......@@ -613,10 +603,11 @@ pub const DeclGen = struct {
613603 }
614604 }
615605
616 fn genDeclRef(self: *DeclGen, result_id: IdRef, decl_index: Decl.Index) Error!void {
606 fn genDeclRef(self: *DeclGen, result_ty_ref: SpvType.Ref, result_id: IdRef, decl_index: Decl.Index) Error!void {
617607 const decl = self.module.declPtr(decl_index);
618608 self.module.markDeclAlive(decl);
619 try self.genConstant(result_id, decl.ty, decl.val, .indirect);
609 const decl_id = try self.genConstant(decl.ty, decl.val, .indirect);
610 try self.variable(.global, result_id, result_ty_ref, decl_id);
620611 }
621612
622613 /// Turn a Zig type into a SPIR-V Type, and return its type result-id.
......@@ -915,7 +906,7 @@ pub const DeclGen = struct {
915906 .name = fqn,
916907 });
917908 } else {
918 try self.genConstant(result_id, decl.ty, decl.val, .direct);
909 try self.genConstantForId(result_id, decl.ty, decl.val, .direct);
919910 }
920911 }
921912