Skip to content

Commit d4fe469

Browse files
authored
Merge pull request #22220 from ziglang/wasm-linker
wasm linker: aggressive rewrite towards Data-Oriented Design
2 parents 7727310 + eda8b6e commit d4fe469

File tree

104 files changed

+13672
-12204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+13672
-12204
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,8 @@ set(ZIG_STAGE2_SOURCES
643643
src/link/StringTable.zig
644644
src/link/Wasm.zig
645645
src/link/Wasm/Archive.zig
646+
src/link/Wasm/Flush.zig
646647
src/link/Wasm/Object.zig
647-
src/link/Wasm/Symbol.zig
648-
src/link/Wasm/ZigObject.zig
649648
src/link/aarch64.zig
650649
src/link/riscv.zig
651650
src/link/table_section.zig

build.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ pub fn build(b: *std.Build) !void {
447447
.skip_single_threaded = skip_single_threaded,
448448
.skip_non_native = skip_non_native,
449449
.skip_libc = skip_libc,
450+
.use_llvm = use_llvm,
450451
.max_rss = 1 * 1024 * 1024 * 1024,
451452
}));
452453

@@ -462,6 +463,7 @@ pub fn build(b: *std.Build) !void {
462463
.skip_single_threaded = true,
463464
.skip_non_native = skip_non_native,
464465
.skip_libc = skip_libc,
466+
.use_llvm = use_llvm,
465467
}));
466468

467469
test_modules_step.dependOn(tests.addModuleTests(b, .{
@@ -476,6 +478,7 @@ pub fn build(b: *std.Build) !void {
476478
.skip_single_threaded = true,
477479
.skip_non_native = skip_non_native,
478480
.skip_libc = true,
481+
.use_llvm = use_llvm,
479482
.no_builtin = true,
480483
}));
481484

@@ -491,6 +494,7 @@ pub fn build(b: *std.Build) !void {
491494
.skip_single_threaded = true,
492495
.skip_non_native = skip_non_native,
493496
.skip_libc = true,
497+
.use_llvm = use_llvm,
494498
.no_builtin = true,
495499
}));
496500

@@ -506,6 +510,7 @@ pub fn build(b: *std.Build) !void {
506510
.skip_single_threaded = skip_single_threaded,
507511
.skip_non_native = skip_non_native,
508512
.skip_libc = skip_libc,
513+
.use_llvm = use_llvm,
509514
// I observed a value of 4572626944 on the M2 CI.
510515
.max_rss = 5029889638,
511516
}));

lib/std/Build/Step/CheckObject.zig

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,22 @@ const WasmDumper = struct {
24242424
}
24252425

