authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-06 13:03:22-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-06 13:03:22-07:00
logf45ba7d0c1489a439725a11e5e36aa69fe75e7c3
treefe5fea980d6ac7ecc3b1b7a45f82bbea4a0a460a
parentf668c8bfd65489d1d38716e2973e0ee1cf0e8c52
parentac165458959d9db9c9fd0362aeb35f8b983cdf6a
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19562 from Snektron/spirv-remove-cache

spirv: remove cache

16 files changed, 1089 insertions(+), 2078 deletions(-)

src/codegen/spirv.zig+777-773
......@@ -22,8 +22,6 @@ const IdResultType = spec.IdResultType;
2222const StorageClass = spec.StorageClass;
2323
2424const SpvModule = @import("spirv/Module.zig");
25const CacheRef = SpvModule.CacheRef;
26const CacheString = SpvModule.CacheString;
2725
2826const SpvSection = @import("spirv/Section.zig");
2927const SpvAssembler = @import("spirv/Assembler.zig");
......@@ -32,14 +30,11 @@ const InstMap = std.AutoHashMapUnmanaged(Air.Inst.Index, IdRef);
3230
3331pub const zig_call_abi_ver = 3;
3432
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.
38const SpvTypeInfo = struct {
39 ty_ref: CacheRef,
40};
41
42const TypeMap = std.AutoHashMapUnmanaged(InternPool.Index, SpvTypeInfo);
33const InternMap = std.AutoHashMapUnmanaged(struct { InternPool.Index, DeclGen.Repr }, IdResult);
34const PtrTypeMap = std.AutoHashMapUnmanaged(
35 struct { InternPool.Index, StorageClass },
36 struct { ty_id: IdRef, fwd_emitted: bool },
37);
4338
4439const ControlFlow = union(enum) {
4540 const Structured = struct {
......@@ -162,14 +157,16 @@ pub const Object = struct {
162157 /// A map of Zig InternPool indices for anonymous decls to SPIR-V decl indices.
163158 anon_decl_link: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, SpvModule.Decl.Index) = .{},
164159
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 = .{},
173170
174171 pub fn init(gpa: Allocator) Object {
175172 return .{
......@@ -182,7 +179,8 @@ pub const Object = struct {
182179 self.spv.deinit();
183180 self.decl_link.deinit(self.gpa);
184181 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);
186184 }
187185
188186 fn genDecl(
......@@ -204,7 +202,8 @@ pub const Object = struct {
204202 .decl_index = decl_index,
205203 .air = air,
206204 .liveness = liveness,
207 .type_map = &self.type_map,
205 .intern_map = &self.intern_map,
206 .ptr_types = &self.ptr_types,
208207 .control_flow = switch (structured_cfg) {
209208 true => .{ .structured = .{} },
210209 false => .{ .unstructured = .{} },
......@@ -309,13 +308,12 @@ const DeclGen = struct {
309308 /// A map keeping track of which instruction generated which result-id.
310309 inst_results: InstMap = .{},
311310
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,
315314
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,
319317
320318 /// This field keeps track of the current state wrt structured or unstructured control flow.
321319 control_flow: ControlFlow,
......@@ -402,7 +400,6 @@ const DeclGen = struct {
402400 pub fn deinit(self: *DeclGen) void {
403401 self.args.deinit(self.gpa);
404402 self.inst_results.deinit(self.gpa);
405 self.wip_pointers.deinit(self.gpa);
406403 self.control_flow.deinit(self.gpa);
407404 self.func.deinit(self.gpa);
408405 }
......@@ -452,7 +449,7 @@ const DeclGen = struct {
452449
453450 const mod = self.module;
454451 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);
456453
457454 const spv_decl_index = blk: {
458455 const entry = try self.object.anon_decl_link.getOrPut(self.object.gpa, .{ val, .Function });
......@@ -460,7 +457,7 @@ const DeclGen = struct {
460457 try self.addFunctionDep(entry.value_ptr.*, .Function);
461458
462459 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);
464461 }
465462
466463 const spv_decl_index = try self.spv.allocDecl(.invocation_global);
......@@ -488,19 +485,14 @@ const DeclGen = struct {
488485 self.func = .{};
489486 defer self.func.deinit(self.gpa);
490487
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, &.{});
496489
497490 const initializer_id = self.spv.allocId();
498
499491 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),
501493 .id_result = initializer_id,
502494 .function_control = .{},
503 .function_type = self.typeId(initializer_proto_ty_ref),
495 .function_type = initializer_proto_ty_id,
504496 });
505497 const root_block_id = self.spv.allocId();
506498 try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{
......@@ -520,9 +512,9 @@ const DeclGen = struct {
520512
521513 try self.spv.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)});
522514
523 const fn_decl_ptr_ty_ref = try self.ptrType(ty, .Function);
515 const fn_decl_ptr_ty_id = try self.ptrType(ty, .Function);
524516 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,
526518 .id_result = result_id,
527519 .set = try self.spv.importInstructionSet(.zig),
528520 .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere...
......@@ -530,7 +522,7 @@ const DeclGen = struct {
530522 });
531523 }
532524
533 return try self.castToGeneric(self.typeId(decl_ptr_ty_ref), result_id);
525 return try self.castToGeneric(decl_ptr_ty_id, result_id);
534526 }
535527
536528 fn addFunctionDep(self: *DeclGen, decl_index: SpvModule.Decl.Index, storage_class: StorageClass) !void {
......@@ -696,14 +688,25 @@ const DeclGen = struct {
696688
697689 /// Emits a bool constant in a particular representation.
698690 fn constBool(self: *DeclGen, value: bool, repr: Repr) !IdRef {
691 // TODO: Cache?
692
693 const section = &self.spv.sections.types_globals_constants;
699694 switch (repr) {
700695 .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);
703697 },
704698 .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;
707710 },
708711 }
709712 }
......@@ -711,68 +714,63 @@ const DeclGen = struct {
711714 /// Emits an integer constant.
712715 /// This function, unlike SpvModule.constInt, takes care to bitcast
713716 /// 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 };
732731
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
745755 }
746 }
747756
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;
766759 }
767760
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;
776774 }
777775
778776 /// Construct a struct at runtime.
......@@ -788,8 +786,8 @@ const DeclGen = struct {
788786 // TODO: Make this OpCompositeConstruct when we can
789787 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });
790788 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))});
793791 try self.func.body.emit(self.spv.gpa, .OpStore, .{
794792 .pointer = ptr_id,
795793 .object = constitent_id,
......@@ -810,9 +808,9 @@ const DeclGen = struct {
810808 // TODO: Make this OpCompositeConstruct when we can
811809 const mod = self.module;
812810 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);
814812 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))});
816814 try self.func.body.emit(self.spv.gpa, .OpStore, .{
817815 .pointer = ptr_id,
818816 .object = constitent_id,
......@@ -834,9 +832,9 @@ const DeclGen = struct {
834832 // TODO: Make this OpCompositeConstruct when we can
835833 const mod = self.module;
836834 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);
838836 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))});
840838 try self.func.body.emit(self.spv.gpa, .OpStore, .{
841839 .pointer = ptr_id,
842840 .object = constitent_id,
......@@ -852,258 +850,279 @@ const DeclGen = struct {
852850 /// is done by emitting a sequence of instructions that initialize the value.
853851 //
854852 /// 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
856861 const mod = self.module;
857862 const target = self.getTarget();
858 const result_ty_ref = try self.resolveType(ty, repr);
863 const result_ty_id = try self.resolveType(ty, repr);
859864 const ip = &mod.intern_pool;
860865
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) });
864867 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,
903900 => unreachable, // non-runtime values
904901
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
907910
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);
993916 } 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);
996960 }
997 }
998
999 // Optional representation is a structure.
1000 // { Payload, Bool }
1001961
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 });
1007966
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 };
1041977 }
1042978
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);
1054985 },
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 }
10591010 }
10601011
1061 var types = std.ArrayList(Type).init(self.gpa);
1062 defer types.deinit();
1012 // Optional representation is a structure.
1013 // { Payload, Bool }
10631014
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 }
10661054
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", .{});
10731071 }
10741072
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();
10781075
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();
10821078
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,
10841099 },
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;
11001117 }
11011118
11021119 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);
11041123 const mod = self.module;
11051124
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);
11071126
11081127 switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) {
11091128 .decl => |decl| return try self.constantDeclRef(ptr_ty, decl),
......@@ -1114,7 +1133,7 @@ const DeclGen = struct {
11141133 // that is not implemented by Mesa yet. Therefore, just generate it
11151134 // as a runtime operation.
11161135 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,
11181137 .id_result = ptr_id,
11191138 .integer_value = try self.constant(Type.usize, Value.fromInterned(int), .direct),
11201139 });
......@@ -1126,23 +1145,23 @@ const DeclGen = struct {
11261145 .elem => |elem_ptr| {
11271146 const parent_ptr_ty = Type.fromInterned(mod.intern_pool.typeOf(elem_ptr.base));
11281147 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);
11311149
11321150 const elem_ptr_id = try self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id);
11331151
11341152 // TODO: Can we consolidate this in ptrElemPtr?
11351153 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)));
11371155
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) {
11391158 return elem_ptr_id;
11401159 }
11411160 // This may happen when we have pointer-to-array and the result is
11421161 // another pointer-to-array instead of a pointer-to-element.
11431162 const result_id = self.spv.allocId();
11441163 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,
11461165 .id_result = result_id,
11471166 .operand = elem_ptr_id,
11481167 });
......@@ -1166,7 +1185,7 @@ const DeclGen = struct {
11661185
11671186 const mod = self.module;
11681187 const ip = &mod.intern_pool;
1169 const ty_ref = try self.resolveType(ty, .direct);
1188 const ty_id = try self.resolveType(ty, .direct);
11701189 const decl_val = anon_decl.val;
11711190 const decl_ty = Type.fromInterned(ip.typeOf(decl_val));
11721191
......@@ -1181,7 +1200,7 @@ const DeclGen = struct {
11811200 // const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn;
11821201 if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) {
11831202 // Pointer to nothing - return undefoined
1184 return self.spv.constUndef(ty_ref);
1203 return self.spv.constUndef(ty_id);
11851204 }
11861205
11871206 if (decl_ty.zigTypeTag(mod) == .Fn) {
......@@ -1190,14 +1209,14 @@ const DeclGen = struct {
11901209
11911210 // Anon decl refs are always generic.
11921211 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);
11941213 const ptr_id = try self.resolveAnonDecl(decl_val);
11951214
1196 if (decl_ptr_ty_ref != ty_ref) {
1215 if (decl_ptr_ty_id != ty_id) {
11971216 // Differing pointer types, insert a cast.
11981217 const casted_ptr_id = self.spv.allocId();
11991218 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
1200 .id_result_type = self.typeId(ty_ref),
1219 .id_result_type = ty_id,
12011220 .id_result = casted_ptr_id,
12021221 .operand = ptr_id,
12031222 });
......@@ -1209,15 +1228,14 @@ const DeclGen = struct {
12091228
12101229 fn constantDeclRef(self: *DeclGen, ty: Type, decl_index: InternPool.DeclIndex) !IdRef {
12111230 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);
12141232 const decl = mod.declPtr(decl_index);
12151233
12161234 switch (mod.intern_pool.indexToKey(decl.val.ip_index)) {
12171235 .func => {
12181236 // TODO: Properly lower function pointers. For now we are going to hack around it and
12191237 // 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);
12211239 },
12221240 .extern_func => unreachable, // TODO
12231241 else => {},
......@@ -1225,7 +1243,7 @@ const DeclGen = struct {
12251243
12261244 if (!decl.typeOf(mod).isFnOrHasRuntimeBitsIgnoreComptime(mod)) {
12271245 // Pointer to nothing - return undefined.
1228 return self.spv.constUndef(ty_ref);
1246 return self.spv.constUndef(ty_id);
12291247 }
12301248
12311249 const spv_decl_index = try self.object.resolveDecl(mod, decl_index);
......@@ -1239,14 +1257,14 @@ const DeclGen = struct {
12391257 const final_storage_class = self.spvStorageClass(decl.@"addrspace");
12401258 try self.addFunctionDep(spv_decl_index, final_storage_class);
12411259
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);
12431261
12441262 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),
12461264 else => decl_id,
12471265 };
12481266
1249 if (decl_ptr_ty_ref != ty_ref) {
1267 if (decl_ptr_ty_id != ty_id) {
12501268 // Differing pointer types, insert a cast.
12511269 const casted_ptr_id = self.spv.allocId();
12521270 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
......@@ -1261,28 +1279,18 @@ const DeclGen = struct {
12611279 }
12621280
12631281 // 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 {
12651283 var name = std.ArrayList(u8).init(self.gpa);
12661284 defer name.deinit();
12671285 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();
12791287 }
12801288
12811289 /// Create an integer type suitable for storing at least 'bits' bits.
12821290 /// The integer type that is returned by this function is the type that is used to perform
12831291 /// actual operations (as well as store) a Zig type of a particular number of bits. To create
12841292 /// 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 {
12861294 const backing_bits = self.backingIntBits(bits) orelse {
12871295 // TODO: Integers too big for any native type are represented as "composite integers":
12881296 // An array of largestSupportedIntBits.
......@@ -1297,36 +1305,69 @@ const DeclGen = struct {
12971305 return self.spv.intType(.unsigned, backing_bits);
12981306 }
12991307
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;
13031319 }
13041320
1305 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !CacheRef {
1321 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !IdRef {
13061322 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);
13081324 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;
13121334 }
13131335
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 };
13191341
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,
13221346 .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??
13261355
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);
13281358
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;
13301371 }
13311372
13321373 /// Generate a union type. Union types are always generated with the
......@@ -1347,7 +1388,7 @@ const DeclGen = struct {
13471388 /// padding: [padding_size]u8,
13481389 /// }
13491390 /// 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 {
13511392 const mod = self.module;
13521393 const ip = &mod.intern_pool;
13531394 const union_obj = mod.typeToUnion(ty).?;
......@@ -1362,48 +1403,43 @@ const DeclGen = struct {
13621403 return try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect);
13631404 }
13641405
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;
13661408
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?
13711410
13721411 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)";
13761415 }
13771416
13781417 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)";
13821421 }
13831422
13841423 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)";
13881427 }
13891428
13901429 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)";
13941433 }
13951434
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;
14041440 }
14051441
1406 fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !CacheRef {
1442 fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !IdRef {
14071443 const mod = self.module;
14081444 if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
14091445 // If the return type is an error set or an error union, then we make this
......@@ -1420,26 +1456,46 @@ const DeclGen = struct {
14201456 }
14211457
14221458 /// 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 {
14241470 const mod = self.module;
14251471 const ip = &mod.intern_pool;
14261472 log.debug("resolveType: ty = {}", .{ty.fmt(mod)});
14271473 const target = self.getTarget();
1474
1475 const section = &self.spv.sections.types_globals_constants;
1476
14281477 switch (ty.zigTypeTag(mod)) {
14291478 .NoReturn => {
14301479 assert(repr == .direct);
1431 return try self.spv.resolve(.void_type);
1480 return try self.spv.voidType();
14321481 },
14331482 .Void => switch (repr) {
1434 .direct => return try self.spv.resolve(.void_type),
1483 .direct => {
1484 return try self.spv.voidType();
1485 },
14351486 // 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 },
14391495 },
14401496 .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),
14431499 },
14441500 .Int => {
14451501 const int_info = ty.intInfo(mod);
......@@ -1447,15 +1503,18 @@ const DeclGen = struct {
14471503 // Some times, the backend will be asked to generate a pointer to i0. OpTypeInt
14481504 // with 0 bits is invalid, so return an opaque type in this case.
14491505 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;
14531512 }
14541513 return try self.intType(int_info.signedness, int_info.bits);
14551514 },
14561515 .Enum => {
14571516 const tag_ty = ty.intTagType(mod);
1458 return self.resolveType(tag_ty, repr);
1517 return try self.resolveType(tag_ty, repr);
14591518 },
14601519 .Float => {
14611520 // 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 {
14731532 return self.fail("Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits});
14741533 }
14751534
1476 return try self.spv.resolve(.{ .float_type = .{ .bits = bits } });
1535 return try self.spv.floatType(bits);
14771536 },
14781537 .Array => {
1479 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1480
14811538 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);
14831540 const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel(mod)) orelse {
14841541 return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)});
14851542 };
1486 const ty_ref = if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) blk: {
1543
1544 if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) {
14871545 // The size of the array would be 0, but that is not allowed in SPIR-V.
14881546 // This path can be reached when the backend is asked to generate a pointer to
14891547 // an array of some zero-bit type. This should always be an indirect path.
14901548 assert(repr == .indirect);
14911549
14921550 // 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) {
14971558 // The size of the array would be 0, but that is not allowed in SPIR-V.
14981559 // This path can be reached for example when there is a slicing of a pointer
14991560 // that produces a zero-length array. In all cases where this type can be generated,
......@@ -1503,16 +1564,13 @@ const DeclGen = struct {
15031564 // In this case, we have an array of a non-zero sized type. In this case,
15041565 // generate an array of 1 element instead, so that ptr_elem_ptr instructions
15051566 // 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 }
15111571 },
15121572 .Fn => switch (repr) {
15131573 .direct => {
1514 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1515
15161574 const fn_info = mod.typeToFunc(ty).?;
15171575
15181576 comptime assert(zig_call_abi_ver == 3);
......@@ -1525,75 +1583,67 @@ const DeclGen = struct {
15251583 if (fn_info.is_var_args)
15261584 return self.fail("VarArgs functions are unsupported for SPIR-V", .{});
15271585
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);
15301589 var param_index: usize = 0;
15311590 for (fn_info.param_types.get(ip)) |param_ty_index| {
15321591 const param_ty = Type.fromInterned(param_ty_index);
15331592 if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue;
15341593
1535 param_ty_refs[param_index] = try self.resolveType(param_ty, .direct);
1594 param_ty_ids[param_index] = try self.resolveType(param_ty, .direct);
15361595 param_index += 1;
15371596 }
1538 const return_ty_ref = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type));
15391597
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 });
15441606
1545 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1546 return ty_ref;
1607 return result_id;
15471608 },
15481609 .indirect => {
15491610 // TODO: Represent function pointers properly.
15501611 // For now, just use an usize type.
1551 return try self.sizeType();
1612 return try self.resolveType(Type.usize, .indirect);
15521613 },
15531614 },
15541615 .Pointer => {
15551616 const ptr_info = ty.ptrInfo(mod);
15561617
1557 // Note: Don't cache this pointer type, it would mess up the recursive pointer functionality
1558 // in ptrType()!
1559
15601618 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);
15621620
15631621 if (ptr_info.flags.size != .Slice) {
1564 return ptr_ty_ref;
1622 return ptr_ty_id;
15651623 }
15661624
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 );
15751630 },
15761631 .Vector => {
1577 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1578
15791632 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);
15811635 const len = ty.vectorLen(mod);
15821636
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 }
15901642 },
15911643 .Struct => {
1592 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1593
15941644 const struct_type = switch (ip.indexToKey(ty.toIntern())) {
15951645 .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);
15971647 defer self.gpa.free(member_types);
15981648
15991649 var member_index: usize = 0;
......@@ -1604,13 +1654,11 @@ const DeclGen = struct {
16041654 member_index += 1;
16051655 }
16061656
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;
16141662 },
16151663 .struct_type => ip.loadStructType(ty.toIntern()),
16161664 else => unreachable,
......@@ -1620,10 +1668,10 @@ const DeclGen = struct {
16201668 return try self.resolveType(Type.fromInterned(struct_type.backingIntType(ip).*), .direct);
16211669 }
16221670
1623 var member_types = std.ArrayList(CacheRef).init(self.gpa);
1671 var member_types = std.ArrayList(IdRef).init(self.gpa);
16241672 defer member_types.deinit();
16251673
1626 var member_names = std.ArrayList(CacheString).init(self.gpa);
1674 var member_names = std.ArrayList([]const u8).init(self.gpa);
16271675 defer member_names.deinit();
16281676
16291677 var it = struct_type.iterateRuntimeOrder(ip);
......@@ -1637,17 +1685,14 @@ const DeclGen = struct {
16371685 const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse
16381686 try ip.getOrPutStringFmt(mod.gpa, "{d}", .{field_index});
16391687 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));
16411689 }
16421690
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;
16511696 },
16521697 .Optional => {
16531698 const payload_ty = ty.optionalChild(mod);
......@@ -1658,77 +1703,58 @@ const DeclGen = struct {
16581703 return try self.resolveType(Type.bool, .indirect);
16591704 }
16601705
1661 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
1706 const payload_ty_id = try self.resolveType(payload_ty, .indirect);
16621707 if (ty.optionalReprIsPayload(mod)) {
16631708 // Optional is actually a pointer or a slice.
1664 return payload_ty_ref;
1709 return payload_ty_id;
16651710 }
16661711
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);
16701713
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 );
16811718 },
16821719 .Union => return try self.resolveUnionType(ty),
1683 .ErrorSet => return try self.intType(.unsigned, 16),
1720 .ErrorSet => return try self.resolveType(Type.u16, repr),
16841721 .ErrorUnion => {
16851722 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);
16871724
16881725 const eu_layout = self.errorUnionLayout(payload_ty);
16891726 if (!eu_layout.payload_has_bits) {
1690 return error_ty_ref;
1727 return error_ty_id;
16911728 }
16921729
1693 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1730 const payload_ty_id = try self.resolveType(payload_ty, .indirect);
16941731
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;
16991734 if (eu_layout.error_first) {
17001735 // 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" };
17061738 // TODO: ABI padding?
17071739 } else {
17081740 // 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" };
17141743 // TODO: ABI padding?
17151744 }
17161745
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);
17251747 },
17261748 .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,
17311756 });
1757 return result_id;
17321758 },
17331759
17341760 .Null,
......@@ -1736,9 +1762,10 @@ const DeclGen = struct {
17361762 .EnumLiteral,
17371763 .ComptimeFloat,
17381764 .ComptimeInt,
1765 .Type,
17391766 => unreachable, // Must be comptime.
17401767
1741 else => |tag| return self.todo("Implement zig type '{}'", .{tag}),
1768 .Frame, .AnyFrame => unreachable, // TODO
17421769 }
17431770 }
17441771
......@@ -1887,7 +1914,6 @@ const DeclGen = struct {
18871914 result_ty: Type,
18881915 ty: Type,
18891916 /// Always in direct representation.
1890 ty_ref: CacheRef,
18911917 ty_id: IdRef,
18921918 /// True if the input is an array type.
18931919 is_array: bool,
......@@ -1947,14 +1973,13 @@ const DeclGen = struct {
19471973 @memset(results, undefined);
19481974
19491975 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);
19511977
19521978 return .{
19531979 .dg = self,
19541980 .result_ty = result_ty,
19551981 .ty = ty,
1956 .ty_ref = ty_ref,
1957 .ty_id = self.typeId(ty_ref),
1982 .ty_id = ty_id,
19581983 .is_array = is_array,
19591984 .results = results,
19601985 };
......@@ -1981,16 +2006,13 @@ const DeclGen = struct {
19812006 /// TODO is to also write out the error as a function call parameter, and to somehow fetch
19822007 /// the name of an error in the text executor.
19832008 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 },
19932013 });
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});
19942016
19952017 const test_id = self.spv.declPtr(spv_test_decl_index).result_id;
19962018
......@@ -2002,20 +2024,20 @@ const DeclGen = struct {
20022024
20032025 const section = &self.spv.sections.functions;
20042026 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),
20062028 .id_result = kernel_id,
20072029 .function_control = .{},
2008 .function_type = self.typeId(kernel_proto_ty_ref),
2030 .function_type = kernel_proto_ty_id,
20092031 });
20102032 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,
20122034 .id_result = p_error_id,
20132035 });
20142036 try section.emit(self.spv.gpa, .OpLabel, .{
20152037 .id_result = self.spv.allocId(),
20162038 });
20172039 try section.emit(self.spv.gpa, .OpFunctionCall, .{
2018 .id_result_type = self.typeId(anyerror_ty_ref),
2040 .id_result_type = anyerror_ty_id,
20192041 .id_result = error_id,
20202042 .function = test_id,
20212043 });
......@@ -2047,17 +2069,17 @@ const DeclGen = struct {
20472069 .func => {
20482070 assert(decl.typeOf(mod).zigTypeTag(mod) == .Fn);
20492071 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));
20512073
2052 const prototype_ty_ref = try self.resolveType(decl.typeOf(mod), .direct);
2074 const prototype_ty_id = try self.resolveType(decl.typeOf(mod), .direct);
20532075 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,
20552077 .id_result = result_id,
20562078 .function_control = switch (fn_info.cc) {
20572079 .Inline => .{ .Inline = true },
20582080 else => .{},
20592081 },
2060 .function_type = self.typeId(prototype_ty_ref),
2082 .function_type = prototype_ty_id,
20612083 });
20622084
20632085 comptime assert(zig_call_abi_ver == 3);
......@@ -2066,7 +2088,7 @@ const DeclGen = struct {
20662088 const param_ty = Type.fromInterned(param_ty_index);
20672089 if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue;
20682090
2069 const param_type_id = try self.resolveTypeId(param_ty);
2091 const param_type_id = try self.resolveType(param_ty, .direct);
20702092 const arg_result_id = self.spv.allocId();
20712093 try self.func.prologue.emit(self.spv.gpa, .OpFunctionParameter, .{
20722094 .id_result_type = param_type_id,
......@@ -2122,10 +2144,10 @@ const DeclGen = struct {
21222144 const final_storage_class = self.spvStorageClass(decl.@"addrspace");
21232145 assert(final_storage_class != .Generic); // These should be instance globals
21242146
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);
21262148
21272149 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,
21292151 .id_result = result_id,
21302152 .storage_class = final_storage_class,
21312153 });
......@@ -2145,22 +2167,18 @@ const DeclGen = struct {
21452167
21462168 try self.spv.declareDeclDeps(spv_decl_index, &.{});
21472169
2148 const ptr_ty_ref = try self.ptrType(decl.typeOf(mod), .Function);
2170 const ptr_ty_id = try self.ptrType(decl.typeOf(mod), .Function);
21492171
21502172 if (maybe_init_val) |init_val| {
21512173 // 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, &.{});
21572175
21582176 const initializer_id = self.spv.allocId();
21592177 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),
21612179 .id_result = initializer_id,
21622180 .function_control = .{},
2163 .function_type = self.typeId(initializer_proto_ty_ref),
2181 .function_type = initializer_proto_ty_id,
21642182 });
21652183
21662184 const root_block_id = self.spv.allocId();
......@@ -2183,7 +2201,7 @@ const DeclGen = struct {
21832201 try self.spv.debugNameFmt(initializer_id, "initializer of {s}", .{fqn});
21842202
21852203 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,
21872205 .id_result = result_id,
21882206 .set = try self.spv.importInstructionSet(.zig),
21892207 .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere...
......@@ -2191,7 +2209,7 @@ const DeclGen = struct {
21912209 });
21922210 } else {
21932211 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,
21952213 .id_result = result_id,
21962214 .set = try self.spv.importInstructionSet(.zig),
21972215 .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere...
......@@ -2202,12 +2220,12 @@ const DeclGen = struct {
22022220 }
22032221 }
22042222
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);
22082226 const result_id = self.spv.allocId();
22092227 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),
22112229 .id_result = result_id,
22122230 .condition = condition_id,
22132231 .object_1 = one_id,
......@@ -2222,15 +2240,12 @@ const DeclGen = struct {
22222240 const mod = self.module;
22232241 return switch (ty.zigTypeTag(mod)) {
22242242 .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);
22282243 const result_id = self.spv.allocId();
22292244 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),
22312246 .id_result = result_id,
22322247 .operand_1 = operand_id,
2233 .operand_2 = zero_id,
2248 .operand_2 = try self.constBool(false, .indirect),
22342249 });
22352250 break :blk result_id;
22362251 },
......@@ -2243,20 +2258,17 @@ const DeclGen = struct {
22432258 fn convertToIndirect(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef {
22442259 const mod = self.module;
22452260 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),
22502262 else => operand_id,
22512263 };
22522264 }
22532265
22542266 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);
22562268 const result_id = self.spv.allocId();
22572269 const indexes = [_]u32{field};
22582270 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,
22602272 .id_result = result_id,
22612273 .composite = object,
22622274 .indexes = &indexes,
......@@ -2270,13 +2282,13 @@ const DeclGen = struct {
22702282 };
22712283
22722284 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);
22742286 const result_id = self.spv.allocId();
22752287 const access = spec.MemoryAccess.Extended{
22762288 .Volatile = options.is_volatile,
22772289 };
22782290 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,
22802292 .id_result = result_id,
22812293 .pointer = ptr_id,
22822294 .memory_access = access,
......@@ -2488,7 +2500,8 @@ const DeclGen = struct {
24882500
24892501 const result_ty = self.typeOfIndex(inst);
24902502 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);
24922505
24932506 const info = self.arithmeticTypeInfo(result_ty);
24942507 switch (info.class) {
......@@ -2505,7 +2518,7 @@ const DeclGen = struct {
25052518
25062519 // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that,
25072520 // 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: {
25092522 const shift_id = self.spv.allocId();
25102523 try self.func.body.emit(self.spv.gpa, .OpUConvert, .{
25112524 .id_result_type = wip.ty_id,
......@@ -2529,7 +2542,7 @@ const DeclGen = struct {
25292542 try self.func.body.emit(self.spv.gpa, unsigned, args);
25302543 }
25312544
2532 result_id.* = try self.normalize(wip.ty_ref, value_id, info);
2545 result_id.* = try self.normalize(wip.ty, value_id, info);
25332546 }
25342547 return try wip.finalize();
25352548 }
......@@ -2622,7 +2635,7 @@ const DeclGen = struct {
26222635 /// - Signed integers are also sign extended if they are negative.
26232636 /// All other values are returned unmodified (this makes strange integer
26242637 /// 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 {
26262639 switch (info.class) {
26272640 .integer, .bool, .float => return value_id,
26282641 .composite_integer => unreachable, // TODO
......@@ -2630,9 +2643,9 @@ const DeclGen = struct {
26302643 .unsigned => {
26312644 const mask_value = if (info.bits == 64) 0xFFFF_FFFF_FFFF_FFFF else (@as(u64, 1) << @as(u6, @intCast(info.bits))) - 1;
26322645 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);
26342647 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),
26362649 .id_result = result_id,
26372650 .operand_1 = value_id,
26382651 .operand_2 = mask_id,
......@@ -2641,17 +2654,17 @@ const DeclGen = struct {
26412654 },
26422655 .signed => {
26432656 // 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);
26452658 const left_id = self.spv.allocId();
26462659 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),
26482661 .id_result = left_id,
26492662 .base = value_id,
26502663 .shift = shift_amt_id,
26512664 });
26522665 const right_id = self.spv.allocId();
26532666 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),
26552668 .id_result = right_id,
26562669 .base = left_id,
26572670 .shift = shift_amt_id,
......@@ -2667,13 +2680,13 @@ const DeclGen = struct {
26672680 const lhs_id = try self.resolve(bin_op.lhs);
26682681 const rhs_id = try self.resolve(bin_op.rhs);
26692682 const ty = self.typeOfIndex(inst);
2670 const ty_ref = try self.resolveType(ty, .direct);
2683 const ty_id = try self.resolveType(ty, .direct);
26712684 const info = self.arithmeticTypeInfo(ty);
26722685 switch (info.class) {
26732686 .composite_integer => unreachable, // TODO
26742687 .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);
26772690
26782691 // (a ^ b) > 0
26792692 const bin_bitwise_id = try self.binOpSimple(ty, lhs_id, rhs_id, .OpBitwiseXor);
......@@ -2696,14 +2709,14 @@ const DeclGen = struct {
26962709 const negative_div_id = try self.arithOp(ty, negative_div_lhs, rhs_abs, .OpFDiv, .OpSDiv, .OpUDiv);
26972710 const negated_negative_div_id = self.spv.allocId();
26982711 try self.func.body.emit(self.spv.gpa, .OpSNegate, .{
2699 .id_result_type = self.typeId(ty_ref),
2712 .id_result_type = ty_id,
27002713 .id_result = negated_negative_div_id,
27012714 .operand = negative_div_id,
27022715 });
27032716
27042717 const result_id = self.spv.allocId();
27052718 try self.func.body.emit(self.spv.gpa, .OpSelect, .{
2706 .id_result_type = self.typeId(ty_ref),
2719 .id_result_type = ty_id,
27072720 .id_result = result_id,
27082721 .condition = is_positive_id,
27092722 .object_1 = positive_div_id,
......@@ -2728,7 +2741,7 @@ const DeclGen = struct {
27282741
27292742 fn floor(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef {
27302743 const target = self.getTarget();
2731 const ty_ref = try self.resolveType(ty, .direct);
2744 const ty_id = try self.resolveType(ty, .direct);
27322745 const ext_inst: Word = switch (target.os.tag) {
27332746 .opencl => 25,
27342747 .vulkan => 8,
......@@ -2742,7 +2755,7 @@ const DeclGen = struct {
27422755
27432756 const result_id = self.spv.allocId();
27442757 try self.func.body.emit(self.spv.gpa, .OpExtInst, .{
2745 .id_result_type = self.typeId(ty_ref),
2758 .id_result_type = ty_id,
27462759 .id_result = result_id,
27472760 .set = set_id,
27482761 .instruction = .{ .inst = ext_inst },
......@@ -2819,7 +2832,7 @@ const DeclGen = struct {
28192832
28202833 // TODO: Trap on overflow? Probably going to be annoying.
28212834 // 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);
28232836 }
28242837
28252838 return try wip.finalize();
......@@ -2897,11 +2910,12 @@ const DeclGen = struct {
28972910 const operand_ty = self.typeOf(extra.lhs);
28982911 const ov_ty = result_ty.structFieldType(1, self.module);
28992912
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))
29032917 else
2904 bool_ty_ref;
2918 bool_ty_id;
29052919
29062920 const info = self.arithmeticTypeInfo(operand_ty);
29072921 switch (info.class) {
......@@ -2929,7 +2943,7 @@ const DeclGen = struct {
29292943 });
29302944
29312945 // 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);
29332947
29342948 const overflowed_id = switch (info.signedness) {
29352949 .unsigned => blk: {
......@@ -2937,7 +2951,7 @@ const DeclGen = struct {
29372951 // For subtraction the conditions need to be swapped.
29382952 const overflowed_id = self.spv.allocId();
29392953 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,
29412955 .id_result = overflowed_id,
29422956 .operand_1 = result_id.*,
29432957 .operand_2 = lhs_elem_id,
......@@ -2963,9 +2977,9 @@ const DeclGen = struct {
29632977 // = (rhs < 0) == (lhs > value)
29642978
29652979 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);
29672981 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,
29692983 .id_result = rhs_lt_zero_id,
29702984 .operand_1 = rhs_elem_id,
29712985 .operand_2 = zero_id,
......@@ -2973,7 +2987,7 @@ const DeclGen = struct {
29732987
29742988 const value_gt_lhs_id = self.spv.allocId();
29752989 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,
29772991 .id_result = value_gt_lhs_id,
29782992 .operand_1 = lhs_elem_id,
29792993 .operand_2 = result_id.*,
......@@ -2981,7 +2995,7 @@ const DeclGen = struct {
29812995
29822996 const overflowed_id = self.spv.allocId();
29832997 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,
29852999 .id_result = overflowed_id,
29863000 .operand_1 = rhs_lt_zero_id,
29873001 .operand_2 = value_gt_lhs_id,
......@@ -2990,7 +3004,7 @@ const DeclGen = struct {
29903004 },
29913005 };
29923006
2993 ov_id.* = try self.intFromBool(wip_ov.ty_ref, overflowed_id);
3007 ov_id.* = try self.intFromBool(wip_ov.ty, overflowed_id);
29943008 }
29953009
29963010 return try self.constructStruct(
......@@ -3022,9 +3036,9 @@ const DeclGen = struct {
30223036 var wip_ov = try self.elementWise(ov_ty, true);
30233037 defer wip_ov.deinit();
30243038
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);
30283042
30293043 for (wip_result.results, wip_ov.results, 0..) |*result_id, *ov_id, i| {
30303044 const lhs_elem_id = try wip_result.elementAt(operand_ty, lhs, i);
......@@ -3065,15 +3079,17 @@ const DeclGen = struct {
30653079 const result_ty = self.typeOfIndex(inst);
30663080 const operand_ty = self.typeOf(extra.lhs);
30673081 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);
30693084
30703085 const ov_ty = result_ty.structFieldType(1, self.module);
30713086
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))
30753091 else
3076 bool_ty_ref;
3092 bool_ty_id;
30773093
30783094 const info = self.arithmeticTypeInfo(operand_ty);
30793095 switch (info.class) {
......@@ -3092,7 +3108,7 @@ const DeclGen = struct {
30923108
30933109 // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that,
30943110 // 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: {
30963112 const shift_id = self.spv.allocId();
30973113 try self.func.body.emit(self.spv.gpa, .OpUConvert, .{
30983114 .id_result_type = wip_result.ty_id,
......@@ -3109,7 +3125,7 @@ const DeclGen = struct {
31093125 .base = lhs_elem_id,
31103126 .shift = shift_id,
31113127 });
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);
31133129
31143130 const right_shift_id = self.spv.allocId();
31153131 switch (info.signedness) {
......@@ -3133,13 +3149,13 @@ const DeclGen = struct {
31333149
31343150 const overflowed_id = self.spv.allocId();
31353151 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,
31373153 .id_result = overflowed_id,
31383154 .operand_1 = lhs_elem_id,
31393155 .operand_2 = right_shift_id,
31403156 });
31413157
3142 ov_id.* = try self.intFromBool(wip_ov.ty_ref, overflowed_id);
3158 ov_id.* = try self.intFromBool(wip_ov.ty, overflowed_id);
31433159 }
31443160
31453161 return try self.constructStruct(
......@@ -3204,8 +3220,7 @@ const DeclGen = struct {
32043220 defer wip.deinit();
32053221
32063222 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);
32093224
32103225 for (wip.results, 0..) |*result_id, i| {
32113226 const elem = try wip.elementAt(operand_ty, operand, i);
......@@ -3230,6 +3245,8 @@ const DeclGen = struct {
32303245 .id_ref_4 = &.{elem},
32313246 });
32323247
3248 // TODO: Comparison should be removed..
3249 // Its valid because SpvModule caches numeric types
32333250 if (wip.ty_id == elem_ty_id) {
32343251 result_id.* = tmp;
32353252 continue;
......@@ -3276,8 +3293,7 @@ const DeclGen = struct {
32763293 const operand = try self.resolve(reduce.operand);
32773294 const operand_ty = self.typeOf(reduce.operand);
32783295 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);
32813297
32823298 const info = self.arithmeticTypeInfo(operand_ty);
32833299
......@@ -3351,7 +3367,7 @@ const DeclGen = struct {
33513367 for (wip.results, 0..) |*result_id, i| {
33523368 const elem = try mask.elemValue(mod, i);
33533369 if (elem.isUndef(mod)) {
3354 result_id.* = try self.spv.constUndef(wip.ty_ref);
3370 result_id.* = try self.spv.constUndef(wip.ty_id);
33553371 continue;
33563372 }
33573373
......@@ -3366,11 +3382,10 @@ const DeclGen = struct {
33663382 }
33673383
33683384 fn indicesToIds(self: *DeclGen, indices: []const u32) ![]IdRef {
3369 const index_ty_ref = try self.intType(.unsigned, 32);
33703385 const ids = try self.gpa.alloc(IdRef, indices.len);
33713386 errdefer self.gpa.free(ids);
33723387 for (indices, ids) |index, *id| {
3373 id.* = try self.constInt(index_ty_ref, index);
3388 id.* = try self.constInt(Type.u32, index, .direct);
33743389 }
33753390
33763391 return ids;
......@@ -3378,13 +3393,13 @@ const DeclGen = struct {
33783393
33793394 fn accessChainId(
33803395 self: *DeclGen,
3381 result_ty_ref: CacheRef,
3396 result_ty_id: IdRef,
33823397 base: IdRef,
33833398 indices: []const IdRef,
33843399 ) !IdRef {
33853400 const result_id = self.spv.allocId();
33863401 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,
33883403 .id_result = result_id,
33893404 .base = base,
33903405 .indexes = indices,
......@@ -3398,18 +3413,18 @@ const DeclGen = struct {
33983413 /// is the latter and PtrAccessChain is the former.
33993414 fn accessChain(
34003415 self: *DeclGen,
3401 result_ty_ref: CacheRef,
3416 result_ty_id: IdRef,
34023417 base: IdRef,
34033418 indices: []const u32,
34043419 ) !IdRef {
34053420 const ids = try self.indicesToIds(indices);
34063421 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);
34083423 }
34093424
34103425 fn ptrAccessChain(
34113426 self: *DeclGen,
3412 result_ty_ref: CacheRef,
3427 result_ty_id: IdRef,
34133428 base: IdRef,
34143429 element: IdRef,
34153430 indices: []const u32,
......@@ -3419,7 +3434,7 @@ const DeclGen = struct {
34193434
34203435 const result_id = self.spv.allocId();
34213436 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,
34233438 .id_result = result_id,
34243439 .base = base,
34253440 .element = element,
......@@ -3430,21 +3445,21 @@ const DeclGen = struct {
34303445
34313446 fn ptrAdd(self: *DeclGen, result_ty: Type, ptr_ty: Type, ptr_id: IdRef, offset_id: IdRef) !IdRef {
34323447 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);
34343449
34353450 switch (ptr_ty.ptrSize(mod)) {
34363451 .One => {
34373452 // Pointer to array
34383453 // 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});
34403455 },
34413456 .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, &.{});
34433458 },
34443459 .Slice => {
34453460 // TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does.
34463461 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, &.{});
34483463 },
34493464 }
34503465 }
......@@ -3467,12 +3482,12 @@ const DeclGen = struct {
34673482 const ptr_ty = self.typeOf(bin_op.lhs);
34683483 const offset_id = try self.resolve(bin_op.rhs);
34693484 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);
34713486 const result_ty = self.typeOfIndex(inst);
34723487
34733488 const negative_offset_id = self.spv.allocId();
34743489 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,
34763491 .id_result = negative_offset_id,
34773492 .operand = offset_id,
34783493 });
......@@ -3490,7 +3505,7 @@ const DeclGen = struct {
34903505 const mod = self.module;
34913506 var cmp_lhs_id = lhs_id;
34923507 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);
34943509 const op_ty = switch (ty.zigTypeTag(mod)) {
34953510 .Int, .Bool, .Float => ty,
34963511 .Enum => ty.intTagType(mod),
......@@ -3502,7 +3517,7 @@ const DeclGen = struct {
35023517 cmp_lhs_id = self.spv.allocId();
35033518 cmp_rhs_id = self.spv.allocId();
35043519
3505 const usize_ty_id = self.typeId(try self.sizeType());
3520 const usize_ty_id = try self.resolveType(Type.usize, .direct);
35063521
35073522 try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{
35083523 .id_result_type = usize_ty_id,
......@@ -3564,20 +3579,20 @@ const DeclGen = struct {
35643579 const pl_eq_id = try self.cmp(op, Type.bool, payload_ty, lhs_pl_id, rhs_pl_id);
35653580 const lhs_not_valid_id = self.spv.allocId();
35663581 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,
35683583 .id_result = lhs_not_valid_id,
35693584 .operand = lhs_valid_id,
35703585 });
35713586 const impl_id = self.spv.allocId();
35723587 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,
35743589 .id_result = impl_id,
35753590 .operand_1 = lhs_not_valid_id,
35763591 .operand_2 = pl_eq_id,
35773592 });
35783593 const result_id = self.spv.allocId();
35793594 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,
35813596 .id_result = result_id,
35823597 .operand_1 = valid_eq_id,
35833598 .operand_2 = impl_id,
......@@ -3590,14 +3605,14 @@ const DeclGen = struct {
35903605
35913606 const impl_id = self.spv.allocId();
35923607 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,
35943609 .id_result = impl_id,
35953610 .operand_1 = lhs_valid_id,
35963611 .operand_2 = pl_neq_id,
35973612 });
35983613 const result_id = self.spv.allocId();
35993614 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,
36013616 .id_result = result_id,
36023617 .operand_1 = valid_neq_id,
36033618 .operand_2 = impl_id,
......@@ -3665,7 +3680,7 @@ const DeclGen = struct {
36653680
36663681 const result_id = self.spv.allocId();
36673682 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);
36693684 self.func.body.writeOperand(spec.IdResult, result_id);
36703685 self.func.body.writeOperand(spec.IdResultType, cmp_lhs_id);
36713686 self.func.body.writeOperand(spec.IdResultType, cmp_rhs_id);
......@@ -3698,6 +3713,7 @@ const DeclGen = struct {
36983713 return try self.cmp(op, result_ty, ty, lhs_id, rhs_id);
36993714 }
37003715
3716 /// Bitcast one type to another. Note: both types, input, output are expected in **direct** representation.
37013717 fn bitCast(
37023718 self: *DeclGen,
37033719 dst_ty: Type,
......@@ -3705,13 +3721,11 @@ const DeclGen = struct {
37053721 src_id: IdRef,
37063722 ) !IdRef {
37073723 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);
37123726
37133727 const result_id = blk: {
3714 if (src_ty_ref == dst_ty_ref) {
3728 if (src_ty_id == dst_ty_id) {
37153729 break :blk src_id;
37163730 }
37173731
......@@ -3721,7 +3735,7 @@ const DeclGen = struct {
37213735 if (src_ty.zigTypeTag(mod) == .Int and dst_ty.isPtrAtRuntime(mod)) {
37223736 const result_id = self.spv.allocId();
37233737 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,
37253739 .id_result = result_id,
37263740 .integer_value = src_id,
37273741 });
......@@ -3731,10 +3745,11 @@ const DeclGen = struct {
37313745 // We can only use OpBitcast for specific conversions: between numerical types, and
37323746 // between pointers. If the resolved spir-v types fall into this category then emit OpBitcast,
37333747 // 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) {
37353750 const result_id = self.spv.allocId();
37363751 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,
37383753 .id_result = result_id,
37393754 .operand = src_id,
37403755 });
......@@ -3742,13 +3757,13 @@ const DeclGen = struct {
37423757 break :blk result_id;
37433758 }
37443759
3745 const dst_ptr_ty_ref = try self.ptrType(dst_ty, .Function);
3760 const dst_ptr_ty_id = try self.ptrType(dst_ty, .Function);
37463761
37473762 const tmp_id = try self.alloc(src_ty, .{ .storage_class = .Function });
37483763 try self.store(src_ty, tmp_id, src_id, .{});
37493764 const casted_ptr_id = self.spv.allocId();
37503765 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,
37523767 .id_result = casted_ptr_id,
37533768 .operand = tmp_id,
37543769 });
......@@ -3761,7 +3776,7 @@ const DeclGen = struct {
37613776 // should we change the representation of strange integers?
37623777 if (dst_ty.zigTypeTag(mod) == .Int) {
37633778 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);
37653780 }
37663781
37673782 return result_id;
......@@ -3811,7 +3826,7 @@ const DeclGen = struct {
38113826 // type, we don't need to normalize when growing the type. The
38123827 // representation is already the same.
38133828 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);
38153830 } else {
38163831 result_id.* = value_id;
38173832 }
......@@ -3820,7 +3835,7 @@ const DeclGen = struct {
38203835 }
38213836
38223837 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);
38243839 const result_id = self.spv.allocId();
38253840 try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{
38263841 .id_result_type = result_type_id,
......@@ -3841,21 +3856,21 @@ const DeclGen = struct {
38413856 const operand_ty = self.typeOf(ty_op.operand);
38423857 const operand_id = try self.resolve(ty_op.operand);
38433858 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);
38463860 }
38473861
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 {
38493863 const operand_info = self.arithmeticTypeInfo(operand_ty);
38503864 const result_id = self.spv.allocId();
3865 const result_ty_id = try self.resolveType(result_ty, .direct);
38513866 switch (operand_info.signedness) {
38523867 .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,
38543869 .id_result = result_id,
38553870 .signed_value = operand_id,
38563871 }),
38573872 .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,
38593874 .id_result = result_id,
38603875 .unsigned_value = operand_id,
38613876 }),
......@@ -3872,16 +3887,16 @@ const DeclGen = struct {
38723887
38733888 fn intFromFloat(self: *DeclGen, result_ty: Type, operand_id: IdRef) !IdRef {
38743889 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);
38763891 const result_id = self.spv.allocId();
38773892 switch (result_info.signedness) {
38783893 .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,
38803895 .id_result = result_id,
38813896 .float_value = operand_id,
38823897 }),
38833898 .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,
38853900 .id_result = result_id,
38863901 .float_value = operand_id,
38873902 }),
......@@ -3898,7 +3913,7 @@ const DeclGen = struct {
38983913 defer wip.deinit();
38993914 for (wip.results, 0..) |*result_id, i| {
39003915 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);
39023917 }
39033918 return try wip.finalize();
39043919 }
......@@ -3907,7 +3922,7 @@ const DeclGen = struct {
39073922 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
39083923 const operand_id = try self.resolve(ty_op.operand);
39093924 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);
39113926
39123927 const result_id = self.spv.allocId();
39133928 try self.func.body.emit(self.spv.gpa, .OpFConvert, .{
......@@ -3957,18 +3972,17 @@ const DeclGen = struct {
39573972 const slice_ty = self.typeOfIndex(inst);
39583973 const elem_ptr_ty = slice_ty.slicePtrFieldType(mod);
39593974
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);
39623976
39633977 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);
39653979
39663980 const elem_ptr_id = if (!array_ty.hasRuntimeBitsIgnoreComptime(mod))
39673981 // Note: The pointer is something like *opaque{}, so we need to bitcast it to the element type.
39683982 try self.bitCast(elem_ptr_ty, array_ptr_ty, array_ptr_id)
39693983 else
39703984 // 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});
39723986
39733987 return try self.constructStruct(
39743988 slice_ty,
......@@ -4092,8 +4106,8 @@ const DeclGen = struct {
40924106 const array_ty = ty.childType(mod);
40934107 const elem_ty = array_ty.childType(mod);
40944108 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);
40974111 },
40984112 .Many, .C => unreachable,
40994113 }
......@@ -4142,10 +4156,10 @@ const DeclGen = struct {
41424156 const index_id = try self.resolve(bin_op.rhs);
41434157
41444158 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);
41464160
41474161 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, &.{});
41494163 }
41504164
41514165 fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -4158,10 +4172,10 @@ const DeclGen = struct {
41584172 const index_id = try self.resolve(bin_op.rhs);
41594173
41604174 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);
41624176
41634177 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, &.{});
41654179 return try self.load(slice_ty.childType(mod), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(mod) });
41664180 }
41674181
......@@ -4169,14 +4183,14 @@ const DeclGen = struct {
41694183 const mod = self.module;
41704184 // Construct new pointer type for the resulting pointer
41714185 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)));
41734187 if (ptr_ty.isSinglePointer(mod)) {
41744188 // Pointer-to-array. In this case, the resulting pointer is not of the same type
41754189 // 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});
41774191 } else {
41784192 // 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, &.{});
41804194 }
41814195 }
41824196
......@@ -4209,11 +4223,11 @@ const DeclGen = struct {
42094223 // For now, just generate a temporary and use that.
42104224 // TODO: This backend probably also should use isByRef from llvm...
42114225
4212 const elem_ptr_ty_ref = try self.ptrType(elem_ty, .Function);
4226 const elem_ptr_ty_id = try self.ptrType(elem_ty, .Function);
42134227
42144228 const tmp_id = try self.alloc(array_ty, .{ .storage_class = .Function });
42154229 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});
42174231 return try self.load(elem_ty, elem_ptr_id, .{});
42184232 }
42194233
......@@ -4238,13 +4252,13 @@ const DeclGen = struct {
42384252 const scalar_ty = vector_ty.scalarType(mod);
42394253
42404254 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);
42424256
42434257 const vector_ptr = try self.resolve(data.vector_ptr);
42444258 const index = try self.resolve(extra.lhs);
42454259 const operand = try self.resolve(extra.rhs);
42464260
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});
42484262 try self.store(scalar_ty, elem_ptr_id, operand, .{
42494263 .is_volatile = vector_ptr_ty.isVolatilePtr(mod),
42504264 });
......@@ -4260,7 +4274,7 @@ const DeclGen = struct {
42604274 if (layout.tag_size == 0) return;
42614275
42624276 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)));
42644278
42654279 const union_ptr_id = try self.resolve(bin_op.lhs);
42664280 const new_tag_id = try self.resolve(bin_op.rhs);
......@@ -4268,7 +4282,7 @@ const DeclGen = struct {
42684282 if (!layout.has_payload) {
42694283 try self.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });
42704284 } 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});
42724286 try self.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });
42734287 }
42744288 }
......@@ -4298,6 +4312,8 @@ const DeclGen = struct {
42984312 // union type, then get the field pointer and pointer-cast it to the
42994313 // right type to store it. Finally load the entire union.
43004314
4315 // Note: The result here is not cached, because it generates runtime code.
4316
43014317 const mod = self.module;
43024318 const ip = &mod.intern_pool;
43034319 const union_ty = mod.typeToUnion(ty).?;
......@@ -4316,28 +4332,26 @@ const DeclGen = struct {
43164332 } else 0;
43174333
43184334 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);
43214336 }
43224337
43234338 const tmp_id = try self.alloc(ty, .{ .storage_class = .Function });
43244339
43254340 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);
43304344 try self.store(tag_ty, ptr_id, tag_id, .{});
43314345 }
43324346
43334347 const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]);
43344348 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);
43384352 const active_pl_ptr_id = self.spv.allocId();
43394353 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,
43414355 .id_result = active_pl_ptr_id,
43424356 .operand = pl_ptr_id,
43434357 });
......@@ -4396,13 +4410,13 @@ const DeclGen = struct {
43964410 const tmp_id = try self.alloc(object_ty, .{ .storage_class = .Function });
43974411 try self.store(object_ty, tmp_id, object_id, .{});
43984412
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});
44014415
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);
44034417 const active_pl_ptr_id = self.spv.allocId();
44044418 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,
44064420 .id_result = active_pl_ptr_id,
44074421 .operand = pl_ptr_id,
44084422 });
......@@ -4419,9 +4433,7 @@ const DeclGen = struct {
44194433 const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
44204434
44214435 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);
44254437
44264438 const field_ptr = try self.resolve(extra.field_ptr);
44274439 const field_ptr_int = try self.intFromPtr(field_ptr);
......@@ -4430,13 +4442,13 @@ const DeclGen = struct {
44304442 const base_ptr_int = base_ptr_int: {
44314443 if (field_offset == 0) break :base_ptr_int field_ptr_int;
44324444
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);
44354447 };
44364448
44374449 const base_ptr = self.spv.allocId();
44384450 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,
44404452 .id_result = base_ptr,
44414453 .integer_value = base_ptr_int,
44424454 });
......@@ -4451,7 +4463,7 @@ const DeclGen = struct {
44514463 object_ptr: IdRef,
44524464 field_index: u32,
44534465 ) !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);
44554467
44564468 const mod = self.module;
44574469 const object_ty = object_ptr_ty.childType(mod);
......@@ -4459,7 +4471,7 @@ const DeclGen = struct {
44594471 .Struct => switch (object_ty.containerLayout(mod)) {
44604472 .@"packed" => unreachable, // TODO
44614473 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});
44634475 },
44644476 },
44654477 .Union => switch (object_ty.containerLayout(mod)) {
......@@ -4469,16 +4481,16 @@ const DeclGen = struct {
44694481 if (!layout.has_payload) {
44704482 // Asked to get a pointer to a zero-sized field. Just lower this
44714483 // 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);
44734485 }
44744486
44754487 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});
44784490
44794491 const active_pl_ptr_id = self.spv.allocId();
44804492 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,
44824494 .id_result = active_pl_ptr_id,
44834495 .operand = pl_ptr_id,
44844496 });
......@@ -4506,7 +4518,7 @@ const DeclGen = struct {
45064518 };
45074519
45084520 // 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`,
45104522 // which is in the Generic address space. The variable is actually
45114523 // placed in the Function address space.
45124524 fn alloc(
......@@ -4514,13 +4526,13 @@ const DeclGen = struct {
45144526 ty: Type,
45154527 options: AllocOptions,
45164528 ) !IdRef {
4517 const ptr_fn_ty_ref = try self.ptrType(ty, .Function);
4529 const ptr_fn_ty_id = try self.ptrType(ty, .Function);
45184530
45194531 // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to
45204532 // directly generate them into func.prologue instead of the body.
45214533 const var_id = self.spv.allocId();
45224534 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,
45244536 .id_result = var_id,
45254537 .storage_class = .Function,
45264538 .initializer = options.initializer,
......@@ -4533,9 +4545,9 @@ const DeclGen = struct {
45334545
45344546 switch (options.storage_class) {
45354547 .Generic => {
4536 const ptr_gn_ty_ref = try self.ptrType(ty, .Generic);
4548 const ptr_gn_ty_id = try self.ptrType(ty, .Generic);
45374549 // 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);
45394551 },
45404552 .Function => return var_id,
45414553 else => unreachable,
......@@ -4563,9 +4575,9 @@ const DeclGen = struct {
45634575 assert(self.control_flow == .structured);
45644576
45654577 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);
45674579 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);
45694581 self.func.body.writeOperand(spec.IdRef, result_id);
45704582
45714583 for (incoming) |incoming_block| {
......@@ -4663,8 +4675,8 @@ const DeclGen = struct {
46634675 // Make sure that we are still in a block when exiting the function.
46644676 // TODO: Can we get rid of that?
46654677 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);
46684680 }
46694681
46704682 // The top-most merge actually only has a single source, the
......@@ -4745,7 +4757,7 @@ const DeclGen = struct {
47454757
47464758 assert(block.label != null);
47474759 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);
47494761
47504762 try self.func.body.emitRaw(
47514763 self.spv.gpa,
......@@ -4781,12 +4793,11 @@ const DeclGen = struct {
47814793 assert(cf.block_stack.items.len > 0);
47824794
47834795 // 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);
47864797 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);
47884799 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,
47904801 .id_result = jump_to_this_block_id,
47914802 .operand_1 = next_block,
47924803 .operand_2 = this_block,
......@@ -4862,8 +4873,7 @@ const DeclGen = struct {
48624873 try self.store(operand_ty, block_result_var_id, operand_id, .{});
48634874 }
48644875
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);
48674877 try self.structuredBreak(next_block);
48684878 },
48694879 .unstructured => |cf| {
......@@ -5026,8 +5036,7 @@ const DeclGen = struct {
50265036 // Functions with an empty error set are emitted with an error code
50275037 // return type and return zero so they can be function pointers coerced
50285038 // 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);
50315040 return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id });
50325041 } else {
50335042 return try self.func.body.emit(self.spv.gpa, .OpReturn, {});
......@@ -5051,8 +5060,7 @@ const DeclGen = struct {
50515060 // Functions with an empty error set are emitted with an error code
50525061 // return type and return zero so they can be function pointers coerced
50535062 // 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);
50565064 return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id });
50575065 } else {
50585066 return try self.func.body.emit(self.spv.gpa, .OpReturn, {});
......@@ -5076,8 +5084,7 @@ const DeclGen = struct {
50765084 const err_union_ty = self.typeOf(pl_op.operand);
50775085 const payload_ty = self.typeOfIndex(inst);
50785086
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);
50815088
50825089 const eu_layout = self.errorUnionLayout(payload_ty);
50835090
......@@ -5087,10 +5094,10 @@ const DeclGen = struct {
50875094 else
50885095 err_union_id;
50895096
5090 const zero_id = try self.constInt(err_ty_ref, 0);
5097 const zero_id = try self.constInt(Type.anyerror, 0, .direct);
50915098 const is_err_id = self.spv.allocId();
50925099 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,
50945101 .id_result = is_err_id,
50955102 .operand_1 = err_id,
50965103 .operand_2 = zero_id,
......@@ -5142,11 +5149,11 @@ const DeclGen = struct {
51425149 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
51435150 const operand_id = try self.resolve(ty_op.operand);
51445151 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);
51465153
51475154 if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) {
51485155 // 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);
51505157 }
51515158
51525159 const payload_ty = err_union_ty.errorUnionPayload(mod);
......@@ -5185,11 +5192,11 @@ const DeclGen = struct {
51855192 return operand_id;
51865193 }
51875194
5188 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
5195 const payload_ty_id = try self.resolveType(payload_ty, .indirect);
51895196
51905197 var members: [2]IdRef = undefined;
51915198 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);
51935200
51945201 var types: [2]Type = undefined;
51955202 types[eu_layout.errorFieldIndex()] = Type.anyerror;
......@@ -5203,15 +5210,14 @@ const DeclGen = struct {
52035210 const err_union_ty = self.typeOfIndex(inst);
52045211 const operand_id = try self.resolve(ty_op.operand);
52055212 const payload_ty = self.typeOf(ty_op.operand);
5206 const err_ty_ref = try self.resolveType(Type.anyerror, .direct);
52075213 const eu_layout = self.errorUnionLayout(payload_ty);
52085214
52095215 if (!eu_layout.payload_has_bits) {
5210 return try self.constInt(err_ty_ref, 0);
5216 return try self.constInt(Type.anyerror, 0, .direct);
52115217 }
52125218
52135219 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);
52155221 members[eu_layout.payloadFieldIndex()] = try self.convertToIndirect(payload_ty, operand_id);
52165222
52175223 var types: [2]Type = undefined;
......@@ -5229,7 +5235,7 @@ const DeclGen = struct {
52295235 const optional_ty = if (is_pointer) operand_ty.childType(mod) else operand_ty;
52305236 const payload_ty = optional_ty.optionalChild(mod);
52315237
5232 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
5238 const bool_ty_id = try self.resolveType(Type.bool, .direct);
52335239
52345240 if (optional_ty.optionalReprIsPayload(mod)) {
52355241 // Pointer payload represents nullability: pointer or slice.
......@@ -5248,8 +5254,8 @@ const DeclGen = struct {
52485254 else
52495255 loaded_id;
52505256
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);
52535259 const op: std.math.CompareOperator = switch (pred) {
52545260 .is_null => .eq,
52555261 .is_non_null => .neq,
......@@ -5261,8 +5267,8 @@ const DeclGen = struct {
52615267 if (is_pointer) {
52625268 if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
52635269 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});
52665272 break :blk try self.load(Type.bool, tag_ptr_id, .{});
52675273 }
52685274
......@@ -5283,7 +5289,7 @@ const DeclGen = struct {
52835289 // Invert condition
52845290 const result_id = self.spv.allocId();
52855291 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,
52875293 .id_result = result_id,
52885294 .operand = is_non_null_id,
52895295 });
......@@ -5305,8 +5311,7 @@ const DeclGen = struct {
53055311
53065312 const payload_ty = err_union_ty.errorUnionPayload(mod);
53075313 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);
53105315
53115316 const error_id = if (!eu_layout.payload_has_bits)
53125317 operand_id
......@@ -5315,10 +5320,10 @@ const DeclGen = struct {
53155320
53165321 const result_id = self.spv.allocId();
53175322 const operands = .{
5318 .id_result_type = self.typeId(bool_ty_ref),
5323 .id_result_type = bool_ty_id,
53195324 .id_result = result_id,
53205325 .operand_1 = error_id,
5321 .operand_2 = try self.constInt(err_ty_ref, 0),
5326 .operand_2 = try self.constInt(Type.anyerror, 0, .direct),
53225327 };
53235328 switch (pred) {
53245329 .is_err => try self.func.body.emit(self.spv.gpa, .OpINotEqual, operands),
......@@ -5351,7 +5356,7 @@ const DeclGen = struct {
53515356 const optional_ty = operand_ty.childType(mod);
53525357 const payload_ty = optional_ty.optionalChild(mod);
53535358 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);
53555360
53565361 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
53575362 // There is no payload, but we still need to return a valid pointer.
......@@ -5364,7 +5369,7 @@ const DeclGen = struct {
53645369 return try self.bitCast(result_ty, operand_ty, operand_id);
53655370 }
53665371
5367 return try self.accessChain(result_ty_ref, operand_id, &.{0});
5372 return try self.accessChain(result_ty_id, operand_id, &.{0});
53685373 }
53695374
53705375 fn airWrapOptional(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -5440,7 +5445,7 @@ const DeclGen = struct {
54405445 };
54415446
54425447 // 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);
54445449 // We always need the default case - if zig has none, we will generate unreachable there.
54455450 const default = self.spv.allocId();
54465451
......@@ -5471,7 +5476,7 @@ const DeclGen = struct {
54715476 const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len];
54725477 extra_index = case.end + case.data.items_len + case_body.len;
54735478
5474 const label: IdRef = @enumFromInt(@intFromEnum(first_case_label) + case_i);
5479 const label = case_labels.at(case_i);
54755480
54765481 for (items) |item| {
54775482 const value = (try self.air.value(item, mod)) orelse unreachable;
......@@ -5511,7 +5516,7 @@ const DeclGen = struct {
55115516 const case_body: []const Air.Inst.Index = @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]);
55125517 extra_index = case.end + case.data.items_len + case_body.len;
55135518
5514 const label: IdResult = @enumFromInt(@intFromEnum(first_case_label) + case_i);
5519 const label = case_labels.at(case_i);
55155520
55165521 try self.beginSpvBlock(label);
55175522
......@@ -5566,9 +5571,8 @@ const DeclGen = struct {
55665571 const mod = self.module;
55675572 const decl = mod.declPtr(self.decl_index);
55685573 const path = decl.getFileScope(mod).sub_file_path;
5569 const src_fname_id = try self.spv.resolveSourceFileName(path);
55705574 try self.func.body.emit(self.spv.gpa, .OpLine, .{
5571 .file = src_fname_id,
5575 .file = try self.spv.resolveString(path),
55725576 .line = self.base_line + dbg_stmt.line + 1,
55735577 .column = dbg_stmt.column + 1,
55745578 });
......@@ -5737,7 +5741,7 @@ const DeclGen = struct {
57375741 const fn_info = mod.typeToFunc(zig_fn_ty).?;
57385742 const return_type = fn_info.return_type;
57395743
5740 const result_type_ref = try self.resolveFnReturnType(Type.fromInterned(return_type));
5744 const result_type_id = try self.resolveFnReturnType(Type.fromInterned(return_type));
57415745 const result_id = self.spv.allocId();
57425746 const callee_id = try self.resolve(pl_op.operand);
57435747
......@@ -5758,7 +5762,7 @@ const DeclGen = struct {
57585762 }
57595763
57605764 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,
57625766 .id_result = result_id,
57635767 .function = callee_id,
57645768 .id_ref_3 = params[0..n_params],
src/codegen/spirv/Assembler.zig+67-51
......@@ -9,10 +9,9 @@ const Opcode = spec.Opcode;
99const Word = spec.Word;
1010const IdRef = spec.IdRef;
1111const IdResult = spec.IdResult;
12const StorageClass = spec.StorageClass;
1213
1314const SpvModule = @import("Module.zig");
14const CacheRef = SpvModule.CacheRef;
15const CacheKey = SpvModule.CacheKey;
1615
1716/// Represents a token in the assembly template.
1817const Token = struct {
......@@ -127,16 +126,16 @@ const AsmValue = union(enum) {
127126 value: IdRef,
128127
129128 /// This result-value represents a type registered into the module's type system.
130 ty: CacheRef,
129 ty: IdRef,
131130
132131 /// Retrieve the result-id of this AsmValue. Asserts that this AsmValue
133132 /// is of a variant that allows the result to be obtained (not an unresolved
134133 /// forward declaration, not in the process of being declared, etc).
135 pub fn resultId(self: AsmValue, spv: *const SpvModule) IdRef {
134 pub fn resultId(self: AsmValue) IdRef {
136135 return switch (self) {
137136 .just_declared, .unresolved_forward_reference => unreachable,
138137 .value => |result| result,
139 .ty => |ref| spv.resultId(ref),
138 .ty => |result| result,
140139 };
141140 }
142141};
......@@ -292,9 +291,10 @@ fn processInstruction(self: *Assembler) !void {
292291/// refers to the result.
293292fn processTypeInstruction(self: *Assembler) !AsmValue {
294293 const operands = self.inst.operands.items;
295 const ref = switch (self.inst.opcode) {
296 .OpTypeVoid => try self.spv.resolve(.void_type),
297 .OpTypeBool => try self.spv.resolve(.bool_type),
294 const section = &self.spv.sections.types_globals_constants;
295 const id = switch (self.inst.opcode) {
296 .OpTypeVoid => try self.spv.voidType(),
297 .OpTypeBool => try self.spv.boolType(),
298298 .OpTypeInt => blk: {
299299 const signedness: std.builtin.Signedness = switch (operands[2].literal32) {
300300 0 => .unsigned,
......@@ -317,43 +317,49 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
317317 return self.fail(0, "{} is not a valid bit count for floats (expected 16, 32 or 64)", .{bits});
318318 },
319319 }
320 break :blk try self.spv.resolve(.{ .float_type = .{ .bits = @intCast(bits) } });
320 break :blk try self.spv.floatType(@intCast(bits));
321 },
322 .OpTypeVector => blk: {
323 const child_type = try self.resolveRefId(operands[1].ref_id);
324 break :blk try self.spv.vectorType(operands[2].literal32, child_type);
321325 },
322 .OpTypeVector => try self.spv.resolve(.{ .vector_type = .{
323 .component_type = try self.resolveTypeRef(operands[1].ref_id),
324 .component_count = operands[2].literal32,
325 } }),
326326 .OpTypeArray => {
327327 // TODO: The length of an OpTypeArray is determined by a constant (which may be a spec constant),
328328 // and so some consideration must be taken when entering this in the type system.
329329 return self.todo("process OpTypeArray", .{});
330330 },
331331 .OpTypePointer => blk: {
332 break :blk try self.spv.resolve(.{
333 .ptr_type = .{
334 .storage_class = @enumFromInt(operands[1].value),
335 .child_type = try self.resolveTypeRef(operands[2].ref_id),
336 // TODO: This should be a proper reference resolved via OpTypeForwardPointer
337 .fwd = @enumFromInt(std.math.maxInt(u32)),
338 },
332 const storage_class: StorageClass = @enumFromInt(operands[1].value);
333 const child_type = try self.resolveRefId(operands[2].ref_id);
334 const result_id = self.spv.allocId();
335 try section.emit(self.spv.gpa, .OpTypePointer, .{
336 .id_result = result_id,
337 .storage_class = storage_class,
338 .type = child_type,
339339 });
340 break :blk result_id;
340341 },
341342 .OpTypeFunction => blk: {
342343 const param_operands = operands[2..];
343 const param_types = try self.spv.gpa.alloc(CacheRef, param_operands.len);
344 const return_type = try self.resolveRefId(operands[1].ref_id);
345
346 const param_types = try self.spv.gpa.alloc(IdRef, param_operands.len);
344347 defer self.spv.gpa.free(param_types);
345 for (param_types, 0..) |*param, i| {
346 param.* = try self.resolveTypeRef(param_operands[i].ref_id);
348 for (param_types, param_operands) |*param, operand| {
349 param.* = try self.resolveRefId(operand.ref_id);
347350 }
348 break :blk try self.spv.resolve(.{ .function_type = .{
349 .return_type = try self.resolveTypeRef(operands[1].ref_id),
350 .parameters = param_types,
351 } });
351 const result_id = self.spv.allocId();
352 try section.emit(self.spv.gpa, .OpTypeFunction, .{
353 .id_result = result_id,
354 .return_type = return_type,
355 .id_ref_2 = param_types,
356 });
357 break :blk result_id;
352358 },
353359 else => return self.todo("process type instruction {s}", .{@tagName(self.inst.opcode)}),
354360 };
355361
356 return AsmValue{ .ty = ref };
362 return AsmValue{ .ty = id };
357363}
358364
359365/// Emit `self.inst` into `self.spv` and `self.func`, and return the AsmValue
......@@ -410,7 +416,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
410416 .ref_id => |index| {
411417 const result = try self.resolveRef(index);
412418 try section.ensureUnusedCapacity(self.spv.gpa, 1);
413 section.writeOperand(spec.IdRef, result.resultId(self.spv));
419 section.writeOperand(spec.IdRef, result.resultId());
414420 },
415421 .string => |offset| {
416422 const text = std.mem.sliceTo(self.inst.string_bytes.items[offset..], 0);
......@@ -459,18 +465,9 @@ fn resolveRef(self: *Assembler, ref: AsmValue.Ref) !AsmValue {
459465 }
460466}
461467
462/// Resolve a value reference as type.
463fn resolveTypeRef(self: *Assembler, ref: AsmValue.Ref) !CacheRef {
468fn resolveRefId(self: *Assembler, ref: AsmValue.Ref) !IdRef {
464469 const value = try self.resolveRef(ref);
465 switch (value) {
466 .just_declared, .unresolved_forward_reference => unreachable,
467 .ty => |ty_ref| return ty_ref,
468 else => {
469 const name = self.value_map.keys()[ref];
470 // TODO: Improve source location.
471 return self.fail(0, "expected operand %{s} to refer to a type", .{name});
472 },
473 }
470 return value.resultId();
474471}
475472
476473/// Attempt to parse an instruction into `self.inst`.
......@@ -709,22 +706,41 @@ fn parseContextDependentNumber(self: *Assembler) !void {
709706 assert(self.inst.opcode == .OpConstant or self.inst.opcode == .OpSpecConstant);
710707
711708 const tok = self.currentToken();
712 const result_type_ref = try self.resolveTypeRef(self.inst.operands.items[0].ref_id);
713 const result_type = self.spv.cache.lookup(result_type_ref);
714 switch (result_type) {
715 .int_type => |int| {
716 try self.parseContextDependentInt(int.signedness, int.bits);
717 },
718 .float_type => |float| {
719 switch (float.bits) {
709 const result = try self.resolveRef(self.inst.operands.items[0].ref_id);
710 const result_id = result.resultId();
711 // We are going to cheat a little bit: The types we are interested in, int and float,
712 // are added to the module and cached via self.spv.intType and self.spv.floatType. Therefore,
713 // we can determine the width of these types by directly checking the cache.
714 // This only works if the Assembler and codegen both use spv.intType and spv.floatType though.
715 // We don't expect there to be many of these types, so just look it up every time.
716 // TODO: Count be improved to be a little bit more efficent.
717
718 {
719 var it = self.spv.cache.int_types.iterator();
720 while (it.next()) |entry| {
721 const id = entry.value_ptr.*;
722 if (id != result_id) continue;
723 const info = entry.key_ptr.*;
724 return try self.parseContextDependentInt(info.signedness, info.bits);
725 }
726 }
727
728 {
729 var it = self.spv.cache.float_types.iterator();
730 while (it.next()) |entry| {
731 const id = entry.value_ptr.*;
732 if (id != result_id) continue;
733 const info = entry.key_ptr.*;
734 switch (info.bits) {
720735 16 => try self.parseContextDependentFloat(16),
721736 32 => try self.parseContextDependentFloat(32),
722737 64 => try self.parseContextDependentFloat(64),
723 else => return self.fail(tok.start, "cannot parse {}-bit float literal", .{float.bits}),
738 else => return self.fail(tok.start, "cannot parse {}-bit info literal", .{info.bits}),
724739 }
725 },
726 else => return self.fail(tok.start, "cannot parse literal constant", .{}),
740 }
727741 }
742
743 return self.fail(tok.start, "cannot parse literal constant", .{});
728744}
729745
730746fn parseContextDependentInt(self: *Assembler, signedness: std.builtin.Signedness, width: u32) !void {
src/codegen/spirv/Cache.zig deleted-1125
......@@ -1,1125 +0,0 @@
1//! This file implements an InternPool-like structure that caches
2//! SPIR-V types and constants. Instead of generating type and
3//! constant instructions directly, we first keep a representation
4//! in a compressed database. This is then only later turned into
5//! actual SPIR-V instructions.
6//! Note: This cache is insertion-ordered. This means that we
7//! can materialize the SPIR-V instructions in the proper order,
8//! as SPIR-V requires that the type is emitted before use.
9//! Note: According to SPIR-V spec section 2.8, Types and Variables,
10//! non-pointer non-aggrerate types (which includes matrices and
11//! vectors) must have a _unique_ representation in the final binary.
12
13const std = @import("std");
14const assert = std.debug.assert;
15const Allocator = std.mem.Allocator;
16
17const Section = @import("Section.zig");
18const Module = @import("Module.zig");
19
20const spec = @import("spec.zig");
21const Opcode = spec.Opcode;
22const IdResult = spec.IdResult;
23const StorageClass = spec.StorageClass;
24
25const InternPool = @import("../../InternPool.zig");
26
27const Self = @This();
28
29map: std.AutoArrayHashMapUnmanaged(void, void) = .{},
30items: std.MultiArrayList(Item) = .{},
31extra: std.ArrayListUnmanaged(u32) = .{},
32
33string_bytes: std.ArrayListUnmanaged(u8) = .{},
34strings: std.AutoArrayHashMapUnmanaged(void, u32) = .{},
35
36recursive_ptrs: std.AutoHashMapUnmanaged(Ref, void) = .{},
37
38const Item = struct {
39 tag: Tag,
40 /// The result-id that this item uses.
41 result_id: IdResult,
42 /// The Tag determines how this should be interpreted.
43 data: u32,
44};
45
46const Tag = enum {
47 // -- Types
48 /// Simple type that has no additional data.
49 /// data is SimpleType.
50 type_simple,
51 /// Signed integer type
52 /// data is number of bits
53 type_int_signed,
54 /// Unsigned integer type
55 /// data is number of bits
56 type_int_unsigned,
57 /// Floating point type
58 /// data is number of bits
59 type_float,
60 /// Vector type
61 /// data is payload to VectorType
62 type_vector,
63 /// Array type
64 /// data is payload to ArrayType
65 type_array,
66 /// Function (proto)type
67 /// data is payload to FunctionType
68 type_function,
69 // /// Pointer type in the CrossWorkgroup storage class
70 // /// data is child type
71 // type_ptr_generic,
72 // /// Pointer type in the CrossWorkgroup storage class
73 // /// data is child type
74 // type_ptr_crosswgp,
75 // /// Pointer type in the Function storage class
76 // /// data is child type
77 // type_ptr_function,
78 /// Simple pointer type that does not have any decorations.
79 /// data is payload to SimplePointerType
80 type_ptr_simple,
81 /// A forward declaration for a pointer.
82 /// data is ForwardPointerType
83 type_fwd_ptr,
84 /// Simple structure type that does not have any decorations.
85 /// data is payload to SimpleStructType
86 type_struct_simple,
87 /// Simple structure type that does not have any decorations, but does
88 /// have member names trailing.
89 /// data is payload to SimpleStructType
90 type_struct_simple_with_member_names,
91 /// Opaque type.
92 /// data is name string.
93 type_opaque,
94
95 // -- Values
96 /// Value of type u8
97 /// data is value
98 uint8,
99 /// Value of type u32
100 /// data is value
101 uint32,
102 // TODO: More specialized tags here.
103 /// Integer value for signed values that are smaller than 32 bits.
104 /// data is pointer to Int32
105 int_small,
106 /// Integer value for unsigned values that are smaller than 32 bits.
107 /// data is pointer to UInt32
108 uint_small,
109 /// Integer value for signed values that are beteen 32 and 64 bits.
110 /// data is pointer to Int64
111 int_large,
112 /// Integer value for unsinged values that are beteen 32 and 64 bits.
113 /// data is pointer to UInt64
114 uint_large,
115 /// Value of type f16
116 /// data is value
117 float16,
118 /// Value of type f32
119 /// data is value
120 float32,
121 /// Value of type f64
122 /// data is payload to Float16
123 float64,
124 /// Undefined value
125 /// data is type
126 undef,
127 /// Null value
128 /// data is type
129 null,
130 /// Bool value that is true
131 /// data is (bool) type
132 bool_true,
133 /// Bool value that is false
134 /// data is (bool) type
135 bool_false,
136
137 const SimpleType = enum {
138 void,
139 bool,
140 };
141
142 const VectorType = Key.VectorType;
143 const ArrayType = Key.ArrayType;
144
145 // Trailing:
146 // - [param_len]Ref: parameter types.
147 const FunctionType = struct {
148 param_len: u32,
149 return_type: Ref,
150 };
151
152 const SimplePointerType = struct {
153 storage_class: StorageClass,
154 child_type: Ref,
155 fwd: Ref,
156 };
157
158 const ForwardPointerType = struct {
159 storage_class: StorageClass,
160 zig_child_type: InternPool.Index,
161 };
162
163 /// Trailing:
164 /// - [members_len]Ref: Member types.
165 /// - [members_len]String: Member names, -- ONLY if the tag is type_struct_simple_with_member_names
166 const SimpleStructType = struct {
167 /// (optional) The name of the struct.
168 name: String,
169 /// Number of members that this struct has.
170 members_len: u32,
171 };
172
173 const Float64 = struct {
174 // Low-order 32 bits of the value.
175 low: u32,
176 // High-order 32 bits of the value.
177 high: u32,
178
179 fn encode(value: f64) Float64 {
180 const bits = @as(u64, @bitCast(value));
181 return .{
182 .low = @truncate(bits),
183 .high = @truncate(bits >> 32),
184 };
185 }
186
187 fn decode(self: Float64) f64 {
188 const bits = @as(u64, self.low) | (@as(u64, self.high) << 32);
189 return @bitCast(bits);
190 }
191 };
192
193 const Int32 = struct {
194 ty: Ref,
195 value: i32,
196 };
197
198 const UInt32 = struct {
199 ty: Ref,
200 value: u32,
201 };
202
203 const UInt64 = struct {
204 ty: Ref,
205 low: u32,
206 high: u32,
207
208 fn encode(ty: Ref, value: u64) Int64 {
209 return .{
210 .ty = ty,
211 .low = @truncate(value),
212 .high = @truncate(value >> 32),
213 };
214 }
215
216 fn decode(self: UInt64) u64 {
217 return @as(u64, self.low) | (@as(u64, self.high) << 32);
218 }
219 };
220
221 const Int64 = struct {
222 ty: Ref,
223 low: u32,
224 high: u32,
225
226 fn encode(ty: Ref, value: i64) Int64 {
227 return .{
228 .ty = ty,
229 .low = @truncate(@as(u64, @bitCast(value))),
230 .high = @truncate(@as(u64, @bitCast(value)) >> 32),
231 };
232 }
233
234 fn decode(self: Int64) i64 {
235 return @as(i64, @bitCast(@as(u64, self.low) | (@as(u64, self.high) << 32)));
236 }
237 };
238};
239
240pub const Ref = enum(u32) { _ };
241
242/// This union represents something that can be interned. This includes
243/// types and constants. This structure is used for interfacing with the
244/// database: Values described for this structure are ephemeral and stored
245/// in a more memory-efficient manner internally.
246pub const Key = union(enum) {
247 // -- Types
248 void_type,
249 bool_type,
250 int_type: IntType,
251 float_type: FloatType,
252 vector_type: VectorType,
253 array_type: ArrayType,
254 function_type: FunctionType,
255 ptr_type: PointerType,
256 fwd_ptr_type: ForwardPointerType,
257 struct_type: StructType,
258 opaque_type: OpaqueType,
259
260 // -- values
261 int: Int,
262 float: Float,
263 undef: Undef,
264 null: Null,
265 bool: Bool,
266
267 pub const IntType = std.builtin.Type.Int;
268 pub const FloatType = std.builtin.Type.Float;
269
270 pub const VectorType = struct {
271 component_type: Ref,
272 component_count: u32,
273 };
274
275 pub const ArrayType = struct {
276 /// Child type of this array.
277 element_type: Ref,
278 /// Reference to a constant.
279 length: Ref,
280 /// Type has the 'ArrayStride' decoration.
281 /// If zero, no stride is present.
282 stride: u32 = 0,
283 };
284
285 pub const FunctionType = struct {
286 return_type: Ref,
287 parameters: []const Ref,
288 };
289
290 pub const PointerType = struct {
291 storage_class: StorageClass,
292 child_type: Ref,
293 /// Ref to a .fwd_ptr_type.
294 fwd: Ref,
295 // TODO: Decorations:
296 // - Alignment
297 // - ArrayStride
298 // - MaxByteOffset
299 };
300
301 pub const ForwardPointerType = struct {
302 zig_child_type: InternPool.Index,
303 storage_class: StorageClass,
304 };
305
306 pub const StructType = struct {
307 // TODO: Decorations.
308 /// The name of the structure. Can be `.none`.
309 name: String = .none,
310 /// The type of each member.
311 member_types: []const Ref,
312 /// Name for each member. May be omitted.
313 member_names: ?[]const String = null,
314
315 fn memberNames(self: @This()) []const String {
316 return if (self.member_names) |member_names| member_names else &.{};
317 }
318 };
319
320 pub const OpaqueType = struct {
321 name: String = .none,
322 };
323
324 pub const Int = struct {
325 /// The type: any bitness integer.
326 ty: Ref,
327 /// The actual value. Only uint64 and int64 types
328 /// are available here: Smaller types should use these
329 /// fields.
330 value: Value,
331
332 pub const Value = union(enum) {
333 uint64: u64,
334 int64: i64,
335 };
336
337 /// Turns this value into the corresponding 32-bit literal, 2s complement signed.
338 fn toBits32(self: Int) u32 {
339 return switch (self.value) {
340 .uint64 => |val| @intCast(val),
341 .int64 => |val| if (val < 0) @bitCast(@as(i32, @intCast(val))) else @intCast(val),
342 };
343 }
344
345 fn toBits64(self: Int) u64 {
346 return switch (self.value) {
347 .uint64 => |val| val,
348 .int64 => |val| @bitCast(val),
349 };
350 }
351
352 fn to(self: Int, comptime T: type) T {
353 return switch (self.value) {
354 inline else => |val| @intCast(val),
355 };
356 }
357 };
358
359 /// Represents a numberic value of some type.
360 pub const Float = struct {
361 /// The type: 16, 32, or 64-bit float.
362 ty: Ref,
363 /// The actual value.
364 value: Value,
365
366 pub const Value = union(enum) {
367 float16: f16,
368 float32: f32,
369 float64: f64,
370 };
371 };
372
373 pub const Undef = struct {
374 ty: Ref,
375 };
376
377 pub const Null = struct {
378 ty: Ref,
379 };
380
381 pub const Bool = struct {
382 ty: Ref,
383 value: bool,
384 };
385
386 fn hash(self: Key) u32 {
387 var hasher = std.hash.Wyhash.init(0);
388 switch (self) {
389 .float => |float| {
390 std.hash.autoHash(&hasher, float.ty);
391 switch (float.value) {
392 .float16 => |value| std.hash.autoHash(&hasher, @as(u16, @bitCast(value))),
393 .float32 => |value| std.hash.autoHash(&hasher, @as(u32, @bitCast(value))),
394 .float64 => |value| std.hash.autoHash(&hasher, @as(u64, @bitCast(value))),
395 }
396 },
397 .function_type => |func| {
398 std.hash.autoHash(&hasher, func.return_type);
399 for (func.parameters) |param_type| {
400 std.hash.autoHash(&hasher, param_type);
401 }
402 },
403 .struct_type => |struct_type| {
404 std.hash.autoHash(&hasher, struct_type.name);
405 for (struct_type.member_types) |member_type| {
406 std.hash.autoHash(&hasher, member_type);
407 }
408 for (struct_type.memberNames()) |member_name| {
409 std.hash.autoHash(&hasher, member_name);
410 }
411 },
412 inline else => |key| std.hash.autoHash(&hasher, key),
413 }
414 return @truncate(hasher.final());
415 }
416
417 fn eql(a: Key, b: Key) bool {
418 const KeyTag = @typeInfo(Key).Union.tag_type.?;
419 const a_tag: KeyTag = a;
420 const b_tag: KeyTag = b;
421 if (a_tag != b_tag) {
422 return false;
423 }
424 return switch (a) {
425 .function_type => |a_func| {
426 const b_func = b.function_type;
427 return a_func.return_type == b_func.return_type and
428 std.mem.eql(Ref, a_func.parameters, b_func.parameters);
429 },
430 .struct_type => |a_struct| {
431 const b_struct = b.struct_type;
432 return a_struct.name == b_struct.name and
433 std.mem.eql(Ref, a_struct.member_types, b_struct.member_types) and
434 std.mem.eql(String, a_struct.memberNames(), b_struct.memberNames());
435 },
436 // TODO: Unroll?
437 else => std.meta.eql(a, b),
438 };
439 }
440
441 pub const Adapter = struct {
442 self: *const Self,
443
444 pub fn eql(ctx: @This(), a: Key, b_void: void, b_index: usize) bool {
445 _ = b_void;
446 return ctx.self.lookup(@enumFromInt(b_index)).eql(a);
447 }
448
449 pub fn hash(ctx: @This(), a: Key) u32 {
450 _ = ctx;
451 return a.hash();
452 }
453 };
454
455 fn toSimpleType(self: Key) Tag.SimpleType {
456 return switch (self) {
457 .void_type => .void,
458 .bool_type => .bool,
459 else => unreachable,
460 };
461 }
462
463 pub fn isNumericalType(self: Key) bool {
464 return switch (self) {
465 .int_type, .float_type => true,
466 else => false,
467 };
468 }
469};
470
471pub fn deinit(self: *Self, spv: *const Module) void {
472 self.map.deinit(spv.gpa);
473 self.items.deinit(spv.gpa);
474 self.extra.deinit(spv.gpa);
475 self.string_bytes.deinit(spv.gpa);
476 self.strings.deinit(spv.gpa);
477 self.recursive_ptrs.deinit(spv.gpa);
478}
479
480/// Actually materialize the database into spir-v instructions.
481/// This function returns a spir-v section of (only) constant and type instructions.
482/// Additionally, decorations, debug names, etc, are all directly emitted into the
483/// `spv` module. The section is allocated with `spv.gpa`.
484pub fn materialize(self: *const Self, spv: *Module) !Section {
485 var section = Section{};
486 errdefer section.deinit(spv.gpa);
487 for (self.items.items(.result_id), 0..) |result_id, index| {
488 try self.emit(spv, result_id, @enumFromInt(index), &section);
489 }
490 return section;
491}
492
493fn emit(
494 self: *const Self,
495 spv: *Module,
496 result_id: IdResult,
497 ref: Ref,
498 section: *Section,
499) !void {
500 const key = self.lookup(ref);
501 const Lit = spec.LiteralContextDependentNumber;
502 switch (key) {
503 .void_type => {
504 try section.emit(spv.gpa, .OpTypeVoid, .{ .id_result = result_id });
505 try spv.debugName(result_id, "void");
506 },
507 .bool_type => {
508 try section.emit(spv.gpa, .OpTypeBool, .{ .id_result = result_id });
509 try spv.debugName(result_id, "bool");
510 },
511 .int_type => |int| {
512 try section.emit(spv.gpa, .OpTypeInt, .{
513 .id_result = result_id,
514 .width = int.bits,
515 .signedness = switch (int.signedness) {
516 .unsigned => @as(spec.Word, 0),
517 .signed => 1,
518 },
519 });
520 const ui: []const u8 = switch (int.signedness) {
521 .unsigned => "u",
522 .signed => "i",
523 };
524 try spv.debugNameFmt(result_id, "{s}{}", .{ ui, int.bits });
525 },
526 .float_type => |float| {
527 try section.emit(spv.gpa, .OpTypeFloat, .{
528 .id_result = result_id,
529 .width = float.bits,
530 });
531 try spv.debugNameFmt(result_id, "f{}", .{float.bits});
532 },
533 .vector_type => |vector| {
534 try section.emit(spv.gpa, .OpTypeVector, .{
535 .id_result = result_id,
536 .component_type = self.resultId(vector.component_type),
537 .component_count = vector.component_count,
538 });
539 },
540 .array_type => |array| {
541 try section.emit(spv.gpa, .OpTypeArray, .{
542 .id_result = result_id,
543 .element_type = self.resultId(array.element_type),
544 .length = self.resultId(array.length),
545 });
546 if (array.stride != 0) {
547 try spv.decorate(result_id, .{ .ArrayStride = .{ .array_stride = array.stride } });
548 }
549 },
550 .function_type => |function| {
551 try section.emitRaw(spv.gpa, .OpTypeFunction, 2 + function.parameters.len);
552 section.writeOperand(IdResult, result_id);
553 section.writeOperand(IdResult, self.resultId(function.return_type));
554 for (function.parameters) |param_type| {
555 section.writeOperand(IdResult, self.resultId(param_type));
556 }
557 },
558 .ptr_type => |ptr| {
559 try section.emit(spv.gpa, .OpTypePointer, .{
560 .id_result = result_id,
561 .storage_class = ptr.storage_class,
562 .type = self.resultId(ptr.child_type),
563 });
564 // TODO: Decorations?
565 },
566 .fwd_ptr_type => |fwd| {
567 // Only emit the OpTypeForwardPointer if its actually required.
568 if (self.recursive_ptrs.contains(ref)) {
569 try section.emit(spv.gpa, .OpTypeForwardPointer, .{
570 .pointer_type = result_id,
571 .storage_class = fwd.storage_class,
572 });
573 }
574 },
575 .struct_type => |struct_type| {
576 try section.emitRaw(spv.gpa, .OpTypeStruct, 1 + struct_type.member_types.len);
577 section.writeOperand(IdResult, result_id);
578 for (struct_type.member_types) |member_type| {
579 section.writeOperand(IdResult, self.resultId(member_type));
580 }
581 if (self.getString(struct_type.name)) |name| {
582 try spv.debugName(result_id, name);
583 }
584 for (struct_type.memberNames(), 0..) |member_name, i| {
585 if (self.getString(member_name)) |name| {
586 try spv.memberDebugName(result_id, @intCast(i), name);
587 }
588 }
589 // TODO: Decorations?
590 },
591 .opaque_type => |opaque_type| {
592 const name = if (self.getString(opaque_type.name)) |name| name else "";
593 try section.emit(spv.gpa, .OpTypeOpaque, .{
594 .id_result = result_id,
595 .literal_string = name,
596 });
597 },
598 .int => |int| {
599 const int_type = self.lookup(int.ty).int_type;
600 const ty_id = self.resultId(int.ty);
601 const lit: Lit = switch (int_type.bits) {
602 1...32 => .{ .uint32 = int.toBits32() },
603 33...64 => .{ .uint64 = int.toBits64() },
604 else => unreachable,
605 };
606
607 try section.emit(spv.gpa, .OpConstant, .{
608 .id_result_type = ty_id,
609 .id_result = result_id,
610 .value = lit,
611 });
612 },
613 .float => |float| {
614 const ty_id = self.resultId(float.ty);
615 const lit: Lit = switch (float.value) {
616 .float16 => |value| .{ .uint32 = @as(u16, @bitCast(value)) },
617 .float32 => |value| .{ .float32 = value },
618 .float64 => |value| .{ .float64 = value },
619 };
620 try section.emit(spv.gpa, .OpConstant, .{
621 .id_result_type = ty_id,
622 .id_result = result_id,
623 .value = lit,
624 });
625 },
626 .undef => |undef| {
627 try section.emit(spv.gpa, .OpUndef, .{
628 .id_result_type = self.resultId(undef.ty),
629 .id_result = result_id,
630 });
631 },
632 .null => |null_info| {
633 try section.emit(spv.gpa, .OpConstantNull, .{
634 .id_result_type = self.resultId(null_info.ty),
635 .id_result = result_id,
636 });
637 },
638 .bool => |bool_info| switch (bool_info.value) {
639 true => {
640 try section.emit(spv.gpa, .OpConstantTrue, .{
641 .id_result_type = self.resultId(bool_info.ty),
642 .id_result = result_id,
643 });
644 },
645 false => {
646 try section.emit(spv.gpa, .OpConstantFalse, .{
647 .id_result_type = self.resultId(bool_info.ty),
648 .id_result = result_id,
649 });
650 },
651 },
652 }
653}
654
655/// Add a key to this cache. Returns a reference to the key that
656/// was added. The corresponding result-id can be queried using
657/// self.resultId with the result.
658pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref {
659 const adapter: Key.Adapter = .{ .self = self };
660 const entry = try self.map.getOrPutAdapted(spv.gpa, key, adapter);
661 if (entry.found_existing) {
662 return @enumFromInt(entry.index);
663 }
664 const item: Item = switch (key) {
665 inline .void_type, .bool_type => .{
666 .tag = .type_simple,
667 .result_id = spv.allocId(),
668 .data = @intFromEnum(key.toSimpleType()),
669 },
670 .int_type => |int| blk: {
671 const t: Tag = switch (int.signedness) {
672 .signed => .type_int_signed,
673 .unsigned => .type_int_unsigned,
674 };
675 break :blk .{
676 .tag = t,
677 .result_id = spv.allocId(),
678 .data = int.bits,
679 };
680 },
681 .float_type => |float| .{
682 .tag = .type_float,
683 .result_id = spv.allocId(),
684 .data = float.bits,
685 },
686 .vector_type => |vector| .{
687 .tag = .type_vector,
688 .result_id = spv.allocId(),
689 .data = try self.addExtra(spv, vector),
690 },
691 .array_type => |array| .{
692 .tag = .type_array,
693 .result_id = spv.allocId(),
694 .data = try self.addExtra(spv, array),
695 },
696 .function_type => |function| blk: {
697 const extra = try self.addExtra(spv, Tag.FunctionType{
698 .param_len = @intCast(function.parameters.len),
699 .return_type = function.return_type,
700 });
701 try self.extra.appendSlice(spv.gpa, @ptrCast(function.parameters));
702 break :blk .{
703 .tag = .type_function,
704 .result_id = spv.allocId(),
705 .data = extra,
706 };
707 },
708 // .ptr_type => |ptr| switch (ptr.storage_class) {
709 // .Generic => Item{
710 // .tag = .type_ptr_generic,
711 // .result_id = spv.allocId(),
712 // .data = @intFromEnum(ptr.child_type),
713 // },
714 // .CrossWorkgroup => Item{
715 // .tag = .type_ptr_crosswgp,
716 // .result_id = spv.allocId(),
717 // .data = @intFromEnum(ptr.child_type),
718 // },
719 // .Function => Item{
720 // .tag = .type_ptr_function,
721 // .result_id = spv.allocId(),
722 // .data = @intFromEnum(ptr.child_type),
723 // },
724 // else => |storage_class| Item{
725 // .tag = .type_ptr_simple,
726 // .result_id = spv.allocId(),
727 // .data = try self.addExtra(spv, Tag.SimplePointerType{
728 // .storage_class = storage_class,
729 // .child_type = ptr.child_type,
730 // }),
731 // },
732 // },
733 .ptr_type => |ptr| Item{
734 .tag = .type_ptr_simple,
735 // For this variant we need to steal the ID of the forward-declaration, instead
736 // of allocating one manually. This will make sure that we get a single result-id
737 // any possibly forward declared pointer type.
738 .result_id = self.resultId(ptr.fwd),
739 .data = try self.addExtra(spv, Tag.SimplePointerType{
740 .storage_class = ptr.storage_class,
741 .child_type = ptr.child_type,
742 .fwd = ptr.fwd,
743 }),
744 },
745 .fwd_ptr_type => |fwd| Item{
746 .tag = .type_fwd_ptr,
747 .result_id = spv.allocId(),
748 .data = try self.addExtra(spv, Tag.ForwardPointerType{
749 .zig_child_type = fwd.zig_child_type,
750 .storage_class = fwd.storage_class,
751 }),
752 },
753 .struct_type => |struct_type| blk: {
754 const extra = try self.addExtra(spv, Tag.SimpleStructType{
755 .name = struct_type.name,
756 .members_len = @intCast(struct_type.member_types.len),
757 });
758 try self.extra.appendSlice(spv.gpa, @ptrCast(struct_type.member_types));
759
760 if (struct_type.member_names) |member_names| {
761 try self.extra.appendSlice(spv.gpa, @ptrCast(member_names));
762 break :blk Item{
763 .tag = .type_struct_simple_with_member_names,
764 .result_id = spv.allocId(),
765 .data = extra,
766 };
767 } else {
768 break :blk Item{
769 .tag = .type_struct_simple,
770 .result_id = spv.allocId(),
771 .data = extra,
772 };
773 }
774 },
775 .opaque_type => |opaque_type| Item{
776 .tag = .type_opaque,
777 .result_id = spv.allocId(),
778 .data = @intFromEnum(opaque_type.name),
779 },
780 .int => |int| blk: {
781 const int_type = self.lookup(int.ty).int_type;
782 if (int_type.signedness == .unsigned and int_type.bits == 8) {
783 break :blk .{
784 .tag = .uint8,
785 .result_id = spv.allocId(),
786 .data = int.to(u8),
787 };
788 } else if (int_type.signedness == .unsigned and int_type.bits == 32) {
789 break :blk .{
790 .tag = .uint32,
791 .result_id = spv.allocId(),
792 .data = int.to(u32),
793 };
794 }
795
796 switch (int.value) {
797 inline else => |val| {
798 if (val >= 0 and val <= std.math.maxInt(u32)) {
799 break :blk .{
800 .tag = .uint_small,
801 .result_id = spv.allocId(),
802 .data = try self.addExtra(spv, Tag.UInt32{
803 .ty = int.ty,
804 .value = @intCast(val),
805 }),
806 };
807 } else if (val >= std.math.minInt(i32) and val <= std.math.maxInt(i32)) {
808 break :blk .{
809 .tag = .int_small,
810 .result_id = spv.allocId(),
811 .data = try self.addExtra(spv, Tag.Int32{
812 .ty = int.ty,
813 .value = @intCast(val),
814 }),
815 };
816 } else if (val < 0) {
817 break :blk .{
818 .tag = .int_large,
819 .result_id = spv.allocId(),
820 .data = try self.addExtra(spv, Tag.Int64.encode(int.ty, @intCast(val))),
821 };
822 } else {
823 break :blk .{
824 .tag = .uint_large,
825 .result_id = spv.allocId(),
826 .data = try self.addExtra(spv, Tag.UInt64.encode(int.ty, @intCast(val))),
827 };
828 }
829 },
830 }
831 },
832 .float => |float| switch (self.lookup(float.ty).float_type.bits) {
833 16 => .{
834 .tag = .float16,
835 .result_id = spv.allocId(),
836 .data = @as(u16, @bitCast(float.value.float16)),
837 },
838 32 => .{
839 .tag = .float32,
840 .result_id = spv.allocId(),
841 .data = @as(u32, @bitCast(float.value.float32)),
842 },
843 64 => .{
844 .tag = .float64,
845 .result_id = spv.allocId(),
846 .data = try self.addExtra(spv, Tag.Float64.encode(float.value.float64)),
847 },
848 else => unreachable,
849 },
850 .undef => |undef| .{
851 .tag = .undef,
852 .result_id = spv.allocId(),
853 .data = @intFromEnum(undef.ty),
854 },
855 .null => |null_info| .{
856 .tag = .null,
857 .result_id = spv.allocId(),
858 .data = @intFromEnum(null_info.ty),
859 },
860 .bool => |bool_info| .{
861 .tag = switch (bool_info.value) {
862 true => Tag.bool_true,
863 false => Tag.bool_false,
864 },
865 .result_id = spv.allocId(),
866 .data = @intFromEnum(bool_info.ty),
867 },
868 };
869 try self.items.append(spv.gpa, item);
870
871 return @enumFromInt(entry.index);
872}
873
874/// Turn a Ref back into a Key.
875/// The Key is valid until the next call to resolve().
876pub fn lookup(self: *const Self, ref: Ref) Key {
877 const item = self.items.get(@intFromEnum(ref));
878 const data = item.data;
879 return switch (item.tag) {
880 .type_simple => switch (@as(Tag.SimpleType, @enumFromInt(data))) {
881 .void => .void_type,
882 .bool => .bool_type,
883 },
884 .type_int_signed => .{ .int_type = .{
885 .signedness = .signed,
886 .bits = @intCast(data),
887 } },
888 .type_int_unsigned => .{ .int_type = .{
889 .signedness = .unsigned,
890 .bits = @intCast(data),
891 } },
892 .type_float => .{ .float_type = .{
893 .bits = @intCast(data),
894 } },
895 .type_vector => .{ .vector_type = self.extraData(Tag.VectorType, data) },
896 .type_array => .{ .array_type = self.extraData(Tag.ArrayType, data) },
897 .type_function => {
898 const payload = self.extraDataTrail(Tag.FunctionType, data);
899 return .{
900 .function_type = .{
901 .return_type = payload.data.return_type,
902 .parameters = @ptrCast(self.extra.items[payload.trail..][0..payload.data.param_len]),
903 },
904 };
905 },
906 .type_ptr_simple => {
907 const payload = self.extraData(Tag.SimplePointerType, data);
908 return .{
909 .ptr_type = .{
910 .storage_class = payload.storage_class,
911 .child_type = payload.child_type,
912 .fwd = payload.fwd,
913 },
914 };
915 },
916 .type_fwd_ptr => {
917 const payload = self.extraData(Tag.ForwardPointerType, data);
918 return .{
919 .fwd_ptr_type = .{
920 .zig_child_type = payload.zig_child_type,
921 .storage_class = payload.storage_class,
922 },
923 };
924 },
925 .type_struct_simple => {
926 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
927 const member_types: []const Ref = @ptrCast(self.extra.items[payload.trail..][0..payload.data.members_len]);
928 return .{
929 .struct_type = .{
930 .name = payload.data.name,
931 .member_types = member_types,
932 .member_names = null,
933 },
934 };
935 },
936 .type_struct_simple_with_member_names => {
937 const payload = self.extraDataTrail(Tag.SimpleStructType, data);
938 const trailing = self.extra.items[payload.trail..];
939 const member_types: []const Ref = @ptrCast(trailing[0..payload.data.members_len]);
940 const member_names: []const String = @ptrCast(trailing[payload.data.members_len..][0..payload.data.members_len]);
941 return .{
942 .struct_type = .{
943 .name = payload.data.name,
944 .member_types = member_types,
945 .member_names = member_names,
946 },
947 };
948 },
949 .type_opaque => .{
950 .opaque_type = .{
951 .name = @enumFromInt(data),
952 },
953 },
954 .float16 => .{ .float = .{
955 .ty = self.get(.{ .float_type = .{ .bits = 16 } }),
956 .value = .{ .float16 = @bitCast(@as(u16, @intCast(data))) },
957 } },
958 .float32 => .{ .float = .{
959 .ty = self.get(.{ .float_type = .{ .bits = 32 } }),
960 .value = .{ .float32 = @bitCast(data) },
961 } },
962 .float64 => .{ .float = .{
963 .ty = self.get(.{ .float_type = .{ .bits = 64 } }),
964 .value = .{ .float64 = self.extraData(Tag.Float64, data).decode() },
965 } },
966 .uint8 => .{ .int = .{
967 .ty = self.get(.{ .int_type = .{ .signedness = .unsigned, .bits = 8 } }),
968 .value = .{ .uint64 = data },
969 } },
970 .uint32 => .{ .int = .{
971 .ty = self.get(.{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }),
972 .value = .{ .uint64 = data },
973 } },
974 .int_small => {
975 const payload = self.extraData(Tag.Int32, data);
976 return .{ .int = .{
977 .ty = payload.ty,
978 .value = .{ .int64 = payload.value },
979 } };
980 },
981 .uint_small => {
982 const payload = self.extraData(Tag.UInt32, data);
983 return .{ .int = .{
984 .ty = payload.ty,
985 .value = .{ .uint64 = payload.value },
986 } };
987 },
988 .int_large => {
989 const payload = self.extraData(Tag.Int64, data);
990 return .{ .int = .{
991 .ty = payload.ty,
992 .value = .{ .int64 = payload.decode() },
993 } };
994 },
995 .uint_large => {
996 const payload = self.extraData(Tag.UInt64, data);
997 return .{ .int = .{
998 .ty = payload.ty,
999 .value = .{ .uint64 = payload.decode() },
1000 } };
1001 },
1002 .undef => .{ .undef = .{
1003 .ty = @enumFromInt(data),
1004 } },
1005 .null => .{ .null = .{
1006 .ty = @enumFromInt(data),
1007 } },
1008 .bool_true => .{ .bool = .{
1009 .ty = @enumFromInt(data),
1010 .value = true,
1011 } },
1012 .bool_false => .{ .bool = .{
1013 .ty = @enumFromInt(data),
1014 .value = false,
1015 } },
1016 };
1017}
1018
1019/// Look op the result-id that corresponds to a particular
1020/// ref.
1021pub fn resultId(self: Self, ref: Ref) IdResult {
1022 return self.items.items(.result_id)[@intFromEnum(ref)];
1023}
1024
1025/// Get the ref for a key that has already been added to the cache.
1026fn get(self: *const Self, key: Key) Ref {
1027 const adapter: Key.Adapter = .{ .self = self };
1028 const index = self.map.getIndexAdapted(key, adapter).?;
1029 return @enumFromInt(index);
1030}
1031
1032fn addExtra(self: *Self, spv: *Module, extra: anytype) !u32 {
1033 const fields = @typeInfo(@TypeOf(extra)).Struct.fields;
1034 try self.extra.ensureUnusedCapacity(spv.gpa, fields.len);
1035 return try self.addExtraAssumeCapacity(extra);
1036}
1037
1038fn addExtraAssumeCapacity(self: *Self, extra: anytype) !u32 {
1039 const payload_offset: u32 = @intCast(self.extra.items.len);
1040 inline for (@typeInfo(@TypeOf(extra)).Struct.fields) |field| {
1041 const field_val = @field(extra, field.name);
1042 const word: u32 = switch (field.type) {
1043 u32 => field_val,
1044 i32 => @bitCast(field_val),
1045 Ref => @intFromEnum(field_val),
1046 StorageClass => @intFromEnum(field_val),
1047 String => @intFromEnum(field_val),
1048 InternPool.Index => @intFromEnum(field_val),
1049 else => @compileError("Invalid type: " ++ @typeName(field.type)),
1050 };
1051 self.extra.appendAssumeCapacity(word);
1052 }
1053 return payload_offset;
1054}
1055
1056fn extraData(self: Self, comptime T: type, offset: u32) T {
1057 return self.extraDataTrail(T, offset).data;
1058}
1059
1060fn extraDataTrail(self: Self, comptime T: type, offset: u32) struct { data: T, trail: u32 } {
1061 var result: T = undefined;
1062 const fields = @typeInfo(T).Struct.fields;
1063 inline for (fields, 0..) |field, i| {
1064 const word = self.extra.items[offset + i];
1065 @field(result, field.name) = switch (field.type) {
1066 u32 => word,
1067 i32 => @bitCast(word),
1068 Ref => @enumFromInt(word),
1069 StorageClass => @enumFromInt(word),
1070 String => @enumFromInt(word),
1071 InternPool.Index => @enumFromInt(word),
1072 else => @compileError("Invalid type: " ++ @typeName(field.type)),
1073 };
1074 }
1075 return .{
1076 .data = result,
1077 .trail = offset + @as(u32, @intCast(fields.len)),
1078 };
1079}
1080
1081/// Represents a reference to some null-terminated string.
1082pub const String = enum(u32) {
1083 none = std.math.maxInt(u32),
1084 _,
1085
1086 pub const Adapter = struct {
1087 self: *const Self,
1088
1089 pub fn eql(ctx: @This(), a: []const u8, _: void, b_index: usize) bool {
1090 const offset = ctx.self.strings.values()[b_index];
1091 const b = std.mem.sliceTo(ctx.self.string_bytes.items[offset..], 0);
1092 return std.mem.eql(u8, a, b);
1093 }
1094
1095 pub fn hash(ctx: @This(), a: []const u8) u32 {
1096 _ = ctx;
1097 var hasher = std.hash.Wyhash.init(0);
1098 hasher.update(a);
1099 return @truncate(hasher.final());
1100 }
1101 };
1102};
1103
1104/// Add a string to the cache. Must not contain any 0 values.
1105pub fn addString(self: *Self, spv: *Module, str: []const u8) !String {
1106 assert(std.mem.indexOfScalar(u8, str, 0) == null);
1107 const adapter = String.Adapter{ .self = self };
1108 const entry = try self.strings.getOrPutAdapted(spv.gpa, str, adapter);
1109 if (!entry.found_existing) {
1110 const offset = self.string_bytes.items.len;
1111 try self.string_bytes.ensureUnusedCapacity(spv.gpa, 1 + str.len);
1112 self.string_bytes.appendSliceAssumeCapacity(str);
1113 self.string_bytes.appendAssumeCapacity(0);
1114 entry.value_ptr.* = @intCast(offset);
1115 }
1116
1117 return @enumFromInt(entry.index);
1118}
1119
1120pub fn getString(self: *const Self, ref: String) ?[]const u8 {
1121 return switch (ref) {
1122 .none => null,
1123 else => std.mem.sliceTo(self.string_bytes.items[self.strings.values()[@intFromEnum(ref)]..], 0),
1124 };
1125}
src/codegen/spirv/Module.zig+146-102
......@@ -20,11 +20,6 @@ const IdResultType = spec.IdResultType;
2020
2121const Section = @import("Section.zig");
2222
23const Cache = @import("Cache.zig");
24pub const CacheKey = Cache.Key;
25pub const CacheRef = Cache.Ref;
26pub const CacheString = Cache.String;
27
2823/// This structure represents a function that isc in-progress of being emitted.
2924/// Commonly, the contents of this structure will be merged with the appropriate
3025/// sections of the module and re-used. Note that the SPIR-V module system makes
......@@ -98,7 +93,7 @@ pub const EntryPoint = struct {
9893 /// The declaration that should be exported.
9994 decl_index: Decl.Index,
10095 /// The name of the kernel to be exported.
101 name: CacheString,
96 name: []const u8,
10297 /// Calling Convention
10398 execution_model: spec.ExecutionModel,
10499};
......@@ -106,6 +101,9 @@ pub const EntryPoint = struct {
106101/// A general-purpose allocator which may be used to allocate resources for this module
107102gpa: Allocator,
108103
104/// Arena for things that need to live for the length of this program.
105arena: std.heap.ArenaAllocator,
106
109107/// Module layout, according to SPIR-V Spec section 2.4, "Logical Layout of a Module".
110108sections: struct {
111109 /// Capability instructions
......@@ -143,14 +141,21 @@ sections: struct {
143141/// SPIR-V instructions return result-ids. This variable holds the module-wide counter for these.
144142next_result_id: Word,
145143
146/// Cache for results of OpString instructions for module file names fed to OpSource.
147/// Since OpString is pretty much only used for those, we don't need to keep track of all strings,
148/// just the ones for OpLine. Note that OpLine needs the result of OpString, and not that of OpSource.
149source_file_names: std.AutoArrayHashMapUnmanaged(CacheString, IdRef) = .{},
150
151/// SPIR-V type- and constant cache. This structure is used to store information about these in a more
152/// efficient manner.
153cache: Cache = .{},
144/// Cache for results of OpString instructions.
145strings: std.StringArrayHashMapUnmanaged(IdRef) = .{},
146
147/// Some types shouldn't be emitted more than one time, but cannot be caught by
148/// the `intern_map` during codegen. Sometimes, IDs are compared to check if
149/// types are the same, so we can't delay until the dedup pass. Therefore,
150/// this is an ad-hoc structure to cache types where required.
151/// According to the SPIR-V specification, section 2.8, this includes all non-aggregate
152/// non-pointer types.
153cache: struct {
154 bool_type: ?IdRef = null,
155 void_type: ?IdRef = null,
156 int_types: std.AutoHashMapUnmanaged(std.builtin.Type.Int, IdRef) = .{},
157 float_types: std.AutoHashMapUnmanaged(std.builtin.Type.Float, IdRef) = .{},
158} = .{},
154159
155160/// Set of Decls, referred to by Decl.Index.
156161decls: std.ArrayListUnmanaged(Decl) = .{},
......@@ -168,6 +173,7 @@ extended_instruction_set: std.AutoHashMapUnmanaged(spec.InstructionSet, IdRef) =
168173pub fn init(gpa: Allocator) Module {
169174 return .{
170175 .gpa = gpa,
176 .arena = std.heap.ArenaAllocator.init(gpa),
171177 .next_result_id = 1, // 0 is an invalid SPIR-V result id, so start counting at 1.
172178 };
173179}
......@@ -184,8 +190,10 @@ pub fn deinit(self: *Module) void {
184190 self.sections.types_globals_constants.deinit(self.gpa);
185191 self.sections.functions.deinit(self.gpa);
186192
187 self.source_file_names.deinit(self.gpa);
188 self.cache.deinit(self);
193 self.strings.deinit(self.gpa);
194
195 self.cache.int_types.deinit(self.gpa);
196 self.cache.float_types.deinit(self.gpa);
189197
190198 self.decls.deinit(self.gpa);
191199 self.decl_deps.deinit(self.gpa);
......@@ -193,38 +201,35 @@ pub fn deinit(self: *Module) void {
193201 self.entry_points.deinit(self.gpa);
194202
195203 self.extended_instruction_set.deinit(self.gpa);
204 self.arena.deinit();
196205
197206 self.* = undefined;
198207}
199208
200pub fn allocId(self: *Module) spec.IdResult {
201 defer self.next_result_id += 1;
202 return @enumFromInt(self.next_result_id);
203}
204
205pub fn allocIds(self: *Module, n: u32) spec.IdResult {
206 defer self.next_result_id += n;
207 return @enumFromInt(self.next_result_id);
208}
209
210pub fn idBound(self: Module) Word {
211 return self.next_result_id;
212}
209pub const IdRange = struct {
210 base: u32,
211 len: u32,
213212
214pub fn resolve(self: *Module, key: CacheKey) !CacheRef {
215 return self.cache.resolve(self, key);
216}
213 pub fn at(range: IdRange, i: usize) IdResult {
214 assert(i < range.len);
215 return @enumFromInt(range.base + i);
216 }
217};
217218
218pub fn resultId(self: *const Module, ref: CacheRef) IdResult {
219 return self.cache.resultId(ref);
219pub fn allocIds(self: *Module, n: u32) IdRange {
220 defer self.next_result_id += n;
221 return .{
222 .base = self.next_result_id,
223 .len = n,
224 };
220225}
221226
222pub fn resolveId(self: *Module, key: CacheKey) !IdResult {
223 return self.resultId(try self.resolve(key));
227pub fn allocId(self: *Module) IdResult {
228 return self.allocIds(1).at(0);
224229}
225230
226pub fn resolveString(self: *Module, str: []const u8) !CacheString {
227 return try self.cache.addString(self, str);
231pub fn idBound(self: Module) Word {
232 return self.next_result_id;
228233}
229234
230235fn addEntryPointDeps(
......@@ -271,7 +276,7 @@ fn entryPoints(self: *Module) !Section {
271276 try entry_points.emit(self.gpa, .OpEntryPoint, .{
272277 .execution_model = entry_point.execution_model,
273278 .entry_point = entry_point_id,
274 .name = self.cache.getString(entry_point.name).?,
279 .name = entry_point.name,
275280 .interface = interface.items,
276281 });
277282 }
......@@ -286,9 +291,6 @@ pub fn finalize(self: *Module, a: Allocator, target: std.Target) ![]Word {
286291 var entry_points = try self.entryPoints();
287292 defer entry_points.deinit(self.gpa);
288293
289 var types_constants = try self.cache.materialize(self);
290 defer types_constants.deinit(self.gpa);
291
292294 const header = [_]Word{
293295 spec.magic_number,
294296 // TODO: From cpu features
......@@ -331,7 +333,6 @@ pub fn finalize(self: *Module, a: Allocator, target: std.Target) ![]Word {
331333 self.sections.debug_strings.toWords(),
332334 self.sections.debug_names.toWords(),
333335 self.sections.annotations.toWords(),
334 types_constants.toWords(),
335336 self.sections.types_globals_constants.toWords(),
336337 self.sections.functions.toWords(),
337338 };
......@@ -376,83 +377,126 @@ pub fn importInstructionSet(self: *Module, set: spec.InstructionSet) !IdRef {
376377 return result_id;
377378}
378379
379/// Fetch the result-id of an OpString instruction that encodes the path of the source
380/// file of the decl. This function may also emit an OpSource with source-level information regarding
381/// the decl.
382pub fn resolveSourceFileName(self: *Module, path: []const u8) !IdRef {
383 const path_ref = try self.resolveString(path);
384 const result = try self.source_file_names.getOrPut(self.gpa, path_ref);
385 if (!result.found_existing) {
386 const file_result_id = self.allocId();
387 result.value_ptr.* = file_result_id;
388 try self.sections.debug_strings.emit(self.gpa, .OpString, .{
389 .id_result = file_result_id,
390 .string = path,
391 });
380/// Fetch the result-id of an instruction corresponding to a string.
381pub fn resolveString(self: *Module, string: []const u8) !IdRef {
382 if (self.strings.get(string)) |id| {
383 return id;
392384 }
393385
394 return result.value_ptr.*;
395}
386 const id = self.allocId();
387 try self.strings.put(self.gpa, try self.arena.allocator().dupe(u8, string), id);
388
389 try self.sections.debug_strings.emit(self.gpa, .OpString, .{
390 .id_result = id,
391 .string = string,
392 });
396393
397pub fn intType(self: *Module, signedness: std.builtin.Signedness, bits: u16) !CacheRef {
398 return try self.resolve(.{ .int_type = .{
399 .signedness = signedness,
400 .bits = bits,
401 } });
394 return id;
402395}
403396
404pub fn vectorType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {
405 return try self.resolve(.{ .vector_type = .{
406 .component_type = elem_ty_ref,
407 .component_count = len,
408 } });
397pub fn structType(self: *Module, types: []const IdRef, maybe_names: ?[]const []const u8) !IdRef {
398 const result_id = self.allocId();
399
400 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeStruct, .{
401 .id_result = result_id,
402 .id_ref = types,
403 });
404
405 if (maybe_names) |names| {
406 assert(names.len == types.len);
407 for (names, 0..) |name, i| {
408 try self.memberDebugName(result_id, @intCast(i), name);
409 }
410 }
411
412 return result_id;
409413}
410414
411pub fn arrayType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {
412 const len_ty_ref = try self.resolve(.{ .int_type = .{
413 .signedness = .unsigned,
414 .bits = 32,
415 } });
416 const len_ref = try self.resolve(.{ .int = .{
417 .ty = len_ty_ref,
418 .value = .{ .uint64 = len },
419 } });
420 return try self.resolve(.{ .array_type = .{
421 .element_type = elem_ty_ref,
422 .length = len_ref,
423 } });
415pub fn boolType(self: *Module) !IdRef {
416 if (self.cache.bool_type) |id| return id;
417
418 const result_id = self.allocId();
419 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeBool, .{
420 .id_result = result_id,
421 });
422 self.cache.bool_type = result_id;
423 return result_id;
424424}
425425
426pub fn constInt(self: *Module, ty_ref: CacheRef, value: anytype) !IdRef {
427 const ty = self.cache.lookup(ty_ref).int_type;
428 const Value = Cache.Key.Int.Value;
429 return try self.resolveId(.{ .int = .{
430 .ty = ty_ref,
431 .value = switch (ty.signedness) {
432 .signed => Value{ .int64 = @intCast(value) },
433 .unsigned => Value{ .uint64 = @intCast(value) },
434 },
435 } });
426pub fn voidType(self: *Module) !IdRef {
427 if (self.cache.void_type) |id| return id;
428
429 const result_id = self.allocId();
430 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVoid, .{
431 .id_result = result_id,
432 });
433 self.cache.void_type = result_id;
434 try self.debugName(result_id, "void");
435 return result_id;
436436}
437437
438pub fn constUndef(self: *Module, ty_ref: CacheRef) !IdRef {
439 return try self.resolveId(.{ .undef = .{ .ty = ty_ref } });
438pub fn intType(self: *Module, signedness: std.builtin.Signedness, bits: u16) !IdRef {
439 assert(bits > 0);
440 const entry = try self.cache.int_types.getOrPut(self.gpa, .{ .signedness = signedness, .bits = bits });
441 if (!entry.found_existing) {
442 const result_id = self.allocId();
443 entry.value_ptr.* = result_id;
444 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeInt, .{
445 .id_result = result_id,
446 .width = bits,
447 .signedness = switch (signedness) {
448 .signed => 1,
449 .unsigned => 0,
450 },
451 });
452
453 switch (signedness) {
454 .signed => try self.debugNameFmt(result_id, "i{}", .{bits}),
455 .unsigned => try self.debugNameFmt(result_id, "u{}", .{bits}),
456 }
457 }
458 return entry.value_ptr.*;
459}
460
461pub fn floatType(self: *Module, bits: u16) !IdRef {
462 assert(bits > 0);
463 const entry = try self.cache.float_types.getOrPut(self.gpa, .{ .bits = bits });
464 if (!entry.found_existing) {
465 const result_id = self.allocId();
466 entry.value_ptr.* = result_id;
467 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeFloat, .{
468 .id_result = result_id,
469 .width = bits,
470 });
471 try self.debugNameFmt(result_id, "f{}", .{bits});
472 }
473 return entry.value_ptr.*;
440474}
441475
442pub fn constNull(self: *Module, ty_ref: CacheRef) !IdRef {
443 return try self.resolveId(.{ .null = .{ .ty = ty_ref } });
476pub fn vectorType(self: *Module, len: u32, child_id: IdRef) !IdRef {
477 const result_id = self.allocId();
478 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVector, .{
479 .id_result = result_id,
480 .component_type = child_id,
481 .component_count = len,
482 });
483 return result_id;
444484}
445485
446pub fn constBool(self: *Module, ty_ref: CacheRef, value: bool) !IdRef {
447 return try self.resolveId(.{ .bool = .{ .ty = ty_ref, .value = value } });
486pub fn constUndef(self: *Module, ty_id: IdRef) !IdRef {
487 const result_id = self.allocId();
488 try self.sections.types_globals_constants.emit(self.gpa, .OpUndef, .{
489 .id_result_type = ty_id,
490 .id_result = result_id,
491 });
492 return result_id;
448493}
449494
450pub fn constComposite(self: *Module, ty_ref: CacheRef, members: []const IdRef) !IdRef {
495pub fn constNull(self: *Module, ty_id: IdRef) !IdRef {
451496 const result_id = self.allocId();
452 try self.sections.types_globals_constants.emit(self.gpa, .OpSpecConstantComposite, .{
453 .id_result_type = self.resultId(ty_ref),
497 try self.sections.types_globals_constants.emit(self.gpa, .OpConstantNull, .{
498 .id_result_type = ty_id,
454499 .id_result = result_id,
455 .constituents = members,
456500 });
457501 return result_id;
458502}
......@@ -520,7 +564,7 @@ pub fn declareEntryPoint(
520564) !void {
521565 try self.entry_points.append(self.gpa, .{
522566 .decl_index = decl_index,
523 .name = try self.resolveString(name),
567 .name = try self.arena.allocator().dupe(u8, name),
524568 .execution_model = execution_model,
525569 });
526570}
src/link/SpirV.zig+5-5
......@@ -245,7 +245,7 @@ pub fn flushModule(self: *SpirV, arena: Allocator, prog_node: *std.Progress.Node
245245 const module = try spv.finalize(arena, target);
246246 errdefer arena.free(module);
247247
248 const linked_module = self.linkModule(arena, module) catch |err| switch (err) {
248 const linked_module = self.linkModule(arena, module, &sub_prog_node) catch |err| switch (err) {
249249 error.OutOfMemory => return error.OutOfMemory,
250250 else => |other| {
251251 log.err("error while linking: {s}\n", .{@errorName(other)});
......@@ -256,7 +256,7 @@ pub fn flushModule(self: *SpirV, arena: Allocator, prog_node: *std.Progress.Node
256256 try self.base.file.?.writeAll(std.mem.sliceAsBytes(linked_module));
257257}
258258
259fn linkModule(self: *SpirV, a: Allocator, module: []Word) ![]Word {
259fn linkModule(self: *SpirV, a: Allocator, module: []Word, progress: *std.Progress.Node) ![]Word {
260260 _ = self;
261261
262262 const lower_invocation_globals = @import("SpirV/lower_invocation_globals.zig");
......@@ -267,9 +267,9 @@ fn linkModule(self: *SpirV, a: Allocator, module: []Word) ![]Word {
267267 defer parser.deinit();
268268 var binary = try parser.parse(module);
269269
270 try lower_invocation_globals.run(&parser, &binary);
271 try prune_unused.run(&parser, &binary);
272 try dedup.run(&parser, &binary);
270 try lower_invocation_globals.run(&parser, &binary, progress);
271 try prune_unused.run(&parser, &binary, progress);
272 try dedup.run(&parser, &binary, progress);
273273
274274 return binary.finalize(a);
275275}
src/link/SpirV/BinaryModule.zig+2-1
......@@ -116,7 +116,8 @@ pub const Instruction = struct {
116116 const instruction_len = self.words[self.offset] >> 16;
117117 defer self.offset += instruction_len;
118118 defer self.index += 1;
119 assert(instruction_len != 0 and self.offset < self.words.len); // Verified in BinaryModule.parse.
119 assert(instruction_len != 0);
120 assert(self.offset < self.words.len);
120121
121122 return Instruction{
122123 .opcode = @enumFromInt(self.words[self.offset] & 0xFFFF),
src/link/SpirV/deduplicate.zig+72-8
......@@ -47,6 +47,10 @@ const ModuleInfo = struct {
4747 result_id_index: u16,
4848 /// The first decoration in `self.decorations`.
4949 first_decoration: u32,
50
51 fn operands(self: Entity, binary: *const BinaryModule) []const Word {
52 return binary.instructions[self.first_operand..][0..self.num_operands];
53 }
5054 };
5155
5256 /// Maps result-id to Entity's
......@@ -210,10 +214,41 @@ const EntityContext = struct {
210214
211215 const entity = self.info.entities.values()[index];
212216
217 // If the current pointer is recursive, don't immediately add it to the map. This is to ensure that
218 // if the current pointer is already recursive, it gets the same hash a pointer that points to the
219 // same child but has a different result-id.
213220 if (entity.kind == .OpTypePointer) {
214221 // This may be either a pointer that is forward-referenced in the future,
215222 // or a forward reference to a pointer.
216 const entry = try self.ptr_map_a.getOrPut(self.a, id);
223 // Note: We use the **struct** here instead of the pointer itself, to avoid an edge case like this:
224 //
225 // A - C*'
226 // \
227 // C - C*'
228 // /
229 // B - C*"
230 //
231 // In this case, hashing A goes like
232 // A -> C*' -> C -> C*' recursion
233 // And hashing B goes like
234 // B -> C*" -> C -> C*' -> C -> C*' recursion
235 // The are several calls to ptrType in codegen that may C*' and C*" to be generated as separate
236 // types. This is not a problem for C itself though - this can only be generated through resolveType()
237 // and so ensures equality by Zig's type system. Technically the above problem is still present, but it
238 // would only be present in a structure such as
239 //
240 // A - C*' - C'
241 // \
242 // C*" - C - C*
243 // /
244 // B
245 //
246 // where there is a duplicate definition of struct C. Resolving this requires a much more time consuming
247 // algorithm though, and because we don't expect any correctness issues with it, we leave that for now.
248
249 // TODO: Do we need to mind the storage class here? Its going to be recursive regardless, right?
250 const struct_id: ResultId = @enumFromInt(entity.operands(self.binary)[2]);
251 const entry = try self.ptr_map_a.getOrPut(self.a, struct_id);
217252 if (entry.found_existing) {
218253 // Pointer already seen. Hash the index instead of recursing into its children.
219254 std.hash.autoHash(hasher, entry.index);
......@@ -228,12 +263,17 @@ const EntityContext = struct {
228263 for (decorations) |decoration| {
229264 try self.hashEntity(hasher, decoration);
230265 }
266
267 if (entity.kind == .OpTypePointer) {
268 const struct_id: ResultId = @enumFromInt(entity.operands(self.binary)[2]);
269 assert(self.ptr_map_a.swapRemove(struct_id));
270 }
231271 }
232272
233273 fn hashEntity(self: *EntityContext, hasher: *std.hash.Wyhash, entity: ModuleInfo.Entity) !void {
234274 std.hash.autoHash(hasher, entity.kind);
235275 // Process operands
236 const operands = self.binary.instructions[entity.first_operand..][0..entity.num_operands];
276 const operands = entity.operands(self.binary);
237277 for (operands, 0..) |operand, i| {
238278 if (i == entity.result_id_index) {
239279 // Not relevant, skip...
......@@ -273,12 +313,19 @@ const EntityContext = struct {
273313 const entity_a = self.info.entities.values()[index_a];
274314 const entity_b = self.info.entities.values()[index_b];
275315
316 if (entity_a.kind != entity_b.kind) {
317 return false;
318 }
319
276320 if (entity_a.kind == .OpTypePointer) {
277321 // May be a forward reference, or should be saved as a potential
278322 // forward reference in the future. Whatever the case, it should
279323 // be the same for both a and b.
280 const entry_a = try self.ptr_map_a.getOrPut(self.a, id_a);
281 const entry_b = try self.ptr_map_b.getOrPut(self.a, id_b);
324 const struct_id_a: ResultId = @enumFromInt(entity_a.operands(self.binary)[2]);
325 const struct_id_b: ResultId = @enumFromInt(entity_b.operands(self.binary)[2]);
326
327 const entry_a = try self.ptr_map_a.getOrPut(self.a, struct_id_a);
328 const entry_b = try self.ptr_map_b.getOrPut(self.a, struct_id_b);
282329
283330 if (entry_a.found_existing != entry_b.found_existing) return false;
284331 if (entry_a.index != entry_b.index) return false;
......@@ -306,6 +353,14 @@ const EntityContext = struct {
306353 }
307354 }
308355
356 if (entity_a.kind == .OpTypePointer) {
357 const struct_id_a: ResultId = @enumFromInt(entity_a.operands(self.binary)[2]);
358 const struct_id_b: ResultId = @enumFromInt(entity_b.operands(self.binary)[2]);
359
360 assert(self.ptr_map_a.swapRemove(struct_id_a));
361 assert(self.ptr_map_b.swapRemove(struct_id_b));
362 }
363
309364 return true;
310365 }
311366
......@@ -316,8 +371,8 @@ const EntityContext = struct {
316371 return false;
317372 }
318373
319 const operands_a = self.binary.instructions[entity_a.first_operand..][0..entity_a.num_operands];
320 const operands_b = self.binary.instructions[entity_b.first_operand..][0..entity_b.num_operands];
374 const operands_a = entity_a.operands(self.binary);
375 const operands_b = entity_b.operands(self.binary);
321376
322377 // Note: returns false for operands that have explicit defaults in optional operands... oh well
323378 if (operands_a.len != operands_b.len) {
......@@ -363,7 +418,11 @@ const EntityHashContext = struct {
363418 }
364419};
365420
366pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
421pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule, progress: *std.Progress.Node) !void {
422 var sub_node = progress.start("deduplicate", 0);
423 sub_node.activate();
424 defer sub_node.end();
425
367426 var arena = std.heap.ArenaAllocator.init(parser.a);
368427 defer arena.deinit();
369428 const a = arena.allocator();
......@@ -376,6 +435,7 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
376435 .info = &info,
377436 .binary = binary,
378437 };
438
379439 for (info.entities.keys()) |id| {
380440 _ = try ctx.hash(id);
381441 }
......@@ -395,6 +455,8 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
395455 }
396456 }
397457
458 sub_node.setEstimatedTotalItems(binary.instructions.len);
459
398460 // Now process the module, and replace instructions where needed.
399461 var section = Section{};
400462 var it = binary.iterateInstructions();
......@@ -402,6 +464,8 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
402464 var new_operands = std.ArrayList(u32).init(a);
403465 var emitted_ptrs = std.AutoHashMap(ResultId, void).init(a);
404466 while (it.next()) |inst| {
467 defer sub_node.setCompletedItems(inst.offset);
468
405469 // Result-id can only be the first or second operand
406470 const inst_spec = parser.getInstSpec(inst.opcode).?;
407471
......@@ -454,7 +518,7 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
454518 if (entity.kind == .OpTypePointer and !emitted_ptrs.contains(id)) {
455519 // Grab the pointer's storage class from its operands in the original
456520 // module.
457 const storage_class: spec.StorageClass = @enumFromInt(binary.instructions[entity.first_operand + 1]);
521 const storage_class: spec.StorageClass = @enumFromInt(entity.operands(binary)[1]);
458522 try section.emit(a, .OpTypeForwardPointer, .{
459523 .pointer_type = id,
460524 .storage_class = storage_class,
src/link/SpirV/lower_invocation_globals.zig+11-1
......@@ -682,7 +682,11 @@ const ModuleBuilder = struct {
682682 }
683683};
684684
685pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
685pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule, progress: *std.Progress.Node) !void {
686 var sub_node = progress.start("Lower invocation globals", 6);
687 sub_node.activate();
688 defer sub_node.end();
689
686690 var arena = std.heap.ArenaAllocator.init(parser.a);
687691 defer arena.deinit();
688692 const a = arena.allocator();
......@@ -691,10 +695,16 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
691695 try info.resolve(a);
692696
693697 var builder = try ModuleBuilder.init(a, binary.*, info);
698 sub_node.completeOne();
694699 try builder.deriveNewFnInfo(info);
700 sub_node.completeOne();
695701 try builder.processPreamble(binary.*, info);
702 sub_node.completeOne();
696703 try builder.emitFunctionTypes(info);
704 sub_node.completeOne();
697705 try builder.rewriteFunctions(parser, binary.*, info);
706 sub_node.completeOne();
698707 try builder.emitNewEntryPoints(info);
708 sub_node.completeOne();
699709 try builder.finalize(parser.a, binary);
700710}
src/link/SpirV/prune_unused.zig+9-1
......@@ -255,7 +255,11 @@ fn removeIdsFromMap(a: Allocator, map: anytype, info: ModuleInfo, alive_marker:
255255 }
256256}
257257
258pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
258pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule, progress: *std.Progress.Node) !void {
259 var sub_node = progress.start("Prune unused IDs", 0);
260 sub_node.activate();
261 defer sub_node.end();
262
259263 var arena = std.heap.ArenaAllocator.init(parser.a);
260264 defer arena.deinit();
261265 const a = arena.allocator();
......@@ -285,9 +289,13 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule) !void {
285289
286290 var section = Section{};
287291
292 sub_node.setEstimatedTotalItems(binary.instructions.len);
293
288294 var new_functions_section: ?usize = null;
289295 var it = binary.iterateInstructions();
290296 skip: while (it.next()) |inst| {
297 defer sub_node.setCompletedItems(inst.offset);
298
291299 const inst_spec = parser.getInstSpec(inst.opcode).?;
292300
293301 reemit: {
test/behavior/destructure.zig-2
......@@ -23,8 +23,6 @@ test "simple destructure" {
2323}
2424
2525test "destructure with comptime syntax" {
26 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
27
2826 const S = struct {
2927 fn doTheTest() !void {
3028 {
test/behavior/fn.zig-1
......@@ -181,7 +181,6 @@ test "function with complex callconv and return type expressions" {
181181 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
182182 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
183183 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
184 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
185184
186185 try expect(fComplexCallconvRet(3).x == 9);
187186}
test/behavior/generics.zig-1
......@@ -447,7 +447,6 @@ test "return type of generic function is function pointer" {
447447
448448test "coerced function body has inequal value with its uncoerced body" {
449449 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
450 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
451450
452451 const S = struct {
453452 const A = B(i32, c);
test/behavior/math.zig-2
......@@ -12,7 +12,6 @@ const math = std.math;
1212test "assignment operators" {
1313 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1414 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
15 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1615
1716 var i: u32 = 0;
1817 i += 5;
......@@ -188,7 +187,6 @@ test "@ctz vectors" {
188187 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
189188 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
190189 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
191 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
192190
193191 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
194192 // This regressed with LLVM 14:
test/behavior/switch.zig-2
......@@ -850,8 +850,6 @@ test "inline switch range that includes the maximum value of the switched type"
850850}
851851
852852test "nested break ignores switch conditions and breaks instead" {
853 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
854
855853 const S = struct {
856854 fn register_to_address(ident: []const u8) !u8 {
857855 const reg: u8 = if (std.mem.eql(u8, ident, "zero")) 0x00 else blk: {
test/behavior/union.zig-1
......@@ -1750,7 +1750,6 @@ test "reinterpret extern union" {
17501750 // https://github.com/ziglang/zig/issues/19389
17511751 return error.SkipZigTest;
17521752 }
1753 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
17541753
17551754 const U = extern union {
17561755 foo: u8,
test/behavior/vector.zig-2
......@@ -76,7 +76,6 @@ test "vector int operators" {
7676 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
7777 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
7878 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
79 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
8079
8180 const S = struct {
8281 fn doTheTest() !void {
......@@ -1037,7 +1036,6 @@ test "multiplication-assignment operator with an array operand" {
10371036 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10381037 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10391038 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1040 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
10411039
10421040 const S = struct {
10431041 fn doTheTest() !void {