Skip to content
New issue

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

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

Already on GitHub? # to your account

implement thread-local storage when not using LLVM or LLD #17794

Closed
Tracked by #17748
andrewrk opened this issue Oct 30, 2023 · 2 comments
Closed
Tracked by #17748

implement thread-local storage when not using LLVM or LLD #17794

andrewrk opened this issue Oct 30, 2023 · 2 comments
Labels
arch-x86_64 64-bit x86 backend-self-hosted bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. linking
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Oct 30, 2023

const std = @import("std");

test "thread local variables" {
    var value1: i32 = 0;
    var value2: i32 = 0;

    const thread1 = try std.Thread.spawn(.{}, run, .{&value1});
    const thread2 = try std.Thread.spawn(.{}, run, .{&value2});

    thread1.join();
    thread2.join();

    try std.testing.expectEqual(@as(i32, 1235), value1);
    try std.testing.expectEqual(@as(i32, 1235), value2);
    try std.testing.expectEqual(@as(i32, 1234), x);
}

threadlocal var x: i32 = 1234;

fn run(value: *i32) void {
    x += 1;
    value.* = x;
}
$ stage3/bin/zig test test.zig  -fllvm -flld
All 1 tests passed.

$ stage3/bin/zig test test.zig  -fno-llvm -fno-lld -fno-single-threaded
Test [1/1] test.thread local variables... expected 1235, found 1236
Test [1/1] test.thread local variables... FAIL (TestExpectedEqual)
0 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/andy/Downloads/zig/zig-cache/o/a01721253d2ad18ca1ea314f4ec3169e/test
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. arch-x86_64 64-bit x86 backend-self-hosted linking labels Oct 30, 2023
@andrewrk andrewrk added this to the 0.12.0 milestone Oct 30, 2023
@andrewrk
Copy link
Member Author

andrewrk commented Nov 4, 2023

zig/src/Compilation.zig

Lines 1124 to 1129 in 98dc28b

const single_threaded = options.single_threaded orelse must_single_thread or
// x86_64 codegen doesn't support TLV for most object formats
(!use_llvm and options.target.cpu.arch == .x86_64 and options.target.ofmt != .macho);
if (must_single_thread and !single_threaded) {
return error.TargetRequiresSingleThreaded;
}

@kubkon
Copy link
Member

kubkon commented Nov 15, 2023

Implemented by #17978.

$ stage3/bin/zig test test.zig -fno-llvm -fno-lld -fno-single-threaded
All 1 tests passed.
$ stage3/bin/zig test test.zig -fno-llvm -fno-lld -fno-single-threaded -fPIC
All 1 tests passed.

@kubkon kubkon closed this as completed Nov 15, 2023
@mlugg mlugg moved this to Ditch LLVM in Performance Aug 22, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
arch-x86_64 64-bit x86 backend-self-hosted bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. linking
Projects
Archived in project
Development

No branches or pull requests

2 participants