| ... | @@ -899,7 +899,7 @@ const NavGen = struct { | ... | @@ -899,7 +899,7 @@ const NavGen = struct { |
| 899 | const result_ty_id = try self.resolveType(ty, repr); | 899 | const result_ty_id = try self.resolveType(ty, repr); |
| 900 | const ip = &zcu.intern_pool; | 900 | const ip = &zcu.intern_pool; |
| 901 | | 901 | |
| 902 | log.debug("lowering constant: ty = {}, val = {}", .{ ty.fmt(pt), val.fmtValue(pt) }); | 902 | log.debug("lowering constant: ty = {}, val = {}, key = {s}", .{ ty.fmt(pt), val.fmtValue(pt), @tagName(ip.indexToKey(val.toIntern())) }); |
| 903 | if (val.isUndefDeep(zcu)) { | 903 | if (val.isUndefDeep(zcu)) { |
| 904 | return self.spv.constUndef(result_ty_id); | 904 | return self.spv.constUndef(result_ty_id); |
| 905 | } | 905 | } |
| ... | @@ -1169,7 +1169,6 @@ const NavGen = struct { | ... | @@ -1169,7 +1169,6 @@ const NavGen = struct { |
| 1169 | | 1169 | |
| 1170 | fn derivePtr(self: *NavGen, derivation: Value.PointerDeriveStep) Error!IdRef { | 1170 | fn derivePtr(self: *NavGen, derivation: Value.PointerDeriveStep) Error!IdRef { |
| 1171 | const pt = self.pt; | 1171 | const pt = self.pt; |
| 1172 | const zcu = pt.zcu; | | |
| 1173 | switch (derivation) { | 1172 | switch (derivation) { |
| 1174 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, | 1173 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| 1175 | .int => |int| { | 1174 | .int => |int| { |
| ... | @@ -1213,10 +1212,6 @@ const NavGen = struct { | ... | @@ -1213,10 +1212,6 @@ const NavGen = struct { |
| 1213 | if (oac.byte_offset != 0) break :disallow; | 1212 | if (oac.byte_offset != 0) break :disallow; |
| 1214 | // Allow changing the pointer type child only to restructure arrays. | 1213 | // Allow changing the pointer type child only to restructure arrays. |
| 1215 | // e.g. [3][2]T to T is fine, as is [2]T -> [2][1]T. | 1214 | // e.g. [3][2]T to T is fine, as is [2]T -> [2][1]T. |
| 1216 | const src_base_ty = parent_ptr_ty.arrayBase(zcu)[0]; | | |
| 1217 | const dest_base_ty = oac.new_ptr_ty.arrayBase(zcu)[0]; | | |
| 1218 | if (self.getTarget().os.tag == .vulkan and src_base_ty.toIntern() != dest_base_ty.toIntern()) break :disallow; | | |
| 1219 | | | |
| 1220 | const result_ty_id = try self.resolveType(oac.new_ptr_ty, .direct); | 1215 | const result_ty_id = try self.resolveType(oac.new_ptr_ty, .direct); |
| 1221 | const result_ptr_id = self.spv.allocId(); | 1216 | const result_ptr_id = self.spv.allocId(); |
| 1222 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ | 1217 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| ... | @@ -1226,7 +1221,7 @@ const NavGen = struct { | ... | @@ -1226,7 +1221,7 @@ const NavGen = struct { |
| 1226 | }); | 1221 | }); |
| 1227 | return result_ptr_id; | 1222 | return result_ptr_id; |
| 1228 | } | 1223 | } |
| 1229 | return self.fail("Cannot perform pointer cast: '{}' to '{}'", .{ | 1224 | return self.fail("cannot perform pointer cast: '{}' to '{}'", .{ |
| 1230 | parent_ptr_ty.fmt(pt), | 1225 | parent_ptr_ty.fmt(pt), |
| 1231 | oac.new_ptr_ty.fmt(pt), | 1226 | oac.new_ptr_ty.fmt(pt), |
| 1232 | }); | 1227 | }); |
| ... | @@ -1310,12 +1305,12 @@ const NavGen = struct { | ... | @@ -1310,12 +1305,12 @@ const NavGen = struct { |
| 1310 | .global, .invocation_global => spv_decl.result_id, | 1305 | .global, .invocation_global => spv_decl.result_id, |
| 1311 | }; | 1306 | }; |
| 1312 | | 1307 | |
| 1313 | const final_storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); | 1308 | const storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); |
| 1314 | try self.addFunctionDep(spv_decl_index, final_storage_class); | 1309 | try self.addFunctionDep(spv_decl_index, storage_class); |
| 1315 | | 1310 | |
| 1316 | const decl_ptr_ty_id = try self.ptrType(nav_ty, final_storage_class); | 1311 | const decl_ptr_ty_id = try self.ptrType(nav_ty, storage_class); |
| 1317 | | 1312 | |
| 1318 | const ptr_id = switch (final_storage_class) { | 1313 | const ptr_id = switch (storage_class) { |
| 1319 | .Generic => try self.castToGeneric(decl_ptr_ty_id, decl_id), | 1314 | .Generic => try self.castToGeneric(decl_ptr_ty_id, decl_id), |
| 1320 | else => decl_id, | 1315 | else => decl_id, |
| 1321 | }; | 1316 | }; |
| ... | @@ -1401,6 +1396,10 @@ const NavGen = struct { | ... | @@ -1401,6 +1396,10 @@ const NavGen = struct { |
| 1401 | | 1396 | |
| 1402 | const child_ty_id = try self.resolveType(child_ty, child_repr); | 1397 | const child_ty_id = try self.resolveType(child_ty, child_repr); |
| 1403 | | 1398 | |
| | 1399 | if (storage_class == .Uniform or storage_class == .PushConstant) { |
| | 1400 | try self.spv.decorate(child_ty_id, .Block); |
| | 1401 | } |
| | 1402 | |
| 1404 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{ | 1403 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{ |
| 1405 | .id_result = result_id, | 1404 | .id_result = result_id, |
| 1406 | .storage_class = storage_class, | 1405 | .storage_class = storage_class, |
| ... | @@ -1505,10 +1504,13 @@ const NavGen = struct { | ... | @@ -1505,10 +1504,13 @@ const NavGen = struct { |
| 1505 | member_names[layout.padding_index] = "(padding)"; | 1504 | member_names[layout.padding_index] = "(padding)"; |
| 1506 | } | 1505 | } |
| 1507 | | 1506 | |
| 1508 | const result_id = try self.spv.structType(member_types[0..layout.total_fields], member_names[0..layout.total_fields]); | 1507 | const result_id = self.spv.allocId(); |
| | 1508 | try self.spv.structType(result_id, member_types[0..layout.total_fields], member_names[0..layout.total_fields]); |
| | 1509 | |
| 1509 | const type_name = try self.resolveTypeName(ty); | 1510 | const type_name = try self.resolveTypeName(ty); |
| 1510 | defer self.gpa.free(type_name); | 1511 | defer self.gpa.free(type_name); |
| 1511 | try self.spv.debugName(result_id, type_name); | 1512 | try self.spv.debugName(result_id, type_name); |
| | 1513 | |
| 1512 | return result_id; | 1514 | return result_id; |
| 1513 | } | 1515 | } |
| 1514 | | 1516 | |
| ... | @@ -1640,7 +1642,9 @@ const NavGen = struct { | ... | @@ -1640,7 +1642,9 @@ const NavGen = struct { |
| 1640 | // can be lowered to ptrAccessChain instead of manually performing the math. | 1642 | // can be lowered to ptrAccessChain instead of manually performing the math. |
| 1641 | return try self.arrayType(1, elem_ty_id); | 1643 | return try self.arrayType(1, elem_ty_id); |
| 1642 | } else { | 1644 | } else { |
| 1643 | return try self.arrayType(total_len, elem_ty_id); | 1645 | const result_id = try self.arrayType(total_len, elem_ty_id); |
| | 1646 | try self.spv.decorate(result_id, .{ .ArrayStride = .{ .array_stride = @intCast(elem_ty.abiSize(zcu)) } }); |
| | 1647 | return result_id; |
| 1644 | } | 1648 | } |
| 1645 | }, | 1649 | }, |
| 1646 | .@"fn" => switch (repr) { | 1650 | .@"fn" => switch (repr) { |
| ... | @@ -1702,10 +1706,13 @@ const NavGen = struct { | ... | @@ -1702,10 +1706,13 @@ const NavGen = struct { |
| 1702 | } | 1706 | } |
| 1703 | | 1707 | |
| 1704 | const size_ty_id = try self.resolveType(Type.usize, .direct); | 1708 | const size_ty_id = try self.resolveType(Type.usize, .direct); |
| 1705 | return self.spv.structType( | 1709 | const result_id = self.spv.allocId(); |
| | 1710 | try self.spv.structType( |
| | 1711 | result_id, |
| 1706 | &.{ ptr_ty_id, size_ty_id }, | 1712 | &.{ ptr_ty_id, size_ty_id }, |
| 1707 | &.{ "ptr", "len" }, | 1713 | &.{ "ptr", "len" }, |
| 1708 | ); | 1714 | ); |
| | 1715 | return result_id; |
| 1709 | }, | 1716 | }, |
| 1710 | .vector => { | 1717 | .vector => { |
| 1711 | const elem_ty = ty.childType(zcu); | 1718 | const elem_ty = ty.childType(zcu); |
| ... | @@ -1732,10 +1739,13 @@ const NavGen = struct { | ... | @@ -1732,10 +1739,13 @@ const NavGen = struct { |
| 1732 | member_index += 1; | 1739 | member_index += 1; |
| 1733 | } | 1740 | } |
| 1734 | | 1741 | |
| 1735 | const result_id = try self.spv.structType(member_types[0..member_index], null); | 1742 | const result_id = self.spv.allocId(); |
| | 1743 | try self.spv.structType(result_id, member_types[0..member_index], null); |
| | 1744 | |
| 1736 | const type_name = try self.resolveTypeName(ty); | 1745 | const type_name = try self.resolveTypeName(ty); |
| 1737 | defer self.gpa.free(type_name); | 1746 | defer self.gpa.free(type_name); |
| 1738 | try self.spv.debugName(result_id, type_name); | 1747 | try self.spv.debugName(result_id, type_name); |
| | 1748 | |
| 1739 | return result_id; | 1749 | return result_id; |
| 1740 | }, | 1750 | }, |
| 1741 | .struct_type => ip.loadStructType(ty.toIntern()), | 1751 | .struct_type => ip.loadStructType(ty.toIntern()), |
| ... | @@ -1752,7 +1762,9 @@ const NavGen = struct { | ... | @@ -1752,7 +1762,9 @@ const NavGen = struct { |
| 1752 | var member_names = std.ArrayList([]const u8).init(self.gpa); | 1762 | var member_names = std.ArrayList([]const u8).init(self.gpa); |
| 1753 | defer member_names.deinit(); | 1763 | defer member_names.deinit(); |
| 1754 | | 1764 | |
| | 1765 | var index: u32 = 0; |
| 1755 | var it = struct_type.iterateRuntimeOrder(ip); | 1766 | var it = struct_type.iterateRuntimeOrder(ip); |
| | 1767 | const result_id = self.spv.allocId(); |
| 1756 | while (it.next()) |field_index| { | 1768 | while (it.next()) |field_index| { |
| 1757 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); | 1769 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1758 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | 1770 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| ... | @@ -1760,16 +1772,25 @@ const NavGen = struct { | ... | @@ -1760,16 +1772,25 @@ const NavGen = struct { |
| 1760 | continue; | 1772 | continue; |
| 1761 | } | 1773 | } |
| 1762 | | 1774 | |
| | 1775 | if (target.os.tag == .vulkan) { |
| | 1776 | try self.spv.decorateMember(result_id, index, .{ .Offset = .{ |
| | 1777 | .byte_offset = @intCast(ty.structFieldOffset(field_index, zcu)), |
| | 1778 | } }); |
| | 1779 | } |
| 1763 | const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse | 1780 | const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse |
| 1764 | try ip.getOrPutStringFmt(zcu.gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls); | 1781 | try ip.getOrPutStringFmt(zcu.gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls); |
| 1765 | try member_types.append(try self.resolveType(field_ty, .indirect)); | 1782 | try member_types.append(try self.resolveType(field_ty, .indirect)); |
| 1766 | try member_names.append(field_name.toSlice(ip)); | 1783 | try member_names.append(field_name.toSlice(ip)); |
| | 1784 | |
| | 1785 | index += 1; |
| 1767 | } | 1786 | } |
| 1768 | | 1787 | |
| 1769 | const result_id = try self.spv.structType(member_types.items, member_names.items); | 1788 | try self.spv.structType(result_id, member_types.items, member_names.items); |
| | 1789 | |
| 1770 | const type_name = try self.resolveTypeName(ty); | 1790 | const type_name = try self.resolveTypeName(ty); |
| 1771 | defer self.gpa.free(type_name); | 1791 | defer self.gpa.free(type_name); |
| 1772 | try self.spv.debugName(result_id, type_name); | 1792 | try self.spv.debugName(result_id, type_name); |
| | 1793 | |
| 1773 | return result_id; | 1794 | return result_id; |
| 1774 | }, | 1795 | }, |
| 1775 | .optional => { | 1796 | .optional => { |
| ... | @@ -1789,10 +1810,13 @@ const NavGen = struct { | ... | @@ -1789,10 +1810,13 @@ const NavGen = struct { |
| 1789 | | 1810 | |
| 1790 | const bool_ty_id = try self.resolveType(Type.bool, .indirect); | 1811 | const bool_ty_id = try self.resolveType(Type.bool, .indirect); |
| 1791 | | 1812 | |
| 1792 | return try self.spv.structType( | 1813 | const result_id = self.spv.allocId(); |
| | 1814 | try self.spv.structType( |
| | 1815 | result_id, |
| 1793 | &.{ payload_ty_id, bool_ty_id }, | 1816 | &.{ payload_ty_id, bool_ty_id }, |
| 1794 | &.{ "payload", "valid" }, | 1817 | &.{ "payload", "valid" }, |
| 1795 | ); | 1818 | ); |
| | 1819 | return result_id; |
| 1796 | }, | 1820 | }, |
| 1797 | .@"union" => return try self.resolveUnionType(ty), | 1821 | .@"union" => return try self.resolveUnionType(ty), |
| 1798 | .error_set => return try self.resolveType(Type.u16, repr), | 1822 | .error_set => return try self.resolveType(Type.u16, repr), |
| ... | @@ -1821,7 +1845,9 @@ const NavGen = struct { | ... | @@ -1821,7 +1845,9 @@ const NavGen = struct { |
| 1821 | // TODO: ABI padding? | 1845 | // TODO: ABI padding? |
| 1822 | } | 1846 | } |
| 1823 | | 1847 | |
| 1824 | return try self.spv.structType(&member_types, &member_names); | 1848 | const result_id = self.spv.allocId(); |
| | 1849 | try self.spv.structType(result_id, &member_types, &member_names); |
| | 1850 | return result_id; |
| 1825 | }, | 1851 | }, |
| 1826 | .@"opaque" => { | 1852 | .@"opaque" => { |
| 1827 | const type_name = try self.resolveTypeName(ty); | 1853 | const type_name = try self.resolveTypeName(ty); |
| ... | @@ -1851,7 +1877,7 @@ const NavGen = struct { | ... | @@ -1851,7 +1877,7 @@ const NavGen = struct { |
| 1851 | const target = self.getTarget(); | 1877 | const target = self.getTarget(); |
| 1852 | return switch (as) { | 1878 | return switch (as) { |
| 1853 | .generic => switch (target.os.tag) { | 1879 | .generic => switch (target.os.tag) { |
| 1854 | .vulkan => .Private, | 1880 | .vulkan => .Function, |
| 1855 | .opencl => .Generic, | 1881 | .opencl => .Generic, |
| 1856 | else => unreachable, | 1882 | else => unreachable, |
| 1857 | }, | 1883 | }, |
| ... | @@ -1863,6 +1889,7 @@ const NavGen = struct { | ... | @@ -1863,6 +1889,7 @@ const NavGen = struct { |
| 1863 | else => unreachable, | 1889 | else => unreachable, |
| 1864 | }, | 1890 | }, |
| 1865 | .constant => .UniformConstant, | 1891 | .constant => .UniformConstant, |
| | 1892 | .push_constant => .PushConstant, |
| 1866 | .input => .Input, | 1893 | .input => .Input, |
| 1867 | .output => .Output, | 1894 | .output => .Output, |
| 1868 | .uniform => .Uniform, | 1895 | .uniform => .Uniform, |
| ... | @@ -2954,10 +2981,8 @@ const NavGen = struct { | ... | @@ -2954,10 +2981,8 @@ const NavGen = struct { |
| 2954 | const spv_err_decl_index = try self.spv.allocDecl(.global); | 2981 | const spv_err_decl_index = try self.spv.allocDecl(.global); |
| 2955 | try self.spv.declareDeclDeps(spv_err_decl_index, &.{}); | 2982 | try self.spv.declareDeclDeps(spv_err_decl_index, &.{}); |
| 2956 | | 2983 | |
| 2957 | const push_constant_struct_ty_id = try self.spv.structType( | 2984 | const push_constant_struct_ty_id = self.spv.allocId(); |
| 2958 | &.{ptr_anyerror_ty_id}, | 2985 | try self.spv.structType(push_constant_struct_ty_id, &.{ptr_anyerror_ty_id}, &.{"error_out_ptr"}); |
| 2959 | &.{"error_out_ptr"}, | | |
| 2960 | ); | | |
| 2961 | try self.spv.decorate(push_constant_struct_ty_id, .Block); | 2986 | try self.spv.decorate(push_constant_struct_ty_id, .Block); |
| 2962 | try self.spv.decorateMember(push_constant_struct_ty_id, 0, .{ .Offset = .{ .byte_offset = 0 } }); | 2987 | try self.spv.decorateMember(push_constant_struct_ty_id, 0, .{ .Offset = .{ .byte_offset = 0 } }); |
| 2963 | | 2988 | |
| ... | @@ -3141,15 +3166,15 @@ const NavGen = struct { | ... | @@ -3141,15 +3166,15 @@ const NavGen = struct { |
| 3141 | }; | 3166 | }; |
| 3142 | assert(maybe_init_val == null); // TODO | 3167 | assert(maybe_init_val == null); // TODO |
| 3143 | | 3168 | |
| 3144 | const final_storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); | 3169 | const storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); |
| 3145 | assert(final_storage_class != .Generic); // These should be instance globals | 3170 | assert(storage_class != .Generic); // These should be instance globals |
| 3146 | | 3171 | |
| 3147 | const ptr_ty_id = try self.ptrType(ty, final_storage_class); | 3172 | const ptr_ty_id = try self.ptrType(ty, storage_class); |
| 3148 | | 3173 | |
| 3149 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{ | 3174 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{ |
| 3150 | .id_result_type = ptr_ty_id, | 3175 | .id_result_type = ptr_ty_id, |
| 3151 | .id_result = result_id, | 3176 | .id_result = result_id, |
| 3152 | .storage_class = final_storage_class, | 3177 | .storage_class = storage_class, |
| 3153 | }); | 3178 | }); |
| 3154 | | 3179 | |
| 3155 | try self.spv.debugName(result_id, nav.fqn.toSlice(ip)); | 3180 | try self.spv.debugName(result_id, nav.fqn.toSlice(ip)); |