| ... | @@ -238,6 +238,7 @@ pub const DeclGen = struct { | ... | @@ -238,6 +238,7 @@ pub const DeclGen = struct { |
| 238 | else => unreachable, | 238 | else => unreachable, |
| 239 | }; | 239 | }; |
| 240 | const spv_decl_index = try self.resolveDecl(fn_decl_index); | 240 | const spv_decl_index = try self.resolveDecl(fn_decl_index); |
| | 241 | try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {}); |
| 241 | return self.spv.declPtr(spv_decl_index).result_id; | 242 | return self.spv.declPtr(spv_decl_index).result_id; |
| 242 | } | 243 | } |
| 243 | | 244 | |
| ... | @@ -459,7 +460,7 @@ pub const DeclGen = struct { | ... | @@ -459,7 +460,7 @@ pub const DeclGen = struct { |
| 459 | /// If full, its flushed. | 460 | /// If full, its flushed. |
| 460 | partial_word: std.BoundedArray(u8, @sizeOf(Word)) = .{}, | 461 | partial_word: std.BoundedArray(u8, @sizeOf(Word)) = .{}, |
| 461 | /// The declaration dependencies of the constant we are lowering. | 462 | /// The declaration dependencies of the constant we are lowering. |
| 462 | decl_deps: std.ArrayList(SpvModule.Decl.Index), | 463 | decl_deps: std.AutoArrayHashMap(SpvModule.Decl.Index, void), |
| 463 | | 464 | |
| 464 | /// Utility function to get the section that instructions should be lowered to. | 465 | /// Utility function to get the section that instructions should be lowered to. |
| 465 | fn section(self: *@This()) *SpvSection { | 466 | fn section(self: *@This()) *SpvSection { |
| ... | @@ -582,14 +583,15 @@ pub const DeclGen = struct { | ... | @@ -582,14 +583,15 @@ pub const DeclGen = struct { |
| 582 | // just generate an empty pointer. Function pointers are represented by usize for now, | 583 | // just generate an empty pointer. Function pointers are represented by usize for now, |
| 583 | // though. | 584 | // though. |
| 584 | try self.addInt(Type.usize, Value.initTag(.zero)); | 585 | try self.addInt(Type.usize, Value.initTag(.zero)); |
| | 586 | // TODO: Add dependency |
| 585 | return; | 587 | return; |
| 586 | }, | 588 | }, |
| 587 | .extern_fn => unreachable, // TODO | 589 | .extern_fn => unreachable, // TODO |
| 588 | else => { | 590 | else => { |
| 589 | const result_id = dg.spv.allocId(); | 591 | const result_id = dg.spv.allocId(); |
| 590 | log.debug("addDeclRef {s} = {}", .{ decl.name, result_id.id }); | 592 | log.debug("addDeclRef: id = {}, index = {}, name = {s}", .{ result_id.id, @enumToInt(spv_decl_index), decl.name }); |
| 591 | | 593 | |
| 592 | try self.decl_deps.append(spv_decl_index); | 594 | try self.decl_deps.put(spv_decl_index, {}); |
| 593 | | 595 | |
| 594 | const decl_id = dg.spv.declPtr(spv_decl_index).result_id; | 596 | const decl_id = dg.spv.declPtr(spv_decl_index).result_id; |
| 595 | // TODO: Do we need a storage class cast here? | 597 | // TODO: Do we need a storage class cast here? |
| ... | @@ -861,7 +863,8 @@ pub const DeclGen = struct { | ... | @@ -861,7 +863,8 @@ pub const DeclGen = struct { |
| 861 | | 863 | |
| 862 | assert(storage_class != .Generic and storage_class != .Function); | 864 | assert(storage_class != .Generic and storage_class != .Function); |
| 863 | | 865 | |
| 864 | log.debug("lowerIndirectConstant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtDebug() }); | 866 | const var_id = self.spv.allocId(); |
| | 867 | log.debug("lowerIndirectConstant: id = {}, index = {}, ty = {}, val = {}", .{ var_id.id, @enumToInt(spv_decl_index), ty.fmt(self.module), val.fmtDebug() }); |
| 865 | | 868 | |
| 866 | const section = &self.spv.globals.section; | 869 | const section = &self.spv.globals.section; |
| 867 | | 870 | |
| ... | @@ -897,7 +900,7 @@ pub const DeclGen = struct { | ... | @@ -897,7 +900,7 @@ pub const DeclGen = struct { |
| 897 | .u32_ty_id = self.typeId(u32_ty_ref), | 900 | .u32_ty_id = self.typeId(u32_ty_ref), |
| 898 | .members = std.ArrayList(SpvType.Payload.Struct.Member).init(self.gpa), | 901 | .members = std.ArrayList(SpvType.Payload.Struct.Member).init(self.gpa), |
| 899 | .initializers = std.ArrayList(IdRef).init(self.gpa), | 902 | .initializers = std.ArrayList(IdRef).init(self.gpa), |
| 900 | .decl_deps = std.ArrayList(SpvModule.Decl.Index).init(self.gpa), | 903 | .decl_deps = std.AutoArrayHashMap(SpvModule.Decl.Index, void).init(self.gpa), |
| 901 | }; | 904 | }; |
| 902 | | 905 | |
| 903 | defer icl.members.deinit(); | 906 | defer icl.members.deinit(); |
| ... | @@ -917,7 +920,6 @@ pub const DeclGen = struct { | ... | @@ -917,7 +920,6 @@ pub const DeclGen = struct { |
| 917 | .constituents = icl.initializers.items, | 920 | .constituents = icl.initializers.items, |
| 918 | }); | 921 | }); |
| 919 | | 922 | |
| 920 | const var_id = self.spv.allocId(); | | |
| 921 | self.spv.globalPtr(spv_decl_index).?.result_id = var_id; | 923 | self.spv.globalPtr(spv_decl_index).?.result_id = var_id; |
| 922 | try section.emit(self.spv.gpa, .OpVariable, .{ | 924 | try section.emit(self.spv.gpa, .OpVariable, .{ |
| 923 | .id_result_type = self.typeId(ptr_constant_struct_ty_ref), | 925 | .id_result_type = self.typeId(ptr_constant_struct_ty_ref), |
| ... | @@ -951,7 +953,7 @@ pub const DeclGen = struct { | ... | @@ -951,7 +953,7 @@ pub const DeclGen = struct { |
| 951 | }); | 953 | }); |
| 952 | } | 954 | } |
| 953 | | 955 | |
| 954 | try self.spv.declareDeclDeps(spv_decl_index, icl.decl_deps.items); | 956 | try self.spv.declareDeclDeps(spv_decl_index, icl.decl_deps.keys()); |
| 955 | self.spv.endGlobal(spv_decl_index, begin_inst); | 957 | self.spv.endGlobal(spv_decl_index, begin_inst); |
| 956 | } | 958 | } |
| 957 | | 959 | |
| ... | @@ -1007,7 +1009,8 @@ pub const DeclGen = struct { | ... | @@ -1007,7 +1009,8 @@ pub const DeclGen = struct { |
| 1007 | false, | 1009 | false, |
| 1008 | alignment, | 1010 | alignment, |
| 1009 | ); | 1011 | ); |
| 1010 | try self.func.decl_deps.append(self.spv.gpa, spv_decl_index); | 1012 | log.debug("indirect constant: index = {}", .{@enumToInt(spv_decl_index)}); |
| | 1013 | try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {}); |
| 1011 | | 1014 | |
| 1012 | try self.func.body.emit(self.spv.gpa, .OpLoad, .{ | 1015 | try self.func.body.emit(self.spv.gpa, .OpLoad, .{ |
| 1013 | .id_result_type = result_ty_id, | 1016 | .id_result_type = result_ty_id, |
| ... | @@ -1471,7 +1474,7 @@ pub const DeclGen = struct { | ... | @@ -1471,7 +1474,7 @@ pub const DeclGen = struct { |
| 1471 | const spv_decl_index = try self.resolveDecl(self.decl_index); | 1474 | const spv_decl_index = try self.resolveDecl(self.decl_index); |
| 1472 | | 1475 | |
| 1473 | const decl_id = self.spv.declPtr(spv_decl_index).result_id; | 1476 | const decl_id = self.spv.declPtr(spv_decl_index).result_id; |
| 1474 | log.debug("genDecl {s} = {}", .{ decl.name, decl_id }); | 1477 | log.debug("genDecl: id = {}, index = {}, name = {s}", .{ decl_id.id, @enumToInt(spv_decl_index), decl.name }); |
| 1475 | | 1478 | |
| 1476 | if (decl.val.castTag(.function)) |_| { | 1479 | if (decl.val.castTag(.function)) |_| { |
| 1477 | assert(decl.ty.zigTypeTag() == .Fn); | 1480 | assert(decl.ty.zigTypeTag() == .Fn); |