| ... | ... | @@ -899,7 +899,7 @@ const NavGen = struct { |
| 899 | 899 | const result_ty_id = try self.resolveType(ty, repr); |
| 900 | 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 | 903 | if (val.isUndefDeep(zcu)) { |
| 904 | 904 | return self.spv.constUndef(result_ty_id); |
| 905 | 905 | } |
| ... | ... | @@ -1169,7 +1169,6 @@ const NavGen = struct { |
| 1169 | 1169 | |
| 1170 | 1170 | fn derivePtr(self: *NavGen, derivation: Value.PointerDeriveStep) Error!IdRef { |
| 1171 | 1171 | const pt = self.pt; |
| 1172 | | const zcu = pt.zcu; |
| 1173 | 1172 | switch (derivation) { |
| 1174 | 1173 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| 1175 | 1174 | .int => |int| { |
| ... | ... | @@ -1213,10 +1212,6 @@ const NavGen = struct { |
| 1213 | 1212 | if (oac.byte_offset != 0) break :disallow; |
| 1214 | 1213 | // Allow changing the pointer type child only to restructure arrays. |
| 1215 | 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 | 1215 | const result_ty_id = try self.resolveType(oac.new_ptr_ty, .direct); |
| 1221 | 1216 | const result_ptr_id = self.spv.allocId(); |
| 1222 | 1217 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| ... | ... | @@ -1226,7 +1221,7 @@ const NavGen = struct { |
| 1226 | 1221 | }); |
| 1227 | 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 | 1225 | parent_ptr_ty.fmt(pt), |
| 1231 | 1226 | oac.new_ptr_ty.fmt(pt), |
| 1232 | 1227 | }); |
| ... | ... | @@ -1310,12 +1305,12 @@ const NavGen = struct { |
| 1310 | 1305 | .global, .invocation_global => spv_decl.result_id, |
| 1311 | 1306 | }; |
| 1312 | 1307 | |
| 1313 | | const final_storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); |
| 1314 | | try self.addFunctionDep(spv_decl_index, final_storage_class); |
| 1308 | const storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); |
| 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 | 1314 | .Generic => try self.castToGeneric(decl_ptr_ty_id, decl_id), |
| 1320 | 1315 | else => decl_id, |
| 1321 | 1316 | }; |
| ... | ... | @@ -1401,6 +1396,10 @@ const NavGen = struct { |
| 1401 | 1396 | |
| 1402 | 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 | 1403 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{ |
| 1405 | 1404 | .id_result = result_id, |
| 1406 | 1405 | .storage_class = storage_class, |
| ... | ... | @@ -1505,10 +1504,13 @@ const NavGen = struct { |
| 1505 | 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 | 1510 | const type_name = try self.resolveTypeName(ty); |
| 1510 | 1511 | defer self.gpa.free(type_name); |
| 1511 | 1512 | try self.spv.debugName(result_id, type_name); |
| 1513 | |
| 1512 | 1514 | return result_id; |
| 1513 | 1515 | } |
| 1514 | 1516 | |
| ... | ... | @@ -1640,7 +1642,9 @@ const NavGen = struct { |
| 1640 | 1642 | // can be lowered to ptrAccessChain instead of manually performing the math. |
| 1641 | 1643 | return try self.arrayType(1, elem_ty_id); |
| 1642 | 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 | 1650 | .@"fn" => switch (repr) { |
| ... | ... | @@ -1702,10 +1706,13 @@ const NavGen = struct { |
| 1702 | 1706 | } |
| 1703 | 1707 | |
| 1704 | 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 | 1712 | &.{ ptr_ty_id, size_ty_id }, |
| 1707 | 1713 | &.{ "ptr", "len" }, |
| 1708 | 1714 | ); |
| 1715 | return result_id; |
| 1709 | 1716 | }, |
| 1710 | 1717 | .vector => { |
| 1711 | 1718 | const elem_ty = ty.childType(zcu); |
| ... | ... | @@ -1732,10 +1739,13 @@ const NavGen = struct { |
| 1732 | 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 | 1745 | const type_name = try self.resolveTypeName(ty); |
| 1737 | 1746 | defer self.gpa.free(type_name); |
| 1738 | 1747 | try self.spv.debugName(result_id, type_name); |
| 1748 | |
| 1739 | 1749 | return result_id; |
| 1740 | 1750 | }, |
| 1741 | 1751 | .struct_type => ip.loadStructType(ty.toIntern()), |
| ... | ... | @@ -1752,7 +1762,9 @@ const NavGen = struct { |
| 1752 | 1762 | var member_names = std.ArrayList([]const u8).init(self.gpa); |
| 1753 | 1763 | defer member_names.deinit(); |
| 1754 | 1764 | |
| 1765 | var index: u32 = 0; |
| 1755 | 1766 | var it = struct_type.iterateRuntimeOrder(ip); |
| 1767 | const result_id = self.spv.allocId(); |
| 1756 | 1768 | while (it.next()) |field_index| { |
| 1757 | 1769 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1758 | 1770 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| ... | ... | @@ -1760,16 +1772,25 @@ const NavGen = struct { |
| 1760 | 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 | 1780 | const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse |
| 1764 | 1781 | try ip.getOrPutStringFmt(zcu.gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls); |
| 1765 | 1782 | try member_types.append(try self.resolveType(field_ty, .indirect)); |
| 1766 | 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 | 1790 | const type_name = try self.resolveTypeName(ty); |
| 1771 | 1791 | defer self.gpa.free(type_name); |
| 1772 | 1792 | try self.spv.debugName(result_id, type_name); |
| 1793 | |
| 1773 | 1794 | return result_id; |
| 1774 | 1795 | }, |
| 1775 | 1796 | .optional => { |
| ... | ... | @@ -1789,10 +1810,13 @@ const NavGen = struct { |
| 1789 | 1810 | |
| 1790 | 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 | 1816 | &.{ payload_ty_id, bool_ty_id }, |
| 1794 | 1817 | &.{ "payload", "valid" }, |
| 1795 | 1818 | ); |
| 1819 | return result_id; |
| 1796 | 1820 | }, |
| 1797 | 1821 | .@"union" => return try self.resolveUnionType(ty), |
| 1798 | 1822 | .error_set => return try self.resolveType(Type.u16, repr), |
| ... | ... | @@ -1821,7 +1845,9 @@ const NavGen = struct { |
| 1821 | 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 | 1852 | .@"opaque" => { |
| 1827 | 1853 | const type_name = try self.resolveTypeName(ty); |
| ... | ... | @@ -1851,7 +1877,7 @@ const NavGen = struct { |
| 1851 | 1877 | const target = self.getTarget(); |
| 1852 | 1878 | return switch (as) { |
| 1853 | 1879 | .generic => switch (target.os.tag) { |
| 1854 | | .vulkan => .Private, |
| 1880 | .vulkan => .Function, |
| 1855 | 1881 | .opencl => .Generic, |
| 1856 | 1882 | else => unreachable, |
| 1857 | 1883 | }, |
| ... | ... | @@ -1863,6 +1889,7 @@ const NavGen = struct { |
| 1863 | 1889 | else => unreachable, |
| 1864 | 1890 | }, |
| 1865 | 1891 | .constant => .UniformConstant, |
| 1892 | .push_constant => .PushConstant, |
| 1866 | 1893 | .input => .Input, |
| 1867 | 1894 | .output => .Output, |
| 1868 | 1895 | .uniform => .Uniform, |
| ... | ... | @@ -2954,10 +2981,8 @@ const NavGen = struct { |
| 2954 | 2981 | const spv_err_decl_index = try self.spv.allocDecl(.global); |
| 2955 | 2982 | try self.spv.declareDeclDeps(spv_err_decl_index, &.{}); |
| 2956 | 2983 | |
| 2957 | | const push_constant_struct_ty_id = try self.spv.structType( |
| 2958 | | &.{ptr_anyerror_ty_id}, |
| 2959 | | &.{"error_out_ptr"}, |
| 2960 | | ); |
| 2984 | const push_constant_struct_ty_id = self.spv.allocId(); |
| 2985 | try self.spv.structType(push_constant_struct_ty_id, &.{ptr_anyerror_ty_id}, &.{"error_out_ptr"}); |
| 2961 | 2986 | try self.spv.decorate(push_constant_struct_ty_id, .Block); |
| 2962 | 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 | 3166 | }; |
| 3142 | 3167 | assert(maybe_init_val == null); // TODO |
| 3143 | 3168 | |
| 3144 | | const final_storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); |
| 3145 | | assert(final_storage_class != .Generic); // These should be instance globals |
| 3169 | const storage_class = self.spvStorageClass(nav.status.resolved.@"addrspace"); |
| 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 | 3174 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{ |
| 3150 | 3175 | .id_result_type = ptr_ty_id, |
| 3151 | 3176 | .id_result = result_id, |
| 3152 | | .storage_class = final_storage_class, |
| 3177 | .storage_class = storage_class, |
| 3153 | 3178 | }); |
| 3154 | 3179 | |
| 3155 | 3180 | try self.spv.debugName(result_id, nav.fqn.toSlice(ip)); |