| ... | @@ -394,6 +394,16 @@ pub const DeclGen = struct { | ... | @@ -394,6 +394,16 @@ pub const DeclGen = struct { |
| 394 | return result_id; | 394 | return result_id; |
| 395 | } | 395 | } |
| 396 | | 396 | |
| | 397 | fn genUndef(self: *DeclGen, ty_ref: SpvType.Ref) Error!IdRef { |
| | 398 | const result_id = self.spv.allocId(); |
| | 399 | try self.spv.sections.types_globals_constants.emit( |
| | 400 | self.spv.gpa, |
| | 401 | .OpUndef, |
| | 402 | .{ .id_result_type = self.typeId(ty_ref), .id_result = result_id }, |
| | 403 | ); |
| | 404 | return result_id; |
| | 405 | } |
| | 406 | |
| 397 | fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) Error!IdRef { | 407 | fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) Error!IdRef { |
| 398 | const result_id = self.spv.allocId(); | 408 | const result_id = self.spv.allocId(); |
| 399 | try self.genConstant(result_id, ty, val, repr); | 409 | try self.genConstant(result_id, ty, val, repr); |
| ... | @@ -543,7 +553,7 @@ pub const DeclGen = struct { | ... | @@ -543,7 +553,7 @@ pub const DeclGen = struct { |
| 543 | for (tuple.types, 0..) |field_ty, i| { | 553 | for (tuple.types, 0..) |field_ty, i| { |
| 544 | const field_val = tuple.values[i]; | 554 | const field_val = tuple.values[i]; |
| 545 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBits()) continue; | 555 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBits()) continue; |
| 546 | constituents[member_i] = try self.constant(field_ty, field_val, repr); | 556 | constituents[member_i] = try self.constant(field_ty, field_val, .indirect); |
| 547 | member_i += 1; | 557 | member_i += 1; |
| 548 | } | 558 | } |
| 549 | | 559 | |
| ... | @@ -561,7 +571,7 @@ pub const DeclGen = struct { | ... | @@ -561,7 +571,7 @@ pub const DeclGen = struct { |
| 561 | var member_i: usize = 0; | 571 | var member_i: usize = 0; |
| 562 | for (struct_ty.fields.values(), 0..) |field, i| { | 572 | for (struct_ty.fields.values(), 0..) |field, i| { |
| 563 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; | 573 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; |
| 564 | constituents[member_i] = try self.constant(field.ty, field_vals[i], repr); | 574 | constituents[member_i] = try self.constant(field.ty, field_vals[i], .indirect); |
| 565 | member_i += 1; | 575 | member_i += 1; |
| 566 | } | 576 | } |
| 567 | | 577 | |
| ... | @@ -633,6 +643,67 @@ pub const DeclGen = struct { | ... | @@ -633,6 +643,67 @@ pub const DeclGen = struct { |
| 633 | .constituents = &constituents, | 643 | .constituents = &constituents, |
| 634 | }); | 644 | }); |
| 635 | }, | 645 | }, |
| | 646 | .Union => { |
| | 647 | const tag_and_val = val.castTag(.@"union").?.data; |
| | 648 | const layout = ty.unionGetLayout(target); |
| | 649 | |
| | 650 | if (layout.payload_size == 0) { |
| | 651 | return try self.genConstant(result_id, ty.unionTagTypeSafety().?, tag_and_val.tag, .indirect); |
| | 652 | } |
| | 653 | |
| | 654 | const union_ty = ty.cast(Type.Payload.Union).?.data; |
| | 655 | if (union_ty.layout == .Packed) { |
| | 656 | return self.todo("packed union constants", .{}); |
| | 657 | } |
| | 658 | |
| | 659 | const active_field = ty.unionTagFieldIndex(tag_and_val.tag, self.module).?; |
| | 660 | const union_ty_ref = try self.resolveUnionType(ty, active_field); |
| | 661 | const active_field_ty = union_ty.fields.values()[active_field].ty; |
| | 662 | |
| | 663 | const tag_first = layout.tag_align >= layout.payload_align; |
| | 664 | const u8_ty_ref = try self.intType(.unsigned, 8); |
| | 665 | |
| | 666 | const tag = if (layout.tag_size != 0) |
| | 667 | try self.constant(ty.unionTagTypeSafety().?, tag_and_val.tag, .indirect) |
| | 668 | else |
| | 669 | null; |
| | 670 | |
| | 671 | var members = std.BoundedArray(IdRef, 4){}; |
| | 672 | |
| | 673 | if (tag_first) { |
| | 674 | if (tag) |id| members.appendAssumeCapacity(id); |
| | 675 | } |
| | 676 | |
| | 677 | const active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime()) blk: { |
| | 678 | const payload = try self.constant(active_field_ty, tag_and_val.val, .indirect); |
| | 679 | members.appendAssumeCapacity(payload); |
| | 680 | break :blk active_field_ty.abiSize(target); |
| | 681 | } else 0; |
| | 682 | |
| | 683 | const payload_padding_len = layout.payload_size - active_field_size; |
| | 684 | if (payload_padding_len != 0) { |
| | 685 | const payload_padding_ty_ref = try self.arrayType(@intCast(u32, payload_padding_len), u8_ty_ref); |
| | 686 | members.appendAssumeCapacity(try self.genUndef(payload_padding_ty_ref)); |
| | 687 | } |
| | 688 | |
| | 689 | if (!tag_first) { |
| | 690 | if (tag) |id| members.appendAssumeCapacity(id); |
| | 691 | } |
| | 692 | |
| | 693 | if (layout.padding != 0) { |
| | 694 | const padding_ty_ref = try self.arrayType(layout.padding, u8_ty_ref); |
| | 695 | members.appendAssumeCapacity(try self.genUndef(padding_ty_ref)); |
| | 696 | } |
| | 697 | |
| | 698 | try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{ |
| | 699 | .id_result_type = self.typeId(union_ty_ref), |
| | 700 | .id_result = result_id, |
| | 701 | .constituents = members.slice(), |
| | 702 | }); |
| | 703 | |
| | 704 | // TODO: Cast to general union type? Required for pointers only or something? |
| | 705 | }, |
| | 706 | |
| 636 | .Fn => switch (repr) { | 707 | .Fn => switch (repr) { |
| 637 | .direct => unreachable, | 708 | .direct => unreachable, |
| 638 | .indirect => return self.todo("function pointers", .{}), | 709 | .indirect => return self.todo("function pointers", .{}), |
| ... | @@ -691,6 +762,91 @@ pub const DeclGen = struct { | ... | @@ -691,6 +762,91 @@ pub const DeclGen = struct { |
| 691 | return self.typeId(type_ref); | 762 | return self.typeId(type_ref); |
| 692 | } | 763 | } |
| 693 | | 764 | |
| | 765 | /// Construct an array type which has 'len' elements of 'type' |
| | 766 | fn arrayType(self: *DeclGen, len: u32, ty: SpvType.Ref) !SpvType.Ref { |
| | 767 | const payload = try self.spv.arena.create(SpvType.Payload.Array); |
| | 768 | payload.* = .{ |
| | 769 | .element_type = ty, |
| | 770 | .length = len, |
| | 771 | }; |
| | 772 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| | 773 | } |
| | 774 | |
| | 775 | /// Generate a union type, optionally with a known field. If the tag alignment is greater |
| | 776 | /// than that of the payload, a regular union (non-packed, with both tag and payload), will |
| | 777 | /// be generated as follows: |
| | 778 | /// If the active field is known: |
| | 779 | /// struct { |
| | 780 | /// tag: TagType, |
| | 781 | /// payload: ActivePayloadType, |
| | 782 | /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8, |
| | 783 | /// padding: [padding_size]u8, |
| | 784 | /// } |
| | 785 | /// If the payload alignment is greater than that of the tag: |
| | 786 | /// struct { |
| | 787 | /// payload: ActivePayloadType, |
| | 788 | /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8, |
| | 789 | /// tag: TagType, |
| | 790 | /// padding: [padding_size]u8, |
| | 791 | /// } |
| | 792 | /// If the active payload is unknown, it will default back to the most aligned field. This is |
| | 793 | /// to make sure that the overal struct has the correct alignment in spir-v. |
| | 794 | /// If any of the fields' size is 0, it will be omitted. |
| | 795 | /// NOTE: When the active field is set to something other than the most aligned field, the |
| | 796 | /// resulting struct will be *underaligned*. |
| | 797 | fn resolveUnionType(self: *DeclGen, ty: Type, maybe_active_field: ?usize) !SpvType.Ref { |
| | 798 | const target = self.getTarget(); |
| | 799 | const layout = ty.unionGetLayout(target); |
| | 800 | const union_ty = ty.cast(Type.Payload.Union).?.data; |
| | 801 | |
| | 802 | if (union_ty.layout == .Packed) { |
| | 803 | return self.todo("packed union types", .{}); |
| | 804 | } |
| | 805 | |
| | 806 | const tag_ty_ref = try self.resolveType(union_ty.tag_ty, .indirect); |
| | 807 | if (layout.payload_size == 0) { |
| | 808 | // No payload, so represent this as just the tag type. |
| | 809 | return tag_ty_ref; |
| | 810 | } |
| | 811 | |
| | 812 | var members = std.BoundedArray(SpvType.Payload.Struct.Member, 4){}; |
| | 813 | |
| | 814 | const has_tag = layout.tag_size != 0; |
| | 815 | const tag_first = layout.tag_align >= layout.payload_align; |
| | 816 | const tag_member = .{ .name = "tag", .ty = tag_ty_ref }; |
| | 817 | const u8_ty_ref = try self.intType(.unsigned, 8); // TODO: What if Int8Type is not enabled? |
| | 818 | |
| | 819 | if (has_tag and tag_first) { |
| | 820 | members.appendAssumeCapacity(tag_member); |
| | 821 | } |
| | 822 | |
| | 823 | const active_field = maybe_active_field orelse layout.most_aligned_field; |
| | 824 | const active_field_ty = union_ty.fields.values()[active_field].ty; |
| | 825 | |
| | 826 | const active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime()) blk: { |
| | 827 | const active_payload_ty_ref = try self.resolveType(active_field_ty, .indirect); |
| | 828 | members.appendAssumeCapacity(.{ .name = "payload", .ty = active_payload_ty_ref }); |
| | 829 | break :blk active_field_ty.abiSize(target); |
| | 830 | } else 0; |
| | 831 | |
| | 832 | const payload_padding_len = layout.payload_size - active_field_size; |
| | 833 | if (payload_padding_len != 0) { |
| | 834 | const payload_padding_ty_ref = try self.arrayType(@intCast(u32, payload_padding_len), u8_ty_ref); |
| | 835 | members.appendAssumeCapacity(.{ .name = "padding_payload", .ty = payload_padding_ty_ref }); |
| | 836 | } |
| | 837 | |
| | 838 | if (has_tag and !tag_first) { |
| | 839 | members.appendAssumeCapacity(tag_member); |
| | 840 | } |
| | 841 | |
| | 842 | if (layout.padding != 0) { |
| | 843 | const padding_ty_ref = try self.arrayType(layout.padding, u8_ty_ref); |
| | 844 | members.appendAssumeCapacity(.{ .name = "padding", .ty = padding_ty_ref }); |
| | 845 | } |
| | 846 | |
| | 847 | return try self.simpleStructType(members.slice()); |
| | 848 | } |
| | 849 | |
| 694 | /// Turn a Zig type into a SPIR-V Type, and return a reference to it. | 850 | /// Turn a Zig type into a SPIR-V Type, and return a reference to it. |
| 695 | fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!SpvType.Ref { | 851 | fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!SpvType.Ref { |
| 696 | log.debug("resolveType: ty = {}", .{ty.fmtDebug()}); | 852 | log.debug("resolveType: ty = {}", .{ty.fmtDebug()}); |
| ... | @@ -733,16 +889,11 @@ pub const DeclGen = struct { | ... | @@ -733,16 +889,11 @@ pub const DeclGen = struct { |
| 733 | }, | 889 | }, |
| 734 | .Array => { | 890 | .Array => { |
| 735 | const elem_ty = ty.childType(); | 891 | const elem_ty = ty.childType(); |
| | 892 | const elem_ty_ref = try self.resolveType(elem_ty, .indirect); |
| 736 | const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel()) orelse { | 893 | const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel()) orelse { |
| 737 | return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel()}); | 894 | return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel()}); |
| 738 | }; | 895 | }; |
| 739 | | 896 | return try self.arrayType(total_len, elem_ty_ref); |
| 740 | const payload = try self.spv.arena.create(SpvType.Payload.Array); | | |
| 741 | payload.* = .{ | | |
| 742 | .element_type = try self.resolveType(elem_ty, repr), | | |
| 743 | .length = total_len, | | |
| 744 | }; | | |
| 745 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); | | |
| 746 | }, | 897 | }, |
| 747 | .Fn => { | 898 | .Fn => { |
| 748 | // TODO: Put this somewhere in Sema.zig | 899 | // TODO: Put this somewhere in Sema.zig |
| ... | @@ -809,7 +960,7 @@ pub const DeclGen = struct { | ... | @@ -809,7 +960,7 @@ pub const DeclGen = struct { |
| 809 | const field_val = tuple.values[i]; | 960 | const field_val = tuple.values[i]; |
| 810 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBitsIgnoreComptime()) continue; | 961 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 811 | members[member_index] = .{ | 962 | members[member_index] = .{ |
| 812 | .ty = try self.resolveType(field_ty, repr), | 963 | .ty = try self.resolveType(field_ty, .indirect), |
| 813 | }; | 964 | }; |
| 814 | member_index += 1; | 965 | member_index += 1; |
| 815 | } | 966 | } |
| ... | @@ -823,7 +974,7 @@ pub const DeclGen = struct { | ... | @@ -823,7 +974,7 @@ pub const DeclGen = struct { |
| 823 | const struct_ty = ty.castTag(.@"struct").?.data; | 974 | const struct_ty = ty.castTag(.@"struct").?.data; |
| 824 | | 975 | |
| 825 | if (struct_ty.layout == .Packed) { | 976 | if (struct_ty.layout == .Packed) { |
| 826 | return try self.resolveType(struct_ty.backing_int_ty, repr); | 977 | return try self.resolveType(struct_ty.backing_int_ty, .indirect); |
| 827 | } | 978 | } |
| 828 | | 979 | |
| 829 | const members = try self.spv.arena.alloc(SpvType.Payload.Struct.Member, struct_ty.fields.count()); | 980 | const members = try self.spv.arena.alloc(SpvType.Payload.Struct.Member, struct_ty.fields.count()); |
| ... | @@ -832,7 +983,7 @@ pub const DeclGen = struct { | ... | @@ -832,7 +983,7 @@ pub const DeclGen = struct { |
| 832 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; | 983 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; |
| 833 | | 984 | |
| 834 | members[member_index] = .{ | 985 | members[member_index] = .{ |
| 835 | .ty = try self.resolveType(field.ty, repr), | 986 | .ty = try self.resolveType(field.ty, .indirect), |
| 836 | .name = struct_ty.fields.keys()[i], | 987 | .name = struct_ty.fields.keys()[i], |
| 837 | }; | 988 | }; |
| 838 | member_index += 1; | 989 | member_index += 1; |
| ... | @@ -872,6 +1023,8 @@ pub const DeclGen = struct { | ... | @@ -872,6 +1023,8 @@ pub const DeclGen = struct { |
| 872 | .{ .ty = bool_ty_ref, .name = "valid" }, | 1023 | .{ .ty = bool_ty_ref, .name = "valid" }, |
| 873 | }); | 1024 | }); |
| 874 | }, | 1025 | }, |
| | 1026 | .Union => return try self.resolveUnionType(ty, null), |
| | 1027 | |
| 875 | .Null, | 1028 | .Null, |
| 876 | .Undefined, | 1029 | .Undefined, |
| 877 | .EnumLiteral, | 1030 | .EnumLiteral, |