authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2025-03-12 16:52:03+03:30
committergravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2025-03-18 07:05:50+03:30
logd18eaf8586cf173d5605d5885fcbe26d64af00c5
treec7fab57686d0931e975ab2f362b4a48b3c01551a
parent54c097f50ddc794dc2b3890490379ab2f8371443

spirv: aligned load for physical storage variables

Resolves #23212

5 files changed, 83 insertions(+), 54 deletions(-)

src/codegen/spirv.zig+81-34
......@@ -1024,6 +1024,11 @@ const NavGen = struct {
10241024 else => unreachable,
10251025 },
10261026 .un => |un| {
1027 if (un.tag == .none) {
1028 assert(ty.containerLayout(zcu) == .@"packed"); // TODO
1029 const int_ty = try pt.intType(.unsigned, @intCast(ty.bitSize(zcu)));
1030 return try self.constant(int_ty, Value.fromInterned(un.val), .direct);
1031 }
10271032 const active_field = ty.unionTagFieldIndex(Value.fromInterned(un.tag), zcu).?;
10281033 const union_obj = zcu.typeToUnion(ty).?;
10291034 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[active_field]);
......@@ -1356,7 +1361,7 @@ const NavGen = struct {
13561361 const union_obj = zcu.typeToUnion(ty).?;
13571362
13581363 if (union_obj.flagsUnordered(ip).layout == .@"packed") {
1359 return self.todo("packed union types", .{});
1364 return try self.intType(.unsigned, @intCast(ty.bitSize(zcu)));
13601365 }
13611366
13621367 const layout = self.unionLayout(ty);
......@@ -3226,10 +3231,13 @@ const NavGen = struct {
32263231 };
32273232
32283233 fn load(self: *NavGen, value_ty: Type, ptr_id: IdRef, options: MemoryOptions) !IdRef {
3234 const zcu = self.pt.zcu;
3235 const alignment: u32 = @intCast(value_ty.abiAlignment(zcu).toByteUnits().?);
32293236 const indirect_value_ty_id = try self.resolveType(value_ty, .indirect);
32303237 const result_id = self.spv.allocId();
32313238 const access = spec.MemoryAccess.Extended{
32323239 .Volatile = options.is_volatile,
3240 .Aligned = .{ .literal_integer = alignment },
32333241 };
32343242 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
32353243 .id_result_type = indirect_value_ty_id,
......@@ -5130,11 +5138,33 @@ const NavGen = struct {
51305138 const union_ty = zcu.typeToUnion(ty).?;
51315139 const tag_ty = Type.fromInterned(union_ty.enum_tag_ty);
51325140
5141 const layout = self.unionLayout(ty);
5142 const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]);
5143
51335144 if (union_ty.flagsUnordered(ip).layout == .@"packed") {
5134 unreachable; // TODO
5135 }
5145 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
5146 const int_ty = try pt.intType(.unsigned, @intCast(ty.bitSize(zcu)));
5147 return self.constInt(int_ty, 0);
5148 }
51365149
5137 const layout = self.unionLayout(ty);
5150 assert(payload != null);
5151 if (payload_ty.isInt(zcu)) {
5152 if (ty.bitSize(zcu) == payload_ty.bitSize(zcu)) {
5153 return self.bitCast(ty, payload_ty, payload.?);
5154 }
5155
5156 const trunc = try self.buildIntConvert(ty, .{ .ty = payload_ty, .value = .{ .singleton = payload.? } });
5157 return try trunc.materialize(self);
5158 }
5159
5160 const payload_int_ty = try pt.intType(.unsigned, @intCast(payload_ty.bitSize(zcu)));
5161 const payload_int = if (payload_ty.ip_index == .bool_type)
5162 try self.convertToIndirect(payload_ty, payload.?)
5163 else
5164 try self.bitCast(payload_int_ty, payload_ty, payload.?);
5165 const trunc = try self.buildIntConvert(ty, .{ .ty = payload_int_ty, .value = .{ .singleton = payload_int } });
5166 return try trunc.materialize(self);
5167 }
51385168
51395169 const tag_int = if (layout.tag_size != 0) blk: {
51405170 const tag_val = try pt.enumValueFieldIndex(tag_ty, active_field);
......@@ -5155,7 +5185,6 @@ const NavGen = struct {
51555185 try self.store(tag_ty, ptr_id, tag_id, .{});
51565186 }
51575187
5158 const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]);
51595188 if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
51605189 const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, .Function, .indirect);
51615190 const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index});
......@@ -5198,7 +5227,6 @@ const NavGen = struct {
51985227 fn airStructFieldVal(self: *NavGen, inst: Air.Inst.Index) !?IdRef {
51995228 const pt = self.pt;
52005229 const zcu = pt.zcu;
5201 const ip = &zcu.intern_pool;
52025230 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
52035231 const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data;
52045232
......@@ -5213,16 +5241,39 @@ const NavGen = struct {
52135241 .@"struct" => switch (object_ty.containerLayout(zcu)) {
52145242 .@"packed" => {
52155243 const struct_ty = zcu.typeToPackedStruct(object_ty).?;
5216 const backing_int_ty = Type.fromInterned(struct_ty.backingIntTypeUnordered(ip));
52175244 const bit_offset = pt.structPackedFieldBitOffset(struct_ty, field_index);
52185245 const bit_offset_id = try self.constInt(.u16, bit_offset);
52195246 const signedness = if (field_ty.isInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned;
52205247 const field_bit_size: u16 = @intCast(field_ty.bitSize(zcu));
5221 const int_ty = try pt.intType(signedness, field_bit_size);
5222 const shift_lhs: Temporary = .{ .ty = backing_int_ty, .value = .{ .singleton = object_id } };
5248 const field_int_ty = try pt.intType(signedness, field_bit_size);
5249 const shift_lhs: Temporary = .{ .ty = object_ty, .value = .{ .singleton = object_id } };
52235250 const shift = try self.buildBinary(.srl, shift_lhs, .{ .ty = .u16, .value = .{ .singleton = bit_offset_id } });
5251 const mask_id = try self.constInt(object_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1);
5252 const masked = try self.buildBinary(.bit_and, shift, .{ .ty = object_ty, .value = .{ .singleton = mask_id } });
5253 const result_id = blk: {
5254 if (self.backingIntBits(field_bit_size).? == self.backingIntBits(@intCast(object_ty.bitSize(zcu))).?)
5255 break :blk try self.bitCast(field_int_ty, object_ty, try masked.materialize(self));
5256 const trunc = try self.buildIntConvert(field_int_ty, masked);
5257 break :blk try trunc.materialize(self);
5258 };
5259 if (field_ty.ip_index == .bool_type) return try self.convertToDirect(.bool, result_id);
5260 if (field_ty.isInt(zcu)) return result_id;
5261 return try self.bitCast(field_ty, field_int_ty, result_id);
5262 },
5263 else => return try self.extractField(field_ty, object_id, field_index),
5264 },
5265 .@"union" => switch (object_ty.containerLayout(zcu)) {
5266 .@"packed" => {
5267 const backing_int_ty = try pt.intType(.unsigned, @intCast(object_ty.bitSize(zcu)));
5268 const signedness = if (field_ty.isInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned;
5269 const field_bit_size: u16 = @intCast(field_ty.bitSize(zcu));
5270 const int_ty = try pt.intType(signedness, field_bit_size);
52245271 const mask_id = try self.constInt(backing_int_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1);
5225 const masked = try self.buildBinary(.bit_and, shift, .{ .ty = backing_int_ty, .value = .{ .singleton = mask_id } });
5272 const masked = try self.buildBinary(
5273 .bit_and,
5274 .{ .ty = backing_int_ty, .value = .{ .singleton = object_id } },
5275 .{ .ty = backing_int_ty, .value = .{ .singleton = mask_id } },
5276 );
52265277 const result_id = blk: {
52275278 if (self.backingIntBits(field_bit_size).? == self.backingIntBits(@intCast(backing_int_ty.bitSize(zcu))).?)
52285279 break :blk try self.bitCast(int_ty, backing_int_ty, try masked.materialize(self));
......@@ -5233,10 +5284,6 @@ const NavGen = struct {
52335284 if (field_ty.isInt(zcu)) return result_id;
52345285 return try self.bitCast(field_ty, int_ty, result_id);
52355286 },
5236 else => return try self.extractField(field_ty, object_id, field_index),
5237 },
5238 .@"union" => switch (object_ty.containerLayout(zcu)) {
5239 .@"packed" => unreachable, // TODO
52405287 else => {
52415288 // Store, ptr-elem-ptr, pointer-cast, load
52425289 const layout = self.unionLayout(object_ty);
......@@ -5317,28 +5364,28 @@ const NavGen = struct {
53175364 return try self.accessChain(result_ty_id, object_ptr, &.{field_index});
53185365 },
53195366 },
5320 .@"union" => switch (object_ty.containerLayout(zcu)) {
5321 .@"packed" => return self.todo("implement field access for packed unions", .{}),
5322 else => {
5323 const layout = self.unionLayout(object_ty);
5324 if (!layout.has_payload) {
5325 // Asked to get a pointer to a zero-sized field. Just lower this
5326 // to undefined, there is no reason to make it be a valid pointer.
5327 return try self.spv.constUndef(result_ty_id);
5328 }
5367 .@"union" => {
5368 const layout = self.unionLayout(object_ty);
5369 if (!layout.has_payload) {
5370 // Asked to get a pointer to a zero-sized field. Just lower this
5371 // to undefined, there is no reason to make it be a valid pointer.
5372 return try self.spv.constUndef(result_ty_id);
5373 }
53295374
5330 const storage_class = self.spvStorageClass(object_ptr_ty.ptrAddressSpace(zcu));
5331 const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, storage_class, .indirect);
5332 const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index});
5375 const storage_class = self.spvStorageClass(object_ptr_ty.ptrAddressSpace(zcu));
5376 const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, storage_class, .indirect);
5377 const pl_ptr_id = blk: {
5378 if (object_ty.containerLayout(zcu) == .@"packed") break :blk object_ptr;
5379 break :blk try self.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index});
5380 };
53335381
5334 const active_pl_ptr_id = self.spv.allocId();
5335 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
5336 .id_result_type = result_ty_id,
5337 .id_result = active_pl_ptr_id,
5338 .operand = pl_ptr_id,
5339 });
5340 return active_pl_ptr_id;
5341 },
5382 const active_pl_ptr_id = self.spv.allocId();
5383 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
5384 .id_result_type = result_ty_id,
5385 .id_result = active_pl_ptr_id,
5386 .operand = pl_ptr_id,
5387 });
5388 return active_pl_ptr_id;
53425389 },
53435390 else => unreachable,
53445391 }
test/behavior/cast_int.zig-4
......@@ -22,7 +22,6 @@ test "coerce i8 to i32 and @intCast back" {
2222 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
2323 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
2424 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
25 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
2625
2726 var x: i8 = -5;
2827 var y: i32 = -5;
......@@ -36,8 +35,6 @@ test "coerce i8 to i32 and @intCast back" {
3635}
3736
3837test "coerce non byte-sized integers accross 32bits boundary" {
39 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
40
4138 {
4239 var v: u21 = 6417;
4340 _ = &v;
......@@ -217,7 +214,6 @@ test "load non byte-sized value in union" {
217214 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
218215 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
219216 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
220 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
221217 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
222218 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
223219
test/behavior/export_keyword.zig-1
......@@ -25,7 +25,6 @@ const PackedUnion = packed union {
2525
2626test "packed struct, enum, union parameters in extern function" {
2727 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
28 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
2928
3029 testPackedStuff(&(PackedStruct{
3130 .a = 1,
test/behavior/packed-union.zig-3
......@@ -137,7 +137,6 @@ test "packed union initialized with a runtime value" {
137137 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
138138 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
139139 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
140 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
141140 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
142141
143142 const Fields = packed struct {
......@@ -174,8 +173,6 @@ test "assigning to non-active field at comptime" {
174173}
175174
176175test "comptime packed union of pointers" {
177 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
178
179176 const U = packed union {
180177 a: *const u32,
181178 b: *const [1]u32,
test/behavior/union.zig+2-12
......@@ -1372,14 +1372,13 @@ test "packed union in packed struct" {
13721372 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
13731373 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
13741374 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1375 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
13761375
13771376 const S = packed struct {
13781377 nested: packed union {
1379 val: usize,
1378 val: u16,
13801379 foo: u32,
13811380 },
1382 bar: u32,
1381 bar: u16,
13831382
13841383 fn unpack(self: @This()) usize {
13851384 return self.nested.foo;
......@@ -1460,7 +1459,6 @@ test "packed union with zero-bit field" {
14601459 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
14611460 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
14621461 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1463 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
14641462
14651463 const S = packed struct {
14661464 nested: packed union {
......@@ -1479,7 +1477,6 @@ test "packed union with zero-bit field" {
14791477test "reinterpreting enum value inside packed union" {
14801478 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
14811479 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1482 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
14831480
14841481 const U = packed union {
14851482 tag: enum(u8) { a, b },
......@@ -1527,7 +1524,6 @@ test "defined-layout union field pointer has correct alignment" {
15271524 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
15281525 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
15291526 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1530 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
15311527
15321528 const S = struct {
15331529 fn doTheTest(comptime U: type) !void {
......@@ -1901,8 +1897,6 @@ test "inner struct initializer uses union layout" {
19011897}
19021898
19031899test "inner struct initializer uses packed union layout" {
1904 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1905
19061900 const namespace = struct {
19071901 const U = packed union {
19081902 a: packed struct {
......@@ -1946,8 +1940,6 @@ test "extern union initialized via reintepreted struct field initializer" {
19461940}
19471941
19481942test "packed union initialized via reintepreted struct field initializer" {
1949 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1950
19511943 const bytes = [_]u8{ 0xaa, 0xbb, 0xcc, 0xdd };
19521944
19531945 const U = packed union {
......@@ -1988,8 +1980,6 @@ test "store of comptime reinterpreted memory to extern union" {
19881980}
19891981
19901982test "store of comptime reinterpreted memory to packed union" {
1991 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1992
19931983 const bytes = [_]u8{ 0xaa, 0xbb, 0xcc, 0xdd };
19941984
19951985 const U = packed union {