authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-13 19:37:18-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
log4cc9cfa7e88b0dd4b47bedba9f190f6731422da6
treeb17b2defe99d9f67469843e450ee82cf309a9953
parentba4521ac85bbfc7cf6e6ea8d36e75907d94878c3

wasm linker: track overaligned uavs


3 files changed, 38 insertions(+), 8 deletions(-)

src/arch/wasm/CodeGen.zig+6-5
......@@ -161,6 +161,7 @@ const WValue = union(enum) {
161161 uav_ref: struct {
162162 ip_index: InternPool.Index,
163163 offset: i32 = 0,
164 orig_ptr_ty: InternPool.Index = .none,
164165 },
165166 /// Offset from the bottom of the virtual stack, with the offset
166167 /// pointing to where the value lives.
......@@ -1062,9 +1063,9 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {
10621063 try cg.addInst(.{
10631064 .tag = .uav_ref,
10641065 .data = if (is_obj) .{
1065 .uav_obj = try wasm.refUavObj(uav.ip_index),
1066 .uav_obj = try wasm.refUavObj(uav.ip_index, uav.orig_ptr_ty),
10661067 } else .{
1067 .uav_exe = try wasm.refUavExe(uav.ip_index),
1068 .uav_exe = try wasm.refUavExe(uav.ip_index, uav.orig_ptr_ty),
10681069 },
10691070 });
10701071 } else {
......@@ -1072,10 +1073,10 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {
10721073 .tag = .uav_ref_off,
10731074 .data = .{
10741075 .payload = if (is_obj) try cg.addExtra(Mir.UavRefOffObj{
1075 .uav_obj = try wasm.refUavObj(uav.ip_index),
1076 .uav_obj = try wasm.refUavObj(uav.ip_index, uav.orig_ptr_ty),
10761077 .offset = uav.offset,
10771078 }) else try cg.addExtra(Mir.UavRefOffExe{
1078 .uav_exe = try wasm.refUavExe(uav.ip_index),
1079 .uav_exe = try wasm.refUavExe(uav.ip_index, uav.orig_ptr_ty),
10791080 .offset = uav.offset,
10801081 }),
10811082 },
......@@ -3093,7 +3094,7 @@ fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerErro
30933094 const offset: u64 = prev_offset + ptr.byte_offset;
30943095 return switch (ptr.base_addr) {
30953096 .nav => |nav| return .{ .nav_ref = .{ .nav_index = nav, .offset = @intCast(offset) } },
3096 .uav => |uav| return .{ .uav_ref = .{ .ip_index = uav.val, .offset = @intCast(offset) } },
3097 .uav => |uav| return .{ .uav_ref = .{ .ip_index = uav.val, .offset = @intCast(offset), .orig_ptr_ty = uav.orig_ty } },
30973098 .int => return cg.lowerConstant(try pt.intValue(Type.usize, offset), Type.usize),
30983099 .eu_payload => return cg.fail("Wasm TODO: lower error union payload pointer", .{}),
30993100 .opt_payload => |opt_ptr| return cg.lowerPtr(opt_ptr, offset),
src/codegen.zig+1-1
......@@ -675,7 +675,7 @@ fn lowerUavRef(
675675 } else {
676676 try wasm.uav_fixups.ensureUnusedCapacity(gpa, 1);
677677 wasm.uav_fixups.appendAssumeCapacity(.{
678 .uavs_exe_index = try wasm.refUavExe(uav.val),
678 .uavs_exe_index = try wasm.refUavExe(uav.val, uav.orig_ty),
679679 .offset = @intCast(code.items.len),
680680 .addend = @intCast(offset),
681681 });
src/link/Wasm.zig+31-2
......@@ -189,6 +189,9 @@ navs_exe: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, ZcuDataExe) = .emp
189189uavs_obj: std.AutoArrayHashMapUnmanaged(InternPool.Index, ZcuDataObj) = .empty,
190190/// Tracks ref count to optimize LEB encodings for UAV references.
191191uavs_exe: std.AutoArrayHashMapUnmanaged(InternPool.Index, ZcuDataExe) = .empty,
192/// Sparse table of uavs that need to be emitted with greater alignment than
193/// the default for the type.
194overaligned_uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment) = .empty,
192195/// When the key is an enum type, this represents a `@tagName` function.
193196zcu_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Index, ZcuFunc) = .empty,
194197nav_exports: std.AutoArrayHashMapUnmanaged(NavExport, Zcu.Export.Index) = .empty,
......@@ -1945,6 +1948,7 @@ pub const DataSegmentId = enum(u32) {
19451948 const zcu = wasm.base.comp.zcu.?;
19461949 const ip = &zcu.intern_pool;
19471950 const ip_index = i.key(wasm).*;
1951 if (wasm.overaligned_uavs.get(ip_index)) |a| return a;
19481952 const ty: Zcu.Type = .fromInterned(ip.typeOf(ip_index));
19491953 const result = ty.abiAlignment(zcu);
19501954 assert(result != .none);
......@@ -3085,6 +3089,7 @@ pub fn deinit(wasm: *Wasm) void {
30853089 wasm.navs_obj.deinit(gpa);
30863090 wasm.uavs_exe.deinit(gpa);
30873091 wasm.uavs_obj.deinit(gpa);
3092 wasm.overaligned_uavs.deinit(gpa);
30883093 wasm.zcu_funcs.deinit(gpa);
30893094 wasm.nav_exports.deinit(gpa);
30903095 wasm.uav_exports.deinit(gpa);
......@@ -4397,10 +4402,22 @@ pub fn symbolNameIndex(wasm: *Wasm, name: String) Allocator.Error!SymbolTableInd
43974402 return @enumFromInt(gop.index);
43984403}
43994404
4400pub fn refUavObj(wasm: *Wasm, ip_index: InternPool.Index) !UavsObjIndex {
4405pub fn refUavObj(wasm: *Wasm, ip_index: InternPool.Index, orig_ptr_ty: InternPool.Index) !UavsObjIndex {
44014406 const comp = wasm.base.comp;
4407 const zcu = comp.zcu.?;
4408 const ip = &zcu.intern_pool;
44024409 const gpa = comp.gpa;
44034410 assert(comp.config.output_mode == .Obj);
4411
4412 if (orig_ptr_ty != .none) {
4413 const abi_alignment = Zcu.Type.fromInterned(ip.typeOf(ip_index)).abiAlignment(zcu);
4414 const explicit_alignment = ip.indexToKey(orig_ptr_ty).ptr_type.flags.alignment;
4415 if (explicit_alignment.compare(.gt, abi_alignment)) {
4416 const gop = try wasm.overaligned_uavs.getOrPut(gpa, ip_index);
4417 gop.value_ptr.* = if (gop.found_existing) gop.value_ptr.maxStrict(explicit_alignment) else explicit_alignment;
4418 }
4419 }
4420
44044421 const gop = try wasm.uavs_obj.getOrPut(gpa, ip_index);
44054422 if (!gop.found_existing) gop.value_ptr.* = .{
44064423 // Lowering the value is delayed to avoid recursion.
......@@ -4410,10 +4427,22 @@ pub fn refUavObj(wasm: *Wasm, ip_index: InternPool.Index) !UavsObjIndex {
44104427 return @enumFromInt(gop.index);
44114428}
44124429
4413pub fn refUavExe(wasm: *Wasm, ip_index: InternPool.Index) !UavsExeIndex {
4430pub fn refUavExe(wasm: *Wasm, ip_index: InternPool.Index, orig_ptr_ty: InternPool.Index) !UavsExeIndex {
44144431 const comp = wasm.base.comp;
4432 const zcu = comp.zcu.?;
4433 const ip = &zcu.intern_pool;
44154434 const gpa = comp.gpa;
44164435 assert(comp.config.output_mode != .Obj);
4436
4437 if (orig_ptr_ty != .none) {
4438 const abi_alignment = Zcu.Type.fromInterned(ip.typeOf(ip_index)).abiAlignment(zcu);
4439 const explicit_alignment = ip.indexToKey(orig_ptr_ty).ptr_type.flags.alignment;
4440 if (explicit_alignment.compare(.gt, abi_alignment)) {
4441 const gop = try wasm.overaligned_uavs.getOrPut(gpa, ip_index);
4442 gop.value_ptr.* = if (gop.found_existing) gop.value_ptr.maxStrict(explicit_alignment) else explicit_alignment;
4443 }
4444 }
4445
44174446 const gop = try wasm.uavs_exe.getOrPut(gpa, ip_index);
44184447 if (gop.found_existing) {
44194448 gop.value_ptr.count += 1;