authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-04-05 00:30:06+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-04-06 13:37:39+02:00
log97a67762ba1fcc363656a59af10a3031332cbd62
tree7e333b5d505b6886dae97ca1f1a734690c588712
parent188922a5448417d1939023b1eab7f70fa1953dde
signaturebadge-check Signed by SSH key SHA256:ZS52FNyUv2WUXvO4njmVaFVO46RHojFuOrxRc4LuKzg

spirv: remove cache usage for types


4 files changed, 555 insertions(+), 496 deletions(-)

src/codegen/spirv.zig+417-433
......@@ -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,16 +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);
43
4433const 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);
4538
4639const ControlFlow = union(enum) {
4740 const Structured = struct {
......@@ -164,17 +157,17 @@ pub const Object = struct {
164157 /// A map of Zig InternPool indices for anonymous decls to SPIR-V decl indices.
165158 anon_decl_link: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, SpvModule.Decl.Index) = .{},
166159
167 /// A map that maps AIR intern pool indices to SPIR-V cache references (which
168 /// is basically the same thing except for SPIR-V).
169 /// This map is typically only used for structures that are deemed heavy enough
170 /// that it is worth to store them here. The SPIR-V module also interns types,
171 /// and so the main purpose of this map is to avoid recomputation and to
172 /// cache extra information about the type rather than to aid in validity
173 /// of the SPIR-V module.
174 type_map: TypeMap = .{},
175
160 /// A map that maps AIR intern pool indices to SPIR-V result-ids.
176161 intern_map: InternMap = .{},
177162
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 = .{},
170
178171 pub fn init(gpa: Allocator) Object {
179172 return .{
180173 .gpa = gpa,
......@@ -186,8 +179,8 @@ pub const Object = struct {
186179 self.spv.deinit();
187180 self.decl_link.deinit(self.gpa);
188181 self.anon_decl_link.deinit(self.gpa);
189 self.type_map.deinit(self.gpa);
190182 self.intern_map.deinit(self.gpa);
183 self.ptr_types.deinit(self.gpa);
191184 }
192185
193186 fn genDecl(
......@@ -209,8 +202,8 @@ pub const Object = struct {
209202 .decl_index = decl_index,
210203 .air = air,
211204 .liveness = liveness,
212 .type_map = &self.type_map,
213205 .intern_map = &self.intern_map,
206 .ptr_types = &self.ptr_types,
214207 .control_flow = switch (structured_cfg) {
215208 true => .{ .structured = .{} },
216209 false => .{ .unstructured = .{} },
......@@ -315,15 +308,12 @@ const DeclGen = struct {
315308 /// A map keeping track of which instruction generated which result-id.
316309 inst_results: InstMap = .{},
317310
318 /// A map that maps AIR intern pool indices to SPIR-V cache references.
319 /// See Object.type_map
320 type_map: *TypeMap,
321
311 /// A map that maps AIR intern pool indices to SPIR-V result-ids.
312 /// See `Object.intern_map`.
322313 intern_map: *InternMap,
323314
324 /// Child types of pointers that are currently in progress of being resolved. If a pointer
325 /// is already in this map, its recursive.
326 wip_pointers: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, CacheRef) = .{},
315 /// Module's pointer types, see `Object.ptr_types`.
316 ptr_types: *PtrTypeMap,
327317
328318 /// This field keeps track of the current state wrt structured or unstructured control flow.
329319 control_flow: ControlFlow,
......@@ -410,7 +400,6 @@ const DeclGen = struct {
410400 pub fn deinit(self: *DeclGen) void {
411401 self.args.deinit(self.gpa);
412402 self.inst_results.deinit(self.gpa);
413 self.wip_pointers.deinit(self.gpa);
414403 self.control_flow.deinit(self.gpa);
415404 self.func.deinit(self.gpa);
416405 }
......@@ -460,7 +449,7 @@ const DeclGen = struct {
460449
461450 const mod = self.module;
462451 const ty = Type.fromInterned(mod.intern_pool.typeOf(val));
463 const decl_ptr_ty_ref = try self.ptrType(ty, .Generic);
452 const decl_ptr_ty_id = try self.ptrType(ty, .Generic);
464453
465454 const spv_decl_index = blk: {
466455 const entry = try self.object.anon_decl_link.getOrPut(self.object.gpa, .{ val, .Function });
......@@ -468,7 +457,7 @@ const DeclGen = struct {
468457 try self.addFunctionDep(entry.value_ptr.*, .Function);
469458
470459 const result_id = self.spv.declPtr(entry.value_ptr.*).result_id;
471 return try self.castToGeneric(self.typeId(decl_ptr_ty_ref), result_id);
460 return try self.castToGeneric(decl_ptr_ty_id, result_id);
472461 }
473462
474463 const spv_decl_index = try self.spv.allocDecl(.invocation_global);
......@@ -496,19 +485,14 @@ const DeclGen = struct {
496485 self.func = .{};
497486 defer self.func.deinit(self.gpa);
498487
499 const void_ty_ref = try self.resolveType(Type.void, .direct);
500 const initializer_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{
501 .return_type = void_ty_ref,
502 .parameters = &.{},
503 } });
488 const initializer_proto_ty_id = try self.functionType(Type.void, &.{});
504489
505490 const initializer_id = self.spv.allocId();
506
507491 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
508 .id_result_type = self.typeId(void_ty_ref),
492 .id_result_type = try self.resolveType(Type.void, .direct),
509493 .id_result = initializer_id,
510494 .function_control = .{},
511 .function_type = self.typeId(initializer_proto_ty_ref),
495 .function_type = initializer_proto_ty_id,
512496 });
513497 const root_block_id = self.spv.allocId();
514498 try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{
......@@ -528,9 +512,9 @@ const DeclGen = struct {
528512
529513 try self.spv.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)});
530514
531 const fn_decl_ptr_ty_ref = try self.ptrType(ty, .Function);
515 const fn_decl_ptr_ty_id = try self.ptrType(ty, .Function);
532516 try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{
533 .id_result_type = self.typeId(fn_decl_ptr_ty_ref),
517 .id_result_type = fn_decl_ptr_ty_id,
534518 .id_result = result_id,
535519 .set = try self.spv.importInstructionSet(.zig),
536520 .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere...
......@@ -538,7 +522,7 @@ const DeclGen = struct {
538522 });
539523 }
540524
541 return try self.castToGeneric(self.typeId(decl_ptr_ty_ref), result_id);
525 return try self.castToGeneric(decl_ptr_ty_id, result_id);
542526 }
543527
544528 fn addFunctionDep(self: *DeclGen, decl_index: SpvModule.Decl.Index, storage_class: StorageClass) !void {
......@@ -712,7 +696,7 @@ const DeclGen = struct {
712696 return try self.constInt(Type.u1, @intFromBool(value), .indirect);
713697 },
714698 .direct => {
715 const result_ty_id = try self.resolveType2(Type.bool, .direct);
699 const result_ty_id = try self.resolveType(Type.bool, .direct);
716700 const result_id = self.spv.allocId();
717701 const operands = .{
718702 .id_result_type = result_ty_id,
......@@ -751,7 +735,7 @@ const DeclGen = struct {
751735 else
752736 bits & (@as(u64, 1) << @intCast(backing_bits)) - 1;
753737
754 const result_ty_id = try self.resolveType2(scalar_ty, repr);
738 const result_ty_id = try self.resolveType(scalar_ty, repr);
755739 const result_id = self.spv.allocId();
756740
757741 const section = &self.spv.sections.types_globals_constants;
......@@ -779,7 +763,7 @@ const DeclGen = struct {
779763 defer self.gpa.free(ids);
780764 @memset(ids, result_id);
781765
782 const vec_ty_id = try self.resolveType2(ty, repr);
766 const vec_ty_id = try self.resolveType(ty, repr);
783767 const vec_result_id = self.spv.allocId();
784768 try self.func.body.emit(self.spv.gpa, .OpCompositeConstruct, .{
785769 .id_result_type = vec_ty_id,
......@@ -802,8 +786,8 @@ const DeclGen = struct {
802786 // TODO: Make this OpCompositeConstruct when we can
803787 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });
804788 for (constituents, types, 0..) |constitent_id, member_ty, index| {
805 const ptr_member_ty_ref = try self.ptrType(member_ty, .Function);
806 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))});
807791 try self.func.body.emit(self.spv.gpa, .OpStore, .{
808792 .pointer = ptr_id,
809793 .object = constitent_id,
......@@ -824,9 +808,9 @@ const DeclGen = struct {
824808 // TODO: Make this OpCompositeConstruct when we can
825809 const mod = self.module;
826810 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });
827 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);
828812 for (constituents, 0..) |constitent_id, index| {
829 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))});
830814 try self.func.body.emit(self.spv.gpa, .OpStore, .{
831815 .pointer = ptr_id,
832816 .object = constitent_id,
......@@ -848,9 +832,9 @@ const DeclGen = struct {
848832 // TODO: Make this OpCompositeConstruct when we can
849833 const mod = self.module;
850834 const ptr_composite_id = try self.alloc(ty, .{ .storage_class = .Function });
851 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);
852836 for (constituents, 0..) |constitent_id, index| {
853 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))});
854838 try self.func.body.emit(self.spv.gpa, .OpStore, .{
855839 .pointer = ptr_id,
856840 .object = constitent_id,
......@@ -876,8 +860,7 @@ const DeclGen = struct {
876860
877861 const mod = self.module;
878862 const target = self.getTarget();
879 const result_ty_ref = try self.resolveType(ty, repr);
880 const result_ty_id = self.typeId(result_ty_ref);
863 const result_ty_id = try self.resolveType(ty, repr);
881864 const ip = &mod.intern_pool;
882865
883866 log.debug("lowering constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(mod) });
......@@ -1033,7 +1016,7 @@ const DeclGen = struct {
10331016 const payload_id = if (maybe_payload_val) |payload_val|
10341017 try self.constant(payload_ty, payload_val, .indirect)
10351018 else
1036 try self.spv.constUndef(try self.resolveType2(payload_ty, .indirect));
1019 try self.spv.constUndef(try self.resolveType(payload_ty, .indirect));
10371020
10381021 return try self.constructStruct(
10391022 ty,
......@@ -1134,8 +1117,9 @@ const DeclGen = struct {
11341117 }
11351118
11361119 fn constantPtr(self: *DeclGen, ptr_ty: Type, ptr_val: Value) Error!IdRef {
1137 const result_ty_id = try self.resolveType2(ptr_ty, .direct);
1138 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);
11391123 const mod = self.module;
11401124
11411125 if (ptr_val.isUndef(mod)) return self.spv.constUndef(result_ty_id);
......@@ -1149,7 +1133,7 @@ const DeclGen = struct {
11491133 // that is not implemented by Mesa yet. Therefore, just generate it
11501134 // as a runtime operation.
11511135 try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{
1152 .id_result_type = self.typeId(result_ty_ref),
1136 .id_result_type = result_ty_id,
11531137 .id_result = ptr_id,
11541138 .integer_value = try self.constant(Type.usize, Value.fromInterned(int), .direct),
11551139 });
......@@ -1167,16 +1151,17 @@ const DeclGen = struct {
11671151
11681152 // TODO: Can we consolidate this in ptrElemPtr?
11691153 const elem_ty = parent_ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
1170 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)));
11711155
1172 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) {
11731158 return elem_ptr_id;
11741159 }
11751160 // This may happen when we have pointer-to-array and the result is
11761161 // another pointer-to-array instead of a pointer-to-element.
11771162 const result_id = self.spv.allocId();
11781163 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
1179 .id_result_type = self.typeId(result_ty_ref),
1164 .id_result_type = result_ty_id,
11801165 .id_result = result_id,
11811166 .operand = elem_ptr_id,
11821167 });
......@@ -1200,7 +1185,7 @@ const DeclGen = struct {
12001185
12011186 const mod = self.module;
12021187 const ip = &mod.intern_pool;
1203 const ty_ref = try self.resolveType(ty, .direct);
1188 const ty_id = try self.resolveType(ty, .direct);
12041189 const decl_val = anon_decl.val;
12051190 const decl_ty = Type.fromInterned(ip.typeOf(decl_val));
12061191
......@@ -1215,7 +1200,7 @@ const DeclGen = struct {
12151200 // const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn;
12161201 if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) {
12171202 // Pointer to nothing - return undefoined
1218 return self.spv.constUndef(self.typeId(ty_ref));
1203 return self.spv.constUndef(ty_id);
12191204 }
12201205
12211206 if (decl_ty.zigTypeTag(mod) == .Fn) {
......@@ -1224,14 +1209,14 @@ const DeclGen = struct {
12241209
12251210 // Anon decl refs are always generic.
12261211 assert(ty.ptrAddressSpace(mod) == .generic);
1227 const decl_ptr_ty_ref = try self.ptrType(decl_ty, .Generic);
1212 const decl_ptr_ty_id = try self.ptrType(decl_ty, .Generic);
12281213 const ptr_id = try self.resolveAnonDecl(decl_val);
12291214
1230 if (decl_ptr_ty_ref != ty_ref) {
1215 if (decl_ptr_ty_id != ty_id) {
12311216 // Differing pointer types, insert a cast.
12321217 const casted_ptr_id = self.spv.allocId();
12331218 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
1234 .id_result_type = self.typeId(ty_ref),
1219 .id_result_type = ty_id,
12351220 .id_result = casted_ptr_id,
12361221 .operand = ptr_id,
12371222 });
......@@ -1243,8 +1228,7 @@ const DeclGen = struct {
12431228
12441229 fn constantDeclRef(self: *DeclGen, ty: Type, decl_index: InternPool.DeclIndex) !IdRef {
12451230 const mod = self.module;
1246 const ty_ref = try self.resolveType(ty, .direct);
1247 const ty_id = self.typeId(ty_ref);
1231 const ty_id = try self.resolveType(ty, .direct);
12481232 const decl = mod.declPtr(decl_index);
12491233
12501234 switch (mod.intern_pool.indexToKey(decl.val.ip_index)) {
......@@ -1273,14 +1257,14 @@ const DeclGen = struct {
12731257 const final_storage_class = self.spvStorageClass(decl.@"addrspace");
12741258 try self.addFunctionDep(spv_decl_index, final_storage_class);
12751259
1276 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);
12771261
12781262 const ptr_id = switch (final_storage_class) {
1279 .Generic => try self.castToGeneric(self.typeId(decl_ptr_ty_ref), decl_id),
1263 .Generic => try self.castToGeneric(decl_ptr_ty_id, decl_id),
12801264 else => decl_id,
12811265 };
12821266
1283 if (decl_ptr_ty_ref != ty_ref) {
1267 if (decl_ptr_ty_id != ty_id) {
12841268 // Differing pointer types, insert a cast.
12851269 const casted_ptr_id = self.spv.allocId();
12861270 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
......@@ -1295,36 +1279,18 @@ const DeclGen = struct {
12951279 }
12961280
12971281 // Turn a Zig type's name into a cache reference.
1298 fn resolveTypeName(self: *DeclGen, ty: Type) !CacheString {
1282 fn resolveTypeName(self: *DeclGen, ty: Type) ![]const u8 {
12991283 var name = std.ArrayList(u8).init(self.gpa);
13001284 defer name.deinit();
13011285 try ty.print(name.writer(), self.module);
1302 return try self.spv.resolveString(name.items);
1303 }
1304
1305 /// Turn a Zig type into a SPIR-V Type, and return its type result-id.
1306 fn resolveTypeId(self: *DeclGen, ty: Type) !IdResultType {
1307 const type_ref = try self.resolveType(ty, .direct);
1308 return self.spv.resultId(type_ref);
1309 }
1310
1311 /// Turn a Zig type into a SPIR-V Type result-id.
1312 /// This function represents the "new interface", where types handled only
1313 /// with Type and IdResult, and CacheRef is not used. Prefer this for now.
1314 fn resolveType2(self: *DeclGen, ty: Type, repr: Repr) !IdResult {
1315 const type_ref = try self.resolveType(ty, repr);
1316 return self.typeId(type_ref);
1317 }
1318
1319 fn typeId(self: *DeclGen, ty_ref: CacheRef) IdRef {
1320 return self.spv.resultId(ty_ref);
1286 return try name.toOwnedSlice();
13211287 }
13221288
13231289 /// Create an integer type suitable for storing at least 'bits' bits.
13241290 /// The integer type that is returned by this function is the type that is used to perform
13251291 /// actual operations (as well as store) a Zig type of a particular number of bits. To create
13261292 /// a type with an exact size, use SpvModule.intType.
1327 fn intType(self: *DeclGen, signedness: std.builtin.Signedness, bits: u16) !CacheRef {
1293 fn intType(self: *DeclGen, signedness: std.builtin.Signedness, bits: u16) !IdRef {
13281294 const backing_bits = self.backingIntBits(bits) orelse {
13291295 // TODO: Integers too big for any native type are represented as "composite integers":
13301296 // An array of largestSupportedIntBits.
......@@ -1339,31 +1305,69 @@ const DeclGen = struct {
13391305 return self.spv.intType(.unsigned, backing_bits);
13401306 }
13411307
1342 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !CacheRef {
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;
1319 }
1320
1321 fn ptrType(self: *DeclGen, child_ty: Type, storage_class: StorageClass) !IdRef {
13431322 const key = .{ child_ty.toIntern(), storage_class };
1344 const entry = try self.wip_pointers.getOrPut(self.gpa, key);
1323 const entry = try self.ptr_types.getOrPut(self.gpa, key);
13451324 if (entry.found_existing) {
1346 const fwd_ref = entry.value_ptr.*;
1347 try self.spv.cache.recursive_ptrs.put(self.spv.gpa, fwd_ref, {});
1348 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;
13491334 }
13501335
1351 const fwd_ref = try self.spv.resolve(.{ .fwd_ptr_type = .{
1352 .zig_child_type = child_ty.toIntern(),
1353 .storage_class = storage_class,
1354 } });
1355 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 };
13561341
1357 const child_ty_ref = try self.resolveType(child_ty, .indirect);
1358 _ = 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,
13591346 .storage_class = storage_class,
1360 .child_type = child_ty_ref,
1361 .fwd = fwd_ref,
1362 } });
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??
13631355
1364 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);
1358
1359 for (param_types, param_ids) |param_ty, *param_id| {
1360 param_id.* = try self.resolveType(param_ty, .direct);
1361 }
13651362
1366 return fwd_ref;
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;
13671371 }
13681372
13691373 /// Generate a union type. Union types are always generated with the
......@@ -1384,7 +1388,7 @@ const DeclGen = struct {
13841388 /// padding: [padding_size]u8,
13851389 /// }
13861390 /// If any of the fields' size is 0, it will be omitted.
1387 fn resolveUnionType(self: *DeclGen, ty: Type) !CacheRef {
1391 fn resolveUnionType(self: *DeclGen, ty: Type) !IdRef {
13881392 const mod = self.module;
13891393 const ip = &mod.intern_pool;
13901394 const union_obj = mod.typeToUnion(ty).?;
......@@ -1399,48 +1403,43 @@ const DeclGen = struct {
13991403 return try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect);
14001404 }
14011405
1402 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1403
1404 var member_types: [4]CacheRef = undefined;
1405 var member_names: [4]CacheString = undefined;
1406 var member_types: [4]IdRef = undefined;
1407 var member_names: [4][]const u8 = undefined;
14061408
1407 const u8_ty_ref = try self.resolveType(Type.u8, .direct); // 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?
14081410
14091411 if (layout.tag_size != 0) {
1410 const tag_ty_ref = try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect);
1411 member_types[layout.tag_index] = tag_ty_ref;
1412 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)";
14131415 }
14141416
14151417 if (layout.payload_size != 0) {
1416 const payload_ty_ref = try self.resolveType(layout.payload_ty, .indirect);
1417 member_types[layout.payload_index] = payload_ty_ref;
1418 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)";
14191421 }
14201422
14211423 if (layout.payload_padding_size != 0) {
1422 const payload_padding_ty_ref = try self.spv.arrayType(@intCast(layout.payload_padding_size), u8_ty_ref);
1423 member_types[layout.payload_padding_index] = payload_padding_ty_ref;
1424 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)";
14251427 }
14261428
14271429 if (layout.padding_size != 0) {
1428 const padding_ty_ref = try self.spv.arrayType(@intCast(layout.padding_size), u8_ty_ref);
1429 member_types[layout.padding_index] = padding_ty_ref;
1430 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)";
14311433 }
14321434
1433 const ty_ref = try self.spv.resolve(.{ .struct_type = .{
1434 .name = try self.resolveTypeName(ty),
1435 .member_types = member_types[0..layout.total_fields],
1436 .member_names = member_names[0..layout.total_fields],
1437 } });
1438
1439 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1440 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;
14411440 }
14421441
1443 fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !CacheRef {
1442 fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !IdRef {
14441443 const mod = self.module;
14451444 if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
14461445 // If the return type is an error set or an error union, then we make this
......@@ -1457,25 +1456,45 @@ const DeclGen = struct {
14571456 }
14581457
14591458 /// Turn a Zig type into a SPIR-V Type, and return a reference to it.
1460 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 {
14611470 const mod = self.module;
14621471 const ip = &mod.intern_pool;
14631472 log.debug("resolveType: ty = {}", .{ty.fmt(mod)});
14641473 const target = self.getTarget();
1474
1475 const section = &self.spv.sections.types_globals_constants;
1476
14651477 switch (ty.zigTypeTag(mod)) {
14661478 .NoReturn => {
14671479 assert(repr == .direct);
1468 return try self.spv.resolve(.void_type);
1480 return try self.spv.voidType();
14691481 },
14701482 .Void => switch (repr) {
1471 .direct => return try self.spv.resolve(.void_type),
1483 .direct => {
1484 return try self.spv.voidType();
1485 },
14721486 // Pointers to void
1473 .indirect => return try self.spv.resolve(.{ .opaque_type = .{
1474 .name = try self.spv.resolveString("void"),
1475 } }),
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 },
14761495 },
14771496 .Bool => switch (repr) {
1478 .direct => return try self.spv.resolve(.bool_type),
1497 .direct => return try self.spv.boolType(),
14791498 .indirect => return try self.resolveType(Type.u1, .indirect),
14801499 },
14811500 .Int => {
......@@ -1484,15 +1503,18 @@ const DeclGen = struct {
14841503 // Some times, the backend will be asked to generate a pointer to i0. OpTypeInt
14851504 // with 0 bits is invalid, so return an opaque type in this case.
14861505 assert(repr == .indirect);
1487 return try self.spv.resolve(.{ .opaque_type = .{
1488 .name = try self.spv.resolveString("u0"),
1489 } });
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;
14901512 }
14911513 return try self.intType(int_info.signedness, int_info.bits);
14921514 },
14931515 .Enum => {
14941516 const tag_ty = ty.intTagType(mod);
1495 return self.resolveType(tag_ty, repr);
1517 return try self.resolveType(tag_ty, repr);
14961518 },
14971519 .Float => {
14981520 // We can (and want) not really emulate floating points with other floating point types like with the integer types,
......@@ -1510,27 +1532,29 @@ const DeclGen = struct {
15101532 return self.fail("Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits});
15111533 }
15121534
1513 return try self.spv.resolve(.{ .float_type = .{ .bits = bits } });
1535 return try self.spv.floatType(bits);
15141536 },
15151537 .Array => {
1516 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1517
15181538 const elem_ty = ty.childType(mod);
1519 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
1539 const elem_ty_id = try self.resolveType(elem_ty, .indirect);
15201540 const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel(mod)) orelse {
15211541 return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)});
15221542 };
1523 const ty_ref = if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) blk: {
1543
1544 if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) {
15241545 // The size of the array would be 0, but that is not allowed in SPIR-V.
15251546 // This path can be reached when the backend is asked to generate a pointer to
15261547 // an array of some zero-bit type. This should always be an indirect path.
15271548 assert(repr == .indirect);
15281549
15291550 // We cannot use the child type here, so just use an opaque type.
1530 break :blk try self.spv.resolve(.{ .opaque_type = .{
1531 .name = try self.spv.resolveString("zero-sized array"),
1532 } });
1533 } 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) {
15341558 // The size of the array would be 0, but that is not allowed in SPIR-V.
15351559 // This path can be reached for example when there is a slicing of a pointer
15361560 // that produces a zero-length array. In all cases where this type can be generated,
......@@ -1540,16 +1564,13 @@ const DeclGen = struct {
15401564 // In this case, we have an array of a non-zero sized type. In this case,
15411565 // generate an array of 1 element instead, so that ptr_elem_ptr instructions
15421566 // can be lowered to ptrAccessChain instead of manually performing the math.
1543 break :blk try self.spv.arrayType(1, elem_ty_ref);
1544 } else try self.spv.arrayType(total_len, elem_ty_ref);
1545
1546 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1547 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 }
15481571 },
15491572 .Fn => switch (repr) {
15501573 .direct => {
1551 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1552
15531574 const fn_info = mod.typeToFunc(ty).?;
15541575
15551576 comptime assert(zig_call_abi_ver == 3);
......@@ -1562,25 +1583,28 @@ const DeclGen = struct {
15621583 if (fn_info.is_var_args)
15631584 return self.fail("VarArgs functions are unsupported for SPIR-V", .{});
15641585
1565 const param_ty_refs = try self.gpa.alloc(CacheRef, fn_info.param_types.len);
1566 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);
15671589 var param_index: usize = 0;
15681590 for (fn_info.param_types.get(ip)) |param_ty_index| {
15691591 const param_ty = Type.fromInterned(param_ty_index);
15701592 if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue;
15711593
1572 param_ty_refs[param_index] = try self.resolveType(param_ty, .direct);
1594 param_ty_ids[param_index] = try self.resolveType(param_ty, .direct);
15731595 param_index += 1;
15741596 }
1575 const return_ty_ref = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type));
15761597
1577 const ty_ref = try self.spv.resolve(.{ .function_type = .{
1578 .return_type = return_ty_ref,
1579 .parameters = param_ty_refs[0..param_index],
1580 } });
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 });
15811606
1582 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1583 return ty_ref;
1607 return result_id;
15841608 },
15851609 .indirect => {
15861610 // TODO: Represent function pointers properly.
......@@ -1591,46 +1615,35 @@ const DeclGen = struct {
15911615 .Pointer => {
15921616 const ptr_info = ty.ptrInfo(mod);
15931617
1594 // Note: Don't cache this pointer type, it would mess up the recursive pointer functionality
1595 // in ptrType()!
1596
15971618 const storage_class = self.spvStorageClass(ptr_info.flags.address_space);
1598 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);
15991620
16001621 if (ptr_info.flags.size != .Slice) {
1601 return ptr_ty_ref;
1622 return ptr_ty_id;
16021623 }
16031624
1604 const size_ty_ref = try self.resolveType(Type.usize, .direct);
1605 return self.spv.resolve(.{ .struct_type = .{
1606 .member_types = &.{ ptr_ty_ref, size_ty_ref },
1607 .member_names = &.{
1608 try self.spv.resolveString("ptr"),
1609 try self.spv.resolveString("len"),
1610 },
1611 } });
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 );
16121630 },
16131631 .Vector => {
1614 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1615
16161632 const elem_ty = ty.childType(mod);
1617 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);
16181635 const len = ty.vectorLen(mod);
16191636
1620 const ty_ref = if (self.isVector(ty))
1621 try self.spv.vectorType(len, elem_ty_ref)
1622 else
1623 try self.spv.arrayType(len, elem_ty_ref);
1624
1625 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1626 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 }
16271642 },
16281643 .Struct => {
1629 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1630
16311644 const struct_type = switch (ip.indexToKey(ty.toIntern())) {
16321645 .anon_struct_type => |tuple| {
1633 const member_types = try self.gpa.alloc(CacheRef, tuple.values.len);
1646 const member_types = try self.gpa.alloc(IdRef, tuple.values.len);
16341647 defer self.gpa.free(member_types);
16351648
16361649 var member_index: usize = 0;
......@@ -1641,13 +1654,11 @@ const DeclGen = struct {
16411654 member_index += 1;
16421655 }
16431656
1644 const ty_ref = try self.spv.resolve(.{ .struct_type = .{
1645 .name = try self.resolveTypeName(ty),
1646 .member_types = member_types[0..member_index],
1647 } });
1648
1649 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1650 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;
16511662 },
16521663 .struct_type => ip.loadStructType(ty.toIntern()),
16531664 else => unreachable,
......@@ -1657,10 +1668,10 @@ const DeclGen = struct {
16571668 return try self.resolveType(Type.fromInterned(struct_type.backingIntType(ip).*), .direct);
16581669 }
16591670
1660 var member_types = std.ArrayList(CacheRef).init(self.gpa);
1671 var member_types = std.ArrayList(IdRef).init(self.gpa);
16611672 defer member_types.deinit();
16621673
1663 var member_names = std.ArrayList(CacheString).init(self.gpa);
1674 var member_names = std.ArrayList([]const u8).init(self.gpa);
16641675 defer member_names.deinit();
16651676
16661677 var it = struct_type.iterateRuntimeOrder(ip);
......@@ -1674,17 +1685,14 @@ const DeclGen = struct {
16741685 const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse
16751686 try ip.getOrPutStringFmt(mod.gpa, "{d}", .{field_index});
16761687 try member_types.append(try self.resolveType(field_ty, .indirect));
1677 try member_names.append(try self.spv.resolveString(ip.stringToSlice(field_name)));
1688 try member_names.append(ip.stringToSlice(field_name));
16781689 }
16791690
1680 const ty_ref = try self.spv.resolve(.{ .struct_type = .{
1681 .name = try self.resolveTypeName(ty),
1682 .member_types = member_types.items,
1683 .member_names = member_names.items,
1684 } });
1685
1686 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1687 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;
16881696 },
16891697 .Optional => {
16901698 const payload_ty = ty.optionalChild(mod);
......@@ -1695,77 +1703,58 @@ const DeclGen = struct {
16951703 return try self.resolveType(Type.bool, .indirect);
16961704 }
16971705
1698 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
1706 const payload_ty_id = try self.resolveType(payload_ty, .indirect);
16991707 if (ty.optionalReprIsPayload(mod)) {
17001708 // Optional is actually a pointer or a slice.
1701 return payload_ty_ref;
1709 return payload_ty_id;
17021710 }
17031711
1704 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1705
1706 const bool_ty_ref = try self.resolveType(Type.bool, .indirect);
1707
1708 const ty_ref = try self.spv.resolve(.{ .struct_type = .{
1709 .member_types = &.{ payload_ty_ref, bool_ty_ref },
1710 .member_names = &.{
1711 try self.spv.resolveString("payload"),
1712 try self.spv.resolveString("valid"),
1713 },
1714 } });
1712 const bool_ty_id = try self.resolveType(Type.bool, .indirect);
17151713
1716 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1717 return ty_ref;
1714 return try self.spv.structType(
1715 &.{ payload_ty_id, bool_ty_id },
1716 &.{ "payload", "valid" },
1717 );
17181718 },
17191719 .Union => return try self.resolveUnionType(ty),
17201720 .ErrorSet => return try self.resolveType(Type.u16, repr),
17211721 .ErrorUnion => {
17221722 const payload_ty = ty.errorUnionPayload(mod);
1723 const error_ty_ref = try self.resolveType(Type.anyerror, .indirect);
1723 const error_ty_id = try self.resolveType(Type.anyerror, .indirect);
17241724
17251725 const eu_layout = self.errorUnionLayout(payload_ty);
17261726 if (!eu_layout.payload_has_bits) {
1727 return error_ty_ref;
1727 return error_ty_id;
17281728 }
17291729
1730 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1731
1732 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
1730 const payload_ty_id = try self.resolveType(payload_ty, .indirect);
17331731
1734 var member_types: [2]CacheRef = undefined;
1735 var member_names: [2]CacheString = undefined;
1732 var member_types: [2]IdRef = undefined;
1733 var member_names: [2][]const u8 = undefined;
17361734 if (eu_layout.error_first) {
17371735 // Put the error first
1738 member_types = .{ error_ty_ref, payload_ty_ref };
1739 member_names = .{
1740 try self.spv.resolveString("error"),
1741 try self.spv.resolveString("payload"),
1742 };
1736 member_types = .{ error_ty_id, payload_ty_id };
1737 member_names = .{ "error", "payload" };
17431738 // TODO: ABI padding?
17441739 } else {
17451740 // Put the payload first.
1746 member_types = .{ payload_ty_ref, error_ty_ref };
1747 member_names = .{
1748 try self.spv.resolveString("payload"),
1749 try self.spv.resolveString("error"),
1750 };
1741 member_types = .{ payload_ty_id, error_ty_id };
1742 member_names = .{ "payload", "error" };
17511743 // TODO: ABI padding?
17521744 }
17531745
1754 const ty_ref = try self.spv.resolve(.{ .struct_type = .{
1755 .name = try self.resolveTypeName(ty),
1756 .member_types = &member_types,
1757 .member_names = &member_names,
1758 } });
1759
1760 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1761 return ty_ref;
1746 return try self.spv.structType(&member_types, &member_names);
17621747 },
17631748 .Opaque => {
1764 return try self.spv.resolve(.{
1765 .opaque_type = .{
1766 .name = .none, // TODO
1767 },
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,
17681756 });
1757 return result_id;
17691758 },
17701759
17711760 .Null,
......@@ -1773,9 +1762,10 @@ const DeclGen = struct {
17731762 .EnumLiteral,
17741763 .ComptimeFloat,
17751764 .ComptimeInt,
1765 .Type,
17761766 => unreachable, // Must be comptime.
17771767
1778 else => |tag| return self.todo("Implement zig type '{}'", .{tag}),
1768 .Frame, .AnyFrame => unreachable, // TODO
17791769 }
17801770 }
17811771
......@@ -1924,7 +1914,6 @@ const DeclGen = struct {
19241914 result_ty: Type,
19251915 ty: Type,
19261916 /// Always in direct representation.
1927 ty_ref: CacheRef,
19281917 ty_id: IdRef,
19291918 /// True if the input is an array type.
19301919 is_array: bool,
......@@ -1984,14 +1973,13 @@ const DeclGen = struct {
19841973 @memset(results, undefined);
19851974
19861975 const ty = if (is_array) result_ty.scalarType(mod) else result_ty;
1987 const ty_ref = try self.resolveType(ty, .direct);
1976 const ty_id = try self.resolveType(ty, .direct);
19881977
19891978 return .{
19901979 .dg = self,
19911980 .result_ty = result_ty,
19921981 .ty = ty,
1993 .ty_ref = ty_ref,
1994 .ty_id = self.typeId(ty_ref),
1982 .ty_id = ty_id,
19951983 .is_array = is_array,
19961984 .results = results,
19971985 };
......@@ -2018,16 +2006,13 @@ const DeclGen = struct {
20182006 /// TODO is to also write out the error as a function call parameter, and to somehow fetch
20192007 /// the name of an error in the text executor.
20202008 fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void {
2021 const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct);
2022 const ptr_anyerror_ty_ref = try self.ptrType(Type.anyerror, .CrossWorkgroup);
2023 const void_ty_ref = try self.resolveType(Type.void, .direct);
2024
2025 const kernel_proto_ty_ref = try self.spv.resolve(.{
2026 .function_type = .{
2027 .return_type = void_ty_ref,
2028 .parameters = &.{ptr_anyerror_ty_ref},
2029 },
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 },
20302013 });
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});
20312016
20322017 const test_id = self.spv.declPtr(spv_test_decl_index).result_id;
20332018
......@@ -2039,20 +2024,20 @@ const DeclGen = struct {
20392024
20402025 const section = &self.spv.sections.functions;
20412026 try section.emit(self.spv.gpa, .OpFunction, .{
2042 .id_result_type = self.typeId(void_ty_ref),
2027 .id_result_type = try self.resolveType(Type.void, .direct),
20432028 .id_result = kernel_id,
20442029 .function_control = .{},
2045 .function_type = self.typeId(kernel_proto_ty_ref),
2030 .function_type = kernel_proto_ty_id,
20462031 });
20472032 try section.emit(self.spv.gpa, .OpFunctionParameter, .{
2048 .id_result_type = self.typeId(ptr_anyerror_ty_ref),
2033 .id_result_type = ptr_anyerror_ty_id,
20492034 .id_result = p_error_id,
20502035 });
20512036 try section.emit(self.spv.gpa, .OpLabel, .{
20522037 .id_result = self.spv.allocId(),
20532038 });
20542039 try section.emit(self.spv.gpa, .OpFunctionCall, .{
2055 .id_result_type = self.typeId(anyerror_ty_ref),
2040 .id_result_type = anyerror_ty_id,
20562041 .id_result = error_id,
20572042 .function = test_id,
20582043 });
......@@ -2084,17 +2069,17 @@ const DeclGen = struct {
20842069 .func => {
20852070 assert(decl.typeOf(mod).zigTypeTag(mod) == .Fn);
20862071 const fn_info = mod.typeToFunc(decl.typeOf(mod)).?;
2087 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));
20882073
2089 const prototype_ty_ref = try self.resolveType(decl.typeOf(mod), .direct);
2074 const prototype_ty_id = try self.resolveType(decl.typeOf(mod), .direct);
20902075 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
2091 .id_result_type = self.typeId(return_ty_ref),
2076 .id_result_type = return_ty_id,
20922077 .id_result = result_id,
20932078 .function_control = switch (fn_info.cc) {
20942079 .Inline => .{ .Inline = true },
20952080 else => .{},
20962081 },
2097 .function_type = self.typeId(prototype_ty_ref),
2082 .function_type = prototype_ty_id,
20982083 });
20992084
21002085 comptime assert(zig_call_abi_ver == 3);
......@@ -2103,7 +2088,7 @@ const DeclGen = struct {
21032088 const param_ty = Type.fromInterned(param_ty_index);
21042089 if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue;
21052090
2106 const param_type_id = try self.resolveTypeId(param_ty);
2091 const param_type_id = try self.resolveType(param_ty, .direct);
21072092 const arg_result_id = self.spv.allocId();
21082093 try self.func.prologue.emit(self.spv.gpa, .OpFunctionParameter, .{
21092094 .id_result_type = param_type_id,
......@@ -2159,10 +2144,10 @@ const DeclGen = struct {
21592144 const final_storage_class = self.spvStorageClass(decl.@"addrspace");
21602145 assert(final_storage_class != .Generic); // These should be instance globals
21612146
2162 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);
21632148
21642149 try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{
2165 .id_result_type = self.typeId(ptr_ty_ref),
2150 .id_result_type = ptr_ty_id,
21662151 .id_result = result_id,
21672152 .storage_class = final_storage_class,
21682153 });
......@@ -2182,22 +2167,18 @@ const DeclGen = struct {
21822167
21832168 try self.spv.declareDeclDeps(spv_decl_index, &.{});
21842169
2185 const ptr_ty_ref = try self.ptrType(decl.typeOf(mod), .Function);
2170 const ptr_ty_id = try self.ptrType(decl.typeOf(mod), .Function);
21862171
21872172 if (maybe_init_val) |init_val| {
21882173 // TODO: Combine with resolveAnonDecl?
2189 const void_ty_ref = try self.resolveType(Type.void, .direct);
2190 const initializer_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{
2191 .return_type = void_ty_ref,
2192 .parameters = &.{},
2193 } });
2174 const initializer_proto_ty_id = try self.functionType(Type.void, &.{});
21942175
21952176 const initializer_id = self.spv.allocId();
21962177 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
2197 .id_result_type = self.typeId(void_ty_ref),
2178 .id_result_type = try self.resolveType(Type.void, .direct),
21982179 .id_result = initializer_id,
21992180 .function_control = .{},
2200 .function_type = self.typeId(initializer_proto_ty_ref),
2181 .function_type = initializer_proto_ty_id,
22012182 });
22022183
22032184 const root_block_id = self.spv.allocId();
......@@ -2220,7 +2201,7 @@ const DeclGen = struct {
22202201 try self.spv.debugNameFmt(initializer_id, "initializer of {s}", .{fqn});
22212202
22222203 try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{
2223 .id_result_type = self.typeId(ptr_ty_ref),
2204 .id_result_type = ptr_ty_id,
22242205 .id_result = result_id,
22252206 .set = try self.spv.importInstructionSet(.zig),
22262207 .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere...
......@@ -2228,7 +2209,7 @@ const DeclGen = struct {
22282209 });
22292210 } else {
22302211 try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{
2231 .id_result_type = self.typeId(ptr_ty_ref),
2212 .id_result_type = ptr_ty_id,
22322213 .id_result = result_id,
22332214 .set = try self.spv.importInstructionSet(.zig),
22342215 .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere...
......@@ -2244,7 +2225,7 @@ const DeclGen = struct {
22442225 const one_id = try self.constInt(ty, 1, .direct);
22452226 const result_id = self.spv.allocId();
22462227 try self.func.body.emit(self.spv.gpa, .OpSelect, .{
2247 .id_result_type = try self.resolveType2(ty, .direct),
2228 .id_result_type = try self.resolveType(ty, .direct),
22482229 .id_result = result_id,
22492230 .condition = condition_id,
22502231 .object_1 = one_id,
......@@ -2261,7 +2242,7 @@ const DeclGen = struct {
22612242 .Bool => blk: {
22622243 const result_id = self.spv.allocId();
22632244 try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{
2264 .id_result_type = try self.resolveType2(Type.bool, .direct),
2245 .id_result_type = try self.resolveType(Type.bool, .direct),
22652246 .id_result = result_id,
22662247 .operand_1 = operand_id,
22672248 .operand_2 = try self.constBool(false, .indirect),
......@@ -2283,11 +2264,11 @@ const DeclGen = struct {
22832264 }
22842265
22852266 fn extractField(self: *DeclGen, result_ty: Type, object: IdRef, field: u32) !IdRef {
2286 const result_ty_ref = try self.resolveType(result_ty, .indirect);
2267 const result_ty_id = try self.resolveType(result_ty, .indirect);
22872268 const result_id = self.spv.allocId();
22882269 const indexes = [_]u32{field};
22892270 try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{
2290 .id_result_type = self.typeId(result_ty_ref),
2271 .id_result_type = result_ty_id,
22912272 .id_result = result_id,
22922273 .composite = object,
22932274 .indexes = &indexes,
......@@ -2301,13 +2282,13 @@ const DeclGen = struct {
23012282 };
23022283
23032284 fn load(self: *DeclGen, value_ty: Type, ptr_id: IdRef, options: MemoryOptions) !IdRef {
2304 const indirect_value_ty_ref = try self.resolveType(value_ty, .indirect);
2285 const indirect_value_ty_id = try self.resolveType(value_ty, .indirect);
23052286 const result_id = self.spv.allocId();
23062287 const access = spec.MemoryAccess.Extended{
23072288 .Volatile = options.is_volatile,
23082289 };
23092290 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
2310 .id_result_type = self.typeId(indirect_value_ty_ref),
2291 .id_result_type = indirect_value_ty_id,
23112292 .id_result = result_id,
23122293 .pointer = ptr_id,
23132294 .memory_access = access,
......@@ -2519,7 +2500,8 @@ const DeclGen = struct {
25192500
25202501 const result_ty = self.typeOfIndex(inst);
25212502 const shift_ty = self.typeOf(bin_op.rhs);
2522 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);
25232505
25242506 const info = self.arithmeticTypeInfo(result_ty);
25252507 switch (info.class) {
......@@ -2536,7 +2518,7 @@ const DeclGen = struct {
25362518
25372519 // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that,
25382520 // so just manually upcast it if required.
2539 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: {
25402522 const shift_id = self.spv.allocId();
25412523 try self.func.body.emit(self.spv.gpa, .OpUConvert, .{
25422524 .id_result_type = wip.ty_id,
......@@ -2663,7 +2645,7 @@ const DeclGen = struct {
26632645 const result_id = self.spv.allocId();
26642646 const mask_id = try self.constInt(ty, mask_value, .direct);
26652647 try self.func.body.emit(self.spv.gpa, .OpBitwiseAnd, .{
2666 .id_result_type = try self.resolveType2(ty, .direct),
2648 .id_result_type = try self.resolveType(ty, .direct),
26672649 .id_result = result_id,
26682650 .operand_1 = value_id,
26692651 .operand_2 = mask_id,
......@@ -2675,14 +2657,14 @@ const DeclGen = struct {
26752657 const shift_amt_id = try self.constInt(ty, info.backing_bits - info.bits, .direct);
26762658 const left_id = self.spv.allocId();
26772659 try self.func.body.emit(self.spv.gpa, .OpShiftLeftLogical, .{
2678 .id_result_type = try self.resolveType2(ty, .direct),
2660 .id_result_type = try self.resolveType(ty, .direct),
26792661 .id_result = left_id,
26802662 .base = value_id,
26812663 .shift = shift_amt_id,
26822664 });
26832665 const right_id = self.spv.allocId();
26842666 try self.func.body.emit(self.spv.gpa, .OpShiftRightArithmetic, .{
2685 .id_result_type = try self.resolveType2(ty, .direct),
2667 .id_result_type = try self.resolveType(ty, .direct),
26862668 .id_result = right_id,
26872669 .base = left_id,
26882670 .shift = shift_amt_id,
......@@ -2698,7 +2680,7 @@ const DeclGen = struct {
26982680 const lhs_id = try self.resolve(bin_op.lhs);
26992681 const rhs_id = try self.resolve(bin_op.rhs);
27002682 const ty = self.typeOfIndex(inst);
2701 const ty_id = try self.resolveType2(ty, .direct);
2683 const ty_id = try self.resolveType(ty, .direct);
27022684 const info = self.arithmeticTypeInfo(ty);
27032685 switch (info.class) {
27042686 .composite_integer => unreachable, // TODO
......@@ -2759,7 +2741,7 @@ const DeclGen = struct {
27592741
27602742 fn floor(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef {
27612743 const target = self.getTarget();
2762 const ty_ref = try self.resolveType(ty, .direct);
2744 const ty_id = try self.resolveType(ty, .direct);
27632745 const ext_inst: Word = switch (target.os.tag) {
27642746 .opencl => 25,
27652747 .vulkan => 8,
......@@ -2773,7 +2755,7 @@ const DeclGen = struct {
27732755
27742756 const result_id = self.spv.allocId();
27752757 try self.func.body.emit(self.spv.gpa, .OpExtInst, .{
2776 .id_result_type = self.typeId(ty_ref),
2758 .id_result_type = ty_id,
27772759 .id_result = result_id,
27782760 .set = set_id,
27792761 .instruction = .{ .inst = ext_inst },
......@@ -2928,11 +2910,12 @@ const DeclGen = struct {
29282910 const operand_ty = self.typeOf(extra.lhs);
29292911 const ov_ty = result_ty.structFieldType(1, self.module);
29302912
2931 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
2932 const cmp_ty_ref = if (self.isVector(operand_ty))
2933 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))
29342917 else
2935 bool_ty_ref;
2918 bool_ty_id;
29362919
29372920 const info = self.arithmeticTypeInfo(operand_ty);
29382921 switch (info.class) {
......@@ -2968,7 +2951,7 @@ const DeclGen = struct {
29682951 // For subtraction the conditions need to be swapped.
29692952 const overflowed_id = self.spv.allocId();
29702953 try self.func.body.emit(self.spv.gpa, ucmp, .{
2971 .id_result_type = self.typeId(cmp_ty_ref),
2954 .id_result_type = cmp_ty_id,
29722955 .id_result = overflowed_id,
29732956 .operand_1 = result_id.*,
29742957 .operand_2 = lhs_elem_id,
......@@ -2996,7 +2979,7 @@ const DeclGen = struct {
29962979 const rhs_lt_zero_id = self.spv.allocId();
29972980 const zero_id = try self.constInt(wip_result.ty, 0, .direct);
29982981 try self.func.body.emit(self.spv.gpa, .OpSLessThan, .{
2999 .id_result_type = self.typeId(cmp_ty_ref),
2982 .id_result_type = cmp_ty_id,
30002983 .id_result = rhs_lt_zero_id,
30012984 .operand_1 = rhs_elem_id,
30022985 .operand_2 = zero_id,
......@@ -3004,7 +2987,7 @@ const DeclGen = struct {
30042987
30052988 const value_gt_lhs_id = self.spv.allocId();
30062989 try self.func.body.emit(self.spv.gpa, scmp, .{
3007 .id_result_type = self.typeId(cmp_ty_ref),
2990 .id_result_type = cmp_ty_id,
30082991 .id_result = value_gt_lhs_id,
30092992 .operand_1 = lhs_elem_id,
30102993 .operand_2 = result_id.*,
......@@ -3012,7 +2995,7 @@ const DeclGen = struct {
30122995
30132996 const overflowed_id = self.spv.allocId();
30142997 try self.func.body.emit(self.spv.gpa, .OpLogicalEqual, .{
3015 .id_result_type = self.typeId(cmp_ty_ref),
2998 .id_result_type = cmp_ty_id,
30162999 .id_result = overflowed_id,
30173000 .operand_1 = rhs_lt_zero_id,
30183001 .operand_2 = value_gt_lhs_id,
......@@ -3096,15 +3079,17 @@ const DeclGen = struct {
30963079 const result_ty = self.typeOfIndex(inst);
30973080 const operand_ty = self.typeOf(extra.lhs);
30983081 const shift_ty = self.typeOf(extra.rhs);
3099 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);
31003084
31013085 const ov_ty = result_ty.structFieldType(1, self.module);
31023086
3103 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
3104 const cmp_ty_ref = if (self.isVector(operand_ty))
3105 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))
31063091 else
3107 bool_ty_ref;
3092 bool_ty_id;
31083093
31093094 const info = self.arithmeticTypeInfo(operand_ty);
31103095 switch (info.class) {
......@@ -3123,7 +3108,7 @@ const DeclGen = struct {
31233108
31243109 // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that,
31253110 // so just manually upcast it if required.
3126 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: {
31273112 const shift_id = self.spv.allocId();
31283113 try self.func.body.emit(self.spv.gpa, .OpUConvert, .{
31293114 .id_result_type = wip_result.ty_id,
......@@ -3164,7 +3149,7 @@ const DeclGen = struct {
31643149
31653150 const overflowed_id = self.spv.allocId();
31663151 try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{
3167 .id_result_type = self.typeId(cmp_ty_ref),
3152 .id_result_type = cmp_ty_id,
31683153 .id_result = overflowed_id,
31693154 .operand_1 = lhs_elem_id,
31703155 .operand_2 = right_shift_id,
......@@ -3235,8 +3220,7 @@ const DeclGen = struct {
32353220 defer wip.deinit();
32363221
32373222 const elem_ty = if (wip.is_array) operand_ty.scalarType(mod) else operand_ty;
3238 const elem_ty_ref = try self.resolveType(elem_ty, .direct);
3239 const elem_ty_id = self.typeId(elem_ty_ref);
3223 const elem_ty_id = try self.resolveType(elem_ty, .direct);
32403224
32413225 for (wip.results, 0..) |*result_id, i| {
32423226 const elem = try wip.elementAt(operand_ty, operand, i);
......@@ -3261,6 +3245,8 @@ const DeclGen = struct {
32613245 .id_ref_4 = &.{elem},
32623246 });
32633247
3248 // TODO: Comparison should be removed..
3249 // Its valid because SpvModule caches numeric types
32643250 if (wip.ty_id == elem_ty_id) {
32653251 result_id.* = tmp;
32663252 continue;
......@@ -3307,8 +3293,7 @@ const DeclGen = struct {
33073293 const operand = try self.resolve(reduce.operand);
33083294 const operand_ty = self.typeOf(reduce.operand);
33093295 const scalar_ty = operand_ty.scalarType(mod);
3310 const scalar_ty_ref = try self.resolveType(scalar_ty, .direct);
3311 const scalar_ty_id = self.typeId(scalar_ty_ref);
3296 const scalar_ty_id = try self.resolveType(scalar_ty, .direct);
33123297
33133298 const info = self.arithmeticTypeInfo(operand_ty);
33143299
......@@ -3408,13 +3393,13 @@ const DeclGen = struct {
34083393
34093394 fn accessChainId(
34103395 self: *DeclGen,
3411 result_ty_ref: CacheRef,
3396 result_ty_id: IdRef,
34123397 base: IdRef,
34133398 indices: []const IdRef,
34143399 ) !IdRef {
34153400 const result_id = self.spv.allocId();
34163401 try self.func.body.emit(self.spv.gpa, .OpInBoundsAccessChain, .{
3417 .id_result_type = self.typeId(result_ty_ref),
3402 .id_result_type = result_ty_id,
34183403 .id_result = result_id,
34193404 .base = base,
34203405 .indexes = indices,
......@@ -3428,18 +3413,18 @@ const DeclGen = struct {
34283413 /// is the latter and PtrAccessChain is the former.
34293414 fn accessChain(
34303415 self: *DeclGen,
3431 result_ty_ref: CacheRef,
3416 result_ty_id: IdRef,
34323417 base: IdRef,
34333418 indices: []const u32,
34343419 ) !IdRef {
34353420 const ids = try self.indicesToIds(indices);
34363421 defer self.gpa.free(ids);
3437 return try self.accessChainId(result_ty_ref, base, ids);
3422 return try self.accessChainId(result_ty_id, base, ids);
34383423 }
34393424
34403425 fn ptrAccessChain(
34413426 self: *DeclGen,
3442 result_ty_ref: CacheRef,
3427 result_ty_id: IdRef,
34433428 base: IdRef,
34443429 element: IdRef,
34453430 indices: []const u32,
......@@ -3449,7 +3434,7 @@ const DeclGen = struct {
34493434
34503435 const result_id = self.spv.allocId();
34513436 try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{
3452 .id_result_type = self.typeId(result_ty_ref),
3437 .id_result_type = result_ty_id,
34533438 .id_result = result_id,
34543439 .base = base,
34553440 .element = element,
......@@ -3460,21 +3445,21 @@ const DeclGen = struct {
34603445
34613446 fn ptrAdd(self: *DeclGen, result_ty: Type, ptr_ty: Type, ptr_id: IdRef, offset_id: IdRef) !IdRef {
34623447 const mod = self.module;
3463 const result_ty_ref = try self.resolveType(result_ty, .direct);
3448 const result_ty_id = try self.resolveType(result_ty, .direct);
34643449
34653450 switch (ptr_ty.ptrSize(mod)) {
34663451 .One => {
34673452 // Pointer to array
34683453 // TODO: Is this correct?
3469 return try self.accessChainId(result_ty_ref, ptr_id, &.{offset_id});
3454 return try self.accessChainId(result_ty_id, ptr_id, &.{offset_id});
34703455 },
34713456 .C, .Many => {
3472 return try self.ptrAccessChain(result_ty_ref, ptr_id, offset_id, &.{});
3457 return try self.ptrAccessChain(result_ty_id, ptr_id, offset_id, &.{});
34733458 },
34743459 .Slice => {
34753460 // TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does.
34763461 const slice_ptr_id = try self.extractField(result_ty, ptr_id, 0);
3477 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, &.{});
34783463 },
34793464 }
34803465 }
......@@ -3497,12 +3482,12 @@ const DeclGen = struct {
34973482 const ptr_ty = self.typeOf(bin_op.lhs);
34983483 const offset_id = try self.resolve(bin_op.rhs);
34993484 const offset_ty = self.typeOf(bin_op.rhs);
3500 const offset_ty_ref = try self.resolveType(offset_ty, .direct);
3485 const offset_ty_id = try self.resolveType(offset_ty, .direct);
35013486 const result_ty = self.typeOfIndex(inst);
35023487
35033488 const negative_offset_id = self.spv.allocId();
35043489 try self.func.body.emit(self.spv.gpa, .OpSNegate, .{
3505 .id_result_type = self.typeId(offset_ty_ref),
3490 .id_result_type = offset_ty_id,
35063491 .id_result = negative_offset_id,
35073492 .operand = offset_id,
35083493 });
......@@ -3520,7 +3505,7 @@ const DeclGen = struct {
35203505 const mod = self.module;
35213506 var cmp_lhs_id = lhs_id;
35223507 var cmp_rhs_id = rhs_id;
3523 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
3508 const bool_ty_id = try self.resolveType(Type.bool, .direct);
35243509 const op_ty = switch (ty.zigTypeTag(mod)) {
35253510 .Int, .Bool, .Float => ty,
35263511 .Enum => ty.intTagType(mod),
......@@ -3532,7 +3517,7 @@ const DeclGen = struct {
35323517 cmp_lhs_id = self.spv.allocId();
35333518 cmp_rhs_id = self.spv.allocId();
35343519
3535 const usize_ty_id = try self.resolveType2(Type.usize, .direct);
3520 const usize_ty_id = try self.resolveType(Type.usize, .direct);
35363521
35373522 try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{
35383523 .id_result_type = usize_ty_id,
......@@ -3594,20 +3579,20 @@ const DeclGen = struct {
35943579 const pl_eq_id = try self.cmp(op, Type.bool, payload_ty, lhs_pl_id, rhs_pl_id);
35953580 const lhs_not_valid_id = self.spv.allocId();
35963581 try self.func.body.emit(self.spv.gpa, .OpLogicalNot, .{
3597 .id_result_type = self.typeId(bool_ty_ref),
3582 .id_result_type = bool_ty_id,
35983583 .id_result = lhs_not_valid_id,
35993584 .operand = lhs_valid_id,
36003585 });
36013586 const impl_id = self.spv.allocId();
36023587 try self.func.body.emit(self.spv.gpa, .OpLogicalOr, .{
3603 .id_result_type = self.typeId(bool_ty_ref),
3588 .id_result_type = bool_ty_id,
36043589 .id_result = impl_id,
36053590 .operand_1 = lhs_not_valid_id,
36063591 .operand_2 = pl_eq_id,
36073592 });
36083593 const result_id = self.spv.allocId();
36093594 try self.func.body.emit(self.spv.gpa, .OpLogicalAnd, .{
3610 .id_result_type = self.typeId(bool_ty_ref),
3595 .id_result_type = bool_ty_id,
36113596 .id_result = result_id,
36123597 .operand_1 = valid_eq_id,
36133598 .operand_2 = impl_id,
......@@ -3620,14 +3605,14 @@ const DeclGen = struct {
36203605
36213606 const impl_id = self.spv.allocId();
36223607 try self.func.body.emit(self.spv.gpa, .OpLogicalAnd, .{
3623 .id_result_type = self.typeId(bool_ty_ref),
3608 .id_result_type = bool_ty_id,
36243609 .id_result = impl_id,
36253610 .operand_1 = lhs_valid_id,
36263611 .operand_2 = pl_neq_id,
36273612 });
36283613 const result_id = self.spv.allocId();
36293614 try self.func.body.emit(self.spv.gpa, .OpLogicalOr, .{
3630 .id_result_type = self.typeId(bool_ty_ref),
3615 .id_result_type = bool_ty_id,
36313616 .id_result = result_id,
36323617 .operand_1 = valid_neq_id,
36333618 .operand_2 = impl_id,
......@@ -3695,7 +3680,7 @@ const DeclGen = struct {
36953680
36963681 const result_id = self.spv.allocId();
36973682 try self.func.body.emitRaw(self.spv.gpa, opcode, 4);
3698 self.func.body.writeOperand(spec.IdResultType, self.typeId(bool_ty_ref));
3683 self.func.body.writeOperand(spec.IdResultType, bool_ty_id);
36993684 self.func.body.writeOperand(spec.IdResult, result_id);
37003685 self.func.body.writeOperand(spec.IdResultType, cmp_lhs_id);
37013686 self.func.body.writeOperand(spec.IdResultType, cmp_rhs_id);
......@@ -3728,6 +3713,7 @@ const DeclGen = struct {
37283713 return try self.cmp(op, result_ty, ty, lhs_id, rhs_id);
37293714 }
37303715
3716 /// Bitcast one type to another. Note: both types, input, output are expected in **direct** representation.
37313717 fn bitCast(
37323718 self: *DeclGen,
37333719 dst_ty: Type,
......@@ -3735,13 +3721,11 @@ const DeclGen = struct {
37353721 src_id: IdRef,
37363722 ) !IdRef {
37373723 const mod = self.module;
3738 const src_ty_ref = try self.resolveType(src_ty, .direct);
3739 const dst_ty_ref = try self.resolveType(dst_ty, .direct);
3740 const src_key = self.spv.cache.lookup(src_ty_ref);
3741 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);
37423726
37433727 const result_id = blk: {
3744 if (src_ty_ref == dst_ty_ref) {
3728 if (src_ty_id == dst_ty_id) {
37453729 break :blk src_id;
37463730 }
37473731
......@@ -3751,7 +3735,7 @@ const DeclGen = struct {
37513735 if (src_ty.zigTypeTag(mod) == .Int and dst_ty.isPtrAtRuntime(mod)) {
37523736 const result_id = self.spv.allocId();
37533737 try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{
3754 .id_result_type = self.typeId(dst_ty_ref),
3738 .id_result_type = dst_ty_id,
37553739 .id_result = result_id,
37563740 .integer_value = src_id,
37573741 });
......@@ -3761,10 +3745,11 @@ const DeclGen = struct {
37613745 // We can only use OpBitcast for specific conversions: between numerical types, and
37623746 // between pointers. If the resolved spir-v types fall into this category then emit OpBitcast,
37633747 // otherwise use a temporary and perform a pointer cast.
3764 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) {
37653750 const result_id = self.spv.allocId();
37663751 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3767 .id_result_type = self.typeId(dst_ty_ref),
3752 .id_result_type = dst_ty_id,
37683753 .id_result = result_id,
37693754 .operand = src_id,
37703755 });
......@@ -3772,13 +3757,13 @@ const DeclGen = struct {
37723757 break :blk result_id;
37733758 }
37743759
3775 const dst_ptr_ty_ref = try self.ptrType(dst_ty, .Function);
3760 const dst_ptr_ty_id = try self.ptrType(dst_ty, .Function);
37763761
37773762 const tmp_id = try self.alloc(src_ty, .{ .storage_class = .Function });
37783763 try self.store(src_ty, tmp_id, src_id, .{});
37793764 const casted_ptr_id = self.spv.allocId();
37803765 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
3781 .id_result_type = self.typeId(dst_ptr_ty_ref),
3766 .id_result_type = dst_ptr_ty_id,
37823767 .id_result = casted_ptr_id,
37833768 .operand = tmp_id,
37843769 });
......@@ -3850,7 +3835,7 @@ const DeclGen = struct {
38503835 }
38513836
38523837 fn intFromPtr(self: *DeclGen, operand_id: IdRef) !IdRef {
3853 const result_type_id = try self.resolveTypeId(Type.usize);
3838 const result_type_id = try self.resolveType(Type.usize, .direct);
38543839 const result_id = self.spv.allocId();
38553840 try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{
38563841 .id_result_type = result_type_id,
......@@ -3871,21 +3856,21 @@ const DeclGen = struct {
38713856 const operand_ty = self.typeOf(ty_op.operand);
38723857 const operand_id = try self.resolve(ty_op.operand);
38733858 const result_ty = self.typeOfIndex(inst);
3874 const result_ty_ref = try self.resolveType(result_ty, .direct);
3875 return try self.floatFromInt(result_ty_ref, operand_ty, operand_id);
3859 return try self.floatFromInt(result_ty, operand_ty, operand_id);
38763860 }
38773861
3878 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 {
38793863 const operand_info = self.arithmeticTypeInfo(operand_ty);
38803864 const result_id = self.spv.allocId();
3865 const result_ty_id = try self.resolveType(result_ty, .direct);
38813866 switch (operand_info.signedness) {
38823867 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{
3883 .id_result_type = self.typeId(result_ty_ref),
3868 .id_result_type = result_ty_id,
38843869 .id_result = result_id,
38853870 .signed_value = operand_id,
38863871 }),
38873872 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{
3888 .id_result_type = self.typeId(result_ty_ref),
3873 .id_result_type = result_ty_id,
38893874 .id_result = result_id,
38903875 .unsigned_value = operand_id,
38913876 }),
......@@ -3902,16 +3887,16 @@ const DeclGen = struct {
39023887
39033888 fn intFromFloat(self: *DeclGen, result_ty: Type, operand_id: IdRef) !IdRef {
39043889 const result_info = self.arithmeticTypeInfo(result_ty);
3905 const result_ty_ref = try self.resolveType(result_ty, .direct);
3890 const result_ty_id = try self.resolveType(result_ty, .direct);
39063891 const result_id = self.spv.allocId();
39073892 switch (result_info.signedness) {
39083893 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{
3909 .id_result_type = self.typeId(result_ty_ref),
3894 .id_result_type = result_ty_id,
39103895 .id_result = result_id,
39113896 .float_value = operand_id,
39123897 }),
39133898 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{
3914 .id_result_type = self.typeId(result_ty_ref),
3899 .id_result_type = result_ty_id,
39153900 .id_result = result_id,
39163901 .float_value = operand_id,
39173902 }),
......@@ -3937,7 +3922,7 @@ const DeclGen = struct {
39373922 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
39383923 const operand_id = try self.resolve(ty_op.operand);
39393924 const dest_ty = self.typeOfIndex(inst);
3940 const dest_ty_id = try self.resolveTypeId(dest_ty);
3925 const dest_ty_id = try self.resolveType(dest_ty, .direct);
39413926
39423927 const result_id = self.spv.allocId();
39433928 try self.func.body.emit(self.spv.gpa, .OpFConvert, .{
......@@ -3987,7 +3972,7 @@ const DeclGen = struct {
39873972 const slice_ty = self.typeOfIndex(inst);
39883973 const elem_ptr_ty = slice_ty.slicePtrFieldType(mod);
39893974
3990 const elem_ptr_ty_ref = try self.resolveType(elem_ptr_ty, .direct);
3975 const elem_ptr_ty_id = try self.resolveType(elem_ptr_ty, .direct);
39913976
39923977 const array_ptr_id = try self.resolve(ty_op.operand);
39933978 const len_id = try self.constInt(Type.usize, array_ty.arrayLen(mod), .direct);
......@@ -3997,7 +3982,7 @@ const DeclGen = struct {
39973982 try self.bitCast(elem_ptr_ty, array_ptr_ty, array_ptr_id)
39983983 else
39993984 // Convert the pointer-to-array to a pointer to the first element.
4000 try self.accessChain(elem_ptr_ty_ref, array_ptr_id, &.{0});
3985 try self.accessChain(elem_ptr_ty_id, array_ptr_id, &.{0});
40013986
40023987 return try self.constructStruct(
40033988 slice_ty,
......@@ -4171,10 +4156,10 @@ const DeclGen = struct {
41714156 const index_id = try self.resolve(bin_op.rhs);
41724157
41734158 const ptr_ty = self.typeOfIndex(inst);
4174 const ptr_ty_ref = try self.resolveType(ptr_ty, .direct);
4159 const ptr_ty_id = try self.resolveType(ptr_ty, .direct);
41754160
41764161 const slice_ptr = try self.extractField(ptr_ty, slice_id, 0);
4177 return try self.ptrAccessChain(ptr_ty_ref, slice_ptr, index_id, &.{});
4162 return try self.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{});
41784163 }
41794164
41804165 fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -4187,10 +4172,10 @@ const DeclGen = struct {
41874172 const index_id = try self.resolve(bin_op.rhs);
41884173
41894174 const ptr_ty = slice_ty.slicePtrFieldType(mod);
4190 const ptr_ty_ref = try self.resolveType(ptr_ty, .direct);
4175 const ptr_ty_id = try self.resolveType(ptr_ty, .direct);
41914176
41924177 const slice_ptr = try self.extractField(ptr_ty, slice_id, 0);
4193 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, &.{});
41944179 return try self.load(slice_ty.childType(mod), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(mod) });
41954180 }
41964181
......@@ -4198,14 +4183,14 @@ const DeclGen = struct {
41984183 const mod = self.module;
41994184 // Construct new pointer type for the resulting pointer
42004185 const elem_ty = ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T.
4201 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)));
42024187 if (ptr_ty.isSinglePointer(mod)) {
42034188 // Pointer-to-array. In this case, the resulting pointer is not of the same type
42044189 // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain.
4205 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});
42064191 } else {
42074192 // Resulting pointer type is the same as the ptr_ty, so use ptrAccessChain
4208 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, &.{});
42094194 }
42104195 }
42114196
......@@ -4238,11 +4223,11 @@ const DeclGen = struct {
42384223 // For now, just generate a temporary and use that.
42394224 // TODO: This backend probably also should use isByRef from llvm...
42404225
4241 const elem_ptr_ty_ref = try self.ptrType(elem_ty, .Function);
4226 const elem_ptr_ty_id = try self.ptrType(elem_ty, .Function);
42424227
42434228 const tmp_id = try self.alloc(array_ty, .{ .storage_class = .Function });
42444229 try self.store(array_ty, tmp_id, array_id, .{});
4245 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});
42464231 return try self.load(elem_ty, elem_ptr_id, .{});
42474232 }
42484233
......@@ -4267,13 +4252,13 @@ const DeclGen = struct {
42674252 const scalar_ty = vector_ty.scalarType(mod);
42684253
42694254 const storage_class = self.spvStorageClass(vector_ptr_ty.ptrAddressSpace(mod));
4270 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);
42714256
42724257 const vector_ptr = try self.resolve(data.vector_ptr);
42734258 const index = try self.resolve(extra.lhs);
42744259 const operand = try self.resolve(extra.rhs);
42754260
4276 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});
42774262 try self.store(scalar_ty, elem_ptr_id, operand, .{
42784263 .is_volatile = vector_ptr_ty.isVolatilePtr(mod),
42794264 });
......@@ -4289,7 +4274,7 @@ const DeclGen = struct {
42894274 if (layout.tag_size == 0) return;
42904275
42914276 const tag_ty = un_ty.unionTagTypeSafety(mod).?;
4292 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)));
42934278
42944279 const union_ptr_id = try self.resolve(bin_op.lhs);
42954280 const new_tag_id = try self.resolve(bin_op.rhs);
......@@ -4297,7 +4282,7 @@ const DeclGen = struct {
42974282 if (!layout.has_payload) {
42984283 try self.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });
42994284 } else {
4300 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});
43014286 try self.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(mod) });
43024287 }
43034288 }
......@@ -4353,20 +4338,20 @@ const DeclGen = struct {
43534338 const tmp_id = try self.alloc(ty, .{ .storage_class = .Function });
43544339
43554340 if (layout.tag_size != 0) {
4356 const tag_ptr_ty_ref = try self.ptrType(tag_ty, .Function);
4357 const ptr_id = try self.accessChain(tag_ptr_ty_ref, tmp_id, &.{@as(u32, @intCast(layout.tag_index))});
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))});
43584343 const tag_id = try self.constInt(tag_ty, tag_int, .direct);
43594344 try self.store(tag_ty, ptr_id, tag_id, .{});
43604345 }
43614346
43624347 const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]);
43634348 if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
4364 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function);
4365 const pl_ptr_id = try self.accessChain(pl_ptr_ty_ref, tmp_id, &.{layout.payload_index});
4366 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);
43674352 const active_pl_ptr_id = self.spv.allocId();
43684353 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
4369 .id_result_type = self.typeId(active_pl_ptr_ty_ref),
4354 .id_result_type = active_pl_ptr_ty_id,
43704355 .id_result = active_pl_ptr_id,
43714356 .operand = pl_ptr_id,
43724357 });
......@@ -4425,13 +4410,13 @@ const DeclGen = struct {
44254410 const tmp_id = try self.alloc(object_ty, .{ .storage_class = .Function });
44264411 try self.store(object_ty, tmp_id, object_id, .{});
44274412
4428 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, .Function);
4429 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});
44304415
4431 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);
44324417 const active_pl_ptr_id = self.spv.allocId();
44334418 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
4434 .id_result_type = self.typeId(active_pl_ptr_ty_ref),
4419 .id_result_type = active_pl_ptr_ty_id,
44354420 .id_result = active_pl_ptr_id,
44364421 .operand = pl_ptr_id,
44374422 });
......@@ -4448,7 +4433,7 @@ const DeclGen = struct {
44484433 const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
44494434
44504435 const parent_ty = ty_pl.ty.toType().childType(mod);
4451 const res_ty = try self.resolveType(ty_pl.ty.toType(), .indirect);
4436 const result_ty_id = try self.resolveType(ty_pl.ty.toType(), .indirect);
44524437
44534438 const field_ptr = try self.resolve(extra.field_ptr);
44544439 const field_ptr_int = try self.intFromPtr(field_ptr);
......@@ -4463,7 +4448,7 @@ const DeclGen = struct {
44634448
44644449 const base_ptr = self.spv.allocId();
44654450 try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{
4466 .id_result_type = self.spv.resultId(res_ty),
4451 .id_result_type = result_ty_id,
44674452 .id_result = base_ptr,
44684453 .integer_value = base_ptr_int,
44694454 });
......@@ -4478,7 +4463,7 @@ const DeclGen = struct {
44784463 object_ptr: IdRef,
44794464 field_index: u32,
44804465 ) !IdRef {
4481 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
4466 const result_ty_id = try self.resolveType(result_ptr_ty, .direct);
44824467
44834468 const mod = self.module;
44844469 const object_ty = object_ptr_ty.childType(mod);
......@@ -4486,7 +4471,7 @@ const DeclGen = struct {
44864471 .Struct => switch (object_ty.containerLayout(mod)) {
44874472 .@"packed" => unreachable, // TODO
44884473 else => {
4489 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index});
4474 return try self.accessChain(result_ty_id, object_ptr, &.{field_index});
44904475 },
44914476 },
44924477 .Union => switch (object_ty.containerLayout(mod)) {
......@@ -4496,16 +4481,16 @@ const DeclGen = struct {
44964481 if (!layout.has_payload) {
44974482 // Asked to get a pointer to a zero-sized field. Just lower this
44984483 // to undefined, there is no reason to make it be a valid pointer.
4499 return try self.spv.constUndef(self.typeId(result_ty_ref));
4484 return try self.spv.constUndef(result_ty_id);
45004485 }
45014486
45024487 const storage_class = self.spvStorageClass(object_ptr_ty.ptrAddressSpace(mod));
4503 const pl_ptr_ty_ref = try self.ptrType(layout.payload_ty, storage_class);
4504 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});
45054490
45064491 const active_pl_ptr_id = self.spv.allocId();
45074492 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
4508 .id_result_type = self.typeId(result_ty_ref),
4493 .id_result_type = result_ty_id,
45094494 .id_result = active_pl_ptr_id,
45104495 .operand = pl_ptr_id,
45114496 });
......@@ -4533,7 +4518,7 @@ const DeclGen = struct {
45334518 };
45344519
45354520 // Allocate a function-local variable, with possible initializer.
4536 // This function returns a pointer to a variable of type `ty_ref`,
4521 // This function returns a pointer to a variable of type `ty`,
45374522 // which is in the Generic address space. The variable is actually
45384523 // placed in the Function address space.
45394524 fn alloc(
......@@ -4541,13 +4526,13 @@ const DeclGen = struct {
45414526 ty: Type,
45424527 options: AllocOptions,
45434528 ) !IdRef {
4544 const ptr_fn_ty_ref = try self.ptrType(ty, .Function);
4529 const ptr_fn_ty_id = try self.ptrType(ty, .Function);
45454530
45464531 // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to
45474532 // directly generate them into func.prologue instead of the body.
45484533 const var_id = self.spv.allocId();
45494534 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
4550 .id_result_type = self.typeId(ptr_fn_ty_ref),
4535 .id_result_type = ptr_fn_ty_id,
45514536 .id_result = var_id,
45524537 .storage_class = .Function,
45534538 .initializer = options.initializer,
......@@ -4560,9 +4545,9 @@ const DeclGen = struct {
45604545
45614546 switch (options.storage_class) {
45624547 .Generic => {
4563 const ptr_gn_ty_ref = try self.ptrType(ty, .Generic);
4548 const ptr_gn_ty_id = try self.ptrType(ty, .Generic);
45644549 // Convert to a generic pointer
4565 return self.castToGeneric(self.typeId(ptr_gn_ty_ref), var_id);
4550 return self.castToGeneric(ptr_gn_ty_id, var_id);
45664551 },
45674552 .Function => return var_id,
45684553 else => unreachable,
......@@ -4590,9 +4575,9 @@ const DeclGen = struct {
45904575 assert(self.control_flow == .structured);
45914576
45924577 const result_id = self.spv.allocId();
4593 const block_id_ty_ref = try self.resolveType(Type.u32, .direct);
4578 const block_id_ty_id = try self.resolveType(Type.u32, .direct);
45944579 try self.func.body.emitRaw(self.spv.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent...
4595 self.func.body.writeOperand(spec.IdResultType, self.typeId(block_id_ty_ref));
4580 self.func.body.writeOperand(spec.IdResultType, block_id_ty_id);
45964581 self.func.body.writeOperand(spec.IdRef, result_id);
45974582
45984583 for (incoming) |incoming_block| {
......@@ -4690,8 +4675,8 @@ const DeclGen = struct {
46904675 // Make sure that we are still in a block when exiting the function.
46914676 // TODO: Can we get rid of that?
46924677 try self.beginSpvBlock(self.spv.allocId());
4693 const block_id_ty_ref = try self.resolveType(Type.u32, .direct);
4694 return try self.spv.constUndef(self.typeId(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);
46954680 }
46964681
46974682 // The top-most merge actually only has a single source, the
......@@ -4772,7 +4757,7 @@ const DeclGen = struct {
47724757
47734758 assert(block.label != null);
47744759 const result_id = self.spv.allocId();
4775 const result_type_id = try self.resolveTypeId(ty);
4760 const result_type_id = try self.resolveType(ty, .direct);
47764761
47774762 try self.func.body.emitRaw(
47784763 self.spv.gpa,
......@@ -4810,9 +4795,9 @@ const DeclGen = struct {
48104795 // Check if the target of the branch was this current block.
48114796 const this_block = try self.constInt(Type.u32, @intFromEnum(inst), .direct);
48124797 const jump_to_this_block_id = self.spv.allocId();
4813 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
4798 const bool_ty_id = try self.resolveType(Type.bool, .direct);
48144799 try self.func.body.emit(self.spv.gpa, .OpIEqual, .{
4815 .id_result_type = self.typeId(bool_ty_ref),
4800 .id_result_type = bool_ty_id,
48164801 .id_result = jump_to_this_block_id,
48174802 .operand_1 = next_block,
48184803 .operand_2 = this_block,
......@@ -5099,7 +5084,7 @@ const DeclGen = struct {
50995084 const err_union_ty = self.typeOf(pl_op.operand);
51005085 const payload_ty = self.typeOfIndex(inst);
51015086
5102 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
5087 const bool_ty_id = try self.resolveType(Type.bool, .direct);
51035088
51045089 const eu_layout = self.errorUnionLayout(payload_ty);
51055090
......@@ -5112,7 +5097,7 @@ const DeclGen = struct {
51125097 const zero_id = try self.constInt(Type.anyerror, 0, .direct);
51135098 const is_err_id = self.spv.allocId();
51145099 try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{
5115 .id_result_type = self.typeId(bool_ty_ref),
5100 .id_result_type = bool_ty_id,
51165101 .id_result = is_err_id,
51175102 .operand_1 = err_id,
51185103 .operand_2 = zero_id,
......@@ -5164,11 +5149,11 @@ const DeclGen = struct {
51645149 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
51655150 const operand_id = try self.resolve(ty_op.operand);
51665151 const err_union_ty = self.typeOf(ty_op.operand);
5167 const err_ty_ref = try self.resolveType(Type.anyerror, .direct);
5152 const err_ty_id = try self.resolveType(Type.anyerror, .direct);
51685153
51695154 if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) {
51705155 // No error possible, so just return undefined.
5171 return try self.spv.constUndef(self.typeId(err_ty_ref));
5156 return try self.spv.constUndef(err_ty_id);
51725157 }
51735158
51745159 const payload_ty = err_union_ty.errorUnionPayload(mod);
......@@ -5207,11 +5192,11 @@ const DeclGen = struct {
52075192 return operand_id;
52085193 }
52095194
5210 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
5195 const payload_ty_id = try self.resolveType(payload_ty, .indirect);
52115196
52125197 var members: [2]IdRef = undefined;
52135198 members[eu_layout.errorFieldIndex()] = operand_id;
5214 members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(self.typeId(payload_ty_ref));
5199 members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_id);
52155200
52165201 var types: [2]Type = undefined;
52175202 types[eu_layout.errorFieldIndex()] = Type.anyerror;
......@@ -5250,7 +5235,7 @@ const DeclGen = struct {
52505235 const optional_ty = if (is_pointer) operand_ty.childType(mod) else operand_ty;
52515236 const payload_ty = optional_ty.optionalChild(mod);
52525237
5253 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
5238 const bool_ty_id = try self.resolveType(Type.bool, .direct);
52545239
52555240 if (optional_ty.optionalReprIsPayload(mod)) {
52565241 // Pointer payload represents nullability: pointer or slice.
......@@ -5269,7 +5254,7 @@ const DeclGen = struct {
52695254 else
52705255 loaded_id;
52715256
5272 const payload_ty_id = try self.resolveType2(ptr_ty, .direct);
5257 const payload_ty_id = try self.resolveType(ptr_ty, .direct);
52735258 const null_id = try self.spv.constNull(payload_ty_id);
52745259 const op: std.math.CompareOperator = switch (pred) {
52755260 .is_null => .eq,
......@@ -5282,8 +5267,8 @@ const DeclGen = struct {
52825267 if (is_pointer) {
52835268 if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
52845269 const storage_class = self.spvStorageClass(operand_ty.ptrAddressSpace(mod));
5285 const bool_ptr_ty = try self.ptrType(Type.bool, storage_class);
5286 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});
52875272 break :blk try self.load(Type.bool, tag_ptr_id, .{});
52885273 }
52895274
......@@ -5304,7 +5289,7 @@ const DeclGen = struct {
53045289 // Invert condition
53055290 const result_id = self.spv.allocId();
53065291 try self.func.body.emit(self.spv.gpa, .OpLogicalNot, .{
5307 .id_result_type = self.typeId(bool_ty_ref),
5292 .id_result_type = bool_ty_id,
53085293 .id_result = result_id,
53095294 .operand = is_non_null_id,
53105295 });
......@@ -5326,7 +5311,7 @@ const DeclGen = struct {
53265311
53275312 const payload_ty = err_union_ty.errorUnionPayload(mod);
53285313 const eu_layout = self.errorUnionLayout(payload_ty);
5329 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
5314 const bool_ty_id = try self.resolveType(Type.bool, .direct);
53305315
53315316 const error_id = if (!eu_layout.payload_has_bits)
53325317 operand_id
......@@ -5335,7 +5320,7 @@ const DeclGen = struct {
53355320
53365321 const result_id = self.spv.allocId();
53375322 const operands = .{
5338 .id_result_type = self.typeId(bool_ty_ref),
5323 .id_result_type = bool_ty_id,
53395324 .id_result = result_id,
53405325 .operand_1 = error_id,
53415326 .operand_2 = try self.constInt(Type.anyerror, 0, .direct),
......@@ -5371,7 +5356,7 @@ const DeclGen = struct {
53715356 const optional_ty = operand_ty.childType(mod);
53725357 const payload_ty = optional_ty.optionalChild(mod);
53735358 const result_ty = self.typeOfIndex(inst);
5374 const result_ty_ref = try self.resolveType(result_ty, .direct);
5359 const result_ty_id = try self.resolveType(result_ty, .direct);
53755360
53765361 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
53775362 // There is no payload, but we still need to return a valid pointer.
......@@ -5384,7 +5369,7 @@ const DeclGen = struct {
53845369 return try self.bitCast(result_ty, operand_ty, operand_id);
53855370 }
53865371
5387 return try self.accessChain(result_ty_ref, operand_id, &.{0});
5372 return try self.accessChain(result_ty_id, operand_id, &.{0});
53885373 }
53895374
53905375 fn airWrapOptional(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
......@@ -5586,9 +5571,8 @@ const DeclGen = struct {
55865571 const mod = self.module;
55875572 const decl = mod.declPtr(self.decl_index);
55885573 const path = decl.getFileScope(mod).sub_file_path;
5589 const src_fname_id = try self.spv.resolveSourceFileName(path);
55905574 try self.func.body.emit(self.spv.gpa, .OpLine, .{
5591 .file = src_fname_id,
5575 .file = try self.spv.resolveString(path),
55925576 .line = self.base_line + dbg_stmt.line + 1,
55935577 .column = dbg_stmt.column + 1,
55945578 });
......@@ -5757,7 +5741,7 @@ const DeclGen = struct {
57575741 const fn_info = mod.typeToFunc(zig_fn_ty).?;
57585742 const return_type = fn_info.return_type;
57595743
5760 const result_type_ref = try self.resolveFnReturnType(Type.fromInterned(return_type));
5744 const result_type_id = try self.resolveFnReturnType(Type.fromInterned(return_type));
57615745 const result_id = self.spv.allocId();
57625746 const callee_id = try self.resolve(pl_op.operand);
57635747
......@@ -5778,7 +5762,7 @@ const DeclGen = struct {
57785762 }
57795763
57805764 try self.func.body.emit(self.spv.gpa, .OpFunctionCall, .{
5781 .id_result_type = self.typeId(result_type_ref),
5765 .id_result_type = result_type_id,
57825766 .id_result = result_id,
57835767 .function = callee_id,
57845768 .id_ref_3 = params[0..n_params],
src/codegen/spirv/Assembler.zig+13-12
......@@ -296,18 +296,19 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
296296 .OpTypeVoid => try self.spv.resolve(.void_type),
297297 .OpTypeBool => try self.spv.resolve(.bool_type),
298298 .OpTypeInt => blk: {
299 const signedness: std.builtin.Signedness = switch (operands[2].literal32) {
300 0 => .unsigned,
301 1 => .signed,
302 else => {
303 // TODO: Improve source location.
304 return self.fail(0, "{} is not a valid signedness (expected 0 or 1)", .{operands[2].literal32});
305 },
306 };
307 const width = std.math.cast(u16, operands[1].literal32) orelse {
308 return self.fail(0, "int type of {} bits is too large", .{operands[1].literal32});
309 };
310 break :blk try self.spv.intType(signedness, width);
299 // const signedness: std.builtin.Signedness = switch (operands[2].literal32) {
300 // 0 => .unsigned,
301 // 1 => .signed,
302 // else => {
303 // // TODO: Improve source location.
304 // return self.fail(0, "{} is not a valid signedness (expected 0 or 1)", .{operands[2].literal32});
305 // },
306 // };
307 // const width = std.math.cast(u16, operands[1].literal32) orelse {
308 // return self.fail(0, "int type of {} bits is too large", .{operands[1].literal32});
309 // };
310 // break :blk try self.spv.intType(signedness, width);
311 break :blk @as(CacheRef, @enumFromInt(0)); // TODO(robin): fix
311312 },
312313 .OpTypeFloat => blk: {
313314 const bits = operands[1].literal32;
src/codegen/spirv/Module.zig+123-50
......@@ -23,7 +23,6 @@ const Section = @import("Section.zig");
2323const Cache = @import("Cache.zig");
2424pub const CacheKey = Cache.Key;
2525pub const CacheRef = Cache.Ref;
26pub const CacheString = Cache.String;
2726
2827/// This structure represents a function that isc in-progress of being emitted.
2928/// Commonly, the contents of this structure will be merged with the appropriate
......@@ -98,7 +97,7 @@ pub const EntryPoint = struct {
9897 /// The declaration that should be exported.
9998 decl_index: Decl.Index,
10099 /// The name of the kernel to be exported.
101 name: CacheString,
100 name: []const u8,
102101 /// Calling Convention
103102 execution_model: spec.ExecutionModel,
104103};
......@@ -106,6 +105,9 @@ pub const EntryPoint = struct {
106105/// A general-purpose allocator which may be used to allocate resources for this module
107106gpa: Allocator,
108107
108/// Arena for things that need to live for the length of this program.
109arena: std.heap.ArenaAllocator,
110
109111/// Module layout, according to SPIR-V Spec section 2.4, "Logical Layout of a Module".
110112sections: struct {
111113 /// Capability instructions
......@@ -143,15 +145,26 @@ sections: struct {
143145/// SPIR-V instructions return result-ids. This variable holds the module-wide counter for these.
144146next_result_id: Word,
145147
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) = .{},
148/// Cache for results of OpString instructions.
149strings: std.StringArrayHashMapUnmanaged(IdRef) = .{},
150150
151151/// SPIR-V type- and constant cache. This structure is used to store information about these in a more
152152/// efficient manner.
153153cache: Cache = .{},
154154
155/// Some types shouldn't be emitted more than one time, but cannot be caught by
156/// the `intern_map` during codegen. Sometimes, IDs are compared to check if
157/// types are the same, so we can't delay until the dedup pass. Therefore,
158/// this is an ad-hoc structure to cache types where required.
159/// According to the SPIR-V specification, section 2.8, this includes all non-aggregate
160/// non-pointer types.
161cache2: struct {
162 bool_type: ?IdRef = null,
163 void_type: ?IdRef = null,
164 int_types: std.AutoHashMapUnmanaged(std.builtin.Type.Int, IdRef) = .{},
165 float_types: std.AutoHashMapUnmanaged(std.builtin.Type.Float, IdRef) = .{},
166} = .{},
167
155168/// Set of Decls, referred to by Decl.Index.
156169decls: std.ArrayListUnmanaged(Decl) = .{},
157170
......@@ -168,6 +181,7 @@ extended_instruction_set: std.AutoHashMapUnmanaged(spec.InstructionSet, IdRef) =
168181pub fn init(gpa: Allocator) Module {
169182 return .{
170183 .gpa = gpa,
184 .arena = std.heap.ArenaAllocator.init(gpa),
171185 .next_result_id = 1, // 0 is an invalid SPIR-V result id, so start counting at 1.
172186 };
173187}
......@@ -184,15 +198,19 @@ pub fn deinit(self: *Module) void {
184198 self.sections.types_globals_constants.deinit(self.gpa);
185199 self.sections.functions.deinit(self.gpa);
186200
187 self.source_file_names.deinit(self.gpa);
201 self.strings.deinit(self.gpa);
188202 self.cache.deinit(self);
189203
204 self.cache2.int_types.deinit(self.gpa);
205 self.cache2.float_types.deinit(self.gpa);
206
190207 self.decls.deinit(self.gpa);
191208 self.decl_deps.deinit(self.gpa);
192209
193210 self.entry_points.deinit(self.gpa);
194211
195212 self.extended_instruction_set.deinit(self.gpa);
213 self.arena.deinit();
196214
197215 self.* = undefined;
198216}
......@@ -235,10 +253,6 @@ pub fn resolveId(self: *Module, key: CacheKey) !IdResult {
235253 return self.resultId(try self.resolve(key));
236254}
237255
238pub fn resolveString(self: *Module, str: []const u8) !CacheString {
239 return try self.cache.addString(self, str);
240}
241
242256fn addEntryPointDeps(
243257 self: *Module,
244258 decl_index: Decl.Index,
......@@ -283,7 +297,7 @@ fn entryPoints(self: *Module) !Section {
283297 try entry_points.emit(self.gpa, .OpEntryPoint, .{
284298 .execution_model = entry_point.execution_model,
285299 .entry_point = entry_point_id,
286 .name = self.cache.getString(entry_point.name).?,
300 .name = entry_point.name,
287301 .interface = interface.items,
288302 });
289303 }
......@@ -388,51 +402,110 @@ pub fn importInstructionSet(self: *Module, set: spec.InstructionSet) !IdRef {
388402 return result_id;
389403}
390404
391/// Fetch the result-id of an OpString instruction that encodes the path of the source
392/// file of the decl. This function may also emit an OpSource with source-level information regarding
393/// the decl.
394pub fn resolveSourceFileName(self: *Module, path: []const u8) !IdRef {
395 const path_ref = try self.resolveString(path);
396 const result = try self.source_file_names.getOrPut(self.gpa, path_ref);
397 if (!result.found_existing) {
398 const file_result_id = self.allocId();
399 result.value_ptr.* = file_result_id;
400 try self.sections.debug_strings.emit(self.gpa, .OpString, .{
401 .id_result = file_result_id,
402 .string = path,
403 });
405/// Fetch the result-id of an instruction corresponding to a string.
406pub fn resolveString(self: *Module, string: []const u8) !IdRef {
407 if (self.strings.get(string)) |id| {
408 return id;
404409 }
405410
406 return result.value_ptr.*;
411 const id = self.allocId();
412 try self.strings.put(self.gpa, try self.arena.allocator().dupe(u8, string), id);
413
414 try self.sections.debug_strings.emit(self.gpa, .OpString, .{
415 .id_result = id,
416 .string = string,
417 });
418
419 return id;
407420}
408421
409pub fn intType(self: *Module, signedness: std.builtin.Signedness, bits: u16) !CacheRef {
410 return try self.resolve(.{ .int_type = .{
411 .signedness = signedness,
412 .bits = bits,
413 } });
422pub fn structType(self: *Module, types: []const IdRef, maybe_names: ?[]const []const u8) !IdRef {
423 const result_id = self.allocId();
424
425 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeStruct, .{
426 .id_result = result_id,
427 .id_ref = types,
428 });
429
430 if (maybe_names) |names| {
431 assert(names.len == types.len);
432 for (names, 0..) |name, i| {
433 try self.memberDebugName(result_id, @intCast(i), name);
434 }
435 }
436
437 return result_id;
414438}
415439
416pub fn vectorType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {
417 return try self.resolve(.{ .vector_type = .{
418 .component_type = elem_ty_ref,
419 .component_count = len,
420 } });
440pub fn boolType(self: *Module) !IdRef {
441 if (self.cache2.bool_type) |id| return id;
442
443 const result_id = self.allocId();
444 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeBool, .{
445 .id_result = result_id,
446 });
447 self.cache2.bool_type = result_id;
448 return result_id;
449}
450
451pub fn voidType(self: *Module) !IdRef {
452 if (self.cache2.void_type) |id| return id;
453
454 const result_id = self.allocId();
455 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVoid, .{
456 .id_result = result_id,
457 });
458 self.cache2.void_type = result_id;
459 try self.debugName(result_id, "void");
460 return result_id;
421461}
422462
423pub fn arrayType(self: *Module, len: u32, elem_ty_ref: CacheRef) !CacheRef {
424 const len_ty_ref = try self.resolve(.{ .int_type = .{
425 .signedness = .unsigned,
426 .bits = 32,
427 } });
428 const len_ref = try self.resolve(.{ .int = .{
429 .ty = len_ty_ref,
430 .value = .{ .uint64 = len },
431 } });
432 return try self.resolve(.{ .array_type = .{
433 .element_type = elem_ty_ref,
434 .length = len_ref,
435 } });
463pub fn intType(self: *Module, signedness: std.builtin.Signedness, bits: u16) !IdRef {
464 assert(bits > 0);
465 const entry = try self.cache2.int_types.getOrPut(self.gpa, .{ .signedness = signedness, .bits = bits });
466 if (!entry.found_existing) {
467 const result_id = self.allocId();
468 entry.value_ptr.* = result_id;
469 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeInt, .{
470 .id_result = result_id,
471 .width = bits,
472 .signedness = switch (signedness) {
473 .signed => 1,
474 .unsigned => 0,
475 },
476 });
477
478 switch (signedness) {
479 .signed => try self.debugNameFmt(result_id, "i{}", .{bits}),
480 .unsigned => try self.debugNameFmt(result_id, "u{}", .{bits}),
481 }
482 }
483 return entry.value_ptr.*;
484}
485
486pub fn floatType(self: *Module, bits: u16) !IdRef {
487 assert(bits > 0);
488 const entry = try self.cache2.float_types.getOrPut(self.gpa, .{ .bits = bits });
489 if (!entry.found_existing) {
490 const result_id = self.allocId();
491 entry.value_ptr.* = result_id;
492 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeFloat, .{
493 .id_result = result_id,
494 .width = bits,
495 });
496 try self.debugNameFmt(result_id, "f{}", .{bits});
497 }
498 return entry.value_ptr.*;
499}
500
501pub fn vectorType(self: *Module, len: u32, child_id: IdRef) !IdRef {
502 const result_id = self.allocId();
503 try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVector, .{
504 .id_result = result_id,
505 .component_type = child_id,
506 .component_count = len,
507 });
508 return result_id;
436509}
437510
438511pub fn constUndef(self: *Module, ty_id: IdRef) !IdRef {
......@@ -526,7 +599,7 @@ pub fn declareEntryPoint(
526599) !void {
527600 try self.entry_points.append(self.gpa, .{
528601 .decl_index = decl_index,
529 .name = try self.resolveString(name),
602 .name = try self.arena.allocator().dupe(u8, name),
530603 .execution_model = execution_model,
531604 });
532605}
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),