24262426
var output = std.ArrayList(u8).init(gpa);
2427-
errdefer output.deinit();
2427+
defer output.deinit();
2428+
parseAndDumpInner(step, check, bytes, &fbs, &output) catch |err| switch (err) {
2429+
error.EndOfStream => try output.appendSlice("\n<UnexpectedEndOfStream>"),
2430+
else => |e| return e,
2431+
};
2432+
return output.toOwnedSlice();
2433+
}
2434+
2435+
fn parseAndDumpInner(
2436+
step: *Step,
2437+
check: Check,
2438+
bytes: []const u8,
2439+
fbs: *std.io.FixedBufferStream([]const u8),
2440+
output: *std.ArrayList(u8),
2441+
) !void {
2442+
const reader = fbs.reader();
24282443
const writer = output.writer();
24292444

24302445
switch (check.kind) {
@@ -2442,8 +2457,6 @@ const WasmDumper = struct {
24422457

24432458
else => return step.fail("invalid check kind for Wasm file format: {s}", .{@tagName(check.kind)}),
24442459
}
2445-
2446-
return output.toOwnedSlice();
24472460
}
24482461

24492462
fn parseAndDumpSection(
@@ -2682,7 +2695,7 @@ const WasmDumper = struct {
26822695
else => unreachable,
26832696
}
26842697
const end_opcode = try std.leb.readUleb128(u8, reader);
2685-
if (end_opcode != std.wasm.opcode(.end)) {
2698+
if (end_opcode != @intFromEnum(std.wasm.Opcode.end)) {
26862699
return step.fail("expected 'end' opcode in init expression", .{});
26872700
}
26882701
}

lib/std/Target.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,12 @@ pub const Cpu = struct {
12191219
} else true;
12201220
}
12211221

1222+
pub fn count(set: Set) std.math.IntFittingRange(0, needed_bit_count) {
1223+
var sum: usize = 0;
1224+
for (set.ints) |x| sum += @popCount(x);
1225+
return @intCast(sum);
1226+
}
1227+
12221228
pub fn isEnabled(set: Set, arch_feature_index: Index) bool {
12231229
const usize_index = arch_feature_index / @bitSizeOf(usize);
12241230
const bit_index: ShiftInt = @intCast(arch_feature_index % @bitSizeOf(usize));

lib/std/Thread.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,12 +1018,15 @@ const WasiThreadImpl = struct {
10181018
return .{ .thread = &instance.thread };
10191019
}
10201020

1021-
/// Bootstrap procedure, called by the host environment after thread creation.
1022-
export fn wasi_thread_start(tid: i32, arg: *Instance) void {
1023-
if (builtin.single_threaded) {
1024-
// ensure function is not analyzed in single-threaded mode
1025-
return;
1021+
comptime {
1022+
if (!builtin.single_threaded) {
1023+
@export(wasi_thread_start, .{ .name = "wasi_thread_start" });
10261024
}
1025+
}
1026+
1027+
/// Called by the host environment after thread creation.
1028+
fn wasi_thread_start(tid: i32, arg: *Instance) callconv(.c) void {
1029+
comptime assert(!builtin.single_threaded);
10271030
__set_stack_pointer(arg.thread.memory.ptr + arg.stack_offset);
10281031
__wasm_init_tls(arg.thread.memory.ptr + arg.tls_offset);
10291032
@atomicStore(u32, &WasiThreadImpl.tls_thread_id, @intCast(tid), .seq_cst);

lib/std/array_hash_map.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,13 @@ pub fn ArrayHashMapUnmanaged(
641641
return self;
642642
}
643643

644+
/// An empty `value_list` may be passed, in which case the values array becomes `undefined`.
644645
pub fn reinit(self: *Self, gpa: Allocator, key_list: []const K, value_list: []const V) Oom!void {
645646
try self.entries.resize(gpa, key_list.len);
646647
@memcpy(self.keys(), key_list);
647-
if (@sizeOf(V) != 0) {
648+
if (value_list.len == 0) {
649+
@memset(self.values(), undefined);
650+
} else {
648651
assert(key_list.len == value_list.len);
649652
@memcpy(self.values(), value_list);
650653
}

lib/std/array_list.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
267267
/// Never invalidates element pointers.
268268
/// Asserts that the list can hold one additional item.
269269
pub fn appendAssumeCapacity(self: *Self, item: T) void {
270-
const new_item_ptr = self.addOneAssumeCapacity();
271-
new_item_ptr.* = item;
270+
self.addOneAssumeCapacity().* = item;
272271
}
273272

274273
/// Remove the element at index `i`, shift elements after index
@@ -879,8 +878,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
879878
/// Never invalidates element pointers.
880879
/// Asserts that the list can hold one additional item.
881880
pub fn appendAssumeCapacity(self: *Self, item: T) void {
882-
const new_item_ptr = self.addOneAssumeCapacity();
883-
new_item_ptr.* = item;
881+
self.addOneAssumeCapacity().* = item;
884882
}
885883

886884
/// Remove the element at index `i` from the list and return its value.

lib/std/io.zig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ const Allocator = std.mem.Allocator;
1616

1717
fn getStdOutHandle() posix.fd_t {
1818
if (is_windows) {
19-
if (builtin.zig_backend == .stage2_aarch64) {
20-
// TODO: this is just a temporary workaround until we advance aarch64 backend further along.
21-
return windows.GetStdHandle(windows.STD_OUTPUT_HANDLE) catch windows.INVALID_HANDLE_VALUE;
22-
}
2319
return windows.peb().ProcessParameters.hStdOutput;
2420
}
2521

@@ -36,10 +32,6 @@ pub fn getStdOut() File {
3632

3733
fn getStdErrHandle() posix.fd_t {
3834
if (is_windows) {
39-
if (builtin.zig_backend == .stage2_aarch64) {
40-
// TODO: this is just a temporary workaround until we advance aarch64 backend further along.
41-
return windows.GetStdHandle(windows.STD_ERROR_HANDLE) catch windows.INVALID_HANDLE_VALUE;
42-
}
4335
return windows.peb().ProcessParameters.hStdError;
4436
}
4537

@@ -56,10 +48,6 @@ pub fn getStdErr() File {
5648

5749
fn getStdInHandle() posix.fd_t {
5850
if (is_windows) {
59-
if (builtin.zig_backend == .stage2_aarch64) {
60-
// TODO: this is just a temporary workaround until we advance aarch64 backend further along.
61-
return windows.GetStdHandle(windows.STD_INPUT_HANDLE) catch windows.INVALID_HANDLE_VALUE;
62-
}
6351
return windows.peb().ProcessParameters.hStdInput;
6452
}
6553

0 commit comments

Comments
 (0)