Skip to content
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

wasm linker: aggressive rewrite towards Data-Oriented Design #22220

Merged
merged 140 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
140 commits
Select commit Hold shift + click to select a range
795e7c6
wasm linker: aggressive DODification
andrewrk Nov 5, 2024
16180f5
macho linker: conform to explicit error sets
andrewrk Dec 3, 2024
6235cc3
remove "FIXME" from codebase
andrewrk Dec 3, 2024
da25ed9
macho linker conforms to explicit error sets, again
andrewrk Dec 4, 2024
77accf5
elf linker: conform to explicit error sets
andrewrk Dec 4, 2024
9bf715d
rework error handling in the backends
andrewrk Dec 4, 2024
943dac3
compiler: add type safety for export indices
andrewrk Dec 5, 2024
b9355ed
std.array_list: tiny refactor for pleasure
andrewrk Dec 6, 2024
e521879
rewrite wasm/Emit.zig
andrewrk Dec 6, 2024
bffa148
wasm codegen: fix some compilation errors
andrewrk Dec 7, 2024
e24f635
wasm: implement errors_len as a MIR opcode with no linker involvement
andrewrk Dec 7, 2024
4a1447d
wasm codegen: switch on bool instead of int
andrewrk Dec 7, 2024
d45e5ac
wasm codegen: rename func: CodeGen to cg: CodeGen
andrewrk Dec 7, 2024
c443a7a
wasm: move error_name lowering to Emit phase
andrewrk Dec 7, 2024
bf20a4a
wasm: use call_intrinsic MIR instruction
andrewrk Dec 8, 2024
b3ecdb2
switch to ArrayListUnmanaged for machine code
andrewrk Dec 8, 2024
031c84c
wasm: fix many compilation errors
andrewrk Dec 9, 2024
26c38b2
wasm linker: support export section as implicit symbols
andrewrk Dec 12, 2024
c96e236
frontend: add const to more Zcu pointers
andrewrk Dec 12, 2024
e21a427
wasm linker: implement name, module name, and type for function imports
andrewrk Dec 12, 2024
bf88059
wasm linker: flush implemented up to the export section
andrewrk Dec 12, 2024
264a63b
wasm linker: flush export section
andrewrk Dec 12, 2024
968941b
wasm linker: finish the flush function
andrewrk Dec 13, 2024
edd592d
fix compilation when enabling llvm
andrewrk Dec 13, 2024
7879d2c
cmake: remove deleted file
andrewrk Dec 13, 2024
4a4fb16
add dev env for wasm
andrewrk Dec 14, 2024
55773ae
remove bad deinit
andrewrk Dec 15, 2024
098e0b1
wasm codegen: fix lowering of 32/64 float rt calls
andrewrk Dec 16, 2024
4ecc4ad
wasm codegen: remove dependency on PerThread where possible
andrewrk Dec 16, 2024
e80a203
wasm linker fixes
andrewrk Dec 16, 2024
d6b42e5
wasm linker: implement name subsection
andrewrk Dec 16, 2024
0555fe8
fix replaceVecSectionHeader
andrewrk Dec 16, 2024
70414c1
std.Thread: don't export wasi_thread_start in single-threaded mode
andrewrk Dec 16, 2024
761387d
wasm linker: implement type index method
andrewrk Dec 16, 2024
458f658
wasm linker: implement missing logic
andrewrk Dec 18, 2024
416fc2d
complete wasm.Emit implementation
andrewrk Dec 18, 2024
0630344
fix calculation of nav alignment
andrewrk Dec 18, 2024
2d899e9
wasm codegen: fix wrong union field for locals
andrewrk Dec 18, 2024
85b5373
add safety for calling functions that get virtual addrs
andrewrk Dec 18, 2024
3c70392
wasm linker: add __zig_error_name_table data when needed
andrewrk Dec 18, 2024
1a58ae2
wasm codegen: fix extra index not relative
andrewrk Dec 18, 2024
91efc5c
wasm linker: fix calling imported functions
andrewrk Dec 19, 2024
3cb00c5
std.ArrayHashMap: allow passing empty values array
andrewrk Dec 19, 2024
766284f
wasm linker: fix data segments memory flow
andrewrk Dec 19, 2024
23d0882
wasm linker: handle extern functions in updateNav
andrewrk Dec 19, 2024
070b973
wasm linker: allow undefined imports when lib name is provided
andrewrk Dec 19, 2024
568d993
wasm codegen: fix call_indirect
andrewrk Dec 19, 2024
5fac6f3
wasm linker: fix eliding empty data segments
andrewrk Dec 19, 2024
4f8a6b0
wasm linker: implement data fixups
andrewrk Dec 19, 2024
389b29f
wasm linker: avoid recursion in lowerZcuData
andrewrk Dec 19, 2024
5e0d33f
wasm linker: also call lowerZcuData in updateFunc
andrewrk Dec 20, 2024
13eef2f
wasm linker: initialize the data segments table in flush
andrewrk Dec 20, 2024
7bf53d2
wasm linker: zcu data fixups are already applied
andrewrk Dec 20, 2024
ee999d5
implement error table and error names data segments
andrewrk Dec 20, 2024
694b129
wasm linker: fix data section in flush
andrewrk Dec 21, 2024
d1cde84
implement the prelink phase in the frontend
andrewrk Dec 21, 2024
2dbf66d
wasm linker: implement stack pointer global
andrewrk Dec 21, 2024
fbbb54b
std.io: remove the "temporary workaround" for stage2_aarch64
andrewrk Dec 21, 2024
7281034
wasm linker: implement indirect function calls
andrewrk Dec 21, 2024
2174d20
fix stack pointer initialized to wrong vaddr
andrewrk Dec 21, 2024
f1e167c
use fixed writer in more places
andrewrk Dec 21, 2024
5062656
wasm linker: fix missing function type entry for import
andrewrk Dec 22, 2024
0d028e4
wasm linker: fix active data segment offset value
andrewrk Dec 22, 2024
9cd7cad
Compilation: account for C objects and resources in prelink
andrewrk Dec 22, 2024
4b9dc29
wasm linker: fix relocation parsing
andrewrk Dec 23, 2024
1a4c583
wasm linker: fix crashes when parsing compiler_rt
andrewrk Dec 24, 2024
aebccb0
fix missing missing entry symbol error when no zcu
andrewrk Dec 24, 2024
eb94389
resolve merge conflicts
andrewrk Dec 27, 2024
a72e738
wasm linker: fix global imports in objects
andrewrk Dec 28, 2024
7898708
can't use source location until return from this function
andrewrk Dec 29, 2024
b7a9591
wasm linker: fix table imports in objects
andrewrk Dec 29, 2024
c582287
fix bad archive name calculation
andrewrk Dec 29, 2024
7d22451
wasm linker: chase relocations for references
andrewrk Dec 30, 2024
4fccb5a
wasm linker: improve error messages by making source locations more lazy
andrewrk Dec 30, 2024
a4895f3
wasm object parsing: fix handling of weak functions and globals
andrewrk Dec 30, 2024
5b18af8
type checking for synthetic functions
andrewrk Dec 30, 2024
9ccf500
implement function relocations
andrewrk Dec 31, 2024
a4bee30
wasm linker: implement __wasm_call_ctors
andrewrk Dec 31, 2024
abdbc38
wasm linker: implement data symbols
andrewrk Jan 4, 2025
1fd708b
wasm linker: implement data relocs
andrewrk Jan 5, 2025
b41b5fe
wasm linker: implement __wasm_init_memory
andrewrk Jan 5, 2025
94648a0
fix merge conflicts with updating line numbers
andrewrk Jan 6, 2025
290d97b
wasm linker: emit __heap_base and __heap_end globals and datas
andrewrk Jan 6, 2025
e6a5fe7
wasm linker: apply object relocations to data segments
andrewrk Jan 6, 2025
3474057
wasm linker: fix not merging object memories
andrewrk Jan 7, 2025
5186c6c
wasm linker: distinguish symbol name vs import name, and implement weak
andrewrk Jan 9, 2025
21a2888
wasm linker: don't assume nav callees are fully resolved
andrewrk Jan 9, 2025
7a4d435
wasm linker: don't try to lower nav zcu data before updateNav is called
andrewrk Jan 9, 2025
d9d49ce
wasm linker: handle weak globals in relocs
andrewrk Jan 10, 2025
1c4b4fb
implement indirect function table for object functions
andrewrk Jan 10, 2025
788b7f8
wasm linker: don't call init functions unless object included
andrewrk Jan 10, 2025
d999a8e
wasm-linker: fix splitSegmentName and add unit test
andrewrk Jan 10, 2025
a327d23
wasm linker: handle function data references properly
andrewrk Jan 10, 2025
9c14645
wasm codegen: fix freeing of locals
andrewrk Jan 11, 2025
e18a397
wasm linker: fix corruption of string bytes
andrewrk Jan 11, 2025
e44bafe
wasm linker: mark symbol deps on intrinsics
andrewrk Jan 11, 2025
7b25523
wasm linker: fix off-by-one in function table indexes
andrewrk Jan 11, 2025
220af3f
wasm-linker: add updateFunc log
andrewrk Jan 11, 2025
0dd0ebb
frontend: don't increment remaining_prelink_tasks for windows implibs
andrewrk Jan 11, 2025
fbdcb22
wasm linker: don't pretend it's possible to export data symbols
andrewrk Jan 12, 2025
e5d78f0
codegen: empty tuple can be stored in a runtime var
andrewrk Jan 12, 2025
d0d0847
wasm linker: don't crash on ref to void
andrewrk Jan 12, 2025
b526159
wasm linker: implement `@tagName` functions when tags are autoassigned
andrewrk Jan 12, 2025
8abdebe
wasm linker: implement `@tagName` for sparse enums
andrewrk Jan 13, 2025
dd9a647
wasm linker: fix bad export index math
andrewrk Jan 14, 2025
cde84c8
wasm linker: fix missed addend for uav and nav fixups
andrewrk Jan 14, 2025
0cf1630
wasm linker: fix `@tagName` for auto-numbered non-exhaustive enums
andrewrk Jan 14, 2025
ba4521a
wasm linker: fix data segment names
andrewrk Jan 14, 2025
4cc9cfa
wasm linker: track overaligned uavs
andrewrk Jan 14, 2025
1afa6e2
build: respect -Duse-llvm option when doing behavior tests
andrewrk Jan 14, 2025
1fe1d55
wasm linker: reset function exports after flush
andrewrk Jan 14, 2025
9285f91
wasm linker: incremental test coverage
andrewrk Jan 14, 2025
f89ef2f
Compilation.saveState implement saving wasm linker state
andrewrk Jan 14, 2025
c535422
wasm linker: implement hidden visibility
andrewrk Jan 14, 2025
b37ad51
wasm linker: always passive when importing memory
andrewrk Jan 15, 2025
7ae2f21
delete bad linker test: bss
andrewrk Jan 15, 2025
9143575
test/wasm/export: delete redundant tests
andrewrk Jan 15, 2025
f7f8878
wasm linker: correct export visibility logic
andrewrk Jan 15, 2025
c2a918b
wasm linker: implement __wasm_init_tls synthetic function
andrewrk Jan 15, 2025
617eca1
wasm-linker: remap function types during flush
andrewrk Jan 15, 2025
c565191
test/link/wasm/type: remove redundant tests
andrewrk Jan 15, 2025
ae119e3
test/link/wasm/segment: delete bad test
andrewrk Jan 15, 2025
ec5fc6a
test/link/wasm/function-table: delete bad test
andrewrk Jan 15, 2025
9ddb1c5
wasm linker: implement --export-table
andrewrk Jan 15, 2025
50d053f
test/wasm/infer-features: update to expected behavior
andrewrk Jan 15, 2025
42602dc
test/link/wasm/export-data: update expected behavior
andrewrk Jan 15, 2025
204e689
tests: remove dead code
andrewrk Jan 15, 2025
a7bd1a6
wasm codegen: fix mistaking extern data as function
andrewrk Jan 15, 2025
ae16414
wasm linker: ability to get data and functions from objects
andrewrk Jan 15, 2025
a4b7e9b
fix compiler unit tests
andrewrk Jan 15, 2025
10db1b9
wasm linker: fix explicit exports not affecting object files
andrewrk Jan 16, 2025
9dd6efb
wasm linker: fix TLS data segments
andrewrk Jan 16, 2025
dcb4134
wasm linker: delete commented out code
andrewrk Jan 16, 2025
744bb5d
wasm linker: change rules about symbol visibility
andrewrk Jan 16, 2025
6053112
wasm linker: omit data count section when value is zero
andrewrk Jan 16, 2025
cf898e2
std.Build.Step.CheckObject: better EOF handling
andrewrk Jan 16, 2025
24bdb42
test/link/wasm/shared-memory: update to better linker behavior
andrewrk Jan 16, 2025
e486869
Compilation: windows doesn't prelink yet
andrewrk Jan 16, 2025
ed810ee
link.Elf: fix error reporting for failed hot swap
andrewrk Jan 16, 2025
eda8b6e
link.MachO: fix error reporting in flushModule
andrewrk Jan 16, 2025
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
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,8 @@ set(ZIG_STAGE2_SOURCES
src/link/StringTable.zig
src/link/Wasm.zig
src/link/Wasm/Archive.zig
src/link/Wasm/Flush.zig
src/link/Wasm/Object.zig
src/link/Wasm/Symbol.zig
src/link/Wasm/ZigObject.zig
src/link/aarch64.zig
src/link/riscv.zig
src/link/table_section.zig
Expand Down
5 changes: 5 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ pub fn build(b: *std.Build) !void {
.skip_single_threaded = skip_single_threaded,
.skip_non_native = skip_non_native,
.skip_libc = skip_libc,
.use_llvm = use_llvm,
.max_rss = 1 * 1024 * 1024 * 1024,
}));

