authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-01-10 03:24:44-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-01-10 06:10:15-05:00
log02692ad78cbdc1086abc4e37739e512c9203f486
tree24805abd8c8d59ce75c8de4c057482a7e1dc5399
parent4e581427d1fee46cdcab088cb2c1d7ad51b06239

cbe: fix miscomps of the compiler


5 files changed, 56 insertions(+), 8 deletions(-)

src/Compilation.zig+6-1
......@@ -251,7 +251,12 @@ reference_trace: ?u32 = null,
251251libcxx_abi_version: libcxx.AbiVersion = libcxx.AbiVersion.default,
252252
253253/// This mutex guards all `Compilation` mutable state.
254mutex: std.Thread.Mutex = .{},
254/// Disabled in single-threaded mode because the thread pool spawns in the same thread.
255mutex: if (builtin.single_threaded) struct {
256 pub inline fn tryLock(_: @This()) void {}
257 pub inline fn lock(_: @This()) void {}
258 pub inline fn unlock(_: @This()) void {}
259} else std.Thread.Mutex = .{},
255260
256261test_filters: []const []const u8,
257262test_name_prefix: ?[]const u8,
src/InternPool.zig+11-6
......@@ -5538,7 +5538,7 @@ pub const Tag = enum(u8) {
55385538 },
55395539 },
55405540 .type_union = .{
5541 .summary = .@"{.payload.name%summary#\"#\"}",
5541 .summary = .@"{.payload.name%summary#\"}",
55425542 .payload = TypeUnion,
55435543 .trailing = struct {
55445544 captures_len: ?u32,
......@@ -6584,7 +6584,7 @@ pub fn init(ip: *InternPool, gpa: Allocator, available_threads: usize) !void {
65846584}
65856585
65866586pub fn deinit(ip: *InternPool, gpa: Allocator) void {
6587 if (!builtin.strip_debug_info) std.debug.assert(debug_state.intern_pool == null);
6587 if (debug_state.enable_checks) std.debug.assert(debug_state.intern_pool == null);
65886588
65896589 ip.file_deps.deinit(gpa);
65906590 ip.src_hash_deps.deinit(gpa);
......@@ -6629,7 +6629,7 @@ pub fn deinit(ip: *InternPool, gpa: Allocator) void {
66296629}
66306630
66316631pub fn activate(ip: *const InternPool) void {
6632 if (builtin.strip_debug_info) return;
6632 if (!debug_state.enable) return;
66336633 _ = Index.Unwrapped.debug_state;
66346634 _ = String.debug_state;
66356635 _ = OptionalString.debug_state;
......@@ -6639,18 +6639,23 @@ pub fn activate(ip: *const InternPool) void {
66396639 _ = TrackedInst.Index.Optional.debug_state;
66406640 _ = Nav.Index.debug_state;
66416641 _ = Nav.Index.Optional.debug_state;
6642 std.debug.assert(debug_state.intern_pool == null);
6642 if (debug_state.enable_checks) std.debug.assert(debug_state.intern_pool == null);
66436643 debug_state.intern_pool = ip;
66446644}
66456645
66466646pub fn deactivate(ip: *const InternPool) void {
6647 if (builtin.strip_debug_info) return;
6647 if (!debug_state.enable) return;
66486648 std.debug.assert(debug_state.intern_pool == ip);
6649 debug_state.intern_pool = null;
6649 if (debug_state.enable_checks) debug_state.intern_pool = null;
66506650}
66516651
66526652/// For debugger access only.
66536653const debug_state = struct {
6654 const enable = switch (builtin.zig_backend) {
6655 else => false,
6656 .stage2_x86_64 => !builtin.strip_debug_info,
6657 };
6658 const enable_checks = enable and !builtin.single_threaded;
66546659 threadlocal var intern_pool: ?*const InternPool = null;
66556660};
66566661
src/codegen/c.zig+20-1
......@@ -7419,7 +7419,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
74197419 .@"packed" => {
74207420 try f.writeCValue(writer, local, .Other);
74217421 try writer.writeAll(" = ");
7422 const int_info = inst_ty.intInfo(zcu);
7422
7423 const backing_int_ty: Type = .fromInterned(loaded_struct.backingIntTypeUnordered(ip));
7424 const int_info = backing_int_ty.intInfo(zcu);
74237425
74247426 const bit_offset_ty = try pt.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1));
74257427
......@@ -7450,6 +7452,12 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
74507452 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty);
74517453 try writer.writeByte('(');
74527454
7455 if (field_ty.isAbiInt(zcu)) {
7456 try writer.writeAll("zig_and_");
7457 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty);
7458 try writer.writeByte('(');
7459 }
7460
74537461 if (inst_ty.isAbiInt(zcu) and (field_ty.isAbiInt(zcu) or field_ty.isPtrAtRuntime(zcu))) {
74547462 try f.renderIntCast(writer, inst_ty, element, .{}, field_ty, .FunctionArgument);
74557463 } else {
......@@ -7467,6 +7475,17 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
74677475 try f.writeCValue(writer, element, .Other);
74687476 }
74697477
7478 if (field_ty.isAbiInt(zcu)) {
7479 try writer.writeAll(", ");
7480 const field_int_info = field_ty.intInfo(zcu);
7481 const field_mask = if (int_info.signedness == .signed and int_info.bits == field_int_info.bits)
7482 try pt.intValue(backing_int_ty, -1)
7483 else
7484 try (try pt.intType(.unsigned, field_int_info.bits)).maxIntScalar(pt, backing_int_ty);
7485 try f.object.dg.renderValue(writer, field_mask, .FunctionArgument);
7486 try writer.writeByte(')');
7487 }
7488
74707489 try writer.print(", {}", .{
74717490 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
74727491 });
test/behavior/export_keyword.zig+2
......@@ -41,6 +41,8 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {
4141}
4242
4343test "export function alias" {
44 if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
45
4446 _ = struct {
4547 fn foo_internal() callconv(.C) u32 {
4648 return 123;
test/behavior/packed-struct.zig+17
......@@ -1327,3 +1327,20 @@ test "packed struct with signed field" {
13271327 try expect(s.a == -1);
13281328 try expect(s.b == 42);
13291329}
1330
1331test "assign packed struct initialized with RLS to packed struct literal field" {
1332 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch.isWasm()) return error.SkipZigTest;
1333 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
1334 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
1335
1336 const Inner = packed struct { x: u17 };
1337 const Outer = packed struct { inner: Inner, x: u15 };
1338
1339 var x: u15 = undefined;
1340 x = 23385;
1341 var inner: Inner = undefined;
1342 inner = .{ .x = x };
1343 const outer = Outer{ .x = x, .inner = inner };
1344 try expect(outer.inner.x == x);
1345 try expect(outer.x == x);
1346}