Skip to content

C backend: generate GCC-compatible code #13575

Open

Description

@andrewrk

This issue can be closed when we add CI test coverage that compiles the behavior tests with -ofmt=c and then compiles & runs them with GCC.

Here's what happens currently, with GCC 11.3.0:

$ stage3/bin/zig test ../test/behavior.zig -I../test -ofmt=c -femit-bin=test.c
$ gcc -o test test.c
test.c: In function ‘behavior_atomics_testAtomicRmwFloat’:
test.c:19080:2: error: operand type ‘_Atomic zig_f32 *’ {aka ‘_Atomic float *’} is incompatible with argument 1 of ‘__atomic_fetch_add’
19080 |  zig_f32 const t10 = zig_atomicrmw_add((zig_atomic(zig_f32 ) *)&t0, (zig_f32 )zig_as_f32(0x1.4p2, zig_as_i32(0x40a00000)), memory_order_seq_cst);
      |  ^~~~~~~
test.c:19094:2: error: operand type ‘_Atomic zig_f32 *’ {aka ‘_Atomic float *’} is incompatible with argument 1 of ‘__atomic_fetch_sub’
19094 |  zig_f32 const t15 = zig_atomicrmw_sub((zig_atomic(zig_f32 ) *)&t0, (zig_f32 )zig_as_f32(0x1p1, zig_as_i32(0x40000000)), memory_order_seq_cst);
      |  ^~~~~~~
In file included from test.c:1:
test.c: In function ‘behavior_atomics_testAtomicRmwInt’:
zig.h:127:45: warning: implicit declaration of function ‘__atomic_fetch_max’; did you mean ‘__atomic_fetch_add’? [-Wimplicit-function-declaration]
  127 | #define  zig_atomicrmw_max(obj, arg, order) __atomic_fetch_max        (obj, arg, order)
      |                                             ^~~~~~~~~~~~~~~~~~
test.c:19263:21: note: in expansion of macro ‘zig_atomicrmw_max’
19263 |  zig_u8 const t41 = zig_atomicrmw_max((zig_u8  *)&t0, zig_as_u8(1), memory_order_seq_cst);
      |                     ^~~~~~~~~~~~~~~~~
zig.h:126:45: warning: implicit declaration of function ‘__atomic_fetch_min’; did you mean ‘__atomic_fetch_add’? [-Wimplicit-function-declaration]
  126 | #define  zig_atomicrmw_min(obj, arg, order) __atomic_fetch_min        (obj, arg, order)
      |                                             ^~~~~~~~~~~~~~~~~~
test.c:19277:21: note: in expansion of macro ‘zig_atomicrmw_min’
19277 |  zig_u8 const t46 = zig_atomicrmw_min((zig_u8  *)&t0, zig_as_u8(1), memory_order_seq_cst);
      |                     ^~~~~~~~~~~~~~~~~