Expand All @@ -462,6 +463,7 @@ pub fn build(b: *std.Build) !void {
.skip_single_threaded = true,
.skip_non_native = skip_non_native,
.skip_libc = skip_libc,
.use_llvm = use_llvm,
}));

test_modules_step.dependOn(tests.addModuleTests(b, .{
Expand All @@ -476,6 +478,7 @@ pub fn build(b: *std.Build) !void {
.skip_single_threaded = true,
.skip_non_native = skip_non_native,
.skip_libc = true,
.use_llvm = use_llvm,
.no_builtin = true,
}));

Expand All @@ -491,6 +494,7 @@ pub fn build(b: *std.Build) !void {
.skip_single_threaded = true,
.skip_non_native = skip_non_native,
.skip_libc = true,
.use_llvm = use_llvm,
.no_builtin = true,
}));

Expand All @@ -506,6 +510,7 @@ pub fn build(b: *std.Build) !void {
.skip_single_threaded = skip_single_threaded,
.skip_non_native = skip_non_native,
.skip_libc = skip_libc,
.use_llvm = use_llvm,
// I observed a value of 4572626944 on the M2 CI.
.max_rss = 5029889638,
}));
Expand Down
21 changes: 17 additions & 4 deletions lib/std/Build/Step/CheckObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,22 @@ const WasmDumper = struct {
}

