Skip to content

atomic fixes and clarification #24278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -4356,7 +4356,7 @@ comptime {
</p>
<p>
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
an integer or an enum.
an integer, an enum, or a packed struct.
</p>
<p>{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.</p>
{#see_also|@atomicStore|@atomicRmw||@cmpxchgWeak|@cmpxchgStrong#}
Expand All @@ -4370,7 +4370,7 @@ comptime {
</p>
<p>
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
an integer or an enum.
an integer, an enum, or a packed struct.
</p>
<p>{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.</p>
<p>{#syntax#}AtomicRmwOp{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicRmwOp{#endsyntax#}.</p>
Expand All @@ -4384,7 +4384,7 @@ comptime {
</p>
<p>
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
an integer or an enum.
an integer, an enum, or a packed struct.
</p>
<p>{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.</p>
{#see_also|@atomicLoad|@atomicRmw|@cmpxchgWeak|@cmpxchgStrong#}
Expand Down Expand Up @@ -4613,8 +4613,8 @@ comptime {
more efficiently in machine instructions.
</p>
<p>
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
an integer or an enum.
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#},
an integer, an enum, or a packed struct.
</p>
<p>{#syntax#}@typeInfo(@TypeOf(ptr)).pointer.alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
<p>{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.</p>
Expand Down Expand Up @@ -4645,8 +4645,8 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}.
</p>
<p>
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
an integer or an enum.
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#},
an integer, an enum, or a packed struct.
</p>
<p>{#syntax#}@typeInfo(@TypeOf(ptr)).pointer.alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
<p>{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.</p>
Expand Down
7 changes: 6 additions & 1 deletion src/Zcu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3958,7 +3958,12 @@ pub fn atomicPtrAlignment(
}
return .none;
}
if (ty.isAbiInt(zcu)) {
if (switch (ty.zigTypeTag(zcu)) {
.int, .@"enum" => true,
.@"struct" => ty.containerLayout(zcu) == .@"packed",
else => false,
}) {
assert(ty.isAbiInt(zcu));
const bit_count = ty.intInfo(zcu).bits;
if (bit_count > max_atomic_bits) {
diags.* = .{
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4356,9 +4356,11 @@ pub const Object = struct {
fn getAtomicAbiType(o: *Object, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type {
const pt = o.pt;
const zcu = pt.zcu;
const ip = &zcu.intern_pool;
const int_ty = switch (ty.zigTypeTag(zcu)) {
.int => ty,
.@"enum" => ty.intTagType(zcu),
.@"struct" => Type.fromInterned(ip.loadStructType(ty.toIntern()).backingIntTypeUnordered(ip)),
.float => {
if (!is_rmw_xchg) return .none;
return o.builder.intType(@intCast(ty.abiSize(zcu) * 8));
Expand Down Expand Up @@ -11410,7 +11412,7 @@ pub const FuncGen = struct {

if (workaround_disable_truncate) {
// see https://github.com/llvm/llvm-project/issues/64222
// disable the truncation codepath for larger that 32bits value - with this heuristic, the backend passes the test suite.
// disable the truncation codepath for larger than 32bits value - with this heuristic, the backend passes the test suite.
return try fg.wip.load(access_kind, payload_llvm_ty, payload_ptr, payload_alignment, "");
}

Expand Down
16 changes: 11 additions & 5 deletions test/behavior/atomics.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;

const supports_128_bit_atomics = switch (builtin.cpu.arch) {
// TODO: Ideally this could be sync'd with the logic in Sema.
Expand Down Expand Up @@ -362,25 +361,32 @@ test "atomics with different types" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.target.cpu.arch.endian() == .big) return error.SkipZigTest; // #24282

try testAtomicsWithType(bool, true, false);

try testAtomicsWithType(u1, 0, 1);
try testAtomicsWithType(i4, 0, 1);
try testAtomicsWithType(u5, 0, 1);
try testAtomicsWithType(i15, 0, 1);
try testAtomicsWithType(u24, 0, 1);
try testAtomicsWithType(i4, 2, 1);
try testAtomicsWithType(u5, 2, 1);
try testAtomicsWithType(i15, 2, 1);
try testAtomicsWithType(u24, 2, 1);

try testAtomicsWithType(u0, 0, 0);
try testAtomicsWithType(i0, 0, 0);

try testAtomicsWithType(enum(u32) { x = 1234, y = 5678 }, .x, .y);
try testAtomicsWithType(enum(u19) { x = 1234, y = 5678 }, .x, .y);

try testAtomicsWithPackedStruct(
packed struct { x: u7, y: u24, z: bool },
.{ .x = 1, .y = 2, .z = true },
.{ .x = 3, .y = 4, .z = false },
);
try testAtomicsWithPackedStruct(
packed struct { x: u19, y: bool },
.{ .x = 1, .y = true },
.{ .x = 3, .y = false },
);
}

fn testAtomicsWithType(comptime T: type, a: T, b: T) !void {
Expand Down
17 changes: 15 additions & 2 deletions test/cases/compile_errors/atomics_with_invalid_type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ export fn float() void {

const NormalStruct = struct { x: u32 };
export fn normalStruct() void {
var x: NormalStruct = 0;
var x: NormalStruct = .{ .x = 0 };
_ = @cmpxchgWeak(NormalStruct, &x, .{ .x = 1 }, .{ .x = 2 }, .seq_cst, .seq_cst);
}

export fn anyError() void {
var x: anyerror = error.A;
_ = @cmpxchgWeak(anyerror, &x, error.A, error.B, .seq_cst, .seq_cst);
}

const ErrorSet = error{ A, B };
export fn errorSet() void {
var x: ErrorSet = error.A;
_ = @cmpxchgWeak(ErrorSet, &x, error.A, error.B, .seq_cst, .seq_cst);
}

// error
// backend=stage2
// target=native
//
// :3:22: error: expected bool, integer, enum, packed struct, or pointer type; found 'f32'
// :8:27: error: expected type 'tmp.NormalStruct', found 'comptime_int'
// :9:22: error: expected bool, integer, float, enum, packed struct, or pointer type; found 'tmp.NormalStruct'
// :6:22: note: struct declared here
// :14:22: error: expected bool, integer, float, enum, packed struct, or pointer type; found 'anyerror'
// :20:22: error: expected bool, integer, float, enum, packed struct, or pointer type; found 'error{A,B}'
Loading