| ... | ... | @@ -22,8 +22,6 @@ const IdResultType = spec.IdResultType; |
| 22 | 22 | const StorageClass = spec.StorageClass; |
| 23 | 23 | |
| 24 | 24 | const SpvModule = @import("spirv/Module.zig"); |
| 25 | | const CacheRef = SpvModule.CacheRef; |
| 26 | | const CacheString = SpvModule.CacheString; |
| 27 | 25 | |
| 28 | 26 | const SpvSection = @import("spirv/Section.zig"); |
| 29 | 27 | const SpvAssembler = @import("spirv/Assembler.zig"); |
| ... | ... | @@ -32,14 +30,11 @@ const InstMap = std.AutoHashMapUnmanaged(Air.Inst.Index, IdRef); |
| 32 | 30 | |
| 33 | 31 | pub const zig_call_abi_ver = 3; |
| 34 | 32 | |
| 35 | | /// We want to store some extra facts about types as mapped from Zig to SPIR-V. |
| 36 | | /// This structure is used to keep that extra information, as well as |
| 37 | | /// the cached reference to the type. |
| 38 | | const SpvTypeInfo = struct { |
| 39 | | ty_ref: CacheRef, |
| 40 | | }; |
| 41 | | |
| 42 | | const TypeMap = std.AutoHashMapUnmanaged(InternPool.Index, SpvTypeInfo); |
| 33 | const InternMap = std.AutoHashMapUnmanaged(struct { InternPool.Index, DeclGen.Repr }, IdResult); |
| 34 | const PtrTypeMap = std.AutoHashMapUnmanaged( |
| 35 | struct { InternPool.Index, StorageClass }, |
| 36 | struct { ty_id: IdRef, fwd_emitted: bool }, |
| 37 | ); |
| 43 | 38 | |
| 44 | 39 | const ControlFlow = union(enum) { |
| 45 | 40 | const Structured = struct { |
| ... | ... | @@ -162,14 +157,16 @@ pub const Object = struct { |
| 162 | 157 | /// A map of Zig InternPool indices for anonymous decls to SPIR-V decl indices. |
| 163 | 158 | anon_decl_link: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, SpvModule.Decl.Index) = .{}, |
| 164 | 159 | |
| 165 | | /// A map that maps AIR intern pool indices to SPIR-V cache references (which |
| 166 | | /// is basically the same thing except for SPIR-V). |
| 167 | | /// This map is typically only used for structures that are deemed heavy enough |
| 168 | | /// that it is worth to store them here. The SPIR-V module also interns types, |
| 169 | | /// and so the main purpose of this map is to avoid recomputation and to |
| 170 | | /// cache extra information about the type rather than to aid in validity |
| 171 | | /// of the SPIR-V module. |
| 172 | | type_map: TypeMap = .{}, |
| 160 | /// A map that maps AIR intern pool indices to SPIR-V result-ids. |
| 161 | intern_map: InternMap = .{}, |
| 162 | |
| 163 | /// This map serves a dual purpose: |
| 164 | /// - It keeps track of pointers that are currently being emitted, so that we can tell |
| 165 | /// if they are recursive and need an OpTypeForwardPointer. |
| 166 | /// - It caches pointers by child-type. This is required because sometimes we rely on |
| 167 | /// ID-equality for pointers, and pointers constructed via `ptrType()` aren't interned |
| 168 | /// via the usual `intern_map` mechanism. |
| 169 | ptr_types: PtrTypeMap = .{}, |
| 173 | 170 | |
| 174 | 171 | pub fn init(gpa: Allocator) Object { |
| 175 | 172 | return .{ |
| ... | ... | @@ -182,7 +179,8 @@ pub const Object = struct { |
| 182 | 179 | self.spv.deinit(); |
| 183 | 180 | self.decl_link.deinit(self.gpa); |
| 184 | 181 | self.anon_decl_link.deinit(self.gpa); |
| 185 | | self.type_map.deinit(self.gpa); |
| 182 | self.intern_map.deinit(self.gpa); |
| 183 | self.ptr_types.deinit(self.gpa); |
| 186 | 184 | } |
| 187 | 185 | |
| 188 | 186 | fn genDecl( |
| ... | ... | @@ -204,7 +202,8 @@ pub const Object = struct { |
| 204 | 202 | .decl_index = decl_index, |
| 205 | 203 | .air = air, |
| 206 | 204 | .liveness = liveness, |
| 207 | | .type_map = &self.type_map, |
| 205 | .intern_map = &self.intern_map, |
| 206 | .ptr_types = &self.ptr_types, |
| 208 | 207 | .control_flow = switch (structured_cfg) { |
| 209 | 208 | true => .{ .structured = .{} }, |
| 210 | 209 | false => .{ .unstructured = .{} }, |
| ... | ... | @@ -309,13 +308,12 @@ const DeclGen = struct { |
| 309 | 308 | /// A map keeping track of which instruction generated which result-id. |
| 310 | 309 | inst_results: InstMap = .{}, |
| 311 | 310 | |
| 312 | | /// A map that maps AIR intern pool indices to SPIR-V cache references. |
| 313 | | /// See Object.type_map |
| 314 | | type_map: *TypeMap, |
| 311 | /// A map that maps AIR intern pool indices to SPIR-V result-ids. |
| 312 | /// See `Object.intern_map`. |
| 313 | intern_map: *InternMap, |
| 315 | 314 | |
| 316 | | /// Child types of pointers that are currently in progress of being resolved. If a pointer |
| 317 | | /// is already in this map, its recursive. |
| 318 | | wip_pointers: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, CacheRef) = .{}, |
| 315 | /// Module's pointer types, see `Object.ptr_types`. |
| 316 | ptr_types: *PtrTypeMap, |
| 319 | 317 | |
| 320 | 318 | /// This field keeps track of the current state wrt structured or unstructured control flow. |
| 321 | 319 | control_flow: ControlFlow, |
| ... | ... | @@ -402,7 +400,6 @@ const DeclGen = struct { |
| 402 | 400 | pub fn deinit(self: *DeclGen) void { |
| 403 | 401 | self.args.deinit(self.gpa); |
| 404 | 402 | self.inst_results.deinit(self.gpa); |
| 405 | | self.wip_pointers.deinit(self.gpa); |
| 406 | 403 | self.control_flow.deinit(self.gpa); |
| 407 | 404 | self.func.deinit(self.gpa); |
| 408 | 405 | } |
| ... | ... | @@ -452,7 +449,7 @@ const DeclGen = struct { |
| 452 | 449 | |
| 453 | 450 | const mod = self.module; |
| 454 | 451 | const ty = Type.fromInterned(mod.intern_pool.typeOf(val)); |
| 455 | | const decl_ptr_ty_ref = try self.ptrType(ty, .Generic); |
| 452 | const decl_ptr_ty_id = try self.ptrType(ty, .Generic); |
| 456 | 453 | |
| 457 | 454 | const spv_decl_index = blk: { |
| 458 | 455 | const entry = try self.object.anon_decl_link.getOrPut(self.object.gpa, .{ val, .Function }); |
| ... | ... | @@ -460,7 +457,7 @@ const DeclGen = struct { |
| 460 | 457 | try self.addFunctionDep(entry.value_ptr.*, .Function); |
| 461 | 458 | |
| 462 | 459 | const result_id = self.spv.declPtr(entry.value_ptr.*).result_id; |
| 463 | | return try self.castToGeneric(self.typeId(decl_ptr_ty_ref), result_id); |
| 460 | return try self.castToGeneric(decl_ptr_ty_id, result_id); |
| 464 | 461 | } |
| 465 | 462 | |
| 466 | 463 | const spv_decl_index = try self.spv.allocDecl(.invocation_global); |
| ... | ... | @@ -488,19 +485,14 @@ const DeclGen = struct { |
| 488 | 485 | self.func = .{}; |
| 489 | 486 | defer self.func.deinit(self.gpa); |
| 490 | 487 | |
| 491 | | const void_ty_ref = try self.resolveType(Type.void, .direct); |
| 492 | | const initializer_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{ |
| 493 | | .return_type = void_ty_ref, |
| 494 | | .parameters = &.{}, |
| 495 | | } }); |
| 488 | const initializer_proto_ty_id = try self.functionType(Type.void, &.{}); |
| 496 | 489 | |
| 497 | 490 | const initializer_id = self.spv.allocId(); |
| 498 | | |
| 499 | 491 | try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ |
| 500 | | .id_result_type = self.typeId(void_ty_ref), |
| 492 | .id_result_type = try self.resolveType(Type.void, .direct), |
| 501 | 493 | .id_result = initializer_id, |
| 502 | 494 | .function_control = .{}, |
| 503 | | .function_type = self.typeId(initializer_proto_ty_ref), |
| 495 | .function_type = initializer_proto_ty_id, |
| 504 | 496 | }); |
| 505 | 497 | const root_block_id = self.spv.allocId(); |
| 506 | 498 | try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ |
| ... | ... | @@ -520,9 +512,9 @@ const DeclGen = struct { |
| 520 | 512 | |
| 521 | 513 | try self.spv.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)}); |
| 522 | 514 | |
| 523 | | const fn_decl_ptr_ty_ref = try self.ptrType(ty, .Function); |
| 515 | const fn_decl_ptr_ty_id = try self.ptrType(ty, .Function); |
| 524 | 516 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ |
| 525 | | .id_result_type = self.typeId(fn_decl_ptr_ty_ref), |
| 517 | .id_result_type = fn_decl_ptr_ty_id, |
| 526 | 518 | .id_result = result_id, |
| 527 | 519 | .set = try self.spv.importInstructionSet(.zig), |
| 528 | 520 | .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... |
| ... | ... | @@ -530,7 +522,7 @@ const DeclGen = struct { |
| 530 | 522 | }); |
| 531 | 523 | } |
| 532 | 524 | |
| 533 | | return try self.castToGeneric(self.typeId(decl_ptr_ty_ref), result_id); |
| 525 | return try self.castToGeneric(decl_ptr_ty_id, result_id); |
| 534 | 526 | } |
| 535 | 527 | |
| 536 | 528 | fn addFunctionDep(self: *DeclGen, decl_index: SpvModule.Decl.Index, storage_class: StorageClass) !void { |
| ... | ... | @@ -696,14 +688,25 @@ const DeclGen = struct { |
| 696 | 688 | |
| 697 | 689 | /// Emits a bool constant in a particular representation. |
| 698 | 690 | fn constBool(self: *DeclGen, value: bool, repr: Repr) !IdRef { |
| 691 | // TODO: Cache? |
| 692 | |
| 693 | const section = &self.spv.sections.types_globals_constants; |
| 699 | 694 | switch (repr) { |
| 700 | 695 | .indirect => { |
| 701 | | const int_ty_ref = try self.intType(.unsigned, 1); |
| 702 | | return self.constInt(int_ty_ref, @intFromBool(value)); |
| 696 | return try self.constInt(Type.u1, @intFromBool(value), .indirect); |
| 703 | 697 | }, |
| 704 | 698 | .direct => { |
| 705 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 706 | | return self.spv.constBool(bool_ty_ref, value); |
| 699 | const result_ty_id = try self.resolveType(Type.bool, .direct); |
| 700 | const result_id = self.spv.allocId(); |
| 701 | const operands = .{ |
| 702 | .id_result_type = result_ty_id, |
| 703 | .id_result = result_id, |
| 704 | }; |
| 705 | switch (value) { |
| 706 | true => try section.emit(self.spv.gpa, .OpConstantTrue, operands), |
| 707 | false => try section.emit(self.spv.gpa, .OpConstantFalse, operands), |
| 708 | } |
| 709 | return result_id; |
| 707 | 710 | }, |
| 708 | 711 | } |
| 709 | 712 | } |
| ... | ... | @@ -711,68 +714,63 @@ const DeclGen = struct { |
| 711 | 714 | /// Emits an integer constant. |
| 712 | 715 | /// This function, unlike SpvModule.constInt, takes care to bitcast |
| 713 | 716 | /// the value to an unsigned int first for Kernels. |
| 714 | | fn constInt(self: *DeclGen, ty_ref: CacheRef, value: anytype) !IdRef { |
| 715 | | switch (self.spv.cache.lookup(ty_ref)) { |
| 716 | | .vector_type => |vec_type| { |
| 717 | | const elem_ids = try self.gpa.alloc(IdRef, vec_type.component_count); |
| 718 | | defer self.gpa.free(elem_ids); |
| 719 | | const int_value = try self.constInt(vec_type.component_type, value); |
| 720 | | @memset(elem_ids, int_value); |
| 721 | | |
| 722 | | const constituents_id = self.spv.allocId(); |
| 723 | | try self.func.body.emit(self.spv.gpa, .OpCompositeConstruct, .{ |
| 724 | | .id_result_type = self.typeId(ty_ref), |
| 725 | | .id_result = constituents_id, |
| 726 | | .constituents = elem_ids, |
| 727 | | }); |
| 728 | | return constituents_id; |
| 729 | | }, |
| 730 | | else => {}, |
| 731 | | } |
| 717 | fn constInt(self: *DeclGen, ty: Type, value: anytype, repr: Repr) !IdRef { |
| 718 | // TODO: Cache? |
| 719 | const mod = self.module; |
| 720 | const scalar_ty = ty.scalarType(mod); |
| 721 | const int_info = scalar_ty.intInfo(mod); |
| 722 | // Use backing bits so that negatives are sign extended |
| 723 | const backing_bits = self.backingIntBits(int_info.bits).?; // Assertion failure means big int |
| 724 | |
| 725 | const bits: u64 = switch (int_info.signedness) { |
| 726 | // Intcast needed to silence compile errors for when the wrong path is compiled. |
| 727 | // Lazy fix. |
| 728 | .signed => @bitCast(@as(i64, @intCast(value))), |
| 729 | .unsigned => @as(u64, @intCast(value)), |
| 730 | }; |
| 732 | 731 | |
| 733 | | if (value < 0) { |
| 734 | | const ty = self.spv.cache.lookup(ty_ref).int_type; |
| 735 | | // Manually truncate the value so that the resulting value |
| 736 | | // fits within the unsigned type. |
| 737 | | const bits: u64 = @bitCast(@as(i64, @intCast(value))); |
| 738 | | const truncated_bits = if (ty.bits == 64) |
| 739 | | bits |
| 740 | | else |
| 741 | | bits & (@as(u64, 1) << @intCast(ty.bits)) - 1; |
| 742 | | return try self.spv.constInt(ty_ref, truncated_bits); |
| 743 | | } else { |
| 744 | | return try self.spv.constInt(ty_ref, value); |
| 732 | // Manually truncate the value to the right amount of bits. |
| 733 | const truncated_bits = if (backing_bits == 64) |
| 734 | bits |
| 735 | else |
| 736 | bits & (@as(u64, 1) << @intCast(backing_bits)) - 1; |
| 737 | |
| 738 | const result_ty_id = try self.resolveType(scalar_ty, repr); |
| 739 | const result_id = self.spv.allocId(); |
| 740 | |
| 741 | const section = &self.spv.sections.types_globals_constants; |
| 742 | switch (backing_bits) { |
| 743 | 0 => unreachable, // u0 is comptime |
| 744 | 1...32 => try section.emit(self.spv.gpa, .OpConstant, .{ |
| 745 | .id_result_type = result_ty_id, |
| 746 | .id_result = result_id, |
| 747 | .value = .{ .uint32 = @truncate(truncated_bits) }, |
| 748 | }), |
| 749 | 33...64 => try section.emit(self.spv.gpa, .OpConstant, .{ |
| 750 | .id_result_type = result_ty_id, |
| 751 | .id_result = result_id, |
| 752 | .value = .{ .uint64 = truncated_bits }, |
| 753 | }), |
| 754 | else => unreachable, // TODO: Large integer constants |
| 745 | 755 | } |
| 746 | | } |
| 747 | 756 | |
| 748 | | /// Emits a float constant |
| 749 | | fn constFloat(self: *DeclGen, ty_ref: CacheRef, value: f128) !IdRef { |
| 750 | | switch (self.spv.cache.lookup(ty_ref)) { |
| 751 | | .vector_type => |vec_type| { |
| 752 | | const elem_ids = try self.gpa.alloc(IdRef, vec_type.component_count); |
| 753 | | defer self.gpa.free(elem_ids); |
| 754 | | const int_value = try self.constFloat(vec_type.component_type, value); |
| 755 | | @memset(elem_ids, int_value); |
| 756 | | |
| 757 | | const constituents_id = self.spv.allocId(); |
| 758 | | try self.func.body.emit(self.spv.gpa, .OpCompositeConstruct, .{ |
| 759 | | .id_result_type = self.typeId(ty_ref), |
| 760 | | .id_result = constituents_id, |
| 761 | | .constituents = elem_ids, |
| 762 | | }); |
| 763 | | return constituents_id; |
| 764 | | }, |
| 765 | | else => {}, |
| 757 | if (!ty.isVector(mod)) { |
| 758 | return result_id; |
| 766 | 759 | } |
| 767 | 760 | |
| 768 | | const ty = self.spv.cache.lookup(ty_ref).float_type; |
| 769 | | return switch (ty.bits) { |
| 770 | | 16 => try self.spv.resolveId(.{ .float = .{ .ty = ty_ref, .value = .{ .float16 = @floatCast(value) } } }), |
| 771 | | 32 => try self.spv.resolveId(.{ .float = .{ .ty = ty_ref, .value = .{ .float32 = @floatCast(value) } } }), |
| 772 | | 64 => try self.spv.resolveId(.{ .float = .{ .ty = ty_ref, .value = .{ .float64 = @floatCast(value) } } }), |
| 773 | | 80, 128 => unreachable, // TODO |
| 774 | | else => unreachable, |
| 775 | | }; |
| 761 | const n = ty.vectorLen(mod); |
| 762 | const ids = try self.gpa.alloc(IdRef, n); |
| 763 | defer self.gpa.free(ids); |
| 764 | @memset(ids, result_id); |
| 765 | |
| 766 | const vec_ty_id = try self.resolveType(ty, repr); |
| 767 | const vec_result_id = self.spv.allocId(); |
| 768 | try self.func.body.emit(self.spv.gpa, .OpCompositeConstruct, .{ |
| 769 | .id_result_type = vec_ty_id, |
| 770 | .id_result = vec_result_id, |
| 771 | .constituents = ids, |
| 772 | }); |
| 773 | return vec_result_id; |
| 776 | 774 | } |
| 777 | 775 | |
| 778 | 776 | /// Construct a struct at runtime. |
| ... | ... | @@ -788,8 +786,8 @@ const DeclGen = struct { |
| 788 | 786 | // TODO: Make this OpCompositeConstruct when we can |
| 789 | 787 | const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function }); |
| 790 | 788 | for (constituents, types, 0..) |constitent_id, member_ty, index| { |
| 791 | | const ptr_member_ty_ref = try self.ptrType(member_ty, .Function); |
| 792 | | const ptr_id = try self.accessChain(ptr_member_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))}); |
| 789 | const ptr_member_ty_id = try self.ptrType(member_ty, .Function); |
| 790 | const ptr_id = try self.accessChain(ptr_member_ty_id, ptr_composite_id, &.{@as(u32, @intCast(index))}); |
| 793 | 791 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| 794 | 792 | .pointer = ptr_id, |
| 795 | 793 | .object = constitent_id, |
| ... | ... | @@ -810,9 +808,9 @@ const DeclGen = struct { |
| 810 | 808 | // TODO: Make this OpCompositeConstruct when we can |
| 811 | 809 | const mod = self.module; |
| 812 | 810 | const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function }); |
| 813 | | const ptr_elem_ty_ref = try self.ptrType(ty.elemType2(mod), .Function); |
| 811 | const ptr_elem_ty_id = try self.ptrType(ty.elemType2(mod), .Function); |
| 814 | 812 | for (constituents, 0..) |constitent_id, index| { |
| 815 | | const ptr_id = try self.accessChain(ptr_elem_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))}); |
| 813 | const ptr_id = try self.accessChain(ptr_elem_ty_id, ptr_composite_id, &.{@as(u32, @intCast(index))}); |
| 816 | 814 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| 817 | 815 | .pointer = ptr_id, |
| 818 | 816 | .object = constitent_id, |
| ... | ... | @@ -834,9 +832,9 @@ const DeclGen = struct { |
| 834 | 832 | // TODO: Make this OpCompositeConstruct when we can |
| 835 | 833 | const mod = self.module; |
| 836 | 834 | const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function }); |
| 837 | | const ptr_elem_ty_ref = try self.ptrType(ty.elemType2(mod), .Function); |
| 835 | const ptr_elem_ty_id = try self.ptrType(ty.elemType2(mod), .Function); |
| 838 | 836 | for (constituents, 0..) |constitent_id, index| { |
| 839 | | const ptr_id = try self.accessChain(ptr_elem_ty_ref, ptr_composite_id, &.{@as(u32, @intCast(index))}); |
| 837 | const ptr_id = try self.accessChain(ptr_elem_ty_id, ptr_composite_id, &.{@as(u32, @intCast(index))}); |
| 840 | 838 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| 841 | 839 | .pointer = ptr_id, |
| 842 | 840 | .object = constitent_id, |
| ... | ... | @@ -852,258 +850,279 @@ const DeclGen = struct { |
| 852 | 850 | /// is done by emitting a sequence of instructions that initialize the value. |
| 853 | 851 | // |
| 854 | 852 | /// This function should only be called during function code generation. |
| 855 | | fn constant(self: *DeclGen, ty: Type, arg_val: Value, repr: Repr) !IdRef { |
| 853 | fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef { |
| 854 | // Note: Using intern_map can only be used with constants that DO NOT generate any runtime code!! |
| 855 | // Ideally that should be all constants in the future, or it should be cleaned up somehow. For |
| 856 | // now, only use the intern_map on case-by-case basis by breaking to :cache. |
| 857 | if (self.intern_map.get(.{ val.toIntern(), repr })) |id| { |
| 858 | return id; |
| 859 | } |
| 860 | |
| 856 | 861 | const mod = self.module; |
| 857 | 862 | const target = self.getTarget(); |
| 858 | | const result_ty_ref = try self.resolveType(ty, repr); |
| 863 | const result_ty_id = try self.resolveType(ty, repr); |
| 859 | 864 | const ip = &mod.intern_pool; |
| 860 | 865 | |
| 861 | | const val = arg_val; |
| 862 | | |
| 863 | | log.debug("constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(mod) }); |
| 866 | log.debug("lowering constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(mod) }); |
| 864 | 867 | if (val.isUndefDeep(mod)) { |
| 865 | | return self.spv.constUndef(result_ty_ref); |
| 866 | | } |
| 867 | | |
| 868 | | switch (ip.indexToKey(val.toIntern())) { |
| 869 | | .int_type, |
| 870 | | .ptr_type, |
| 871 | | .array_type, |
| 872 | | .vector_type, |
| 873 | | .opt_type, |
| 874 | | .anyframe_type, |
| 875 | | .error_union_type, |
| 876 | | .simple_type, |
| 877 | | .struct_type, |
| 878 | | .anon_struct_type, |
| 879 | | .union_type, |
| 880 | | .opaque_type, |
| 881 | | .enum_type, |
| 882 | | .func_type, |
| 883 | | .error_set_type, |
| 884 | | .inferred_error_set_type, |
| 885 | | => unreachable, // types, not values |
| 886 | | |
| 887 | | .undef => unreachable, // handled above |
| 888 | | |
| 889 | | .variable, |
| 890 | | .extern_func, |
| 891 | | .func, |
| 892 | | .enum_literal, |
| 893 | | .empty_enum_value, |
| 894 | | => unreachable, // non-runtime values |
| 895 | | |
| 896 | | .simple_value => |simple_value| switch (simple_value) { |
| 897 | | .undefined, |
| 898 | | .void, |
| 899 | | .null, |
| 900 | | .empty_struct, |
| 901 | | .@"unreachable", |
| 902 | | .generic_poison, |
| 868 | return self.spv.constUndef(result_ty_id); |
| 869 | } |
| 870 | |
| 871 | const section = &self.spv.sections.types_globals_constants; |
| 872 | |
| 873 | const cacheable_id = cache: { |
| 874 | switch (ip.indexToKey(val.toIntern())) { |
| 875 | .int_type, |
| 876 | .ptr_type, |
| 877 | .array_type, |
| 878 | .vector_type, |
| 879 | .opt_type, |
| 880 | .anyframe_type, |
| 881 | .error_union_type, |
| 882 | .simple_type, |
| 883 | .struct_type, |
| 884 | .anon_struct_type, |
| 885 | .union_type, |
| 886 | .opaque_type, |
| 887 | .enum_type, |
| 888 | .func_type, |
| 889 | .error_set_type, |
| 890 | .inferred_error_set_type, |
| 891 | => unreachable, // types, not values |
| 892 | |
| 893 | .undef => unreachable, // handled above |
| 894 | |
| 895 | .variable, |
| 896 | .extern_func, |
| 897 | .func, |
| 898 | .enum_literal, |
| 899 | .empty_enum_value, |
| 903 | 900 | => unreachable, // non-runtime values |
| 904 | 901 | |
| 905 | | .false, .true => return try self.constBool(val.toBool(), repr), |
| 906 | | }, |
| 902 | .simple_value => |simple_value| switch (simple_value) { |
| 903 | .undefined, |
| 904 | .void, |
| 905 | .null, |
| 906 | .empty_struct, |
| 907 | .@"unreachable", |
| 908 | .generic_poison, |
| 909 | => unreachable, // non-runtime values |
| 907 | 910 | |
| 908 | | .int => { |
| 909 | | if (ty.isSignedInt(mod)) { |
| 910 | | return try self.constInt(result_ty_ref, val.toSignedInt(mod)); |
| 911 | | } else { |
| 912 | | return try self.constInt(result_ty_ref, val.toUnsignedInt(mod)); |
| 913 | | } |
| 914 | | }, |
| 915 | | .float => return switch (ty.floatBits(target)) { |
| 916 | | 16 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float16 = val.toFloat(f16, mod) } } }), |
| 917 | | 32 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float32 = val.toFloat(f32, mod) } } }), |
| 918 | | 64 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float64 = val.toFloat(f64, mod) } } }), |
| 919 | | 80, 128 => unreachable, // TODO |
| 920 | | else => unreachable, |
| 921 | | }, |
| 922 | | .err => |err| { |
| 923 | | const value = try mod.getErrorValue(err.name); |
| 924 | | return try self.constInt(result_ty_ref, value); |
| 925 | | }, |
| 926 | | .error_union => |error_union| { |
| 927 | | // TODO: Error unions may be constructed with constant instructions if the payload type |
| 928 | | // allows it. For now, just generate it here regardless. |
| 929 | | const err_int_ty = try mod.errorIntType(); |
| 930 | | const err_ty = switch (error_union.val) { |
| 931 | | .err_name => ty.errorUnionSet(mod), |
| 932 | | .payload => err_int_ty, |
| 933 | | }; |
| 934 | | const err_val = switch (error_union.val) { |
| 935 | | .err_name => |err_name| Value.fromInterned((try mod.intern(.{ .err = .{ |
| 936 | | .ty = ty.errorUnionSet(mod).toIntern(), |
| 937 | | .name = err_name, |
| 938 | | } }))), |
| 939 | | .payload => try mod.intValue(err_int_ty, 0), |
| 940 | | }; |
| 941 | | const payload_ty = ty.errorUnionPayload(mod); |
| 942 | | const eu_layout = self.errorUnionLayout(payload_ty); |
| 943 | | if (!eu_layout.payload_has_bits) { |
| 944 | | // We use the error type directly as the type. |
| 945 | | return try self.constant(err_ty, err_val, .indirect); |
| 946 | | } |
| 947 | | |
| 948 | | const payload_val = Value.fromInterned(switch (error_union.val) { |
| 949 | | .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }), |
| 950 | | .payload => |payload| payload, |
| 951 | | }); |
| 952 | | |
| 953 | | var constituents: [2]IdRef = undefined; |
| 954 | | var types: [2]Type = undefined; |
| 955 | | if (eu_layout.error_first) { |
| 956 | | constituents[0] = try self.constant(err_ty, err_val, .indirect); |
| 957 | | constituents[1] = try self.constant(payload_ty, payload_val, .indirect); |
| 958 | | types = .{ err_ty, payload_ty }; |
| 959 | | } else { |
| 960 | | constituents[0] = try self.constant(payload_ty, payload_val, .indirect); |
| 961 | | constituents[1] = try self.constant(err_ty, err_val, .indirect); |
| 962 | | types = .{ payload_ty, err_ty }; |
| 963 | | } |
| 964 | | |
| 965 | | return try self.constructStruct(ty, &types, &constituents); |
| 966 | | }, |
| 967 | | .enum_tag => { |
| 968 | | const int_val = try val.intFromEnum(ty, mod); |
| 969 | | const int_ty = ty.intTagType(mod); |
| 970 | | return try self.constant(int_ty, int_val, repr); |
| 971 | | }, |
| 972 | | .ptr => return self.constantPtr(ty, val), |
| 973 | | .slice => |slice| { |
| 974 | | const ptr_ty = ty.slicePtrFieldType(mod); |
| 975 | | const ptr_id = try self.constantPtr(ptr_ty, Value.fromInterned(slice.ptr)); |
| 976 | | const len_id = try self.constant(Type.usize, Value.fromInterned(slice.len), .indirect); |
| 977 | | return self.constructStruct( |
| 978 | | ty, |
| 979 | | &.{ ptr_ty, Type.usize }, |
| 980 | | &.{ ptr_id, len_id }, |
| 981 | | ); |
| 982 | | }, |
| 983 | | .opt => { |
| 984 | | const payload_ty = ty.optionalChild(mod); |
| 985 | | const maybe_payload_val = val.optionalValue(mod); |
| 986 | | |
| 987 | | if (!payload_ty.hasRuntimeBits(mod)) { |
| 988 | | return try self.constBool(maybe_payload_val != null, .indirect); |
| 989 | | } else if (ty.optionalReprIsPayload(mod)) { |
| 990 | | // Optional representation is a nullable pointer or slice. |
| 991 | | if (maybe_payload_val) |payload_val| { |
| 992 | | return try self.constant(payload_ty, payload_val, .indirect); |
| 911 | .false, .true => break :cache try self.constBool(val.toBool(), repr), |
| 912 | }, |
| 913 | .int => { |
| 914 | if (ty.isSignedInt(mod)) { |
| 915 | break :cache try self.constInt(ty, val.toSignedInt(mod), repr); |
| 993 | 916 | } else { |
| 994 | | const ptr_ty_ref = try self.resolveType(ty, .indirect); |
| 995 | | return self.spv.constNull(ptr_ty_ref); |
| 917 | break :cache try self.constInt(ty, val.toUnsignedInt(mod), repr); |
| 918 | } |
| 919 | }, |
| 920 | .float => { |
| 921 | const lit: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { |
| 922 | 16 => .{ .uint32 = @as(u16, @bitCast(val.toFloat(f16, mod))) }, |
| 923 | 32 => .{ .float32 = val.toFloat(f32, mod) }, |
| 924 | 64 => .{ .float64 = val.toFloat(f64, mod) }, |
| 925 | 80, 128 => unreachable, // TODO |
| 926 | else => unreachable, |
| 927 | }; |
| 928 | const result_id = self.spv.allocId(); |
| 929 | try section.emit(self.spv.gpa, .OpConstant, .{ |
| 930 | .id_result_type = result_ty_id, |
| 931 | .id_result = result_id, |
| 932 | .value = lit, |
| 933 | }); |
| 934 | break :cache result_id; |
| 935 | }, |
| 936 | .err => |err| { |
| 937 | const value = try mod.getErrorValue(err.name); |
| 938 | break :cache try self.constInt(ty, value, repr); |
| 939 | }, |
| 940 | .error_union => |error_union| { |
| 941 | // TODO: Error unions may be constructed with constant instructions if the payload type |
| 942 | // allows it. For now, just generate it here regardless. |
| 943 | const err_int_ty = try mod.errorIntType(); |
| 944 | const err_ty = switch (error_union.val) { |
| 945 | .err_name => ty.errorUnionSet(mod), |
| 946 | .payload => err_int_ty, |
| 947 | }; |
| 948 | const err_val = switch (error_union.val) { |
| 949 | .err_name => |err_name| Value.fromInterned((try mod.intern(.{ .err = .{ |
| 950 | .ty = ty.errorUnionSet(mod).toIntern(), |
| 951 | .name = err_name, |
| 952 | } }))), |
| 953 | .payload => try mod.intValue(err_int_ty, 0), |
| 954 | }; |
| 955 | const payload_ty = ty.errorUnionPayload(mod); |
| 956 | const eu_layout = self.errorUnionLayout(payload_ty); |
| 957 | if (!eu_layout.payload_has_bits) { |
| 958 | // We use the error type directly as the type. |
| 959 | break :cache try self.constant(err_ty, err_val, .indirect); |
| 996 | 960 | } |
| 997 | | } |
| 998 | | |
| 999 | | // Optional representation is a structure. |
| 1000 | | // { Payload, Bool } |
| 1001 | 961 | |
| 1002 | | const has_pl_id = try self.constBool(maybe_payload_val != null, .indirect); |
| 1003 | | const payload_id = if (maybe_payload_val) |payload_val| |
| 1004 | | try self.constant(payload_ty, payload_val, .indirect) |
| 1005 | | else |
| 1006 | | try self.spv.constUndef(try self.resolveType(payload_ty, .indirect)); |
| 962 | const payload_val = Value.fromInterned(switch (error_union.val) { |
| 963 | .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }), |
| 964 | .payload => |payload| payload, |
| 965 | }); |
| 1007 | 966 | |
| 1008 | | return try self.constructStruct( |
| 1009 | | ty, |
| 1010 | | &.{ payload_ty, Type.bool }, |
| 1011 | | &.{ payload_id, has_pl_id }, |
| 1012 | | ); |
| 1013 | | }, |
| 1014 | | .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) { |
| 1015 | | inline .array_type, .vector_type => |array_type, tag| { |
| 1016 | | const elem_ty = Type.fromInterned(array_type.child); |
| 1017 | | const elem_ty_ref = try self.resolveType(elem_ty, .indirect); |
| 1018 | | |
| 1019 | | const constituents = try self.gpa.alloc(IdRef, @intCast(ty.arrayLenIncludingSentinel(mod))); |
| 1020 | | defer self.gpa.free(constituents); |
| 1021 | | |
| 1022 | | switch (aggregate.storage) { |
| 1023 | | .bytes => |bytes| { |
| 1024 | | // TODO: This is really space inefficient, perhaps there is a better |
| 1025 | | // way to do it? |
| 1026 | | for (bytes, 0..) |byte, i| { |
| 1027 | | constituents[i] = try self.constInt(elem_ty_ref, byte); |
| 1028 | | } |
| 1029 | | }, |
| 1030 | | .elems => |elems| { |
| 1031 | | for (0..@as(usize, @intCast(array_type.len))) |i| { |
| 1032 | | constituents[i] = try self.constant(elem_ty, Value.fromInterned(elems[i]), .indirect); |
| 1033 | | } |
| 1034 | | }, |
| 1035 | | .repeated_elem => |elem| { |
| 1036 | | const val_id = try self.constant(elem_ty, Value.fromInterned(elem), .indirect); |
| 1037 | | for (0..@as(usize, @intCast(array_type.len))) |i| { |
| 1038 | | constituents[i] = val_id; |
| 1039 | | } |
| 1040 | | }, |
| 967 | var constituents: [2]IdRef = undefined; |
| 968 | var types: [2]Type = undefined; |
| 969 | if (eu_layout.error_first) { |
| 970 | constituents[0] = try self.constant(err_ty, err_val, .indirect); |
| 971 | constituents[1] = try self.constant(payload_ty, payload_val, .indirect); |
| 972 | types = .{ err_ty, payload_ty }; |
| 973 | } else { |
| 974 | constituents[0] = try self.constant(payload_ty, payload_val, .indirect); |
| 975 | constituents[1] = try self.constant(err_ty, err_val, .indirect); |
| 976 | types = .{ payload_ty, err_ty }; |
| 1041 | 977 | } |
| 1042 | 978 | |
| 1043 | | switch (tag) { |
| 1044 | | inline .array_type => { |
| 1045 | | if (array_type.sentinel != .none) { |
| 1046 | | const sentinel = Value.fromInterned(array_type.sentinel); |
| 1047 | | constituents[constituents.len - 1] = try self.constant(elem_ty, sentinel, .indirect); |
| 1048 | | } |
| 1049 | | return self.constructArray(ty, constituents); |
| 1050 | | }, |
| 1051 | | inline .vector_type => return self.constructVector(ty, constituents), |
| 1052 | | else => unreachable, |
| 1053 | | } |
| 979 | return try self.constructStruct(ty, &types, &constituents); |
| 980 | }, |
| 981 | .enum_tag => { |
| 982 | const int_val = try val.intFromEnum(ty, mod); |
| 983 | const int_ty = ty.intTagType(mod); |
| 984 | break :cache try self.constant(int_ty, int_val, repr); |
| 1054 | 985 | }, |
| 1055 | | .struct_type => { |
| 1056 | | const struct_type = mod.typeToStruct(ty).?; |
| 1057 | | if (struct_type.layout == .@"packed") { |
| 1058 | | return self.todo("packed struct constants", .{}); |
| 986 | .ptr => return self.constantPtr(ty, val), |
| 987 | .slice => |slice| { |
| 988 | const ptr_ty = ty.slicePtrFieldType(mod); |
| 989 | const ptr_id = try self.constantPtr(ptr_ty, Value.fromInterned(slice.ptr)); |
| 990 | const len_id = try self.constant(Type.usize, Value.fromInterned(slice.len), .indirect); |
| 991 | return self.constructStruct( |
| 992 | ty, |
| 993 | &.{ ptr_ty, Type.usize }, |
| 994 | &.{ ptr_id, len_id }, |
| 995 | ); |
| 996 | }, |
| 997 | .opt => { |
| 998 | const payload_ty = ty.optionalChild(mod); |
| 999 | const maybe_payload_val = val.optionalValue(mod); |
| 1000 | |
| 1001 | if (!payload_ty.hasRuntimeBits(mod)) { |
| 1002 | break :cache try self.constBool(maybe_payload_val != null, .indirect); |
| 1003 | } else if (ty.optionalReprIsPayload(mod)) { |
| 1004 | // Optional representation is a nullable pointer or slice. |
| 1005 | if (maybe_payload_val) |payload_val| { |
| 1006 | return try self.constant(payload_ty, payload_val, .indirect); |
| 1007 | } else { |
| 1008 | break :cache try self.spv.constNull(result_ty_id); |
| 1009 | } |
| 1059 | 1010 | } |
| 1060 | 1011 | |
| 1061 | | var types = std.ArrayList(Type).init(self.gpa); |
| 1062 | | defer types.deinit(); |
| 1012 | // Optional representation is a structure. |
| 1013 | // { Payload, Bool } |
| 1063 | 1014 | |
| 1064 | | var constituents = std.ArrayList(IdRef).init(self.gpa); |
| 1065 | | defer constituents.deinit(); |
| 1015 | const has_pl_id = try self.constBool(maybe_payload_val != null, .indirect); |
| 1016 | const payload_id = if (maybe_payload_val) |payload_val| |
| 1017 | try self.constant(payload_ty, payload_val, .indirect) |
| 1018 | else |
| 1019 | try self.spv.constUndef(try self.resolveType(payload_ty, .indirect)); |
| 1020 | |
| 1021 | return try self.constructStruct( |
| 1022 | ty, |
| 1023 | &.{ payload_ty, Type.bool }, |
| 1024 | &.{ payload_id, has_pl_id }, |
| 1025 | ); |
| 1026 | }, |
| 1027 | .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) { |
| 1028 | inline .array_type, .vector_type => |array_type, tag| { |
| 1029 | const elem_ty = Type.fromInterned(array_type.child); |
| 1030 | |
| 1031 | const constituents = try self.gpa.alloc(IdRef, @as(u32, @intCast(ty.arrayLenIncludingSentinel(mod)))); |
| 1032 | defer self.gpa.free(constituents); |
| 1033 | |
| 1034 | switch (aggregate.storage) { |
| 1035 | .bytes => |bytes| { |
| 1036 | // TODO: This is really space inefficient, perhaps there is a better |
| 1037 | // way to do it? |
| 1038 | for (bytes, 0..) |byte, i| { |
| 1039 | constituents[i] = try self.constInt(elem_ty, byte, .indirect); |
| 1040 | } |
| 1041 | }, |
| 1042 | .elems => |elems| { |
| 1043 | for (0..@as(usize, @intCast(array_type.len))) |i| { |
| 1044 | constituents[i] = try self.constant(elem_ty, Value.fromInterned(elems[i]), .indirect); |
| 1045 | } |
| 1046 | }, |
| 1047 | .repeated_elem => |elem| { |
| 1048 | const val_id = try self.constant(elem_ty, Value.fromInterned(elem), .indirect); |
| 1049 | for (0..@as(usize, @intCast(array_type.len))) |i| { |
| 1050 | constituents[i] = val_id; |
| 1051 | } |
| 1052 | }, |
| 1053 | } |
| 1066 | 1054 | |
| 1067 | | var it = struct_type.iterateRuntimeOrder(ip); |
| 1068 | | while (it.next()) |field_index| { |
| 1069 | | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1070 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 1071 | | // This is a zero-bit field - we only needed it for the alignment. |
| 1072 | | continue; |
| 1055 | switch (tag) { |
| 1056 | inline .array_type => { |
| 1057 | if (array_type.sentinel != .none) { |
| 1058 | const sentinel = Value.fromInterned(array_type.sentinel); |
| 1059 | constituents[constituents.len - 1] = try self.constant(elem_ty, sentinel, .indirect); |
| 1060 | } |
| 1061 | return self.constructArray(ty, constituents); |
| 1062 | }, |
| 1063 | inline .vector_type => return self.constructVector(ty, constituents), |
| 1064 | else => unreachable, |
| 1065 | } |
| 1066 | }, |
| 1067 | .struct_type => { |
| 1068 | const struct_type = mod.typeToStruct(ty).?; |
| 1069 | if (struct_type.layout == .@"packed") { |
| 1070 | return self.todo("packed struct constants", .{}); |
| 1073 | 1071 | } |
| 1074 | 1072 | |
| 1075 | | // TODO: Padding? |
| 1076 | | const field_val = try val.fieldValue(mod, field_index); |
| 1077 | | const field_id = try self.constant(field_ty, field_val, .indirect); |
| 1073 | var types = std.ArrayList(Type).init(self.gpa); |
| 1074 | defer types.deinit(); |
| 1078 | 1075 | |
| 1079 | | try types.append(field_ty); |
| 1080 | | try constituents.append(field_id); |
| 1081 | | } |
| 1076 | var constituents = std.ArrayList(IdRef).init(self.gpa); |
| 1077 | defer constituents.deinit(); |
| 1082 | 1078 | |
| 1083 | | return try self.constructStruct(ty, types.items, constituents.items); |
| 1079 | var it = struct_type.iterateRuntimeOrder(ip); |
| 1080 | while (it.next()) |field_index| { |
| 1081 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1082 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 1083 | // This is a zero-bit field - we only needed it for the alignment. |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
| 1087 | // TODO: Padding? |
| 1088 | const field_val = try val.fieldValue(mod, field_index); |
| 1089 | const field_id = try self.constant(field_ty, field_val, .indirect); |
| 1090 | |
| 1091 | try types.append(field_ty); |
| 1092 | try constituents.append(field_id); |
| 1093 | } |
| 1094 | |
| 1095 | return try self.constructStruct(ty, types.items, constituents.items); |
| 1096 | }, |
| 1097 | .anon_struct_type => unreachable, // TODO |
| 1098 | else => unreachable, |
| 1084 | 1099 | }, |
| 1085 | | .anon_struct_type => unreachable, // TODO |
| 1086 | | else => unreachable, |
| 1087 | | }, |
| 1088 | | .un => |un| { |
| 1089 | | const active_field = ty.unionTagFieldIndex(Value.fromInterned(un.tag), mod).?; |
| 1090 | | const union_obj = mod.typeToUnion(ty).?; |
| 1091 | | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[active_field]); |
| 1092 | | const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 1093 | | try self.constant(field_ty, Value.fromInterned(un.val), .direct) |
| 1094 | | else |
| 1095 | | null; |
| 1096 | | return try self.unionInit(ty, active_field, payload); |
| 1097 | | }, |
| 1098 | | .memoized_call => unreachable, |
| 1099 | | } |
| 1100 | .un => |un| { |
| 1101 | const active_field = ty.unionTagFieldIndex(Value.fromInterned(un.tag), mod).?; |
| 1102 | const union_obj = mod.typeToUnion(ty).?; |
| 1103 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[active_field]); |
| 1104 | const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 1105 | try self.constant(field_ty, Value.fromInterned(un.val), .direct) |
| 1106 | else |
| 1107 | null; |
| 1108 | return try self.unionInit(ty, active_field, payload); |
| 1109 | }, |
| 1110 | .memoized_call => unreachable, |
| 1111 | } |
| 1112 | }; |
| 1113 | |
| 1114 | try self.intern_map.putNoClobber(self.gpa, .{ val.toIntern(), repr }, cacheable_id); |
| 1115 | |
| 1116 | return cacheable_id; |
| 1100 | 1117 | } |
| 1101 | 1118 | |
| 1102 | 1119 | fn constantPtr(self: *DeclGen, ptr_ty: Type, ptr_val: Value) Error!IdRef { |
| 1103 | | const result_ty_ref = try self.resolveType(ptr_ty, .direct); |
| 1120 | // TODO: Caching?? |
| 1121 | |
| 1122 | const result_ty_id = try self.resolveType(ptr_ty, .direct); |
| 1104 | 1123 | const mod = self.module; |
| 1105 | 1124 | |
| 1106 | | if (ptr_val.isUndef(mod)) return self.spv.constUndef(result_ty_ref); |
| 1125 | if (ptr_val.isUndef(mod)) return self.spv.constUndef(result_ty_id); |
| 1107 | 1126 | |
| 1108 | 1127 | switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) { |
| 1109 | 1128 | .decl => |decl| return try self.constantDeclRef(ptr_ty, decl), |
| ... | ... | @@ -1114,7 +1133,7 @@ const DeclGen = struct { |
| 1114 | 1133 | // that is not implemented by Mesa yet. Therefore, just generate it |
| 1115 | 1134 | // as a runtime operation. |
| 1116 | 1135 | try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ |
| 1117 | | .id_result_type = self.typeId(result_ty_ref), |
| 1136 | .id_result_type = result_ty_id, |
| 1118 | 1137 | .id_result = ptr_id, |
| 1119 | 1138 | .integer_value = try self.constant(Type.usize, Value.fromInterned(int), .direct), |
| 1120 | 1139 | }); |
| ... | ... | @@ -1126,23 +1145,23 @@ const DeclGen = struct { |
| 1126 | 1145 | .elem => |elem_ptr| { |
| 1127 | 1146 | const parent_ptr_ty = Type.fromInterned(mod.intern_pool.typeOf(elem_ptr.base)); |
| 1128 | 1147 | const parent_ptr_id = try self.constantPtr(parent_ptr_ty, Value.fromInterned(elem_ptr.base)); |
| 1129 | | const size_ty_ref = try self.sizeType(); |
| 1130 | | const index_id = try self.constInt(size_ty_ref, elem_ptr.index); |
| 1148 | const index_id = try self.constInt(Type.usize, elem_ptr.index, .direct); |
| 1131 | 1149 | |
| 1132 | 1150 | const elem_ptr_id = try self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id); |
| 1133 | 1151 | |
| 1134 | 1152 | // TODO: Can we consolidate this in ptrElemPtr? |
| 1135 | 1153 | const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T. |
| 1136 | | const elem_ptr_ty_ref = try self.ptrType(elem_ty, self.spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod))); |
| 1154 | const elem_ptr_ty_id = try self.ptrType(elem_ty, self.spvStorageClass(parent_ptr_ty.ptrAddressSpace(mod))); |
| 1137 | 1155 | |
| 1138 | | if (elem_ptr_ty_ref == result_ty_ref) { |
| 1156 | // TODO: Can we remove this ID comparison? |
| 1157 | if (elem_ptr_ty_id == result_ty_id) { |
| 1139 | 1158 | return elem_ptr_id; |
| 1140 | 1159 | } |
| 1141 | 1160 | // This may happen when we have pointer-to-array and the result is |
| 1142 | 1161 | // another pointer-to-array instead of a pointer-to-element. |
| 1143 | 1162 | const result_id = self.spv.allocId(); |
| 1144 | 1163 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 1145 | | .id_result_type = self.typeId(result_ty_ref), |
| 1164 | .id_result_type = result_ty_id, |
| 1146 | 1165 | .id_result = result_id, |
| 1147 | 1166 | .operand = elem_ptr_id, |
| 1148 | 1167 | }); |
| ... | ... | @@ -1166,7 +1185,7 @@ const DeclGen = struct { |
| 1166 | 1185 | |
| 1167 | 1186 | const mod = self.module; |
| 1168 | 1187 | const ip = &mod.intern_pool; |
| 1169 | | const ty_ref = try self.resolveType(ty, .direct); |
| 1188 | const ty_id = try self.resolveType(ty, .direct); |
| 1170 | 1189 | const decl_val = anon_decl.val; |
| 1171 | 1190 | const decl_ty = Type.fromInterned(ip.typeOf(decl_val)); |
| 1172 | 1191 | |
| ... | ... | @@ -1181,7 +1200,7 @@ const DeclGen = struct { |
| 1181 | 1200 | // const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn; |
| 1182 | 1201 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 1183 | 1202 | // Pointer to nothing - return undefoined |
| 1184 | | return self.spv.constUndef(ty_ref); |
| 1203 | return self.spv.constUndef(ty_id); |
| 1185 | 1204 | } |
| 1186 | 1205 | |
| 1187 | 1206 | if (decl_ty.zigTypeTag(mod) == .Fn) { |
| ... | ... | @@ -1190,14 +1209,14 @@ const DeclGen = struct { |
| 1190 | 1209 | |
| 1191 | 1210 | // Anon decl refs are always generic. |
| 1192 | 1211 | assert(ty.ptrAddressSpace(mod) == .generic); |
| 1193 | | const decl_ptr_ty_ref = try self.ptrType(decl_ty, .Generic); |
| 1212 | const decl_ptr_ty_id = try self.ptrType(decl_ty, .Generic); |
| 1194 | 1213 | const ptr_id = try self.resolveAnonDecl(decl_val); |
| 1195 | 1214 | |
| 1196 | | if (decl_ptr_ty_ref != ty_ref) { |
| 1215 | if (decl_ptr_ty_id != ty_id) { |
| 1197 | 1216 | // Differing pointer types, insert a cast. |
| 1198 | 1217 | const casted_ptr_id = self.spv.allocId(); |
| 1199 | 1218 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 1200 | | .id_result_type = self.typeId(ty_ref), |
| 1219 | .id_result_type = ty_id, |
| 1201 | 1220 | .id_result = casted_ptr_id, |
| 1202 | 1221 | .operand = ptr_id, |
| 1203 | 1222 | }); |
| ... | ... | @@ -1209,15 +1228,14 @@ const DeclGen = struct { |
| 1209 | 1228 | |
| 1210 | 1229 | fn constantDeclRef(self: *DeclGen, ty: Type, decl_index: InternPool.DeclIndex) !IdRef { |
| 1211 | 1230 | const mod = self.module; |
| 1212 | | const ty_ref = try self.resolveType(ty, .direct); |
| 1213 | | const ty_id = self.typeId(ty_ref); |
| 1231 | const ty_id = try self.resolveType(ty, .direct); |
| 1214 | 1232 | const decl = mod.declPtr(decl_index); |
| 1215 | 1233 | |
| 1216 | 1234 | switch (mod.intern_pool.indexToKey(decl.val.ip_index)) { |
| 1217 | 1235 | .func => { |
| 1218 | 1236 | // TODO: Properly lower function pointers. For now we are going to hack around it and |
| 1219 | 1237 | // just generate an empty pointer. Function pointers are represented by a pointer to usize. |
| 1220 | | return try self.spv.constUndef(ty_ref); |
| 1238 | return try self.spv.constUndef(ty_id); |
| 1221 | 1239 | }, |
| 1222 | 1240 | .extern_func => unreachable, // TODO |
| 1223 | 1241 | else => {}, |
| ... | ... | @@ -1225,7 +1243,7 @@ const DeclGen = struct { |
| 1225 | 1243 | |
| 1226 | 1244 | if (!decl.typeOf(mod).isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 1227 | 1245 | // Pointer to nothing - return undefined. |
| 1228 | | return self.spv.constUndef(ty_ref); |
| 1246 | return self.spv.constUndef(ty_id); |
| 1229 | 1247 | } |
| 1230 | 1248 | |
| 1231 | 1249 | const spv_decl_index = try self.object.resolveDecl(mod, decl_index); |
| ... | ... | @@ -1239,14 +1257,14 @@ const DeclGen = struct { |
| 1239 | 1257 | const final_storage_class = self.spvStorageClass(decl.@"addrspace"); |
| 1240 | 1258 | try self.addFunctionDep(spv_decl_index, final_storage_class); |
| 1241 | 1259 | |
| 1242 | | const decl_ptr_ty_ref = try self.ptrType(decl.typeOf(mod), final_storage_class); |
| 1260 | const decl_ptr_ty_id = try self.ptrType(decl.typeOf(mod), final_storage_class); |
| 1243 | 1261 | |
| 1244 | 1262 | const ptr_id = switch (final_storage_class) { |
| 1245 | | .Generic => try self.castToGeneric(self.typeId(decl_ptr_ty_ref), decl_id), |
| 1263 | .Generic => try self.castToGeneric(decl_ptr_ty_id, decl_id), |
| 1246 | 1264 | else => decl_id, |
| 1247 | 1265 | }; |
| 1248 | 1266 | |
| 1249 | | if (decl_ptr_ty_ref != ty_ref) { |
| 1267 | if (decl_ptr_ty_id != ty_id) { |
| 1250 | 1268 | // Differing pointer types, insert a cast. |
| 1251 | 1269 | const casted_ptr_id = self.spv.allocId(); |
| 1252 | 1270 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| ... | ... | @@ -1261,28 +1279,18 @@ const DeclGen = struct { |
| 1261 | 1279 | } |
| 1262 | 1280 | |
| 1263 | 1281 | // Turn a Zig type's name into a cache reference. |
| 1264 | | fn resolveTypeName(self: *DeclGen, ty: Type) !CacheString { |
| 1282 | fn resolveTypeName(self: *DeclGen, ty: Type) ![]const u8 { |
| 1265 | 1283 | var name = std.ArrayList(u8).init(self.gpa); |
| 1266 | 1284 | defer name.deinit(); |
| 1267 | 1285 | try ty.print(name.writer(), self.module); |
| 1268 | | return try self.spv.resolveString(name.items); |
| 1269 | | } |
| 1270 | | |
| 1271 | | /// Turn a Zig type into a SPIR-V Type, and return its type result-id. |
| 1272 | | fn resolveTypeId(self: *DeclGen, ty: Type) !IdResultType { |
| 1273 | | const type_ref = try self.resolveType(ty, .direct); |
| 1274 | | return self.spv.resultId(type_ref); |
| 1275 | | } |
| 1276 | | |
| 1277 | | fn typeId(self: *DeclGen, ty_ref: CacheRef) IdRef { |
| 1278 | | return self.spv.resultId(ty_ref); |
| 1286 | return try name.toOwnedSlice(); |
| 1279 | 1287 | } |
| 1280 | 1288 | |
| 1281 | 1289 | /// Create an integer type suitable for storing at least 'bits' bits. |
| 1282 | 1290 | /// The integer type that is returned by this function is the type that is used to perform |
| 1283 | 1291 | /// actual operations (as well as store) a Zig type of a particular number of bits. To create |
| 1284 | 1292 | /// a type with an exact size, use SpvModule.intType. |
| 1285 | | fn intType(self: *DeclGen, signedness: std.builtin.Signedness, bits: u16) !CacheRef { |
| 1293 | fn intType(self: *DeclGen, signedness: std.builtin.Signedness, bits: u16) !IdRef { |
| 1286 | 1294 | const backing_bits = self.backingIntBits(bits) orelse { |
| 1287 | 1295 | // TODO: Integers too big for any native type are represented as "composite integers": |
| 1288 | 1296 | // An array of largestSupportedIntBits. |
| ... | ... | @@ -1297,36 +1305,69 @@ const DeclGen = struct { |
| 1297 | 1305 | return self.spv.intType(.unsigned, backing_bits); |
| 1298 | 1306 | } |
| 1299 | 1307 | |
| 1300 | | /// Create an integer type that represents 'usize'. |
| 1301 | | fn sizeType(self: *DeclGen) !CacheRef { |
| 1302 | | return try self.intType(.unsigned, self.getTarget().ptrBitWidth()); |
| 1308 | fn arrayType(self: *DeclGen, len: u32, child_ty: IdRef) !IdRef { |
| 1309 | // TODO: Cache?? |
| 1310 | const len_id = try self.constInt(Type.u32, len, .direct); |
| 1311 | const result_id = self.spv.allocId(); |
| 1312 | |
| 1313 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypeArray, .{ |
| 1314 | .id_result = result_id, |
| 1315 | .element_type = child_ty, |
| 1316 | .length = len_id, |
| 1317 | }); |
| 1318 | return result_id; |
| 1303 | 1319 | } |
| 1304 | 1320 | |
| 1305 | | fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !CacheRef { |
| 1321 | fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !IdRef { |
| 1306 | 1322 | const key = .{ child_ty.toIntern(), storage_class }; |
| 1307 | | const entry = try self.wip_pointers.getOrPut(self.gpa, key); |
| 1323 | const entry = try self.ptr_types.getOrPut(self.gpa, key); |
| 1308 | 1324 | if (entry.found_existing) { |
| 1309 | | const fwd_ref = entry.value_ptr.*; |
| 1310 | | try self.spv.cache.recursive_ptrs.put(self.spv.gpa, fwd_ref, {}); |
| 1311 | | return fwd_ref; |
| 1325 | const fwd_id = entry.value_ptr.ty_id; |
| 1326 | if (!entry.value_ptr.fwd_emitted) { |
| 1327 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypeForwardPointer, .{ |
| 1328 | .pointer_type = fwd_id, |
| 1329 | .storage_class = storage_class, |
| 1330 | }); |
| 1331 | entry.value_ptr.fwd_emitted = true; |
| 1332 | } |
| 1333 | return fwd_id; |
| 1312 | 1334 | } |
| 1313 | 1335 | |
| 1314 | | const fwd_ref = try self.spv.resolve(.{ .fwd_ptr_type = .{ |
| 1315 | | .zig_child_type = child_ty.toIntern(), |
| 1316 | | .storage_class = storage_class, |
| 1317 | | } }); |
| 1318 | | entry.value_ptr.* = fwd_ref; |
| 1336 | const result_id = self.spv.allocId(); |
| 1337 | entry.value_ptr.* = .{ |
| 1338 | .ty_id = result_id, |
| 1339 | .fwd_emitted = false, |
| 1340 | }; |
| 1319 | 1341 | |
| 1320 | | const child_ty_ref = try self.resolveType(child_ty, .indirect); |
| 1321 | | _ = try self.spv.resolve(.{ .ptr_type = .{ |
| 1342 | const child_ty_id = try self.resolveType(child_ty, .indirect); |
| 1343 | |
| 1344 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{ |
| 1345 | .id_result = result_id, |
| 1322 | 1346 | .storage_class = storage_class, |
| 1323 | | .child_type = child_ty_ref, |
| 1324 | | .fwd = fwd_ref, |
| 1325 | | } }); |
| 1347 | .type = child_ty_id, |
| 1348 | }); |
| 1349 | |
| 1350 | return result_id; |
| 1351 | } |
| 1352 | |
| 1353 | fn functionType(self: *DeclGen, return_ty: Type, param_types: []const Type) !IdRef { |
| 1354 | // TODO: Cache?? |
| 1326 | 1355 | |
| 1327 | | assert(self.wip_pointers.remove(key)); |
| 1356 | const param_ids = try self.gpa.alloc(IdRef, param_types.len); |
| 1357 | defer self.gpa.free(param_ids); |
| 1328 | 1358 | |
| 1329 | | return fwd_ref; |
| 1359 | for (param_types, param_ids) |param_ty, *param_id| { |
| 1360 | param_id.* = try self.resolveType(param_ty, .direct); |
| 1361 | } |
| 1362 | |
| 1363 | const ty_id = self.spv.allocId(); |
| 1364 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypeFunction, .{ |
| 1365 | .id_result = ty_id, |
| 1366 | .return_type = try self.resolveFnReturnType(return_ty), |
| 1367 | .id_ref_2 = param_ids, |
| 1368 | }); |
| 1369 | |
| 1370 | return ty_id; |
| 1330 | 1371 | } |
| 1331 | 1372 | |
| 1332 | 1373 | /// Generate a union type. Union types are always generated with the |
| ... | ... | @@ -1347,7 +1388,7 @@ const DeclGen = struct { |
| 1347 | 1388 | /// padding: [padding_size]u8, |
| 1348 | 1389 | /// } |
| 1349 | 1390 | /// If any of the fields' size is 0, it will be omitted. |
| 1350 | | fn resolveUnionType(self: *DeclGen, ty: Type) !CacheRef { |
| 1391 | fn resolveUnionType(self: *DeclGen, ty: Type) !IdRef { |
| 1351 | 1392 | const mod = self.module; |
| 1352 | 1393 | const ip = &mod.intern_pool; |
| 1353 | 1394 | const union_obj = mod.typeToUnion(ty).?; |
| ... | ... | @@ -1362,48 +1403,43 @@ const DeclGen = struct { |
| 1362 | 1403 | return try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect); |
| 1363 | 1404 | } |
| 1364 | 1405 | |
| 1365 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1406 | var member_types: [4]IdRef = undefined; |
| 1407 | var member_names: [4][]const u8 = undefined; |
| 1366 | 1408 | |
| 1367 | | var member_types: [4]CacheRef = undefined; |
| 1368 | | var member_names: [4]CacheString = undefined; |
| 1369 | | |
| 1370 | | const u8_ty_ref = try self.intType(.unsigned, 8); // TODO: What if Int8Type is not enabled? |
| 1409 | const u8_ty_id = try self.resolveType(Type.u8, .direct); // TODO: What if Int8Type is not enabled? |
| 1371 | 1410 | |
| 1372 | 1411 | if (layout.tag_size != 0) { |
| 1373 | | const tag_ty_ref = try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect); |
| 1374 | | member_types[layout.tag_index] = tag_ty_ref; |
| 1375 | | member_names[layout.tag_index] = try self.spv.resolveString("(tag)"); |
| 1412 | const tag_ty_id = try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect); |
| 1413 | member_types[layout.tag_index] = tag_ty_id; |
| 1414 | member_names[layout.tag_index] = "(tag)"; |
| 1376 | 1415 | } |
| 1377 | 1416 | |
| 1378 | 1417 | if (layout.payload_size != 0) { |
| 1379 | | const payload_ty_ref = try self.resolveType(layout.payload_ty, .indirect); |
| 1380 | | member_types[layout.payload_index] = payload_ty_ref; |
| 1381 | | member_names[layout.payload_index] = try self.spv.resolveString("(payload)"); |
| 1418 | const payload_ty_id = try self.resolveType(layout.payload_ty, .indirect); |
| 1419 | member_types[layout.payload_index] = payload_ty_id; |
| 1420 | member_names[layout.payload_index] = "(payload)"; |
| 1382 | 1421 | } |
| 1383 | 1422 | |
| 1384 | 1423 | if (layout.payload_padding_size != 0) { |
| 1385 | | const payload_padding_ty_ref = try self.spv.arrayType(@intCast(layout.payload_padding_size), u8_ty_ref); |
| 1386 | | member_types[layout.payload_padding_index] = payload_padding_ty_ref; |
| 1387 | | member_names[layout.payload_padding_index] = try self.spv.resolveString("(payload padding)"); |
| 1424 | const payload_padding_ty_id = try self.arrayType(@intCast(layout.payload_padding_size), u8_ty_id); |
| 1425 | member_types[layout.payload_padding_index] = payload_padding_ty_id; |
| 1426 | member_names[layout.payload_padding_index] = "(payload padding)"; |
| 1388 | 1427 | } |
| 1389 | 1428 | |
| 1390 | 1429 | if (layout.padding_size != 0) { |
| 1391 | | const padding_ty_ref = try self.spv.arrayType(@intCast(layout.padding_size), u8_ty_ref); |
| 1392 | | member_types[layout.padding_index] = padding_ty_ref; |
| 1393 | | member_names[layout.padding_index] = try self.spv.resolveString("(padding)"); |
| 1430 | const padding_ty_id = try self.arrayType(@intCast(layout.padding_size), u8_ty_id); |
| 1431 | member_types[layout.padding_index] = padding_ty_id; |
| 1432 | member_names[layout.padding_index] = "(padding)"; |
| 1394 | 1433 | } |
| 1395 | 1434 | |
| 1396 | | const ty_ref = try self.spv.resolve(.{ .struct_type = .{ |
| 1397 | | .name = try self.resolveTypeName(ty), |
| 1398 | | .member_types = member_types[0..layout.total_fields], |
| 1399 | | .member_names = member_names[0..layout.total_fields], |
| 1400 | | } }); |
| 1401 | | |
| 1402 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1403 | | return ty_ref; |
| 1435 | const result_id = try self.spv.structType(member_types[0..layout.total_fields], member_names[0..layout.total_fields]); |
| 1436 | const type_name = try self.resolveTypeName(ty); |
| 1437 | defer self.gpa.free(type_name); |
| 1438 | try self.spv.debugName(result_id, type_name); |
| 1439 | return result_id; |
| 1404 | 1440 | } |
| 1405 | 1441 | |
| 1406 | | fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !CacheRef { |
| 1442 | fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !IdRef { |
| 1407 | 1443 | const mod = self.module; |
| 1408 | 1444 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 1409 | 1445 | // If the return type is an error set or an error union, then we make this |
| ... | ... | @@ -1420,26 +1456,46 @@ const DeclGen = struct { |
| 1420 | 1456 | } |
| 1421 | 1457 | |
| 1422 | 1458 | /// Turn a Zig type into a SPIR-V Type, and return a reference to it. |
| 1423 | | fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!CacheRef { |
| 1459 | fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!IdRef { |
| 1460 | if (self.intern_map.get(.{ ty.toIntern(), repr })) |id| { |
| 1461 | return id; |
| 1462 | } |
| 1463 | |
| 1464 | const id = try self.resolveTypeInner(ty, repr); |
| 1465 | try self.intern_map.put(self.gpa, .{ ty.toIntern(), repr }, id); |
| 1466 | return id; |
| 1467 | } |
| 1468 | |
| 1469 | fn resolveTypeInner(self: *DeclGen, ty: Type, repr: Repr) Error!IdRef { |
| 1424 | 1470 | const mod = self.module; |
| 1425 | 1471 | const ip = &mod.intern_pool; |
| 1426 | 1472 | log.debug("resolveType: ty = {}", .{ty.fmt(mod)}); |
| 1427 | 1473 | const target = self.getTarget(); |
| 1474 | |
| 1475 | const section = &self.spv.sections.types_globals_constants; |
| 1476 | |
| 1428 | 1477 | switch (ty.zigTypeTag(mod)) { |
| 1429 | 1478 | .NoReturn => { |
| 1430 | 1479 | assert(repr == .direct); |
| 1431 | | return try self.spv.resolve(.void_type); |
| 1480 | return try self.spv.voidType(); |
| 1432 | 1481 | }, |
| 1433 | 1482 | .Void => switch (repr) { |
| 1434 | | .direct => return try self.spv.resolve(.void_type), |
| 1483 | .direct => { |
| 1484 | return try self.spv.voidType(); |
| 1485 | }, |
| 1435 | 1486 | // Pointers to void |
| 1436 | | .indirect => return try self.spv.resolve(.{ .opaque_type = .{ |
| 1437 | | .name = try self.spv.resolveString("void"), |
| 1438 | | } }), |
| 1487 | .indirect => { |
| 1488 | const result_id = self.spv.allocId(); |
| 1489 | try section.emit(self.spv.gpa, .OpTypeOpaque, .{ |
| 1490 | .id_result = result_id, |
| 1491 | .literal_string = "void", |
| 1492 | }); |
| 1493 | return result_id; |
| 1494 | }, |
| 1439 | 1495 | }, |
| 1440 | 1496 | .Bool => switch (repr) { |
| 1441 | | .direct => return try self.spv.resolve(.bool_type), |
| 1442 | | .indirect => return try self.intType(.unsigned, 1), |
| 1497 | .direct => return try self.spv.boolType(), |
| 1498 | .indirect => return try self.resolveType(Type.u1, .indirect), |
| 1443 | 1499 | }, |
| 1444 | 1500 | .Int => { |
| 1445 | 1501 | const int_info = ty.intInfo(mod); |
| ... | ... | @@ -1447,15 +1503,18 @@ const DeclGen = struct { |
| 1447 | 1503 | // Some times, the backend will be asked to generate a pointer to i0. OpTypeInt |
| 1448 | 1504 | // with 0 bits is invalid, so return an opaque type in this case. |
| 1449 | 1505 | assert(repr == .indirect); |
| 1450 | | return try self.spv.resolve(.{ .opaque_type = .{ |
| 1451 | | .name = try self.spv.resolveString("u0"), |
| 1452 | | } }); |
| 1506 | const result_id = self.spv.allocId(); |
| 1507 | try section.emit(self.spv.gpa, .OpTypeOpaque, .{ |
| 1508 | .id_result = result_id, |
| 1509 | .literal_string = "u0", |
| 1510 | }); |
| 1511 | return result_id; |
| 1453 | 1512 | } |
| 1454 | 1513 | return try self.intType(int_info.signedness, int_info.bits); |
| 1455 | 1514 | }, |
| 1456 | 1515 | .Enum => { |
| 1457 | 1516 | const tag_ty = ty.intTagType(mod); |
| 1458 | | return self.resolveType(tag_ty, repr); |
| 1517 | return try self.resolveType(tag_ty, repr); |
| 1459 | 1518 | }, |
| 1460 | 1519 | .Float => { |
| 1461 | 1520 | // We can (and want) not really emulate floating points with other floating point types like with the integer types, |
| ... | ... | @@ -1473,27 +1532,29 @@ const DeclGen = struct { |
| 1473 | 1532 | return self.fail("Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); |
| 1474 | 1533 | } |
| 1475 | 1534 | |
| 1476 | | return try self.spv.resolve(.{ .float_type = .{ .bits = bits } }); |
| 1535 | return try self.spv.floatType(bits); |
| 1477 | 1536 | }, |
| 1478 | 1537 | .Array => { |
| 1479 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1480 | | |
| 1481 | 1538 | const elem_ty = ty.childType(mod); |
| 1482 | | const elem_ty_ref = try self.resolveType(elem_ty, .indirect); |
| 1539 | const elem_ty_id = try self.resolveType(elem_ty, .indirect); |
| 1483 | 1540 | const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel(mod)) orelse { |
| 1484 | 1541 | return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)}); |
| 1485 | 1542 | }; |
| 1486 | | const ty_ref = if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) blk: { |
| 1543 | |
| 1544 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 1487 | 1545 | // The size of the array would be 0, but that is not allowed in SPIR-V. |
| 1488 | 1546 | // This path can be reached when the backend is asked to generate a pointer to |
| 1489 | 1547 | // an array of some zero-bit type. This should always be an indirect path. |
| 1490 | 1548 | assert(repr == .indirect); |
| 1491 | 1549 | |
| 1492 | 1550 | // We cannot use the child type here, so just use an opaque type. |
| 1493 | | break :blk try self.spv.resolve(.{ .opaque_type = .{ |
| 1494 | | .name = try self.spv.resolveString("zero-sized array"), |
| 1495 | | } }); |
| 1496 | | } else if (total_len == 0) blk: { |
| 1551 | const result_id = self.spv.allocId(); |
| 1552 | try section.emit(self.spv.gpa, .OpTypeOpaque, .{ |
| 1553 | .id_result = result_id, |
| 1554 | .literal_string = "zero-sized array", |
| 1555 | }); |
| 1556 | return result_id; |
| 1557 | } else if (total_len == 0) { |
| 1497 | 1558 | // The size of the array would be 0, but that is not allowed in SPIR-V. |
| 1498 | 1559 | // This path can be reached for example when there is a slicing of a pointer |
| 1499 | 1560 | // that produces a zero-length array. In all cases where this type can be generated, |
| ... | ... | @@ -1503,16 +1564,13 @@ const DeclGen = struct { |
| 1503 | 1564 | // In this case, we have an array of a non-zero sized type. In this case, |
| 1504 | 1565 | // generate an array of 1 element instead, so that ptr_elem_ptr instructions |
| 1505 | 1566 | // can be lowered to ptrAccessChain instead of manually performing the math. |
| 1506 | | break :blk try self.spv.arrayType(1, elem_ty_ref); |
| 1507 | | } else try self.spv.arrayType(total_len, elem_ty_ref); |
| 1508 | | |
| 1509 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1510 | | return ty_ref; |
| 1567 | return try self.arrayType(1, elem_ty_id); |
| 1568 | } else { |
| 1569 | return try self.arrayType(total_len, elem_ty_id); |
| 1570 | } |
| 1511 | 1571 | }, |
| 1512 | 1572 | .Fn => switch (repr) { |
| 1513 | 1573 | .direct => { |
| 1514 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1515 | | |
| 1516 | 1574 | const fn_info = mod.typeToFunc(ty).?; |
| 1517 | 1575 | |
| 1518 | 1576 | comptime assert(zig_call_abi_ver == 3); |
| ... | ... | @@ -1525,75 +1583,67 @@ const DeclGen = struct { |
| 1525 | 1583 | if (fn_info.is_var_args) |
| 1526 | 1584 | return self.fail("VarArgs functions are unsupported for SPIR-V", .{}); |
| 1527 | 1585 | |
| 1528 | | const param_ty_refs = try self.gpa.alloc(CacheRef, fn_info.param_types.len); |
| 1529 | | defer self.gpa.free(param_ty_refs); |
| 1586 | // Note: Logic is different from functionType(). |
| 1587 | const param_ty_ids = try self.gpa.alloc(IdRef, fn_info.param_types.len); |
| 1588 | defer self.gpa.free(param_ty_ids); |
| 1530 | 1589 | var param_index: usize = 0; |
| 1531 | 1590 | for (fn_info.param_types.get(ip)) |param_ty_index| { |
| 1532 | 1591 | const param_ty = Type.fromInterned(param_ty_index); |
| 1533 | 1592 | if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 1534 | 1593 | |
| 1535 | | param_ty_refs[param_index] = try self.resolveType(param_ty, .direct); |
| 1594 | param_ty_ids[param_index] = try self.resolveType(param_ty, .direct); |
| 1536 | 1595 | param_index += 1; |
| 1537 | 1596 | } |
| 1538 | | const return_ty_ref = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type)); |
| 1539 | 1597 | |
| 1540 | | const ty_ref = try self.spv.resolve(.{ .function_type = .{ |
| 1541 | | .return_type = return_ty_ref, |
| 1542 | | .parameters = param_ty_refs[0..param_index], |
| 1543 | | } }); |
| 1598 | const return_ty_id = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type)); |
| 1599 | |
| 1600 | const result_id = self.spv.allocId(); |
| 1601 | try section.emit(self.spv.gpa, .OpTypeFunction, .{ |
| 1602 | .id_result = result_id, |
| 1603 | .return_type = return_ty_id, |
| 1604 | .id_ref_2 = param_ty_ids[0..param_index], |
| 1605 | }); |
| 1544 | 1606 | |
| 1545 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1546 | | return ty_ref; |
| 1607 | return result_id; |
| 1547 | 1608 | }, |
| 1548 | 1609 | .indirect => { |
| 1549 | 1610 | // TODO: Represent function pointers properly. |
| 1550 | 1611 | // For now, just use an usize type. |
| 1551 | | return try self.sizeType(); |
| 1612 | return try self.resolveType(Type.usize, .indirect); |
| 1552 | 1613 | }, |
| 1553 | 1614 | }, |
| 1554 | 1615 | .Pointer => { |
| 1555 | 1616 | const ptr_info = ty.ptrInfo(mod); |
| 1556 | 1617 | |
| 1557 | | // Note: Don't cache this pointer type, it would mess up the recursive pointer functionality |
| 1558 | | // in ptrType()! |
| 1559 | | |
| 1560 | 1618 | const storage_class = self.spvStorageClass(ptr_info.flags.address_space); |
| 1561 | | const ptr_ty_ref = try self.ptrType(Type.fromInterned(ptr_info.child), storage_class); |
| 1619 | const ptr_ty_id = try self.ptrType(Type.fromInterned(ptr_info.child), storage_class); |
| 1562 | 1620 | |
| 1563 | 1621 | if (ptr_info.flags.size != .Slice) { |
| 1564 | | return ptr_ty_ref; |
| 1622 | return ptr_ty_id; |
| 1565 | 1623 | } |
| 1566 | 1624 | |
| 1567 | | const size_ty_ref = try self.sizeType(); |
| 1568 | | return self.spv.resolve(.{ .struct_type = .{ |
| 1569 | | .member_types = &.{ ptr_ty_ref, size_ty_ref }, |
| 1570 | | .member_names = &.{ |
| 1571 | | try self.spv.resolveString("ptr"), |
| 1572 | | try self.spv.resolveString("len"), |
| 1573 | | }, |
| 1574 | | } }); |
| 1625 | const size_ty_id = try self.resolveType(Type.usize, .direct); |
| 1626 | return self.spv.structType( |
| 1627 | &.{ ptr_ty_id, size_ty_id }, |
| 1628 | &.{ "ptr", "len" }, |
| 1629 | ); |
| 1575 | 1630 | }, |
| 1576 | 1631 | .Vector => { |
| 1577 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1578 | | |
| 1579 | 1632 | const elem_ty = ty.childType(mod); |
| 1580 | | const elem_ty_ref = try self.resolveType(elem_ty, .indirect); |
| 1633 | // TODO: Make `.direct`. |
| 1634 | const elem_ty_id = try self.resolveType(elem_ty, .indirect); |
| 1581 | 1635 | const len = ty.vectorLen(mod); |
| 1582 | 1636 | |
| 1583 | | const ty_ref = if (self.isVector(ty)) |
| 1584 | | try self.spv.vectorType(len, elem_ty_ref) |
| 1585 | | else |
| 1586 | | try self.spv.arrayType(len, elem_ty_ref); |
| 1587 | | |
| 1588 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1589 | | return ty_ref; |
| 1637 | if (self.isVector(ty)) { |
| 1638 | return try self.spv.vectorType(len, elem_ty_id); |
| 1639 | } else { |
| 1640 | return try self.arrayType(len, elem_ty_id); |
| 1641 | } |
| 1590 | 1642 | }, |
| 1591 | 1643 | .Struct => { |
| 1592 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1593 | | |
| 1594 | 1644 | const struct_type = switch (ip.indexToKey(ty.toIntern())) { |
| 1595 | 1645 | .anon_struct_type => |tuple| { |
| 1596 | | const member_types = try self.gpa.alloc(CacheRef, tuple.values.len); |
| 1646 | const member_types = try self.gpa.alloc(IdRef, tuple.values.len); |
| 1597 | 1647 | defer self.gpa.free(member_types); |
| 1598 | 1648 | |
| 1599 | 1649 | var member_index: usize = 0; |
| ... | ... | @@ -1604,13 +1654,11 @@ const DeclGen = struct { |
| 1604 | 1654 | member_index += 1; |
| 1605 | 1655 | } |
| 1606 | 1656 | |
| 1607 | | const ty_ref = try self.spv.resolve(.{ .struct_type = .{ |
| 1608 | | .name = try self.resolveTypeName(ty), |
| 1609 | | .member_types = member_types[0..member_index], |
| 1610 | | } }); |
| 1611 | | |
| 1612 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1613 | | return ty_ref; |
| 1657 | const result_id = try self.spv.structType(member_types[0..member_index], null); |
| 1658 | const type_name = try self.resolveTypeName(ty); |
| 1659 | defer self.gpa.free(type_name); |
| 1660 | try self.spv.debugName(result_id, type_name); |
| 1661 | return result_id; |
| 1614 | 1662 | }, |
| 1615 | 1663 | .struct_type => ip.loadStructType(ty.toIntern()), |
| 1616 | 1664 | else => unreachable, |
| ... | ... | @@ -1620,10 +1668,10 @@ const DeclGen = struct { |
| 1620 | 1668 | return try self.resolveType(Type.fromInterned(struct_type.backingIntType(ip).*), .direct); |
| 1621 | 1669 | } |
| 1622 | 1670 | |
| 1623 | | var member_types = std.ArrayList(CacheRef).init(self.gpa); |
| 1671 | var member_types = std.ArrayList(IdRef).init(self.gpa); |
| 1624 | 1672 | defer member_types.deinit(); |
| 1625 | 1673 | |
| 1626 | | var member_names = std.ArrayList(CacheString).init(self.gpa); |
| 1674 | var member_names = std.ArrayList([]const u8).init(self.gpa); |
| 1627 | 1675 | defer member_names.deinit(); |
| 1628 | 1676 | |
| 1629 | 1677 | var it = struct_type.iterateRuntimeOrder(ip); |
| ... | ... | @@ -1637,17 +1685,14 @@ const DeclGen = struct { |
| 1637 | 1685 | const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse |
| 1638 | 1686 | try ip.getOrPutStringFmt(mod.gpa, "{d}", .{field_index}); |
| 1639 | 1687 | try member_types.append(try self.resolveType(field_ty, .indirect)); |
| 1640 | | try member_names.append(try self.spv.resolveString(ip.stringToSlice(field_name))); |
| 1688 | try member_names.append(ip.stringToSlice(field_name)); |
| 1641 | 1689 | } |
| 1642 | 1690 | |
| 1643 | | const ty_ref = try self.spv.resolve(.{ .struct_type = .{ |
| 1644 | | .name = try self.resolveTypeName(ty), |
| 1645 | | .member_types = member_types.items, |
| 1646 | | .member_names = member_names.items, |
| 1647 | | } }); |
| 1648 | | |
| 1649 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1650 | | return ty_ref; |
| 1691 | const result_id = try self.spv.structType(member_types.items, member_names.items); |
| 1692 | const type_name = try self.resolveTypeName(ty); |
| 1693 | defer self.gpa.free(type_name); |
| 1694 | try self.spv.debugName(result_id, type_name); |
| 1695 | return result_id; |
| 1651 | 1696 | }, |
| 1652 | 1697 | .Optional => { |
| 1653 | 1698 | const payload_ty = ty.optionalChild(mod); |
| ... | ... | @@ -1658,77 +1703,58 @@ const DeclGen = struct { |
| 1658 | 1703 | return try self.resolveType(Type.bool, .indirect); |
| 1659 | 1704 | } |
| 1660 | 1705 | |
| 1661 | | const payload_ty_ref = try self.resolveType(payload_ty, .indirect); |
| 1706 | const payload_ty_id = try self.resolveType(payload_ty, .indirect); |
| 1662 | 1707 | if (ty.optionalReprIsPayload(mod)) { |
| 1663 | 1708 | // Optional is actually a pointer or a slice. |
| 1664 | | return payload_ty_ref; |
| 1709 | return payload_ty_id; |
| 1665 | 1710 | } |
| 1666 | 1711 | |
| 1667 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1668 | | |
| 1669 | | const bool_ty_ref = try self.resolveType(Type.bool, .indirect); |
| 1712 | const bool_ty_id = try self.resolveType(Type.bool, .indirect); |
| 1670 | 1713 | |
| 1671 | | const ty_ref = try self.spv.resolve(.{ .struct_type = .{ |
| 1672 | | .member_types = &.{ payload_ty_ref, bool_ty_ref }, |
| 1673 | | .member_names = &.{ |
| 1674 | | try self.spv.resolveString("payload"), |
| 1675 | | try self.spv.resolveString("valid"), |
| 1676 | | }, |
| 1677 | | } }); |
| 1678 | | |
| 1679 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1680 | | return ty_ref; |
| 1714 | return try self.spv.structType( |
| 1715 | &.{ payload_ty_id, bool_ty_id }, |
| 1716 | &.{ "payload", "valid" }, |
| 1717 | ); |
| 1681 | 1718 | }, |
| 1682 | 1719 | .Union => return try self.resolveUnionType(ty), |
| 1683 | | .ErrorSet => return try self.intType(.unsigned, 16), |
| 1720 | .ErrorSet => return try self.resolveType(Type.u16, repr), |
| 1684 | 1721 | .ErrorUnion => { |
| 1685 | 1722 | const payload_ty = ty.errorUnionPayload(mod); |
| 1686 | | const error_ty_ref = try self.resolveType(Type.anyerror, .indirect); |
| 1723 | const error_ty_id = try self.resolveType(Type.anyerror, .indirect); |
| 1687 | 1724 | |
| 1688 | 1725 | const eu_layout = self.errorUnionLayout(payload_ty); |
| 1689 | 1726 | if (!eu_layout.payload_has_bits) { |
| 1690 | | return error_ty_ref; |
| 1727 | return error_ty_id; |
| 1691 | 1728 | } |
| 1692 | 1729 | |
| 1693 | | if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref; |
| 1730 | const payload_ty_id = try self.resolveType(payload_ty, .indirect); |
| 1694 | 1731 | |
| 1695 | | const payload_ty_ref = try self.resolveType(payload_ty, .indirect); |
| 1696 | | |
| 1697 | | var member_types: [2]CacheRef = undefined; |
| 1698 | | var member_names: [2]CacheString = undefined; |
| 1732 | var member_types: [2]IdRef = undefined; |
| 1733 | var member_names: [2][]const u8 = undefined; |
| 1699 | 1734 | if (eu_layout.error_first) { |
| 1700 | 1735 | // Put the error first |
| 1701 | | member_types = .{ error_ty_ref, payload_ty_ref }; |
| 1702 | | member_names = .{ |
| 1703 | | try self.spv.resolveString("error"), |
| 1704 | | try self.spv.resolveString("payload"), |
| 1705 | | }; |
| 1736 | member_types = .{ error_ty_id, payload_ty_id }; |
| 1737 | member_names = .{ "error", "payload" }; |
| 1706 | 1738 | // TODO: ABI padding? |
| 1707 | 1739 | } else { |
| 1708 | 1740 | // Put the payload first. |
| 1709 | | member_types = .{ payload_ty_ref, error_ty_ref }; |
| 1710 | | member_names = .{ |
| 1711 | | try self.spv.resolveString("payload"), |
| 1712 | | try self.spv.resolveString("error"), |
| 1713 | | }; |
| 1741 | member_types = .{ payload_ty_id, error_ty_id }; |
| 1742 | member_names = .{ "payload", "error" }; |
| 1714 | 1743 | // TODO: ABI padding? |
| 1715 | 1744 | } |
| 1716 | 1745 | |
| 1717 | | const ty_ref = try self.spv.resolve(.{ .struct_type = .{ |
| 1718 | | .name = try self.resolveTypeName(ty), |
| 1719 | | .member_types = &member_types, |
| 1720 | | .member_names = &member_names, |
| 1721 | | } }); |
| 1722 | | |
| 1723 | | try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref }); |
| 1724 | | return ty_ref; |
| 1746 | return try self.spv.structType(&member_types, &member_names); |
| 1725 | 1747 | }, |
| 1726 | 1748 | .Opaque => { |
| 1727 | | return try self.spv.resolve(.{ |
| 1728 | | .opaque_type = .{ |
| 1729 | | .name = .none, // TODO |
| 1730 | | }, |
| 1749 | const type_name = try self.resolveTypeName(ty); |
| 1750 | defer self.gpa.free(type_name); |
| 1751 | |
| 1752 | const result_id = self.spv.allocId(); |
| 1753 | try section.emit(self.spv.gpa, .OpTypeOpaque, .{ |
| 1754 | .id_result = result_id, |
| 1755 | .literal_string = type_name, |
| 1731 | 1756 | }); |
| 1757 | return result_id; |
| 1732 | 1758 | }, |
| 1733 | 1759 | |
| 1734 | 1760 | .Null, |
| ... | ... | @@ -1736,9 +1762,10 @@ const DeclGen = struct { |
| 1736 | 1762 | .EnumLiteral, |
| 1737 | 1763 | .ComptimeFloat, |
| 1738 | 1764 | .ComptimeInt, |
| 1765 | .Type, |
| 1739 | 1766 | => unreachable, // Must be comptime. |
| 1740 | 1767 | |
| 1741 | | else => |tag| return self.todo("Implement zig type '{}'", .{tag}), |
| 1768 | .Frame, .AnyFrame => unreachable, // TODO |
| 1742 | 1769 | } |
| 1743 | 1770 | } |
| 1744 | 1771 | |
| ... | ... | @@ -1887,7 +1914,6 @@ const DeclGen = struct { |
| 1887 | 1914 | result_ty: Type, |
| 1888 | 1915 | ty: Type, |
| 1889 | 1916 | /// Always in direct representation. |
| 1890 | | ty_ref: CacheRef, |
| 1891 | 1917 | ty_id: IdRef, |
| 1892 | 1918 | /// True if the input is an array type. |
| 1893 | 1919 | is_array: bool, |
| ... | ... | @@ -1947,14 +1973,13 @@ const DeclGen = struct { |
| 1947 | 1973 | @memset(results, undefined); |
| 1948 | 1974 | |
| 1949 | 1975 | const ty = if (is_array) result_ty.scalarType(mod) else result_ty; |
| 1950 | | const ty_ref = try self.resolveType(ty, .direct); |
| 1976 | const ty_id = try self.resolveType(ty, .direct); |
| 1951 | 1977 | |
| 1952 | 1978 | return .{ |
| 1953 | 1979 | .dg = self, |
| 1954 | 1980 | .result_ty = result_ty, |
| 1955 | 1981 | .ty = ty, |
| 1956 | | .ty_ref = ty_ref, |
| 1957 | | .ty_id = self.typeId(ty_ref), |
| 1982 | .ty_id = ty_id, |
| 1958 | 1983 | .is_array = is_array, |
| 1959 | 1984 | .results = results, |
| 1960 | 1985 | }; |
| ... | ... | @@ -1981,16 +2006,13 @@ const DeclGen = struct { |
| 1981 | 2006 | /// TODO is to also write out the error as a function call parameter, and to somehow fetch |
| 1982 | 2007 | /// the name of an error in the text executor. |
| 1983 | 2008 | fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void { |
| 1984 | | const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 1985 | | const ptr_anyerror_ty_ref = try self.ptrType(Type.anyerror, .CrossWorkgroup); |
| 1986 | | const void_ty_ref = try self.resolveType(Type.void, .direct); |
| 1987 | | |
| 1988 | | const kernel_proto_ty_ref = try self.spv.resolve(.{ |
| 1989 | | .function_type = .{ |
| 1990 | | .return_type = void_ty_ref, |
| 1991 | | .parameters = &.{ptr_anyerror_ty_ref}, |
| 1992 | | }, |
| 2009 | const anyerror_ty_id = try self.resolveType(Type.anyerror, .direct); |
| 2010 | const ptr_anyerror_ty = try self.module.ptrType(.{ |
| 2011 | .child = Type.anyerror.toIntern(), |
| 2012 | .flags = .{ .address_space = .global }, |
| 1993 | 2013 | }); |
| 2014 | const ptr_anyerror_ty_id = try self.resolveType(ptr_anyerror_ty, .direct); |
| 2015 | const kernel_proto_ty_id = try self.functionType(Type.void, &.{ptr_anyerror_ty}); |
| 1994 | 2016 | |
| 1995 | 2017 | const test_id = self.spv.declPtr(spv_test_decl_index).result_id; |
| 1996 | 2018 | |
| ... | ... | @@ -2002,20 +2024,20 @@ const DeclGen = struct { |
| 2002 | 2024 | |
| 2003 | 2025 | const section = &self.spv.sections.functions; |
| 2004 | 2026 | try section.emit(self.spv.gpa, .OpFunction, .{ |
| 2005 | | .id_result_type = self.typeId(void_ty_ref), |
| 2027 | .id_result_type = try self.resolveType(Type.void, .direct), |
| 2006 | 2028 | .id_result = kernel_id, |
| 2007 | 2029 | .function_control = .{}, |
| 2008 | | .function_type = self.typeId(kernel_proto_ty_ref), |
| 2030 | .function_type = kernel_proto_ty_id, |
| 2009 | 2031 | }); |
| 2010 | 2032 | try section.emit(self.spv.gpa, .OpFunctionParameter, .{ |
| 2011 | | .id_result_type = self.typeId(ptr_anyerror_ty_ref), |
| 2033 | .id_result_type = ptr_anyerror_ty_id, |
| 2012 | 2034 | .id_result = p_error_id, |
| 2013 | 2035 | }); |
| 2014 | 2036 | try section.emit(self.spv.gpa, .OpLabel, .{ |
| 2015 | 2037 | .id_result = self.spv.allocId(), |
| 2016 | 2038 | }); |
| 2017 | 2039 | try section.emit(self.spv.gpa, .OpFunctionCall, .{ |
| 2018 | | .id_result_type = self.typeId(anyerror_ty_ref), |
| 2040 | .id_result_type = anyerror_ty_id, |
| 2019 | 2041 | .id_result = error_id, |
| 2020 | 2042 | .function = test_id, |
| 2021 | 2043 | }); |
| ... | ... | @@ -2047,17 +2069,17 @@ const DeclGen = struct { |
| 2047 | 2069 | .func => { |
| 2048 | 2070 | assert(decl.typeOf(mod).zigTypeTag(mod) == .Fn); |
| 2049 | 2071 | const fn_info = mod.typeToFunc(decl.typeOf(mod)).?; |
| 2050 | | const return_ty_ref = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type)); |
| 2072 | const return_ty_id = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type)); |
| 2051 | 2073 | |
| 2052 | | const prototype_ty_ref = try self.resolveType(decl.typeOf(mod), .direct); |
| 2074 | const prototype_ty_id = try self.resolveType(decl.typeOf(mod), .direct); |
| 2053 | 2075 | try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ |
| 2054 | | .id_result_type = self.typeId(return_ty_ref), |
| 2076 | .id_result_type = return_ty_id, |
| 2055 | 2077 | .id_result = result_id, |
| 2056 | 2078 | .function_control = switch (fn_info.cc) { |
| 2057 | 2079 | .Inline => .{ .Inline = true }, |
| 2058 | 2080 | else => .{}, |
| 2059 | 2081 | }, |
| 2060 | | .function_type = self.typeId(prototype_ty_ref), |
| 2082 | .function_type = prototype_ty_id, |
| 2061 | 2083 | }); |
| 2062 | 2084 | |
| 2063 | 2085 | comptime assert(zig_call_abi_ver == 3); |
| ... | ... | @@ -2066,7 +2088,7 @@ const DeclGen = struct { |
| 2066 | 2088 | const param_ty = Type.fromInterned(param_ty_index); |
| 2067 | 2089 | if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 2068 | 2090 | |
| 2069 | | const param_type_id = try self.resolveTypeId(param_ty); |
| 2091 | const param_type_id = try self.resolveType(param_ty, .direct); |
| 2070 | 2092 | const arg_result_id = self.spv.allocId(); |
| 2071 | 2093 | try self.func.prologue.emit(self.spv.gpa, .OpFunctionParameter, .{ |
| 2072 | 2094 | .id_result_type = param_type_id, |
| ... | ... | @@ -2122,10 +2144,10 @@ const DeclGen = struct { |
| 2122 | 2144 | const final_storage_class = self.spvStorageClass(decl.@"addrspace"); |
| 2123 | 2145 | assert(final_storage_class != .Generic); // These should be instance globals |
| 2124 | 2146 | |
| 2125 | | const ptr_ty_ref = try self.ptrType(decl.typeOf(mod), final_storage_class); |
| 2147 | const ptr_ty_id = try self.ptrType(decl.typeOf(mod), final_storage_class); |
| 2126 | 2148 | |
| 2127 | 2149 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{ |
| 2128 | | .id_result_type = self.typeId(ptr_ty_ref), |
| 2150 | .id_result_type = ptr_ty_id, |
| 2129 | 2151 | .id_result = result_id, |
| 2130 | 2152 | .storage_class = final_storage_class, |
| 2131 | 2153 | }); |
| ... | ... | @@ -2145,22 +2167,18 @@ const DeclGen = struct { |
| 2145 | 2167 | |
| 2146 | 2168 | try self.spv.declareDeclDeps(spv_decl_index, &.{}); |
| 2147 | 2169 | |
| 2148 | | const ptr_ty_ref = try self.ptrType(decl.typeOf(mod), .Function); |
| 2170 | const ptr_ty_id = try self.ptrType(decl.typeOf(mod), .Function); |
| 2149 | 2171 | |
| 2150 | 2172 | if (maybe_init_val) |init_val| { |
| 2151 | 2173 | // TODO: Combine with resolveAnonDecl? |
| 2152 | | const void_ty_ref = try self.resolveType(Type.void, .direct); |
| 2153 | | const initializer_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{ |
| 2154 | | .return_type = void_ty_ref, |
| 2155 | | .parameters = &.{}, |
| 2156 | | } }); |
| 2174 | const initializer_proto_ty_id = try self.functionType(Type.void, &.{}); |
| 2157 | 2175 | |
| 2158 | 2176 | const initializer_id = self.spv.allocId(); |
| 2159 | 2177 | try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ |
| 2160 | | .id_result_type = self.typeId(void_ty_ref), |
| 2178 | .id_result_type = try self.resolveType(Type.void, .direct), |
| 2161 | 2179 | .id_result = initializer_id, |
| 2162 | 2180 | .function_control = .{}, |
| 2163 | | .function_type = self.typeId(initializer_proto_ty_ref), |
| 2181 | .function_type = initializer_proto_ty_id, |
| 2164 | 2182 | }); |
| 2165 | 2183 | |
| 2166 | 2184 | const root_block_id = self.spv.allocId(); |
| ... | ... | @@ -2183,7 +2201,7 @@ const DeclGen = struct { |
| 2183 | 2201 | try self.spv.debugNameFmt(initializer_id, "initializer of {s}", .{fqn}); |
| 2184 | 2202 | |
| 2185 | 2203 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ |
| 2186 | | .id_result_type = self.typeId(ptr_ty_ref), |
| 2204 | .id_result_type = ptr_ty_id, |
| 2187 | 2205 | .id_result = result_id, |
| 2188 | 2206 | .set = try self.spv.importInstructionSet(.zig), |
| 2189 | 2207 | .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... |
| ... | ... | @@ -2191,7 +2209,7 @@ const DeclGen = struct { |
| 2191 | 2209 | }); |
| 2192 | 2210 | } else { |
| 2193 | 2211 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ |
| 2194 | | .id_result_type = self.typeId(ptr_ty_ref), |
| 2212 | .id_result_type = ptr_ty_id, |
| 2195 | 2213 | .id_result = result_id, |
| 2196 | 2214 | .set = try self.spv.importInstructionSet(.zig), |
| 2197 | 2215 | .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... |
| ... | ... | @@ -2202,12 +2220,12 @@ const DeclGen = struct { |
| 2202 | 2220 | } |
| 2203 | 2221 | } |
| 2204 | 2222 | |
| 2205 | | fn intFromBool(self: *DeclGen, result_ty_ref: CacheRef, condition_id: IdRef) !IdRef { |
| 2206 | | const zero_id = try self.constInt(result_ty_ref, 0); |
| 2207 | | const one_id = try self.constInt(result_ty_ref, 1); |
| 2223 | fn intFromBool(self: *DeclGen, ty: Type, condition_id: IdRef) !IdRef { |
| 2224 | const zero_id = try self.constInt(ty, 0, .direct); |
| 2225 | const one_id = try self.constInt(ty, 1, .direct); |
| 2208 | 2226 | const result_id = self.spv.allocId(); |
| 2209 | 2227 | try self.func.body.emit(self.spv.gpa, .OpSelect, .{ |
| 2210 | | .id_result_type = self.typeId(result_ty_ref), |
| 2228 | .id_result_type = try self.resolveType(ty, .direct), |
| 2211 | 2229 | .id_result = result_id, |
| 2212 | 2230 | .condition = condition_id, |
| 2213 | 2231 | .object_1 = one_id, |
| ... | ... | @@ -2222,15 +2240,12 @@ const DeclGen = struct { |
| 2222 | 2240 | const mod = self.module; |
| 2223 | 2241 | return switch (ty.zigTypeTag(mod)) { |
| 2224 | 2242 | .Bool => blk: { |
| 2225 | | const direct_bool_ty_ref = try self.resolveType(ty, .direct); |
| 2226 | | const indirect_bool_ty_ref = try self.resolveType(ty, .indirect); |
| 2227 | | const zero_id = try self.constInt(indirect_bool_ty_ref, 0); |
| 2228 | 2243 | const result_id = self.spv.allocId(); |
| 2229 | 2244 | try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{ |
| 2230 | | .id_result_type = self.typeId(direct_bool_ty_ref), |
| 2245 | .id_result_type = try self.resolveType(Type.bool, .direct), |
| 2231 | 2246 | .id_result = result_id, |
| 2232 | 2247 | .operand_1 = operand_id, |
| 2233 | | .operand_2 = zero_id, |
| 2248 | .operand_2 = try self.constBool(false, .indirect), |
| 2234 | 2249 | }); |
| 2235 | 2250 | break :blk result_id; |
| 2236 | 2251 | }, |
| ... | ... | @@ -2243,20 +2258,17 @@ const DeclGen = struct { |
| 2243 | 2258 | fn convertToIndirect(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef { |
| 2244 | 2259 | const mod = self.module; |
| 2245 | 2260 | return switch (ty.zigTypeTag(mod)) { |
| 2246 | | .Bool => blk: { |
| 2247 | | const indirect_bool_ty_ref = try self.resolveType(ty, .indirect); |
| 2248 | | break :blk self.intFromBool(indirect_bool_ty_ref, operand_id); |
| 2249 | | }, |
| 2261 | .Bool => try self.intFromBool(Type.u1, operand_id), |
| 2250 | 2262 | else => operand_id, |
| 2251 | 2263 | }; |
| 2252 | 2264 | } |
| 2253 | 2265 | |
| 2254 | 2266 | fn extractField(self: *DeclGen, result_ty: Type, object: IdRef, field: u32) !IdRef { |
| 2255 | | const result_ty_ref = try self.resolveType(result_ty, .indirect); |
| 2267 | const result_ty_id = try self.resolveType(result_ty, .indirect); |
| 2256 | 2268 | const result_id = self.spv.allocId(); |
| 2257 | 2269 | const indexes = [_]u32{field}; |
| 2258 | 2270 | try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ |
| 2259 | | .id_result_type = self.typeId(result_ty_ref), |
| 2271 | .id_result_type = result_ty_id, |
| 2260 | 2272 | .id_result = result_id, |
| 2261 | 2273 | .composite = object, |
| 2262 | 2274 | .indexes = &indexes, |
| ... | ... | @@ -2270,13 +2282,13 @@ const DeclGen = struct { |
| 2270 | 2282 | }; |
| 2271 | 2283 | |
| 2272 | 2284 | fn load(self: *DeclGen, value_ty: Type, ptr_id: IdRef, options: MemoryOptions) !IdRef { |
| 2273 | | const indirect_value_ty_ref = try self.resolveType(value_ty, .indirect); |
| 2285 | const indirect_value_ty_id = try self.resolveType(value_ty, .indirect); |
| 2274 | 2286 | const result_id = self.spv.allocId(); |
| 2275 | 2287 | const access = spec.MemoryAccess.Extended{ |
| 2276 | 2288 | .Volatile = options.is_volatile, |
| 2277 | 2289 | }; |
| 2278 | 2290 | try self.func.body.emit(self.spv.gpa, .OpLoad, .{ |
| 2279 | | .id_result_type = self.typeId(indirect_value_ty_ref), |
| 2291 | .id_result_type = indirect_value_ty_id, |
| 2280 | 2292 | .id_result = result_id, |
| 2281 | 2293 | .pointer = ptr_id, |
| 2282 | 2294 | .memory_access = access, |
| ... | ... | @@ -2488,7 +2500,8 @@ const DeclGen = struct { |
| 2488 | 2500 | |
| 2489 | 2501 | const result_ty = self.typeOfIndex(inst); |
| 2490 | 2502 | const shift_ty = self.typeOf(bin_op.rhs); |
| 2491 | | const shift_ty_ref = try self.resolveType(shift_ty, .direct); |
| 2503 | const scalar_result_ty_id = try self.resolveType(result_ty.scalarType(mod), .direct); |
| 2504 | const scalar_shift_ty_id = try self.resolveType(shift_ty.scalarType(mod), .direct); |
| 2492 | 2505 | |
| 2493 | 2506 | const info = self.arithmeticTypeInfo(result_ty); |
| 2494 | 2507 | switch (info.class) { |
| ... | ... | @@ -2505,7 +2518,7 @@ const DeclGen = struct { |
| 2505 | 2518 | |
| 2506 | 2519 | // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that, |
| 2507 | 2520 | // so just manually upcast it if required. |
| 2508 | | const shift_id = if (shift_ty_ref != wip.ty_ref) blk: { |
| 2521 | const shift_id = if (scalar_shift_ty_id != scalar_result_ty_id) blk: { |
| 2509 | 2522 | const shift_id = self.spv.allocId(); |
| 2510 | 2523 | try self.func.body.emit(self.spv.gpa, .OpUConvert, .{ |
| 2511 | 2524 | .id_result_type = wip.ty_id, |
| ... | ... | @@ -2529,7 +2542,7 @@ const DeclGen = struct { |
| 2529 | 2542 | try self.func.body.emit(self.spv.gpa, unsigned, args); |
| 2530 | 2543 | } |
| 2531 | 2544 | |
| 2532 | | result_id.* = try self.normalize(wip.ty_ref, value_id, info); |
| 2545 | result_id.* = try self.normalize(wip.ty, value_id, info); |
| 2533 | 2546 | } |
| 2534 | 2547 | return try wip.finalize(); |
| 2535 | 2548 | } |
| ... | ... | @@ -2622,7 +2635,7 @@ const DeclGen = struct { |
| 2622 | 2635 | /// - Signed integers are also sign extended if they are negative. |
| 2623 | 2636 | /// All other values are returned unmodified (this makes strange integer |
| 2624 | 2637 | /// wrapping easier to use in generic operations). |
| 2625 | | fn normalize(self: *DeclGen, ty_ref: CacheRef, value_id: IdRef, info: ArithmeticTypeInfo) !IdRef { |
| 2638 | fn normalize(self: *DeclGen, ty: Type, value_id: IdRef, info: ArithmeticTypeInfo) !IdRef { |
| 2626 | 2639 | switch (info.class) { |
| 2627 | 2640 | .integer, .bool, .float => return value_id, |
| 2628 | 2641 | .composite_integer => unreachable, // TODO |
| ... | ... | @@ -2630,9 +2643,9 @@ const DeclGen = struct { |
| 2630 | 2643 | .unsigned => { |
| 2631 | 2644 | const mask_value = if (info.bits == 64) 0xFFFF_FFFF_FFFF_FFFF else (@as(u64, 1) << @as(u6, @intCast(info.bits))) - 1; |
| 2632 | 2645 | const result_id = self.spv.allocId(); |
| 2633 | | const mask_id = try self.constInt(ty_ref, mask_value); |
| 2646 | const mask_id = try self.constInt(ty, mask_value, .direct); |
| 2634 | 2647 | try self.func.body.emit(self.spv.gpa, .OpBitwiseAnd, .{ |
| 2635 | | .id_result_type = self.typeId(ty_ref), |
| 2648 | .id_result_type = try self.resolveType(ty, .direct), |
| 2636 | 2649 | .id_result = result_id, |
| 2637 | 2650 | .operand_1 = value_id, |
| 2638 | 2651 | .operand_2 = mask_id, |
| ... | ... | @@ -2641,17 +2654,17 @@ const DeclGen = struct { |
| 2641 | 2654 | }, |
| 2642 | 2655 | .signed => { |
| 2643 | 2656 | // Shift left and right so that we can copy the sight bit that way. |
| 2644 | | const shift_amt_id = try self.constInt(ty_ref, info.backing_bits - info.bits); |
| 2657 | const shift_amt_id = try self.constInt(ty, info.backing_bits - info.bits, .direct); |
| 2645 | 2658 | const left_id = self.spv.allocId(); |
| 2646 | 2659 | try self.func.body.emit(self.spv.gpa, .OpShiftLeftLogical, .{ |
| 2647 | | .id_result_type = self.typeId(ty_ref), |
| 2660 | .id_result_type = try self.resolveType(ty, .direct), |
| 2648 | 2661 | .id_result = left_id, |
| 2649 | 2662 | .base = value_id, |
| 2650 | 2663 | .shift = shift_amt_id, |
| 2651 | 2664 | }); |
| 2652 | 2665 | const right_id = self.spv.allocId(); |
| 2653 | 2666 | try self.func.body.emit(self.spv.gpa, .OpShiftRightArithmetic, .{ |
| 2654 | | .id_result_type = self.typeId(ty_ref), |
| 2667 | .id_result_type = try self.resolveType(ty, .direct), |
| 2655 | 2668 | .id_result = right_id, |
| 2656 | 2669 | .base = left_id, |
| 2657 | 2670 | .shift = shift_amt_id, |
| ... | ... | @@ -2667,13 +2680,13 @@ const DeclGen = struct { |
| 2667 | 2680 | const lhs_id = try self.resolve(bin_op.lhs); |
| 2668 | 2681 | const rhs_id = try self.resolve(bin_op.rhs); |
| 2669 | 2682 | const ty = self.typeOfIndex(inst); |
| 2670 | | const ty_ref = try self.resolveType(ty, .direct); |
| 2683 | const ty_id = try self.resolveType(ty, .direct); |
| 2671 | 2684 | const info = self.arithmeticTypeInfo(ty); |
| 2672 | 2685 | switch (info.class) { |
| 2673 | 2686 | .composite_integer => unreachable, // TODO |
| 2674 | 2687 | .integer, .strange_integer => { |
| 2675 | | const zero_id = try self.constInt(ty_ref, 0); |
| 2676 | | const one_id = try self.constInt(ty_ref, 1); |
| 2688 | const zero_id = try self.constInt(ty, 0, .direct); |
| 2689 | const one_id = try self.constInt(ty, 1, .direct); |
| 2677 | 2690 | |
| 2678 | 2691 | // (a ^ b) > 0 |
| 2679 | 2692 | const bin_bitwise_id = try self.binOpSimple(ty, lhs_id, rhs_id, .OpBitwiseXor); |
| ... | ... | @@ -2696,14 +2709,14 @@ const DeclGen = struct { |
| 2696 | 2709 | const negative_div_id = try self.arithOp(ty, negative_div_lhs, rhs_abs, .OpFDiv, .OpSDiv, .OpUDiv); |
| 2697 | 2710 | const negated_negative_div_id = self.spv.allocId(); |
| 2698 | 2711 | try self.func.body.emit(self.spv.gpa, .OpSNegate, .{ |
| 2699 | | .id_result_type = self.typeId(ty_ref), |
| 2712 | .id_result_type = ty_id, |
| 2700 | 2713 | .id_result = negated_negative_div_id, |
| 2701 | 2714 | .operand = negative_div_id, |
| 2702 | 2715 | }); |
| 2703 | 2716 | |
| 2704 | 2717 | const result_id = self.spv.allocId(); |
| 2705 | 2718 | try self.func.body.emit(self.spv.gpa, .OpSelect, .{ |
| 2706 | | .id_result_type = self.typeId(ty_ref), |
| 2719 | .id_result_type = ty_id, |
| 2707 | 2720 | .id_result = result_id, |
| 2708 | 2721 | .condition = is_positive_id, |
| 2709 | 2722 | .object_1 = positive_div_id, |
| ... | ... | @@ -2728,7 +2741,7 @@ const DeclGen = struct { |
| 2728 | 2741 | |
| 2729 | 2742 | fn floor(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef { |
| 2730 | 2743 | const target = self.getTarget(); |
| 2731 | | const ty_ref = try self.resolveType(ty, .direct); |
| 2744 | const ty_id = try self.resolveType(ty, .direct); |
| 2732 | 2745 | const ext_inst: Word = switch (target.os.tag) { |
| 2733 | 2746 | .opencl => 25, |
| 2734 | 2747 | .vulkan => 8, |
| ... | ... | @@ -2742,7 +2755,7 @@ const DeclGen = struct { |
| 2742 | 2755 | |
| 2743 | 2756 | const result_id = self.spv.allocId(); |
| 2744 | 2757 | try self.func.body.emit(self.spv.gpa, .OpExtInst, .{ |
| 2745 | | .id_result_type = self.typeId(ty_ref), |
| 2758 | .id_result_type = ty_id, |
| 2746 | 2759 | .id_result = result_id, |
| 2747 | 2760 | .set = set_id, |
| 2748 | 2761 | .instruction = .{ .inst = ext_inst }, |
| ... | ... | @@ -2819,7 +2832,7 @@ const DeclGen = struct { |
| 2819 | 2832 | |
| 2820 | 2833 | // TODO: Trap on overflow? Probably going to be annoying. |
| 2821 | 2834 | // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap. |
| 2822 | | result_id.* = try self.normalize(wip.ty_ref, value_id, info); |
| 2835 | result_id.* = try self.normalize(wip.ty, value_id, info); |
| 2823 | 2836 | } |
| 2824 | 2837 | |
| 2825 | 2838 | return try wip.finalize(); |
| ... | ... | @@ -2897,11 +2910,12 @@ const DeclGen = struct { |
| 2897 | 2910 | const operand_ty = self.typeOf(extra.lhs); |
| 2898 | 2911 | const ov_ty = result_ty.structFieldType(1, self.module); |
| 2899 | 2912 | |
| 2900 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 2901 | | const cmp_ty_ref = if (self.isVector(operand_ty)) |
| 2902 | | try self.spv.vectorType(operand_ty.vectorLen(mod), bool_ty_ref) |
| 2913 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 2914 | const cmp_ty_id = if (self.isVector(operand_ty)) |
| 2915 | // TODO: Resolving a vector type with .direct should return a SPIR-V vector |
| 2916 | try self.spv.vectorType(operand_ty.vectorLen(mod), try self.resolveType(Type.bool, .direct)) |
| 2903 | 2917 | else |
| 2904 | | bool_ty_ref; |
| 2918 | bool_ty_id; |
| 2905 | 2919 | |
| 2906 | 2920 | const info = self.arithmeticTypeInfo(operand_ty); |
| 2907 | 2921 | switch (info.class) { |
| ... | ... | @@ -2929,7 +2943,7 @@ const DeclGen = struct { |
| 2929 | 2943 | }); |
| 2930 | 2944 | |
| 2931 | 2945 | // Normalize the result so that the comparisons go well |
| 2932 | | result_id.* = try self.normalize(wip_result.ty_ref, value_id, info); |
| 2946 | result_id.* = try self.normalize(wip_result.ty, value_id, info); |
| 2933 | 2947 | |
| 2934 | 2948 | const overflowed_id = switch (info.signedness) { |
| 2935 | 2949 | .unsigned => blk: { |
| ... | ... | @@ -2937,7 +2951,7 @@ const DeclGen = struct { |
| 2937 | 2951 | // For subtraction the conditions need to be swapped. |
| 2938 | 2952 | const overflowed_id = self.spv.allocId(); |
| 2939 | 2953 | try self.func.body.emit(self.spv.gpa, ucmp, .{ |
| 2940 | | .id_result_type = self.typeId(cmp_ty_ref), |
| 2954 | .id_result_type = cmp_ty_id, |
| 2941 | 2955 | .id_result = overflowed_id, |
| 2942 | 2956 | .operand_1 = result_id.*, |
| 2943 | 2957 | .operand_2 = lhs_elem_id, |
| ... | ... | @@ -2963,9 +2977,9 @@ const DeclGen = struct { |
| 2963 | 2977 | // = (rhs < 0) == (lhs > value) |
| 2964 | 2978 | |
| 2965 | 2979 | const rhs_lt_zero_id = self.spv.allocId(); |
| 2966 | | const zero_id = try self.constInt(wip_result.ty_ref, 0); |
| 2980 | const zero_id = try self.constInt(wip_result.ty, 0, .direct); |
| 2967 | 2981 | try self.func.body.emit(self.spv.gpa, .OpSLessThan, .{ |
| 2968 | | .id_result_type = self.typeId(cmp_ty_ref), |
| 2982 | .id_result_type = cmp_ty_id, |
| 2969 | 2983 | .id_result = rhs_lt_zero_id, |
| 2970 | 2984 | .operand_1 = rhs_elem_id, |
| 2971 | 2985 | .operand_2 = zero_id, |
| ... | ... | @@ -2973,7 +2987,7 @@ const DeclGen = struct { |
| 2973 | 2987 | |
| 2974 | 2988 | const value_gt_lhs_id = self.spv.allocId(); |
| 2975 | 2989 | try self.func.body.emit(self.spv.gpa, scmp, .{ |
| 2976 | | .id_result_type = self.typeId(cmp_ty_ref), |
| 2990 | .id_result_type = cmp_ty_id, |
| 2977 | 2991 | .id_result = value_gt_lhs_id, |
| 2978 | 2992 | .operand_1 = lhs_elem_id, |
| 2979 | 2993 | .operand_2 = result_id.*, |
| ... | ... | @@ -2981,7 +2995,7 @@ const DeclGen = struct { |
| 2981 | 2995 | |
| 2982 | 2996 | const overflowed_id = self.spv.allocId(); |
| 2983 | 2997 | try self.func.body.emit(self.spv.gpa, .OpLogicalEqual, .{ |
| 2984 | | .id_result_type = self.typeId(cmp_ty_ref), |
| 2998 | .id_result_type = cmp_ty_id, |
| 2985 | 2999 | .id_result = overflowed_id, |
| 2986 | 3000 | .operand_1 = rhs_lt_zero_id, |
| 2987 | 3001 | .operand_2 = value_gt_lhs_id, |
| ... | ... | @@ -2990,7 +3004,7 @@ const DeclGen = struct { |
| 2990 | 3004 | }, |
| 2991 | 3005 | }; |
| 2992 | 3006 | |
| 2993 | | ov_id.* = try self.intFromBool(wip_ov.ty_ref, overflowed_id); |
| 3007 | ov_id.* = try self.intFromBool(wip_ov.ty, overflowed_id); |
| 2994 | 3008 | } |
| 2995 | 3009 | |
| 2996 | 3010 | return try self.constructStruct( |
| ... | ... | @@ -3022,9 +3036,9 @@ const DeclGen = struct { |
| 3022 | 3036 | var wip_ov = try self.elementWise(ov_ty, true); |
| 3023 | 3037 | defer wip_ov.deinit(); |
| 3024 | 3038 | |
| 3025 | | const zero_id = try self.constInt(wip_result.ty_ref, 0); |
| 3026 | | const zero_ov_id = try self.constInt(wip_ov.ty_ref, 0); |
| 3027 | | const one_ov_id = try self.constInt(wip_ov.ty_ref, 1); |
| 3039 | const zero_id = try self.constInt(wip_result.ty, 0, .direct); |
| 3040 | const zero_ov_id = try self.constInt(wip_ov.ty, 0, .direct); |
| 3041 | const one_ov_id = try self.constInt(wip_ov.ty, 1, .direct); |
| 3028 | 3042 | |
| 3029 | 3043 | for (wip_result.results, wip_ov.results, 0..) |*result_id, *ov_id, i| { |
| 3030 | 3044 | const lhs_elem_id = try wip_result.elementAt(operand_ty, lhs, i); |
| ... | ... | @@ -3065,15 +3079,17 @@ const DeclGen = struct { |
| 3065 | 3079 | const result_ty = self.typeOfIndex(inst); |
| 3066 | 3080 | const operand_ty = self.typeOf(extra.lhs); |
| 3067 | 3081 | const shift_ty = self.typeOf(extra.rhs); |
| 3068 | | const shift_ty_ref = try self.resolveType(shift_ty, .direct); |
| 3082 | const scalar_shift_ty_id = try self.resolveType(shift_ty.scalarType(mod), .direct); |
| 3083 | const scalar_operand_ty_id = try self.resolveType(operand_ty.scalarType(mod), .direct); |
| 3069 | 3084 | |
| 3070 | 3085 | const ov_ty = result_ty.structFieldType(1, self.module); |
| 3071 | 3086 | |
| 3072 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 3073 | | const cmp_ty_ref = if (self.isVector(operand_ty)) |
| 3074 | | try self.spv.vectorType(operand_ty.vectorLen(mod), bool_ty_ref) |
| 3087 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 3088 | const cmp_ty_id = if (self.isVector(operand_ty)) |
| 3089 | // TODO: Resolving a vector type with .direct should return a SPIR-V vector |
| 3090 | try self.spv.vectorType(operand_ty.vectorLen(mod), try self.resolveType(Type.bool, .direct)) |
| 3075 | 3091 | else |
| 3076 | | bool_ty_ref; |
| 3092 | bool_ty_id; |
| 3077 | 3093 | |
| 3078 | 3094 | const info = self.arithmeticTypeInfo(operand_ty); |
| 3079 | 3095 | switch (info.class) { |
| ... | ... | @@ -3092,7 +3108,7 @@ const DeclGen = struct { |
| 3092 | 3108 | |
| 3093 | 3109 | // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that, |
| 3094 | 3110 | // so just manually upcast it if required. |
| 3095 | | const shift_id = if (shift_ty_ref != wip_result.ty_ref) blk: { |
| 3111 | const shift_id = if (scalar_shift_ty_id != scalar_operand_ty_id) blk: { |
| 3096 | 3112 | const shift_id = self.spv.allocId(); |
| 3097 | 3113 | try self.func.body.emit(self.spv.gpa, .OpUConvert, .{ |
| 3098 | 3114 | .id_result_type = wip_result.ty_id, |
| ... | ... | @@ -3109,7 +3125,7 @@ const DeclGen = struct { |
| 3109 | 3125 | .base = lhs_elem_id, |
| 3110 | 3126 | .shift = shift_id, |
| 3111 | 3127 | }); |
| 3112 | | result_id.* = try self.normalize(wip_result.ty_ref, value_id, info); |
| 3128 | result_id.* = try self.normalize(wip_result.ty, value_id, info); |
| 3113 | 3129 | |
| 3114 | 3130 | const right_shift_id = self.spv.allocId(); |
| 3115 | 3131 | switch (info.signedness) { |
| ... | ... | @@ -3133,13 +3149,13 @@ const DeclGen = struct { |
| 3133 | 3149 | |
| 3134 | 3150 | const overflowed_id = self.spv.allocId(); |
| 3135 | 3151 | try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{ |
| 3136 | | .id_result_type = self.typeId(cmp_ty_ref), |
| 3152 | .id_result_type = cmp_ty_id, |
| 3137 | 3153 | .id_result = overflowed_id, |
| 3138 | 3154 | .operand_1 = lhs_elem_id, |
| 3139 | 3155 | .operand_2 = right_shift_id, |
| 3140 | 3156 | }); |
| 3141 | 3157 | |
| 3142 | | ov_id.* = try self.intFromBool(wip_ov.ty_ref, overflowed_id); |
| 3158 | ov_id.* = try self.intFromBool(wip_ov.ty, overflowed_id); |
| 3143 | 3159 | } |
| 3144 | 3160 | |
| 3145 | 3161 | return try self.constructStruct( |
| ... | ... | @@ -3204,8 +3220,7 @@ const DeclGen = struct { |
| 3204 | 3220 | defer wip.deinit(); |
| 3205 | 3221 | |
| 3206 | 3222 | const elem_ty = if (wip.is_array) operand_ty.scalarType(mod) else operand_ty; |
| 3207 | | const elem_ty_ref = try self.resolveType(elem_ty, .direct); |
| 3208 | | const elem_ty_id = self.typeId(elem_ty_ref); |
| 3223 | const elem_ty_id = try self.resolveType(elem_ty, .direct); |
| 3209 | 3224 | |
| 3210 | 3225 | for (wip.results, 0..) |*result_id, i| { |
| 3211 | 3226 | const elem = try wip.elementAt(operand_ty, operand, i); |
| ... | ... | @@ -3230,6 +3245,8 @@ const DeclGen = struct { |
| 3230 | 3245 | .id_ref_4 = &.{elem}, |
| 3231 | 3246 | }); |
| 3232 | 3247 | |
| 3248 | // TODO: Comparison should be removed.. |
| 3249 | // Its valid because SpvModule caches numeric types |
| 3233 | 3250 | if (wip.ty_id == elem_ty_id) { |
| 3234 | 3251 | result_id.* = tmp; |
| 3235 | 3252 | continue; |
| ... | ... | @@ -3276,8 +3293,7 @@ const DeclGen = struct { |
| 3276 | 3293 | const operand = try self.resolve(reduce.operand); |
| 3277 | 3294 | const operand_ty = self.typeOf(reduce.operand); |
| 3278 | 3295 | const scalar_ty = operand_ty.scalarType(mod); |
| 3279 | | const scalar_ty_ref = try self.resolveType(scalar_ty, .direct); |
| 3280 | | const scalar_ty_id = self.typeId(scalar_ty_ref); |
| 3296 | const scalar_ty_id = try self.resolveType(scalar_ty, .direct); |
| 3281 | 3297 | |
| 3282 | 3298 | const info = self.arithmeticTypeInfo(operand_ty); |
| 3283 | 3299 | |
| ... | ... | @@ -3351,7 +3367,7 @@ const DeclGen = struct { |
| 3351 | 3367 | for (wip.results, 0..) |*result_id, i| { |
| 3352 | 3368 | const elem = try mask.elemValue(mod, i); |
| 3353 | 3369 | if (elem.isUndef(mod)) { |
| 3354 | | result_id.* = try self.spv.constUndef(wip.ty_ref); |
| 3370 | result_id.* = try self.spv.constUndef(wip.ty_id); |
| 3355 | 3371 | continue; |
| 3356 | 3372 | } |
| 3357 | 3373 | |
| ... | ... | @@ -3366,11 +3382,10 @@ const DeclGen = struct { |
| 3366 | 3382 | } |
| 3367 | 3383 | |
| 3368 | 3384 | fn indicesToIds(self: *DeclGen, indices: []const u32) ![]IdRef { |
| 3369 | | const index_ty_ref = try self.intType(.unsigned, 32); |
| 3370 | 3385 | const ids = try self.gpa.alloc(IdRef, indices.len); |
| 3371 | 3386 | errdefer self.gpa.free(ids); |
| 3372 | 3387 | for (indices, ids) |index, *id| { |
| 3373 | | id.* = try self.constInt(index_ty_ref, index); |
| 3388 | id.* = try self.constInt(Type.u32, index, .direct); |
| 3374 | 3389 | } |
| 3375 | 3390 | |
| 3376 | 3391 | return ids; |
| ... | ... | @@ -3378,13 +3393,13 @@ const DeclGen = struct { |
| 3378 | 3393 | |
| 3379 | 3394 | fn accessChainId( |
| 3380 | 3395 | self: *DeclGen, |
| 3381 | | result_ty_ref: CacheRef, |
| 3396 | result_ty_id: IdRef, |
| 3382 | 3397 | base: IdRef, |
| 3383 | 3398 | indices: []const IdRef, |
| 3384 | 3399 | ) !IdRef { |
| 3385 | 3400 | const result_id = self.spv.allocId(); |
| 3386 | 3401 | try self.func.body.emit(self.spv.gpa, .OpInBoundsAccessChain, .{ |
| 3387 | | .id_result_type = self.typeId(result_ty_ref), |
| 3402 | .id_result_type = result_ty_id, |
| 3388 | 3403 | .id_result = result_id, |
| 3389 | 3404 | .base = base, |
| 3390 | 3405 | .indexes = indices, |
| ... | ... | @@ -3398,18 +3413,18 @@ const DeclGen = struct { |
| 3398 | 3413 | /// is the latter and PtrAccessChain is the former. |
| 3399 | 3414 | fn accessChain( |
| 3400 | 3415 | self: *DeclGen, |
| 3401 | | result_ty_ref: CacheRef, |
| 3416 | result_ty_id: IdRef, |
| 3402 | 3417 | base: IdRef, |
| 3403 | 3418 | indices: []const u32, |
| 3404 | 3419 | ) !IdRef { |
| 3405 | 3420 | const ids = try self.indicesToIds(indices); |
| 3406 | 3421 | defer self.gpa.free(ids); |
| 3407 | | return try self.accessChainId(result_ty_ref, base, ids); |
| 3422 | return try self.accessChainId(result_ty_id, base, ids); |
| 3408 | 3423 | } |
| 3409 | 3424 | |
| 3410 | 3425 | fn ptrAccessChain( |
| 3411 | 3426 | self: *DeclGen, |
| 3412 | | result_ty_ref: CacheRef, |
| 3427 | result_ty_id: IdRef, |
| 3413 | 3428 | base: IdRef, |
| 3414 | 3429 | element: IdRef, |
| 3415 | 3430 | indices: []const u32, |
| ... | ... | @@ -3419,7 +3434,7 @@ const DeclGen = struct { |
| 3419 | 3434 | |
| 3420 | 3435 | const result_id = self.spv.allocId(); |
| 3421 | 3436 | try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{ |
| 3422 | | .id_result_type = self.typeId(result_ty_ref), |
| 3437 | .id_result_type = result_ty_id, |
| 3423 | 3438 | .id_result = result_id, |
| 3424 | 3439 | .base = base, |
| 3425 | 3440 | .element = element, |
| ... | ... | @@ -3430,21 +3445,21 @@ const DeclGen = struct { |
| 3430 | 3445 | |
| 3431 | 3446 | fn ptrAdd(self: *DeclGen, result_ty: Type, ptr_ty: Type, ptr_id: IdRef, offset_id: IdRef) !IdRef { |
| 3432 | 3447 | const mod = self.module; |
| 3433 | | const result_ty_ref = try self.resolveType(result_ty, .direct); |
| 3448 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 3434 | 3449 | |
| 3435 | 3450 | switch (ptr_ty.ptrSize(mod)) { |
| 3436 | 3451 | .One => { |
| 3437 | 3452 | // Pointer to array |
| 3438 | 3453 | // TODO: Is this correct? |
| 3439 | | return try self.accessChainId(result_ty_ref, ptr_id, &.{offset_id}); |
| 3454 | return try self.accessChainId(result_ty_id, ptr_id, &.{offset_id}); |
| 3440 | 3455 | }, |
| 3441 | 3456 | .C, .Many => { |
| 3442 | | return try self.ptrAccessChain(result_ty_ref, ptr_id, offset_id, &.{}); |
| 3457 | return try self.ptrAccessChain(result_ty_id, ptr_id, offset_id, &.{}); |
| 3443 | 3458 | }, |
| 3444 | 3459 | .Slice => { |
| 3445 | 3460 | // TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does. |
| 3446 | 3461 | const slice_ptr_id = try self.extractField(result_ty, ptr_id, 0); |
| 3447 | | return try self.ptrAccessChain(result_ty_ref, slice_ptr_id, offset_id, &.{}); |
| 3462 | return try self.ptrAccessChain(result_ty_id, slice_ptr_id, offset_id, &.{}); |
| 3448 | 3463 | }, |
| 3449 | 3464 | } |
| 3450 | 3465 | } |
| ... | ... | @@ -3467,12 +3482,12 @@ const DeclGen = struct { |
| 3467 | 3482 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 3468 | 3483 | const offset_id = try self.resolve(bin_op.rhs); |
| 3469 | 3484 | const offset_ty = self.typeOf(bin_op.rhs); |
| 3470 | | const offset_ty_ref = try self.resolveType(offset_ty, .direct); |
| 3485 | const offset_ty_id = try self.resolveType(offset_ty, .direct); |
| 3471 | 3486 | const result_ty = self.typeOfIndex(inst); |
| 3472 | 3487 | |
| 3473 | 3488 | const negative_offset_id = self.spv.allocId(); |
| 3474 | 3489 | try self.func.body.emit(self.spv.gpa, .OpSNegate, .{ |
| 3475 | | .id_result_type = self.typeId(offset_ty_ref), |
| 3490 | .id_result_type = offset_ty_id, |
| 3476 | 3491 | .id_result = negative_offset_id, |
| 3477 | 3492 | .operand = offset_id, |
| 3478 | 3493 | }); |
| ... | ... | @@ -3490,7 +3505,7 @@ const DeclGen = struct { |
| 3490 | 3505 | const mod = self.module; |
| 3491 | 3506 | var cmp_lhs_id = lhs_id; |
| 3492 | 3507 | var cmp_rhs_id = rhs_id; |
| 3493 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 3508 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 3494 | 3509 | const op_ty = switch (ty.zigTypeTag(mod)) { |
| 3495 | 3510 | .Int, .Bool, .Float => ty, |
| 3496 | 3511 | .Enum => ty.intTagType(mod), |
| ... | ... | @@ -3502,7 +3517,7 @@ const DeclGen = struct { |
| 3502 | 3517 | cmp_lhs_id = self.spv.allocId(); |
| 3503 | 3518 | cmp_rhs_id = self.spv.allocId(); |
| 3504 | 3519 | |
| 3505 | | const usize_ty_id = self.typeId(try self.sizeType()); |
| 3520 | const usize_ty_id = try self.resolveType(Type.usize, .direct); |
| 3506 | 3521 | |
| 3507 | 3522 | try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{ |
| 3508 | 3523 | .id_result_type = usize_ty_id, |
| ... | ... | @@ -3564,20 +3579,20 @@ const DeclGen = struct { |
| 3564 | 3579 | const pl_eq_id = try self.cmp(op, Type.bool, payload_ty, lhs_pl_id, rhs_pl_id); |
| 3565 | 3580 | const lhs_not_valid_id = self.spv.allocId(); |
| 3566 | 3581 | try self.func.body.emit(self.spv.gpa, .OpLogicalNot, .{ |
| 3567 | | .id_result_type = self.typeId(bool_ty_ref), |
| 3582 | .id_result_type = bool_ty_id, |
| 3568 | 3583 | .id_result = lhs_not_valid_id, |
| 3569 | 3584 | .operand = lhs_valid_id, |
| 3570 | 3585 | }); |
| 3571 | 3586 | const impl_id = self.spv.allocId(); |
| 3572 | 3587 | try self.func.body.emit(self.spv.gpa, .OpLogicalOr, .{ |
| 3573 | | .id_result_type = self.typeId(bool_ty_ref), |
| 3588 | .id_result_type = bool_ty_id, |
| 3574 | 3589 | .id_result = impl_id, |
| 3575 | 3590 | .operand_1 = lhs_not_valid_id, |
| 3576 | 3591 | .operand_2 = pl_eq_id, |
| 3577 | 3592 | }); |
| 3578 | 3593 | const result_id = self.spv.allocId(); |
| 3579 | 3594 | try self.func.body.emit(self.spv.gpa, .OpLogicalAnd, .{ |
| 3580 | | .id_result_type = self.typeId(bool_ty_ref), |
| 3595 | .id_result_type = bool_ty_id, |
| 3581 | 3596 | .id_result = result_id, |
| 3582 | 3597 | .operand_1 = valid_eq_id, |
| 3583 | 3598 | .operand_2 = impl_id, |
| ... | ... | @@ -3590,14 +3605,14 @@ const DeclGen = struct { |
| 3590 | 3605 | |
| 3591 | 3606 | const impl_id = self.spv.allocId(); |
| 3592 | 3607 | try self.func.body.emit(self.spv.gpa, .OpLogicalAnd, .{ |
| 3593 | | .id_result_type = self.typeId(bool_ty_ref), |
| 3608 | .id_result_type = bool_ty_id, |
| 3594 | 3609 | .id_result = impl_id, |
| 3595 | 3610 | .operand_1 = lhs_valid_id, |
| 3596 | 3611 | .operand_2 = pl_neq_id, |
| 3597 | 3612 | }); |
| 3598 | 3613 | const result_id = self.spv.allocId(); |
| 3599 | 3614 | try self.func.body.emit(self.spv.gpa, .OpLogicalOr, .{ |
| 3600 | | .id_result_type = self.typeId(bool_ty_ref), |
| 3615 | .id_result_type = bool_ty_id, |
| 3601 | 3616 | .id_result = result_id, |
| 3602 | 3617 | .operand_1 = valid_neq_id, |
| 3603 | 3618 | .operand_2 = impl_id, |
| ... | ... | @@ -3665,7 +3680,7 @@ const DeclGen = struct { |
| 3665 | 3680 | |
| 3666 | 3681 | const result_id = self.spv.allocId(); |
| 3667 | 3682 | try self.func.body.emitRaw(self.spv.gpa, opcode, 4); |
| 3668 | | self.func.body.writeOperand(spec.IdResultType, self.typeId(bool_ty_ref)); |
| 3683 | self.func.body.writeOperand(spec.IdResultType, bool_ty_id); |
| 3669 | 3684 | self.func.body.writeOperand(spec.IdResult, result_id); |
| 3670 | 3685 | self.func.body.writeOperand(spec.IdResultType, cmp_lhs_id); |
| 3671 | 3686 | self.func.body.writeOperand(spec.IdResultType, cmp_rhs_id); |
| ... | ... | @@ -3698,6 +3713,7 @@ const DeclGen = struct { |
| 3698 | 3713 | return try self.cmp(op, result_ty, ty, lhs_id, rhs_id); |
| 3699 | 3714 | } |
| 3700 | 3715 | |
| 3716 | /// Bitcast one type to another. Note: both types, input, output are expected in **direct** representation. |
| 3701 | 3717 | fn bitCast( |
| 3702 | 3718 | self: *DeclGen, |
| 3703 | 3719 | dst_ty: Type, |
| ... | ... | @@ -3705,13 +3721,11 @@ const DeclGen = struct { |
| 3705 | 3721 | src_id: IdRef, |
| 3706 | 3722 | ) !IdRef { |
| 3707 | 3723 | const mod = self.module; |
| 3708 | | const src_ty_ref = try self.resolveType(src_ty, .direct); |
| 3709 | | const dst_ty_ref = try self.resolveType(dst_ty, .direct); |
| 3710 | | const src_key = self.spv.cache.lookup(src_ty_ref); |
| 3711 | | const dst_key = self.spv.cache.lookup(dst_ty_ref); |
| 3724 | const src_ty_id = try self.resolveType(src_ty, .direct); |
| 3725 | const dst_ty_id = try self.resolveType(dst_ty, .direct); |
| 3712 | 3726 | |
| 3713 | 3727 | const result_id = blk: { |
| 3714 | | if (src_ty_ref == dst_ty_ref) { |
| 3728 | if (src_ty_id == dst_ty_id) { |
| 3715 | 3729 | break :blk src_id; |
| 3716 | 3730 | } |
| 3717 | 3731 | |
| ... | ... | @@ -3721,7 +3735,7 @@ const DeclGen = struct { |
| 3721 | 3735 | if (src_ty.zigTypeTag(mod) == .Int and dst_ty.isPtrAtRuntime(mod)) { |
| 3722 | 3736 | const result_id = self.spv.allocId(); |
| 3723 | 3737 | try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ |
| 3724 | | .id_result_type = self.typeId(dst_ty_ref), |
| 3738 | .id_result_type = dst_ty_id, |
| 3725 | 3739 | .id_result = result_id, |
| 3726 | 3740 | .integer_value = src_id, |
| 3727 | 3741 | }); |
| ... | ... | @@ -3731,10 +3745,11 @@ const DeclGen = struct { |
| 3731 | 3745 | // We can only use OpBitcast for specific conversions: between numerical types, and |
| 3732 | 3746 | // between pointers. If the resolved spir-v types fall into this category then emit OpBitcast, |
| 3733 | 3747 | // otherwise use a temporary and perform a pointer cast. |
| 3734 | | if ((src_key.isNumericalType() and dst_key.isNumericalType()) or (src_key == .ptr_type and dst_key == .ptr_type)) { |
| 3748 | const can_bitcast = (src_ty.isNumeric(mod) and dst_ty.isNumeric(mod)) or (src_ty.isPtrAtRuntime(mod) and dst_ty.isPtrAtRuntime(mod)); |
| 3749 | if (can_bitcast) { |
| 3735 | 3750 | const result_id = self.spv.allocId(); |
| 3736 | 3751 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 3737 | | .id_result_type = self.typeId(dst_ty_ref), |
| 3752 | .id_result_type = dst_ty_id, |
| 3738 | 3753 | .id_result = result_id, |
| 3739 | 3754 | .operand = src_id, |
| 3740 | 3755 | }); |
| ... | ... | @@ -3742,13 +3757,13 @@ const DeclGen = struct { |
| 3742 | 3757 | break :blk result_id; |
| 3743 | 3758 | } |
| 3744 | 3759 | |
| 3745 | | const dst_ptr_ty_ref = try self.ptrType(dst_ty, .Function); |
| 3760 | const dst_ptr_ty_id = try self.ptrType(dst_ty, .Function); |
| 3746 | 3761 | |
| 3747 | 3762 | const tmp_id = try self.alloc(src_ty, .{ .storage_class = .Function }); |
| 3748 | 3763 | try self.store(src_ty, tmp_id, src_id, .{}); |
| 3749 | 3764 | const casted_ptr_id = self.spv.allocId(); |
| 3750 | 3765 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 3751 | | .id_result_type = self.typeId(dst_ptr_ty_ref), |
| 3766 | .id_result_type = dst_ptr_ty_id, |
| 3752 | 3767 | .id_result = casted_ptr_id, |
| 3753 | 3768 | .operand = tmp_id, |
| 3754 | 3769 | }); |
| ... | ... | @@ -3761,7 +3776,7 @@ const DeclGen = struct { |
| 3761 | 3776 | // should we change the representation of strange integers? |
| 3762 | 3777 | if (dst_ty.zigTypeTag(mod) == .Int) { |
| 3763 | 3778 | const info = self.arithmeticTypeInfo(dst_ty); |
| 3764 | | return try self.normalize(dst_ty_ref, result_id, info); |
| 3779 | return try self.normalize(dst_ty, result_id, info); |
| 3765 | 3780 | } |
| 3766 | 3781 | |
| 3767 | 3782 | return result_id; |
| ... | ... | @@ -3811,7 +3826,7 @@ const DeclGen = struct { |
| 3811 | 3826 | // type, we don't need to normalize when growing the type. The |
| 3812 | 3827 | // representation is already the same. |
| 3813 | 3828 | if (dst_info.bits < src_info.bits) { |
| 3814 | | result_id.* = try self.normalize(wip.ty_ref, value_id, dst_info); |
| 3829 | result_id.* = try self.normalize(wip.ty, value_id, dst_info); |
| 3815 | 3830 | } else { |
| 3816 | 3831 | result_id.* = value_id; |
| 3817 | 3832 | } |
| ... | ... | @@ -3820,7 +3835,7 @@ const DeclGen = struct { |
| 3820 | 3835 | } |
| 3821 | 3836 | |
| 3822 | 3837 | fn intFromPtr(self: *DeclGen, operand_id: IdRef) !IdRef { |
| 3823 | | const result_type_id = try self.resolveTypeId(Type.usize); |
| 3838 | const result_type_id = try self.resolveType(Type.usize, .direct); |
| 3824 | 3839 | const result_id = self.spv.allocId(); |
| 3825 | 3840 | try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{ |
| 3826 | 3841 | .id_result_type = result_type_id, |
| ... | ... | @@ -3841,21 +3856,21 @@ const DeclGen = struct { |
| 3841 | 3856 | const operand_ty = self.typeOf(ty_op.operand); |
| 3842 | 3857 | const operand_id = try self.resolve(ty_op.operand); |
| 3843 | 3858 | const result_ty = self.typeOfIndex(inst); |
| 3844 | | const result_ty_ref = try self.resolveType(result_ty, .direct); |
| 3845 | | return try self.floatFromInt(result_ty_ref, operand_ty, operand_id); |
| 3859 | return try self.floatFromInt(result_ty, operand_ty, operand_id); |
| 3846 | 3860 | } |
| 3847 | 3861 | |
| 3848 | | fn floatFromInt(self: *DeclGen, result_ty_ref: CacheRef, operand_ty: Type, operand_id: IdRef) !IdRef { |
| 3862 | fn floatFromInt(self: *DeclGen, result_ty: Type, operand_ty: Type, operand_id: IdRef) !IdRef { |
| 3849 | 3863 | const operand_info = self.arithmeticTypeInfo(operand_ty); |
| 3850 | 3864 | const result_id = self.spv.allocId(); |
| 3865 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 3851 | 3866 | switch (operand_info.signedness) { |
| 3852 | 3867 | .signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{ |
| 3853 | | .id_result_type = self.typeId(result_ty_ref), |
| 3868 | .id_result_type = result_ty_id, |
| 3854 | 3869 | .id_result = result_id, |
| 3855 | 3870 | .signed_value = operand_id, |
| 3856 | 3871 | }), |
| 3857 | 3872 | .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{ |
| 3858 | | .id_result_type = self.typeId(result_ty_ref), |
| 3873 | .id_result_type = result_ty_id, |
| 3859 | 3874 | .id_result = result_id, |
| 3860 | 3875 | .unsigned_value = operand_id, |
| 3861 | 3876 | }), |
| ... | ... | @@ -3872,16 +3887,16 @@ const DeclGen = struct { |
| 3872 | 3887 | |
| 3873 | 3888 | fn intFromFloat(self: *DeclGen, result_ty: Type, operand_id: IdRef) !IdRef { |
| 3874 | 3889 | const result_info = self.arithmeticTypeInfo(result_ty); |
| 3875 | | const result_ty_ref = try self.resolveType(result_ty, .direct); |
| 3890 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 3876 | 3891 | const result_id = self.spv.allocId(); |
| 3877 | 3892 | switch (result_info.signedness) { |
| 3878 | 3893 | .signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{ |
| 3879 | | .id_result_type = self.typeId(result_ty_ref), |
| 3894 | .id_result_type = result_ty_id, |
| 3880 | 3895 | .id_result = result_id, |
| 3881 | 3896 | .float_value = operand_id, |
| 3882 | 3897 | }), |
| 3883 | 3898 | .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{ |
| 3884 | | .id_result_type = self.typeId(result_ty_ref), |
| 3899 | .id_result_type = result_ty_id, |
| 3885 | 3900 | .id_result = result_id, |
| 3886 | 3901 | .float_value = operand_id, |
| 3887 | 3902 | }), |
| ... | ... | @@ -3898,7 +3913,7 @@ const DeclGen = struct { |
| 3898 | 3913 | defer wip.deinit(); |
| 3899 | 3914 | for (wip.results, 0..) |*result_id, i| { |
| 3900 | 3915 | const elem_id = try wip.elementAt(Type.bool, operand_id, i); |
| 3901 | | result_id.* = try self.intFromBool(wip.ty_ref, elem_id); |
| 3916 | result_id.* = try self.intFromBool(wip.ty, elem_id); |
| 3902 | 3917 | } |
| 3903 | 3918 | return try wip.finalize(); |
| 3904 | 3919 | } |
| ... | ... | @@ -3907,7 +3922,7 @@ const DeclGen = struct { |
| 3907 | 3922 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3908 | 3923 | const operand_id = try self.resolve(ty_op.operand); |
| 3909 | 3924 | const dest_ty = self.typeOfIndex(inst); |
| 3910 | | const dest_ty_id = try self.resolveTypeId(dest_ty); |
| 3925 | const dest_ty_id = try self.resolveType(dest_ty, .direct); |
| 3911 | 3926 | |
| 3912 | 3927 | const result_id = self.spv.allocId(); |
| 3913 | 3928 | try self.func.body.emit(self.spv.gpa, .OpFConvert, .{ |
| ... | ... | @@ -3957,18 +3972,17 @@ const DeclGen = struct { |
| 3957 | 3972 | const slice_ty = self.typeOfIndex(inst); |
| 3958 | 3973 | const elem_ptr_ty = slice_ty.slicePtrFieldType(mod); |
| 3959 | 3974 | |
| 3960 | | const elem_ptr_ty_ref = try self.resolveType(elem_ptr_ty, .direct); |
| 3961 | | const size_ty_ref = try self.sizeType(); |
| 3975 | const elem_ptr_ty_id = try self.resolveType(elem_ptr_ty, .direct); |
| 3962 | 3976 | |
| 3963 | 3977 | const array_ptr_id = try self.resolve(ty_op.operand); |
| 3964 | | const len_id = try self.constInt(size_ty_ref, array_ty.arrayLen(mod)); |
| 3978 | const len_id = try self.constInt(Type.usize, array_ty.arrayLen(mod), .direct); |
| 3965 | 3979 | |
| 3966 | 3980 | const elem_ptr_id = if (!array_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 3967 | 3981 | // Note: The pointer is something like *opaque{}, so we need to bitcast it to the element type. |
| 3968 | 3982 | try self.bitCast(elem_ptr_ty, array_ptr_ty, array_ptr_id) |
| 3969 | 3983 | else |
| 3970 | 3984 | // Convert the pointer-to-array to a pointer to the first element. |
| 3971 | | try self.accessChain(elem_ptr_ty_ref, array_ptr_id, &.{0}); |
| 3985 | try self.accessChain(elem_ptr_ty_id, array_ptr_id, &.{0}); |
| 3972 | 3986 | |
| 3973 | 3987 | return try self.constructStruct( |
| 3974 | 3988 | slice_ty, |
| ... | ... | @@ -4092,8 +4106,8 @@ const DeclGen = struct { |
| 4092 | 4106 | const array_ty = ty.childType(mod); |
| 4093 | 4107 | const elem_ty = array_ty.childType(mod); |
| 4094 | 4108 | const abi_size = elem_ty.abiSize(mod); |
| 4095 | | const usize_ty_ref = try self.resolveType(Type.usize, .direct); |
| 4096 | | return self.spv.constInt(usize_ty_ref, array_ty.arrayLenIncludingSentinel(mod) * abi_size); |
| 4109 | const size = array_ty.arrayLenIncludingSentinel(mod) * abi_size; |
| 4110 | return try self.constInt(Type.usize, size, .direct); |
| 4097 | 4111 | }, |
| 4098 | 4112 | .Many, .C => unreachable, |
| 4099 | 4113 | } |
| ... | ... | @@ -4142,10 +4156,10 @@ const DeclGen = struct { |
| 4142 | 4156 | const index_id = try self.resolve(bin_op.rhs); |
| 4143 | 4157 | |
| 4144 | 4158 | const ptr_ty = self.typeOfIndex(inst); |
| 4145 | | const ptr_ty_ref = try self.resolveType(ptr_ty, .direct); |
| 4159 | const ptr_ty_id = try self.resolveType(ptr_ty, .direct); |
| 4146 | 4160 | |
| 4147 | 4161 | const slice_ptr = try self.extractField(ptr_ty, slice_id, 0); |
| 4148 | | return try self.ptrAccessChain(ptr_ty_ref, slice_ptr, index_id, &.{}); |
| 4162 | return try self.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{}); |
| 4149 | 4163 | } |
| 4150 | 4164 | |
| 4151 | 4165 | fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| ... | ... | @@ -4158,10 +4172,10 @@ const DeclGen = struct { |
| 4158 | 4172 | const index_id = try self.resolve(bin_op.rhs); |
| 4159 | 4173 | |
| 4160 | 4174 | const ptr_ty = slice_ty.slicePtrFieldType(mod); |
| 4161 | | const ptr_ty_ref = try self.resolveType(ptr_ty, .direct); |
| 4175 | const ptr_ty_id = try self.resolveType(ptr_ty, .direct); |
| 4162 | 4176 | |
| 4163 | 4177 | const slice_ptr = try self.extractField(ptr_ty, slice_id, 0); |
| 4164 | | const elem_ptr = try self.ptrAccessChain(ptr_ty_ref, slice_ptr, index_id, &.{}); |
| 4178 | const elem_ptr = try self.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{}); |
| 4165 | 4179 | return try self.load(slice_ty.childType(mod), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(mod) }); |
| 4166 | 4180 | } |
| 4167 | 4181 | |
| ... | ... | @@ -4169,14 +4183,14 @@ const DeclGen = struct { |
| 4169 | 4183 | const mod = self.module; |
| 4170 | 4184 | // Construct new pointer type for the resulting pointer |
| 4171 | 4185 | const elem_ty = ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T. |
| 4172 | | const elem_ptr_ty_ref = try self.ptrType(elem_ty, self.spvStorageClass(ptr_ty.ptrAddressSpace(mod))); |
| 4186 | const elem_ptr_ty_id = try self.ptrType(elem_ty, self.spvStorageClass(ptr_ty.ptrAddressSpace(mod))); |
| 4173 | 4187 | if (ptr_ty.isSinglePointer(mod)) { |
| 4174 | 4188 | // Pointer-to-array. In this case, the resulting pointer is not of the same type |
| 4175 | 4189 | // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain. |
| 4176 | | return try self.accessChainId(elem_ptr_ty_ref, ptr_id, &.{index_id}); |
| 4190 | return try self.accessChainId(elem_ptr_ty_id, ptr_id, &.{index_id}); |
| 4177 | 4191 | } else { |
| 4178 | 4192 | // Resulting pointer type is the same as the ptr_ty, so use ptrAccessChain |
| 4179 | | return try self.ptrAccessChain(elem_ptr_ty_ref, ptr_id, index_id, &.{}); |
| 4193 | return try self.ptrAccessChain(elem_ptr_ty_id, ptr_id, index_id, &.{}); |
| 4180 | 4194 | } |
| 4181 | 4195 | } |
| 4182 | 4196 | |
| ... | ... | @@ -4209,11 +4223,11 @@ const DeclGen = struct { |
| 4209 | 4223 | // For now, just generate a temporary and use that. |
| 4210 | 4224 | // TODO: This backend probably also should use isByRef from llvm... |
| 4211 | 4225 | |
| 4212 | | const elem_ptr_ty_ref = try self.ptrType(elem_ty, .Function); |
| 4226 | const elem_ptr_ty_id = try self.ptrType(elem_ty, .Function); |
| 4213 | 4227 | |
| 4214 | 4228 | const tmp_id = try self.alloc(array_ty, .{ .storage_class = .Function }); |
| 4215 | 4229 | try self.store(array_ty, tmp_id, array_id, .{}); |
| 4216 | | const elem_ptr_id = try self.accessChainId(elem_ptr_ty_ref, tmp_id, &.{index_id}); |
| 4230 | const elem_ptr_id = try self.accessChainId(elem_ptr_ty_id, tmp_id, &.{index_id}); |
| 4217 | 4231 | return try self.load(elem_ty, elem_ptr_id, .{}); |
| 4218 | 4232 | } |
| 4219 | 4233 | |
| ... | ... | @@ -4238,13 +4252,13 @@ const DeclGen = struct { |
| 4238 | 4252 | const scalar_ty = vector_ty.scalarType(mod); |
| 4239 | 4253 | |
| 4240 | 4254 | const storage_class = self.spvStorageClass(vector_ptr_ty.ptrAddressSpace(mod)); |
| 4241 | | const scalar_ptr_ty_ref = try self.ptrType(scalar_ty, storage_class); |
| 4255 | const scalar_ptr_ty_id = try self.ptrType(scalar_ty, storage_class); |
| 4242 | 4256 | |
| 4243 | 4257 | const vector_ptr = try self.resolve(data.vector_ptr); |
| 4244 | 4258 | const index = try self.resolve(extra.lhs); |
| 4245 | 4259 | const operand = try self.resolve(extra.rhs); |
| 4246 | 4260 | |
| 4247 | | const elem_ptr_id = try self.accessChainId(scalar_ptr_ty_ref, vector_ptr, &.{index}); |
| 4261 | const elem_ptr_id = try self.accessChainId(scalar_ptr_ty_id, vector_ptr, &.{index}); |
| 4248 | 4262 | try self.store(scalar_ty, elem_ptr_id, operand, .{ |
| 4249 | 4263 | .is_volatile = vector_ptr_ty.isVolatilePtr(mod), |
| 4250 | 4264 | }); |
| ... | ... | @@ -4260,7 +4274,7 @@ const DeclGen = struct { |
| 4260 | 4274 | if (layout.tag_size == 0) return; |
| 4261 | 4275 | |
| 4262 | 4276 | const tag_ty = un_ty.unionTagTypeSafety(mod).?; |
| 4263 | | const tag_ptr_ty_ref = try self.ptrType(tag_ty, self.spvStorageClass(un_ptr_ty.ptrAddressSpace(mod))); |
| 4277 | const tag_ptr_ty_id = try self.ptrType(tag_ty, self.spvStorageClass(un_ptr_ty.ptrAddressSpace(mod))); |
| 4264 | 4278 | |
| 4265 | 4279 | const union_ptr_id = try self.resolve(bin_op.lhs); |
| 4266 | 4280 | const new_tag_id = try self.resolve(bin_op.rhs); |
| ... | ... | @@ -4268,7 +4282,7 @@ const DeclGen = struct { |
| 4268 | 4282 | if (!layout.has_payload) { |
| 4269 | 4283 | try self.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) }); |
| 4270 | 4284 | } else { |
| 4271 | | const ptr_id = try self.accessChain(tag_ptr_ty_ref, union_ptr_id, &.{layout.tag_index}); |
| 4285 | const ptr_id = try self.accessChain(tag_ptr_ty_id, union_ptr_id, &.{layout.tag_index}); |
| 4272 | 4286 | try self.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) }); |
| 4273 | 4287 | } |
| 4274 | 4288 | } |
| ... | ... | @@ -4298,6 +4312,8 @@ const DeclGen = struct { |
| 4298 | 4312 | // union type, then get the field pointer and pointer-cast it to the |
| 4299 | 4313 | // right type to store it. Finally load the entire union. |
| 4300 | 4314 | |
| 4315 | // Note: The result here is not cached, because it generates runtime code. |
| 4316 | |
| 4301 | 4317 | const mod = self.module; |
| 4302 | 4318 | const ip = &mod.intern_pool; |
| 4303 | 4319 | const union_ty = mod.typeToUnion(ty).?; |
| ... | ... | @@ -4316,28 +4332,26 @@ const DeclGen = struct { |
| 4316 | 4332 | } else 0; |
| 4317 | 4333 | |
| 4318 | 4334 | if (!layout.has_payload) { |
| 4319 | | const tag_ty_ref = try self.resolveType(tag_ty, .direct); |
| 4320 | | return try self.constInt(tag_ty_ref, tag_int); |
| 4335 | return try self.constInt(tag_ty, tag_int, .direct); |
| 4321 | 4336 | } |
| 4322 | 4337 | |
| 4323 | 4338 | const tmp_id = try self.alloc(ty, .{ .storage_class = .Function }); |
| 4324 | 4339 | |
| 4325 | 4340 | if (layout.tag_size != 0) { |
| 4326 | | const tag_ty_ref = try self.resolveType(tag_ty, .direct); |
| 4327 | | const tag_ptr_ty_ref = try self.ptrType(tag_ty, .Function); |
| 4328 | | const ptr_id = try self.accessChain(tag_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.tag_index))}); |
| 4329 | | const tag_id = try self.constInt(tag_ty_ref, tag_int); |
| 4341 | const tag_ptr_ty_id = try self.ptrType(tag_ty, .Function); |
| 4342 | const ptr_id = try self.accessChain(tag_ptr_ty_id, tmp_id, &.{@as(u32, @intCast(layout.tag_index))}); |
| 4343 | const tag_id = try self.constInt(tag_ty, tag_int, .direct); |
| 4330 | 4344 | try self.store(tag_ty, ptr_id, tag_id, .{}); |
| 4331 | 4345 | } |
| 4332 | 4346 | |
| 4333 | 4347 | const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]); |
| 4334 | 4348 | if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 4335 | | const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function); |
| 4336 | | const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index}); |
| 4337 | | const active_pl_ptr_ty_ref = try self.ptrType(payload_ty, .Function); |
| 4349 | const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, .Function); |
| 4350 | const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); |
| 4351 | const active_pl_ptr_ty_id = try self.ptrType(payload_ty, .Function); |
| 4338 | 4352 | const active_pl_ptr_id = self.spv.allocId(); |
| 4339 | 4353 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 4340 | | .id_result_type = self.typeId(active_pl_ptr_ty_ref), |
| 4354 | .id_result_type = active_pl_ptr_ty_id, |
| 4341 | 4355 | .id_result = active_pl_ptr_id, |
| 4342 | 4356 | .operand = pl_ptr_id, |
| 4343 | 4357 | }); |
| ... | ... | @@ -4396,13 +4410,13 @@ const DeclGen = struct { |
| 4396 | 4410 | const tmp_id = try self.alloc(object_ty, .{ .storage_class = .Function }); |
| 4397 | 4411 | try self.store(object_ty, tmp_id, object_id, .{}); |
| 4398 | 4412 | |
| 4399 | | const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function); |
| 4400 | | const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index}); |
| 4413 | const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, .Function); |
| 4414 | const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); |
| 4401 | 4415 | |
| 4402 | | const active_pl_ptr_ty_ref = try self.ptrType(field_ty, .Function); |
| 4416 | const active_pl_ptr_ty_id = try self.ptrType(field_ty, .Function); |
| 4403 | 4417 | const active_pl_ptr_id = self.spv.allocId(); |
| 4404 | 4418 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 4405 | | .id_result_type = self.typeId(active_pl_ptr_ty_ref), |
| 4419 | .id_result_type = active_pl_ptr_ty_id, |
| 4406 | 4420 | .id_result = active_pl_ptr_id, |
| 4407 | 4421 | .operand = pl_ptr_id, |
| 4408 | 4422 | }); |
| ... | ... | @@ -4419,9 +4433,7 @@ const DeclGen = struct { |
| 4419 | 4433 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 4420 | 4434 | |
| 4421 | 4435 | const parent_ty = ty_pl.ty.toType().childType(mod); |
| 4422 | | const res_ty = try self.resolveType(ty_pl.ty.toType(), .indirect); |
| 4423 | | const usize_ty = Type.usize; |
| 4424 | | const usize_ty_ref = try self.resolveType(usize_ty, .direct); |
| 4436 | const result_ty_id = try self.resolveType(ty_pl.ty.toType(), .indirect); |
| 4425 | 4437 | |
| 4426 | 4438 | const field_ptr = try self.resolve(extra.field_ptr); |
| 4427 | 4439 | const field_ptr_int = try self.intFromPtr(field_ptr); |
| ... | ... | @@ -4430,13 +4442,13 @@ const DeclGen = struct { |
| 4430 | 4442 | const base_ptr_int = base_ptr_int: { |
| 4431 | 4443 | if (field_offset == 0) break :base_ptr_int field_ptr_int; |
| 4432 | 4444 | |
| 4433 | | const field_offset_id = try self.constInt(usize_ty_ref, field_offset); |
| 4434 | | break :base_ptr_int try self.binOpSimple(usize_ty, field_ptr_int, field_offset_id, .OpISub); |
| 4445 | const field_offset_id = try self.constInt(Type.usize, field_offset, .direct); |
| 4446 | break :base_ptr_int try self.binOpSimple(Type.usize, field_ptr_int, field_offset_id, .OpISub); |
| 4435 | 4447 | }; |
| 4436 | 4448 | |
| 4437 | 4449 | const base_ptr = self.spv.allocId(); |
| 4438 | 4450 | try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ |
| 4439 | | .id_result_type = self.spv.resultId(res_ty), |
| 4451 | .id_result_type = result_ty_id, |
| 4440 | 4452 | .id_result = base_ptr, |
| 4441 | 4453 | .integer_value = base_ptr_int, |
| 4442 | 4454 | }); |
| ... | ... | @@ -4451,7 +4463,7 @@ const DeclGen = struct { |
| 4451 | 4463 | object_ptr: IdRef, |
| 4452 | 4464 | field_index: u32, |
| 4453 | 4465 | ) !IdRef { |
| 4454 | | const result_ty_ref = try self.resolveType(result_ptr_ty, .direct); |
| 4466 | const result_ty_id = try self.resolveType(result_ptr_ty, .direct); |
| 4455 | 4467 | |
| 4456 | 4468 | const mod = self.module; |
| 4457 | 4469 | const object_ty = object_ptr_ty.childType(mod); |
| ... | ... | @@ -4459,7 +4471,7 @@ const DeclGen = struct { |
| 4459 | 4471 | .Struct => switch (object_ty.containerLayout(mod)) { |
| 4460 | 4472 | .@"packed" => unreachable, // TODO |
| 4461 | 4473 | else => { |
| 4462 | | return try self.accessChain(result_ty_ref, object_ptr, &.{field_index}); |
| 4474 | return try self.accessChain(result_ty_id, object_ptr, &.{field_index}); |
| 4463 | 4475 | }, |
| 4464 | 4476 | }, |
| 4465 | 4477 | .Union => switch (object_ty.containerLayout(mod)) { |
| ... | ... | @@ -4469,16 +4481,16 @@ const DeclGen = struct { |
| 4469 | 4481 | if (!layout.has_payload) { |
| 4470 | 4482 | // Asked to get a pointer to a zero-sized field. Just lower this |
| 4471 | 4483 | // to undefined, there is no reason to make it be a valid pointer. |
| 4472 | | return try self.spv.constUndef(result_ty_ref); |
| 4484 | return try self.spv.constUndef(result_ty_id); |
| 4473 | 4485 | } |
| 4474 | 4486 | |
| 4475 | 4487 | const storage_class = self.spvStorageClass(object_ptr_ty.ptrAddressSpace(mod)); |
| 4476 | | const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, storage_class); |
| 4477 | | const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, object_ptr, &.{layout.payload_index}); |
| 4488 | const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, storage_class); |
| 4489 | const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index}); |
| 4478 | 4490 | |
| 4479 | 4491 | const active_pl_ptr_id = self.spv.allocId(); |
| 4480 | 4492 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 4481 | | .id_result_type = self.typeId(result_ty_ref), |
| 4493 | .id_result_type = result_ty_id, |
| 4482 | 4494 | .id_result = active_pl_ptr_id, |
| 4483 | 4495 | .operand = pl_ptr_id, |
| 4484 | 4496 | }); |
| ... | ... | @@ -4506,7 +4518,7 @@ const DeclGen = struct { |
| 4506 | 4518 | }; |
| 4507 | 4519 | |
| 4508 | 4520 | // Allocate a function-local variable, with possible initializer. |
| 4509 | | // This function returns a pointer to a variable of type `ty_ref`, |
| 4521 | // This function returns a pointer to a variable of type `ty`, |
| 4510 | 4522 | // which is in the Generic address space. The variable is actually |
| 4511 | 4523 | // placed in the Function address space. |
| 4512 | 4524 | fn alloc( |
| ... | ... | @@ -4514,13 +4526,13 @@ const DeclGen = struct { |
| 4514 | 4526 | ty: Type, |
| 4515 | 4527 | options: AllocOptions, |
| 4516 | 4528 | ) !IdRef { |
| 4517 | | const ptr_fn_ty_ref = try self.ptrType(ty, .Function); |
| 4529 | const ptr_fn_ty_id = try self.ptrType(ty, .Function); |
| 4518 | 4530 | |
| 4519 | 4531 | // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to |
| 4520 | 4532 | // directly generate them into func.prologue instead of the body. |
| 4521 | 4533 | const var_id = self.spv.allocId(); |
| 4522 | 4534 | try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{ |
| 4523 | | .id_result_type = self.typeId(ptr_fn_ty_ref), |
| 4535 | .id_result_type = ptr_fn_ty_id, |
| 4524 | 4536 | .id_result = var_id, |
| 4525 | 4537 | .storage_class = .Function, |
| 4526 | 4538 | .initializer = options.initializer, |
| ... | ... | @@ -4533,9 +4545,9 @@ const DeclGen = struct { |
| 4533 | 4545 | |
| 4534 | 4546 | switch (options.storage_class) { |
| 4535 | 4547 | .Generic => { |
| 4536 | | const ptr_gn_ty_ref = try self.ptrType(ty, .Generic); |
| 4548 | const ptr_gn_ty_id = try self.ptrType(ty, .Generic); |
| 4537 | 4549 | // Convert to a generic pointer |
| 4538 | | return self.castToGeneric(self.typeId(ptr_gn_ty_ref), var_id); |
| 4550 | return self.castToGeneric(ptr_gn_ty_id, var_id); |
| 4539 | 4551 | }, |
| 4540 | 4552 | .Function => return var_id, |
| 4541 | 4553 | else => unreachable, |
| ... | ... | @@ -4563,9 +4575,9 @@ const DeclGen = struct { |
| 4563 | 4575 | assert(self.control_flow == .structured); |
| 4564 | 4576 | |
| 4565 | 4577 | const result_id = self.spv.allocId(); |
| 4566 | | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 4578 | const block_id_ty_id = try self.resolveType(Type.u32, .direct); |
| 4567 | 4579 | try self.func.body.emitRaw(self.spv.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent... |
| 4568 | | self.func.body.writeOperand(spec.IdResultType, self.typeId(block_id_ty_ref)); |
| 4580 | self.func.body.writeOperand(spec.IdResultType, block_id_ty_id); |
| 4569 | 4581 | self.func.body.writeOperand(spec.IdRef, result_id); |
| 4570 | 4582 | |
| 4571 | 4583 | for (incoming) |incoming_block| { |
| ... | ... | @@ -4663,8 +4675,8 @@ const DeclGen = struct { |
| 4663 | 4675 | // Make sure that we are still in a block when exiting the function. |
| 4664 | 4676 | // TODO: Can we get rid of that? |
| 4665 | 4677 | try self.beginSpvBlock(self.spv.allocId()); |
| 4666 | | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 4667 | | return try self.spv.constUndef(block_id_ty_ref); |
| 4678 | const block_id_ty_id = try self.resolveType(Type.u32, .direct); |
| 4679 | return try self.spv.constUndef(block_id_ty_id); |
| 4668 | 4680 | } |
| 4669 | 4681 | |
| 4670 | 4682 | // The top-most merge actually only has a single source, the |
| ... | ... | @@ -4745,7 +4757,7 @@ const DeclGen = struct { |
| 4745 | 4757 | |
| 4746 | 4758 | assert(block.label != null); |
| 4747 | 4759 | const result_id = self.spv.allocId(); |
| 4748 | | const result_type_id = try self.resolveTypeId(ty); |
| 4760 | const result_type_id = try self.resolveType(ty, .direct); |
| 4749 | 4761 | |
| 4750 | 4762 | try self.func.body.emitRaw( |
| 4751 | 4763 | self.spv.gpa, |
| ... | ... | @@ -4781,12 +4793,11 @@ const DeclGen = struct { |
| 4781 | 4793 | assert(cf.block_stack.items.len > 0); |
| 4782 | 4794 | |
| 4783 | 4795 | // Check if the target of the branch was this current block. |
| 4784 | | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 4785 | | const this_block = try self.constInt(block_id_ty_ref, @intFromEnum(inst)); |
| 4796 | const this_block = try self.constInt(Type.u32, @intFromEnum(inst), .direct); |
| 4786 | 4797 | const jump_to_this_block_id = self.spv.allocId(); |
| 4787 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 4798 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 4788 | 4799 | try self.func.body.emit(self.spv.gpa, .OpIEqual, .{ |
| 4789 | | .id_result_type = self.typeId(bool_ty_ref), |
| 4800 | .id_result_type = bool_ty_id, |
| 4790 | 4801 | .id_result = jump_to_this_block_id, |
| 4791 | 4802 | .operand_1 = next_block, |
| 4792 | 4803 | .operand_2 = this_block, |
| ... | ... | @@ -4862,8 +4873,7 @@ const DeclGen = struct { |
| 4862 | 4873 | try self.store(operand_ty, block_result_var_id, operand_id, .{}); |
| 4863 | 4874 | } |
| 4864 | 4875 | |
| 4865 | | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 4866 | | const next_block = try self.constInt(block_id_ty_ref, @intFromEnum(br.block_inst)); |
| 4876 | const next_block = try self.constInt(Type.u32, @intFromEnum(br.block_inst), .direct); |
| 4867 | 4877 | try self.structuredBreak(next_block); |
| 4868 | 4878 | }, |
| 4869 | 4879 | .unstructured => |cf| { |
| ... | ... | @@ -5026,8 +5036,7 @@ const DeclGen = struct { |
| 5026 | 5036 | // Functions with an empty error set are emitted with an error code |
| 5027 | 5037 | // return type and return zero so they can be function pointers coerced |
| 5028 | 5038 | // to functions that return anyerror. |
| 5029 | | const err_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 5030 | | const no_err_id = try self.constInt(err_ty_ref, 0); |
| 5039 | const no_err_id = try self.constInt(Type.anyerror, 0, .direct); |
| 5031 | 5040 | return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id }); |
| 5032 | 5041 | } else { |
| 5033 | 5042 | return try self.func.body.emit(self.spv.gpa, .OpReturn, {}); |
| ... | ... | @@ -5051,8 +5060,7 @@ const DeclGen = struct { |
| 5051 | 5060 | // Functions with an empty error set are emitted with an error code |
| 5052 | 5061 | // return type and return zero so they can be function pointers coerced |
| 5053 | 5062 | // to functions that return anyerror. |
| 5054 | | const err_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 5055 | | const no_err_id = try self.constInt(err_ty_ref, 0); |
| 5063 | const no_err_id = try self.constInt(Type.anyerror, 0, .direct); |
| 5056 | 5064 | return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id }); |
| 5057 | 5065 | } else { |
| 5058 | 5066 | return try self.func.body.emit(self.spv.gpa, .OpReturn, {}); |
| ... | ... | @@ -5076,8 +5084,7 @@ const DeclGen = struct { |
| 5076 | 5084 | const err_union_ty = self.typeOf(pl_op.operand); |
| 5077 | 5085 | const payload_ty = self.typeOfIndex(inst); |
| 5078 | 5086 | |
| 5079 | | const err_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 5080 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 5087 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 5081 | 5088 | |
| 5082 | 5089 | const eu_layout = self.errorUnionLayout(payload_ty); |
| 5083 | 5090 | |
| ... | ... | @@ -5087,10 +5094,10 @@ const DeclGen = struct { |
| 5087 | 5094 | else |
| 5088 | 5095 | err_union_id; |
| 5089 | 5096 | |
| 5090 | | const zero_id = try self.constInt(err_ty_ref, 0); |
| 5097 | const zero_id = try self.constInt(Type.anyerror, 0, .direct); |
| 5091 | 5098 | const is_err_id = self.spv.allocId(); |
| 5092 | 5099 | try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{ |
| 5093 | | .id_result_type = self.typeId(bool_ty_ref), |
| 5100 | .id_result_type = bool_ty_id, |
| 5094 | 5101 | .id_result = is_err_id, |
| 5095 | 5102 | .operand_1 = err_id, |
| 5096 | 5103 | .operand_2 = zero_id, |
| ... | ... | @@ -5142,11 +5149,11 @@ const DeclGen = struct { |
| 5142 | 5149 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5143 | 5150 | const operand_id = try self.resolve(ty_op.operand); |
| 5144 | 5151 | const err_union_ty = self.typeOf(ty_op.operand); |
| 5145 | | const err_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 5152 | const err_ty_id = try self.resolveType(Type.anyerror, .direct); |
| 5146 | 5153 | |
| 5147 | 5154 | if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 5148 | 5155 | // No error possible, so just return undefined. |
| 5149 | | return try self.spv.constUndef(err_ty_ref); |
| 5156 | return try self.spv.constUndef(err_ty_id); |
| 5150 | 5157 | } |
| 5151 | 5158 | |
| 5152 | 5159 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| ... | ... | @@ -5185,11 +5192,11 @@ const DeclGen = struct { |
| 5185 | 5192 | return operand_id; |
| 5186 | 5193 | } |
| 5187 | 5194 | |
| 5188 | | const payload_ty_ref = try self.resolveType(payload_ty, .indirect); |
| 5195 | const payload_ty_id = try self.resolveType(payload_ty, .indirect); |
| 5189 | 5196 | |
| 5190 | 5197 | var members: [2]IdRef = undefined; |
| 5191 | 5198 | members[eu_layout.errorFieldIndex()] = operand_id; |
| 5192 | | members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_ref); |
| 5199 | members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_id); |
| 5193 | 5200 | |
| 5194 | 5201 | var types: [2]Type = undefined; |
| 5195 | 5202 | types[eu_layout.errorFieldIndex()] = Type.anyerror; |
| ... | ... | @@ -5203,15 +5210,14 @@ const DeclGen = struct { |
| 5203 | 5210 | const err_union_ty = self.typeOfIndex(inst); |
| 5204 | 5211 | const operand_id = try self.resolve(ty_op.operand); |
| 5205 | 5212 | const payload_ty = self.typeOf(ty_op.operand); |
| 5206 | | const err_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 5207 | 5213 | const eu_layout = self.errorUnionLayout(payload_ty); |
| 5208 | 5214 | |
| 5209 | 5215 | if (!eu_layout.payload_has_bits) { |
| 5210 | | return try self.constInt(err_ty_ref, 0); |
| 5216 | return try self.constInt(Type.anyerror, 0, .direct); |
| 5211 | 5217 | } |
| 5212 | 5218 | |
| 5213 | 5219 | var members: [2]IdRef = undefined; |
| 5214 | | members[eu_layout.errorFieldIndex()] = try self.constInt(err_ty_ref, 0); |
| 5220 | members[eu_layout.errorFieldIndex()] = try self.constInt(Type.anyerror, 0, .direct); |
| 5215 | 5221 | members[eu_layout.payloadFieldIndex()] = try self.convertToIndirect(payload_ty, operand_id); |
| 5216 | 5222 | |
| 5217 | 5223 | var types: [2]Type = undefined; |
| ... | ... | @@ -5229,7 +5235,7 @@ const DeclGen = struct { |
| 5229 | 5235 | const optional_ty = if (is_pointer) operand_ty.childType(mod) else operand_ty; |
| 5230 | 5236 | const payload_ty = optional_ty.optionalChild(mod); |
| 5231 | 5237 | |
| 5232 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 5238 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 5233 | 5239 | |
| 5234 | 5240 | if (optional_ty.optionalReprIsPayload(mod)) { |
| 5235 | 5241 | // Pointer payload represents nullability: pointer or slice. |
| ... | ... | @@ -5248,8 +5254,8 @@ const DeclGen = struct { |
| 5248 | 5254 | else |
| 5249 | 5255 | loaded_id; |
| 5250 | 5256 | |
| 5251 | | const payload_ty_ref = try self.resolveType(ptr_ty, .direct); |
| 5252 | | const null_id = try self.spv.constNull(payload_ty_ref); |
| 5257 | const payload_ty_id = try self.resolveType(ptr_ty, .direct); |
| 5258 | const null_id = try self.spv.constNull(payload_ty_id); |
| 5253 | 5259 | const op: std.math.CompareOperator = switch (pred) { |
| 5254 | 5260 | .is_null => .eq, |
| 5255 | 5261 | .is_non_null => .neq, |
| ... | ... | @@ -5261,8 +5267,8 @@ const DeclGen = struct { |
| 5261 | 5267 | if (is_pointer) { |
| 5262 | 5268 | if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 5263 | 5269 | const storage_class = self.spvStorageClass(operand_ty.ptrAddressSpace(mod)); |
| 5264 | | const bool_ptr_ty = try self.ptrType(Type.bool, storage_class); |
| 5265 | | const tag_ptr_id = try self.accessChain(bool_ptr_ty, operand_id, &.{1}); |
| 5270 | const bool_ptr_ty_id = try self.ptrType(Type.bool, storage_class); |
| 5271 | const tag_ptr_id = try self.accessChain(bool_ptr_ty_id, operand_id, &.{1}); |
| 5266 | 5272 | break :blk try self.load(Type.bool, tag_ptr_id, .{}); |
| 5267 | 5273 | } |
| 5268 | 5274 | |
| ... | ... | @@ -5283,7 +5289,7 @@ const DeclGen = struct { |
| 5283 | 5289 | // Invert condition |
| 5284 | 5290 | const result_id = self.spv.allocId(); |
| 5285 | 5291 | try self.func.body.emit(self.spv.gpa, .OpLogicalNot, .{ |
| 5286 | | .id_result_type = self.typeId(bool_ty_ref), |
| 5292 | .id_result_type = bool_ty_id, |
| 5287 | 5293 | .id_result = result_id, |
| 5288 | 5294 | .operand = is_non_null_id, |
| 5289 | 5295 | }); |
| ... | ... | @@ -5305,8 +5311,7 @@ const DeclGen = struct { |
| 5305 | 5311 | |
| 5306 | 5312 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 5307 | 5313 | const eu_layout = self.errorUnionLayout(payload_ty); |
| 5308 | | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 5309 | | const err_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 5314 | const bool_ty_id = try self.resolveType(Type.bool, .direct); |
| 5310 | 5315 | |
| 5311 | 5316 | const error_id = if (!eu_layout.payload_has_bits) |
| 5312 | 5317 | operand_id |
| ... | ... | @@ -5315,10 +5320,10 @@ const DeclGen = struct { |
| 5315 | 5320 | |
| 5316 | 5321 | const result_id = self.spv.allocId(); |
| 5317 | 5322 | const operands = .{ |
| 5318 | | .id_result_type = self.typeId(bool_ty_ref), |
| 5323 | .id_result_type = bool_ty_id, |
| 5319 | 5324 | .id_result = result_id, |
| 5320 | 5325 | .operand_1 = error_id, |
| 5321 | | .operand_2 = try self.constInt(err_ty_ref, 0), |
| 5326 | .operand_2 = try self.constInt(Type.anyerror, 0, .direct), |
| 5322 | 5327 | }; |
| 5323 | 5328 | switch (pred) { |
| 5324 | 5329 | .is_err => try self.func.body.emit(self.spv.gpa, .OpINotEqual, operands), |
| ... | ... | @@ -5351,7 +5356,7 @@ const DeclGen = struct { |
| 5351 | 5356 | const optional_ty = operand_ty.childType(mod); |
| 5352 | 5357 | const payload_ty = optional_ty.optionalChild(mod); |
| 5353 | 5358 | const result_ty = self.typeOfIndex(inst); |
| 5354 | | const result_ty_ref = try self.resolveType(result_ty, .direct); |
| 5359 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 5355 | 5360 | |
| 5356 | 5361 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 5357 | 5362 | // There is no payload, but we still need to return a valid pointer. |
| ... | ... | @@ -5364,7 +5369,7 @@ const DeclGen = struct { |
| 5364 | 5369 | return try self.bitCast(result_ty, operand_ty, operand_id); |
| 5365 | 5370 | } |
| 5366 | 5371 | |
| 5367 | | return try self.accessChain(result_ty_ref, operand_id, &.{0}); |
| 5372 | return try self.accessChain(result_ty_id, operand_id, &.{0}); |
| 5368 | 5373 | } |
| 5369 | 5374 | |
| 5370 | 5375 | fn airWrapOptional(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| ... | ... | @@ -5440,7 +5445,7 @@ const DeclGen = struct { |
| 5440 | 5445 | }; |
| 5441 | 5446 | |
| 5442 | 5447 | // First, pre-allocate the labels for the cases. |
| 5443 | | const first_case_label = self.spv.allocIds(num_cases); |
| 5448 | const case_labels = self.spv.allocIds(num_cases); |
| 5444 | 5449 | // We always need the default case - if zig has none, we will generate unreachable there. |
| 5445 | 5450 | const default = self.spv.allocId(); |
| 5446 | 5451 | |
| ... | ... | @@ -5471,7 +5476,7 @@ const DeclGen = struct { |
| 5471 | 5476 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 5472 | 5477 | extra_index = case.end + case.data.items_len + case_body.len; |
| 5473 | 5478 | |
| 5474 | | const label: IdRef = @enumFromInt(@intFromEnum(first_case_label) + case_i); |
| 5479 | const label = case_labels.at(case_i); |
| 5475 | 5480 | |
| 5476 | 5481 | for (items) |item| { |
| 5477 | 5482 | const value = (try self.air.value(item, mod)) orelse unreachable; |
| ... | ... | @@ -5511,7 +5516,7 @@ const DeclGen = struct { |
| 5511 | 5516 | const case_body: []const Air.Inst.Index = @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]); |
| 5512 | 5517 | extra_index = case.end + case.data.items_len + case_body.len; |
| 5513 | 5518 | |
| 5514 | | const label: IdResult = @enumFromInt(@intFromEnum(first_case_label) + case_i); |
| 5519 | const label = case_labels.at(case_i); |
| 5515 | 5520 | |
| 5516 | 5521 | try self.beginSpvBlock(label); |
| 5517 | 5522 | |
| ... | ... | @@ -5566,9 +5571,8 @@ const DeclGen = struct { |
| 5566 | 5571 | const mod = self.module; |
| 5567 | 5572 | const decl = mod.declPtr(self.decl_index); |
| 5568 | 5573 | const path = decl.getFileScope(mod).sub_file_path; |
| 5569 | | const src_fname_id = try self.spv.resolveSourceFileName(path); |
| 5570 | 5574 | try self.func.body.emit(self.spv.gpa, .OpLine, .{ |
| 5571 | | .file = src_fname_id, |
| 5575 | .file = try self.spv.resolveString(path), |
| 5572 | 5576 | .line = self.base_line + dbg_stmt.line + 1, |
| 5573 | 5577 | .column = dbg_stmt.column + 1, |
| 5574 | 5578 | }); |
| ... | ... | @@ -5737,7 +5741,7 @@ const DeclGen = struct { |
| 5737 | 5741 | const fn_info = mod.typeToFunc(zig_fn_ty).?; |
| 5738 | 5742 | const return_type = fn_info.return_type; |
| 5739 | 5743 | |
| 5740 | | const result_type_ref = try self.resolveFnReturnType(Type.fromInterned(return_type)); |
| 5744 | const result_type_id = try self.resolveFnReturnType(Type.fromInterned(return_type)); |
| 5741 | 5745 | const result_id = self.spv.allocId(); |
| 5742 | 5746 | const callee_id = try self.resolve(pl_op.operand); |
| 5743 | 5747 | |
| ... | ... | @@ -5758,7 +5762,7 @@ const DeclGen = struct { |
| 5758 | 5762 | } |
| 5759 | 5763 | |
| 5760 | 5764 | try self.func.body.emit(self.spv.gpa, .OpFunctionCall, .{ |
| 5761 | | .id_result_type = self.typeId(result_type_ref), |
| 5765 | .id_result_type = result_type_id, |
| 5762 | 5766 | .id_result = result_id, |
| 5763 | 5767 | .function = callee_id, |
| 5764 | 5768 | .id_ref_3 = params[0..n_params], |