var output = std.ArrayList(u8).init(gpa);
errdefer output.deinit();
defer output.deinit();
parseAndDumpInner(step, check, bytes, &fbs, &output) catch |err| switch (err) {
error.EndOfStream => try output.appendSlice("\n<UnexpectedEndOfStream>"),
else => |e| return e,
};
return output.toOwnedSlice();
}

fn parseAndDumpInner(
step: *Step,
check: Check,
bytes: []const u8,
fbs: *std.io.FixedBufferStream([]const u8),
output: *std.ArrayList(u8),
) !void {
const reader = fbs.reader();
const writer = output.writer();

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

else => return step.fail("invalid check kind for Wasm file format: {s}", .{@tagName(check.kind)}),
}

return output.toOwnedSlice();
}

fn parseAndDumpSection(
Expand Down Expand Up @@ -2682,7 +2695,7 @@ const WasmDumper = struct {
else => unreachable,
}
const end_opcode = try std.leb.readUleb128(u8, reader);
if (end_opcode != std.wasm.opcode(.end)) {
if (end_opcode != @intFromEnum(std.wasm.Opcode.end)) {
return step.fail("expected 'end' opcode in init expression", .{});
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,12 @@ pub const Cpu = struct {
} else true;
}

pub fn count(set: Set) std.math.IntFittingRange(0, needed_bit_count) {
var sum: usize = 0;
for (set.ints) |x| sum += @popCount(x);
return @intCast(sum);
}

pub fn isEnabled(set: Set, arch_feature_index: Index) bool {
const usize_index = arch_feature_index / @bitSizeOf(usize);
const bit_index: ShiftInt = @intCast(arch_feature_index % @bitSizeOf(usize));
Expand Down
13 changes: 8 additions & 5 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1018,12 +1018,15 @@ const WasiThreadImpl = struct {
return .{ .thread = &instance.thread };
}

/// Bootstrap procedure, called by the host environment after thread creation.
export fn wasi_thread_start(tid: i32, arg: *Instance) void {
if (builtin.single_threaded) {
// ensure function is not analyzed in single-threaded mode
return;
comptime {
if (!builtin.single_threaded) {
@export(wasi_thread_start, .{ .name = "wasi_thread_start" });
}
}

/// Called by the host environment after thread creation.
fn wasi_thread_start(tid: i32, arg: *Instance) callconv(.c) void {
comptime assert(!builtin.single_threaded);
__set_stack_pointer(arg.thread.memory.ptr + arg.stack_offset);
__wasm_init_tls(arg.thread.memory.ptr + arg.tls_offset);
@atomicStore(u32, &WasiThreadImpl.tls_thread_id, @intCast(tid), .seq_cst);
Expand Down
5 changes: 4 additions & 1 deletion lib/std/array_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,13 @@ pub fn ArrayHashMapUnmanaged(
return self;
}

/// An empty `value_list` may be passed, in which case the values array becomes `undefined`.
pub fn reinit(self: *Self, gpa: Allocator, key_list: []const K, value_list: []const V) Oom!void {
try self.entries.resize(gpa, key_list.len);
@memcpy(self.keys(), key_list);
if (@sizeOf(V) != 0) {
if (value_list.len == 0) {
@memset(self.values(), undefined);
} else {
assert(key_list.len == value_list.len);
@memcpy(self.values(), value_list);
}
Expand Down
6 changes: 2 additions & 4 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
/// Never invalidates element pointers.
/// Asserts that the list can hold one additional item.
pub fn appendAssumeCapacity(self: *Self, item: T) void {
const new_item_ptr = self.addOneAssumeCapacity();
new_item_ptr.* = item;
self.addOneAssumeCapacity().* = item;
}

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

/// Remove the element at index `i` from the list and return its value.
Expand Down
12 changes: 0 additions & 12 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ const Allocator = std.mem.Allocator;

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

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

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

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

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

Expand Down
Loading