Activity

  1. added
    enhancement
    contributor friendly
    backend-c
    on Nov 17, 2022
  2. added this to the 0.11.0 milestone on Nov 17, 2022
  3. modified the milestones: 0.11.0, 0.12.0 on Jun 19, 2023
  4. matu3ba commented on Jun 24, 2023

    @matu3ba
    Contributor

    2 issues:

    • 1. removing the atomics test yields in linker error:
    diff --git a/test/behavior/atomics.zig b/test/behavior/atomics.zig
    index 4394e62f6f..f4308ad969 100644
    --- a/test/behavior/atomics.zig
    +++ b/test/behavior/atomics.zig
    @@ -3,224 +3,20 @@ 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.
    -    .aarch64, .aarch64_be, .aarch64_32 => true,
    -    .x86_64 => std.Target.x86.featureSetHas(builtin.cpu.features, .cx16),
    -    else => false,
    -};
    -
    -test "cmpxchg" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    try testCmpxchg();
    -    try comptime testCmpxchg();
    -}
    -
    -fn testCmpxchg() !void {
    -    var x: i32 = 1234;
    -    if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
    -        try expect(x1 == 1234);
    -    } else {
    -        @panic("cmpxchg should have failed");
    -    }
    -
    -    while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
    -        try expect(x1 == 1234);
    -    }
    -    try expect(x == 5678);
    -
    -    try expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);
    -    try expect(x == 42);
    -}
    -
    -test "fence" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    var x: i32 = 1234;
    -    @fence(.SeqCst);
    -    x = 5678;
    -}
    -
    -test "atomicrmw and atomicload" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    var data: u8 = 200;
    -    try testAtomicRmw(&data);
    -    try expect(data == 42);
    -    try testAtomicLoad(&data);
    -}
    -
    -fn testAtomicRmw(ptr: *u8) !void {
    -    const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst);
    -    try expect(prev_value == 200);
    -    comptime {
    -        var x: i32 = 1234;
    -        const y: i32 = 12345;
    -        try expect(@atomicLoad(i32, &x, .SeqCst) == 1234);
    -        try expect(@atomicLoad(i32, &y, .SeqCst) == 12345);
    -    }
    -}
    -
    -fn testAtomicLoad(ptr: *u8) !void {
    -    const x = @atomicLoad(u8, ptr, .SeqCst);
    -    try expect(x == 42);
    -}
    -
    -test "cmpxchg with ptr" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    var data1: i32 = 1234;
    -    var data2: i32 = 5678;
    -    var data3: i32 = 9101;
    -    var x: *i32 = &data1;
    -    if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| {
    -        try expect(x1 == &data1);
    -    } else {
    -        @panic("cmpxchg should have failed");
    -    }
    -
    -    while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| {
    -        try expect(x1 == &data1);
    -    }
    -    try expect(x == &data3);
    -
    -    try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);
    -    try expect(x == &data2);
    -}
    -
    -test "cmpxchg with ignored result" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    var x: i32 = 1234;
    -
    -    _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);
    -
    -    try expect(5678 == x);
    -}
    -
    -test "128-bit cmpxchg" {
    -    if (!supports_128_bit_atomics) return error.SkipZigTest;
    -
    -    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -
    -    try test_u128_cmpxchg();
    -    try comptime test_u128_cmpxchg();
    -}
    -
    -fn test_u128_cmpxchg() !void {
    -    var x: u128 align(16) = 1234;
    -    if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
    -        try expect(x1 == 1234);
    -    } else {
    -        @panic("cmpxchg should have failed");
    -    }
    -
    -    while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
    -        try expect(x1 == 1234);
    -    }
    -    try expect(x == 5678);
    -
    -    try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
    -    try expect(x == 42);
    -}
    -
    -var a_global_variable = @as(u32, 1234);
    -
    -test "cmpxchg on a global variable" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
    -        // https://github.com/ziglang/zig/issues/10627
    -        return error.SkipZigTest;
    -    }
    -
    -    _ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
    -    try expect(a_global_variable == 42);
    -}
    -
    -test "atomic load and rmw with enum" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    const Value = enum(u8) { a, b, c };
    -    var x = Value.a;
    -
    -    try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
    -
    -    _ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst);
    -    try expect(@atomicLoad(Value, &x, .SeqCst) == .c);
    -    try expect(@atomicLoad(Value, &x, .SeqCst) != .a);
    -    try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
    -}
    -
    -test "atomic store" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    var x: u32 = 0;
    -    @atomicStore(u32, &x, 1, .SeqCst);
    -    try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
    -    @atomicStore(u32, &x, 12345678, .SeqCst);
    -    try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
    -}
    -
    -test "atomic store comptime" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    try comptime testAtomicStore();
    -    try testAtomicStore();
    -}
    -
    -fn testAtomicStore() !void {
    -    var x: u32 = 0;
    -    @atomicStore(u32, &x, 1, .SeqCst);
    -    try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
    -    @atomicStore(u32, &x, 12345678, .SeqCst);
    -    try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
    -}
    -
    -test "atomicrmw with floats" {
    -    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
    -        // https://github.com/ziglang/zig/issues/10627
    -        return error.SkipZigTest;
    -    }
    -    try testAtomicRmwFloat();
    -    try comptime testAtomicRmwFloat();
    -}
    +// test "atomicrmw with floats" {
    +//     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
    +//     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    +//     if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    +//     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    +//     if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    +//
    +//     if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
    +//         // https://github.com/ziglang/zig/issues/10627
    +//         return error.SkipZigTest;
    +//     }
    +//     try testAtomicRmwFloat();
    +//     // try comptime testAtomicRmwFloat();
    +// }
     
     fn testAtomicRmwFloat() !void {
         var x: f32 = 0;
    @@ -236,210 +32,3 @@ fn testAtomicRmwFloat() !void {
         _ = @atomicRmw(f32, &x, .Min, 42, .SeqCst);
         try expect(x == 13);
     }
    -
    -test "atomicrmw with ints" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    try testAtomicRmwInts();
    -    try comptime testAtomicRmwInts();
    -}
    -
    -fn testAtomicRmwInts() !void {
    -    // TODO: Use the max atomic bit size for the target, maybe builtin?
    -    try testAtomicRmwInt(.unsigned, 8);
    -
    -    if (builtin.cpu.arch == .x86_64) {
    -        try testAtomicRmwInt(.unsigned, 16);
    -        try testAtomicRmwInt(.unsigned, 32);
    -        try testAtomicRmwInt(.unsigned, 64);
    -    }
    -}
    -
    -fn testAtomicRmwInt(comptime signedness: std.builtin.Signedness, comptime N: usize) !void {
    -    const int = std.meta.Int(signedness, N);
    -
    -    var x: int = 1;
    -    var res = @atomicRmw(int, &x, .Xchg, 3, .SeqCst);
    -    try expect(x == 3 and res == 1);
    -
    -    res = @atomicRmw(int, &x, .Add, 3, .SeqCst);
    -    var y: int = 3;
    -    try expect(res == y);
    -    y = y + 3;
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Sub, 1, .SeqCst);
    -    try expect(res == y);
    -    y = y - 1;
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .And, 4, .SeqCst);
    -    try expect(res == y);
    -    y = y & 4;
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Nand, 4, .SeqCst);
    -    try expect(res == y);
    -    y = ~(y & 4);
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Or, 6, .SeqCst);
    -    try expect(res == y);
    -    y = y | 6;
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Xor, 2, .SeqCst);
    -    try expect(res == y);
    -    y = y ^ 2;
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Max, 1, .SeqCst);
    -    try expect(res == y);
    -    y = @max(y, 1);
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Min, 1, .SeqCst);
    -    try expect(res == y);
    -    y = @min(y, 1);
    -    try expect(x == y);
    -}
    -
    -test "atomicrmw with 128-bit ints" {
    -    if (!supports_128_bit_atomics) return error.SkipZigTest;
    -
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
    -
    -    // TODO "ld.lld: undefined symbol: __sync_lock_test_and_set_16" on -mcpu x86_64
    -    if (builtin.cpu.arch == .x86_64 and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest;
    -
    -    try testAtomicRmwInt128(.signed);
    -    try testAtomicRmwInt128(.unsigned);
    -    try comptime testAtomicRmwInt128(.signed);
    -    try comptime testAtomicRmwInt128(.unsigned);
    -}
    -
    -fn testAtomicRmwInt128(comptime signedness: std.builtin.Signedness) !void {
    -    const uint = std.meta.Int(.unsigned, 128);
    -    const int = std.meta.Int(signedness, 128);
    -
    -    const initial: int = @bitCast(int, @as(uint, 0xaaaaaaaa_bbbbbbbb_cccccccc_dddddddd));
    -    const replacement: int = 0x00000000_00000005_00000000_00000003;
    -
    -    var x: int align(16) = initial;
    -    var res = @atomicRmw(int, &x, .Xchg, replacement, .SeqCst);
    -    try expect(x == replacement and res == initial);
    -
    -    var operator: int = 0x00000001_00000000_20000000_00000000;
    -    res = @atomicRmw(int, &x, .Add, operator, .SeqCst);
    -    var y: int = replacement;
    -    try expect(res == y);
    -    y = y + operator;
    -    try expect(x == y);
    -
    -    operator = 0x00000000_10000000_00000000_20000000;
    -    res = @atomicRmw(int, &x, .Sub, operator, .SeqCst);
    -    try expect(res == y);
    -    y = y - operator;
    -    try expect(x == y);
    -
    -    operator = 0x12345678_87654321_12345678_87654321;
    -    res = @atomicRmw(int, &x, .And, operator, .SeqCst);
    -    try expect(res == y);
    -    y = y & operator;
    -    try expect(x == y);
    -
    -    operator = 0x00000000_10000000_00000000_20000000;
    -    res = @atomicRmw(int, &x, .Nand, operator, .SeqCst);
    -    try expect(res == y);
    -    y = ~(y & operator);
    -    try expect(x == y);
    -
    -    operator = 0x12340000_56780000_67890000_98760000;
    -    res = @atomicRmw(int, &x, .Or, operator, .SeqCst);
    -    try expect(res == y);
    -    y = y | operator;
    -    try expect(x == y);
    -
    -    operator = 0x0a0b0c0d_0e0f0102_03040506_0708090a;
    -    res = @atomicRmw(int, &x, .Xor, operator, .SeqCst);
    -    try expect(res == y);
    -    y = y ^ operator;
    -    try expect(x == y);
    -
    -    operator = 0x00000000_10000000_00000000_20000000;
    -    res = @atomicRmw(int, &x, .Max, operator, .SeqCst);
    -    try expect(res == y);
    -    y = @max(y, operator);
    -    try expect(x == y);
    -
    -    res = @atomicRmw(int, &x, .Min, operator, .SeqCst);
    -    try expect(res == y);
    -    y = @min(y, operator);
    -    try expect(x == y);
    -}
    -
    -test "atomics with different types" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    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(u0, 0, 0);
    -    try testAtomicsWithType(i0, 0, 0);
    -}
    -
    -fn testAtomicsWithType(comptime T: type, a: T, b: T) !void {
    -    var x: T = b;
    -    @atomicStore(T, &x, a, .SeqCst);
    -    try expect(x == a);
    -    try expect(@atomicLoad(T, &x, .SeqCst) == a);
    -    try expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);
    -    try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);
    -    if (@sizeOf(T) != 0)
    -        try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a);
    -}
    -
    -test "return @atomicStore, using it as a void value" {
    -    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    -    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    -
    -    const S = struct {
    -        const A = struct {
    -            value: usize,
    -
    -            pub fn store(self: *A, value: usize) void {
    -                return @atomicStore(usize, &self.value, value, .Unordered);
    -            }
    -
    -            pub fn store2(self: *A, value: usize) void {
    -                return switch (value) {
    -                    else => @atomicStore(usize, &self.value, value, .Unordered),
    -                };
    -            }
    -        };
    -
    -        fn doTheTest() !void {
    -            var x: A = .{ .value = 5 };
    -            x.store(10);
    -            try expect(x.value == 10);
    -            x.store(100);
    -            try expect(x.value == 100);
    -        }
    -    };
    -    try S.doTheTest();
    -    try comptime S.doTheTest();
    -}
    git apply atomics_hack.diff
    ./deb/bin/zig test test/behavior.zig -Itest -ofmt=c -femit-bin=test_beh.c && gcc -o test_beh test_beh.c -Ilib
    # a bunch of [-Wincompatible-pointer-types] + linker error:
    /usr/bin/ld: /tmp/ccBWLma6.o: in function `_start':
    test_beh.c:(.text+0x7f18): multiple definition of `_start'; /usr/lib/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../lib/Scrt1.o:(.text+0x0): first defined here
    /usr/bin/ld: /tmp/ccBWLma6.o: relocation R_X86_64_32S against symbol `argc_argv_ptr' can not be used when making a PIE object; recompile with -fPIE
    /usr/bin/ld: failed to set dynamic section sizes: bad value
    collect2: Fehler: ld gab 1 als Ende-Status zurück
    ./deb/bin/zig test test/behavior.zig -Itest -ofmt=c -femit-bin=test_beh.c && gcc -o test_beh test_beh.c -Ilib -fPIE
    # same multiple definition and same linker error (-fPIE not applied?!?!?!?!):
    /usr/bin/ld: /tmp/ccaVDtJo.o: in function `_start':
    test_beh.c:(.text+0x7f18): multiple definition of `_start'; /usr/lib/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../lib/Scrt1.o:(.text+0x0): first defined here
    /usr/bin/ld: /tmp/ccaVDtJo.o: relocation R_X86_64_32S against symbol `argc_argv_ptr' can not be used when making a PIE object; recompile with -fPIE
    /usr/bin/ld: failed to set dynamic section sizes: bad value
    collect2: Fehler: ld gab 1 als Ende-Status zurück
    • 2. minimal reproducible test case:
    // ./deb/bin/zig test atomicrw_floats.zig -Itest -ofmt=c -femit-bin=test2.c && gcc -o test test2.c -Ilib
    const std = @import("std");
    const builtin = @import("builtin");
    const expect = std.testing.expect;
    const expectEqual = std.testing.expectEqual;
    
    test "atomicrmw with floats" {
        if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
        if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
        if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
        if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
        if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
    
        if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
            // https://github.com/ziglang/zig/issues/10627
            return error.SkipZigTest;
        }
        try testAtomicRmwFloat();
        // try comptime testAtomicRmwFloat();
    }
    
    fn testAtomicRmwFloat() !void {
        var x: f32 = 0;
        try expect(x == 0);
        _ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst);
        try expect(x == 1);
        _ = @atomicRmw(f32, &x, .Add, 5, .SeqCst);
        // try expect(x == 6);
        // _ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst);
        // try expect(x == 4);
        // _ = @atomicRmw(f32, &x, .Max, 13, .SeqCst);
        // try expect(x == 13);
        // _ = @atomicRmw(f32, &x, .Min, 42, .SeqCst);
        // try expect(x == 13);
    }
    
    user@pc ~/d/g/z/z/cbe_gcc_compat1 (cbe_gcc_compat1) [1]> ./deb/bin/zig test atomicrw_floats.zig -Itest -ofmt=c -femit-bin=t2.c && gcc -o t2 t2.c -Il
    ib
    All 1 tests passed.
    t2.c: In Funktion »atomicrw_floats_testAtomicRmwFloat__111«:
    t2.c:5362:2: Fehler: Operandentyp »_Atomic zig_f32 *« {alias »_Atomic float *«} ist inkompatibel mit Argument 1 von »__atomic_fetch_add«
     5362 |  zig_atomicrmw_add_float(t1, (zig_atomic(zig_f32) *)&t0, t4, memory_order_seq_cst, f32, uint32_t);
          |  ^~~~~~~~~~~~~~~~~~~~~~~
    

    I suspect problem 2 can be fixed with braces, see here https://www.ibm.com/docs/en/zos/2.1.0?topic=specifiers-atomic-type-c11and there is also -Wmissing-braces not being used yet besides a pile of other flags.

  5. matu3ba commented on Jul 21, 2023

    @matu3ba
    Contributor

    So it looks like

      1. gcc and clang dont like multiple _start symbols
      1. pp-trace is useless (~/.local/llvm/bin/pp-trace -output test2.pptrace -callbacks=MacroExpands test2.c -- -Ilib) truncates output
      1. gcc -Ilib -E test2.c > test2.c.gcc and clang -Ilib -E test2.c > test2.c.clang show nothing obvious.
      1. Some updates made clang now complain, but generate a test2.c file:
    user@pc ~/d/g/z/z/cbe_gcc_compat1 (cbe_gcc_compat1)> ./deb/bin/zig test atomicrw_floats.zig -Itest -ofmt=c 
    -femit-bin=test2.c 
    error(compilation): clang failed with stderr: ./test2.c:42097:147: error: asm-specifier for input or output variab
    le conflicts with asm clobber list
    ./test2.c:59928:2: error: non-ASM statement in naked function is not supported
    ./test2.c:59926:8: note: attribute is here
    deb/lib/zig/zig.h:74:34: note: expanded from macro 'zig_naked'
    
    ./test2.c:1:1: error: unable to build C object: clang exited with code 1
    error: the following test command failed with exit code 1:
    /home/user/dev/git/zi/zig/cbe_gcc_compat1/deb/bin/zig run -I deb/lib/zig ./test2.c
    
  6. modified the milestones: 0.14.0, 0.15.0 on Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-ccontributor friendlyenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions