authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-07-04 10:31:59+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-10 11:20:08-04:00
logf93a10f664fbbb67aeda031583a790e2a842fb01
tree04d3bdc234ecb3e58f2d8f31a504e2f8cefaa8fb
parent3aa48bf859b3eba17c0431c15ae79ab303219eff

Air: store param names directly instead of referencing Zir


11 files changed, 87 insertions(+), 51 deletions(-)

src/Air.zig+6-1
...@@ -1034,7 +1034,12 @@ pub const Inst = struct {...@@ -1034,7 +1034,12 @@ pub const Inst = struct {
1034 ty: Type,1034 ty: Type,
1035 arg: struct {1035 arg: struct {
1036 ty: Ref,1036 ty: Ref,
1037 src_index: u32,1037 /// Index into `extra` of a null-terminated string representing the parameter name.
1038 /// This is `.none` if debug info is stripped.
1039 name: enum(u32) {
1040 none = std.math.maxInt(u32),
1041 _,
1042 },
1038 },1043 },
1039 ty_op: struct {1044 ty_op: struct {
1040 ty: Ref,1045 ty: Ref,
src/Sema.zig+21-14
...@@ -6718,13 +6718,7 @@ fn addDbgVar(...@@ -6718,13 +6718,7 @@ fn addDbgVar(
6718 if (block.need_debug_scope) |ptr| ptr.* = true;6718 if (block.need_debug_scope) |ptr| ptr.* = true;
67196719
6720 // Add the name to the AIR.6720 // Add the name to the AIR.
6721 const name_extra_index: u32 = @intCast(sema.air_extra.items.len);6721 const name_extra_index = try sema.appendAirString(name);
6722 const elements_used = name.len / 4 + 1;
6723 try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements_used);
6724 const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
6725 @memcpy(buffer[0..name.len], name);
6726 buffer[name.len] = 0;
6727 sema.air_extra.items.len += elements_used;
67286722
6729 _ = try block.addInst(.{6723 _ = try block.addInst(.{
6730 .tag = air_tag,6724 .tag = air_tag,
...@@ -6735,6 +6729,16 @@ fn addDbgVar(...@@ -6735,6 +6729,16 @@ fn addDbgVar(
6735 });6729 });
6736}6730}
67376731
6732pub fn appendAirString(sema: *Sema, str: []const u8) Allocator.Error!u32 {
6733 const str_extra_index: u32 = @intCast(sema.air_extra.items.len);
6734 const elements_used = str.len / 4 + 1;
6735 const elements = try sema.air_extra.addManyAsSlice(sema.gpa, elements_used);
6736 const buffer = mem.sliceAsBytes(elements);
6737 @memcpy(buffer[0..str.len], str);
6738 buffer[str.len] = 0;
6739 return str_extra_index;
6740}
6741
6738fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {6742fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6739 const pt = sema.pt;6743 const pt = sema.pt;
6740 const mod = pt.zcu;6744 const mod = pt.zcu;
...@@ -8354,13 +8358,6 @@ fn instantiateGenericCall(...@@ -8354,13 +8358,6 @@ fn instantiateGenericCall(
8354 }8358 }
8355 } else {8359 } else {
8356 // The parameter is runtime-known.8360 // The parameter is runtime-known.
8357 child_sema.inst_map.putAssumeCapacityNoClobber(param_inst, try child_block.addInst(.{
8358 .tag = .arg,
8359 .data = .{ .arg = .{
8360 .ty = Air.internedToRef(arg_ty.toIntern()),
8361 .src_index = @intCast(arg_index),
8362 } },
8363 }));
8364 const param_name: Zir.NullTerminatedString = switch (param_tag) {8361 const param_name: Zir.NullTerminatedString = switch (param_tag) {
8365 .param_anytype => fn_zir.instructions.items(.data)[@intFromEnum(param_inst)].str_tok.start,8362 .param_anytype => fn_zir.instructions.items(.data)[@intFromEnum(param_inst)].str_tok.start,
8366 .param => name: {8363 .param => name: {
...@@ -8370,6 +8367,16 @@ fn instantiateGenericCall(...@@ -8370,6 +8367,16 @@ fn instantiateGenericCall(
8370 },8367 },
8371 else => unreachable,8368 else => unreachable,
8372 };8369 };
8370 child_sema.inst_map.putAssumeCapacityNoClobber(param_inst, try child_block.addInst(.{
8371 .tag = .arg,
8372 .data = .{ .arg = .{
8373 .ty = Air.internedToRef(arg_ty.toIntern()),
8374 .name = if (child_block.ownerModule().strip)
8375 .none
8376 else
8377 @enumFromInt(try sema.appendAirString(fn_zir.nullTerminatedString(param_name))),
8378 } },
8379 }));
8373 try child_block.params.append(sema.arena, .{8380 try child_block.params.append(sema.arena, .{
8374 .ty = arg_ty.toIntern(), // This is the type after coercion8381 .ty = arg_ty.toIntern(), // This is the type after coercion
8375 .is_comptime = false, // We're adding only runtime args to the instantiation8382 .is_comptime = false, // We're adding only runtime args to the instantiation
src/Zcu/PerThread.zig+12-2
...@@ -1907,10 +1907,17 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All...@@ -1907,10 +1907,17 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
1907 runtime_params_len;1907 runtime_params_len;
19081908
1909 var runtime_param_index: usize = 0;1909 var runtime_param_index: usize = 0;
1910 for (fn_info.param_body[0..src_params_len], 0..) |inst, src_param_index| {1910 for (fn_info.param_body[0..src_params_len]) |inst| {
1911 const gop = sema.inst_map.getOrPutAssumeCapacity(inst);1911 const gop = sema.inst_map.getOrPutAssumeCapacity(inst);
1912 if (gop.found_existing) continue; // provided above by comptime arg1912 if (gop.found_existing) continue; // provided above by comptime arg
19131913
1914 const inst_info = sema.code.instructions.get(@intFromEnum(inst));
1915 const param_name: Zir.NullTerminatedString = switch (inst_info.tag) {
1916 .param_anytype => inst_info.data.str_tok.start,
1917 .param => sema.code.extraData(Zir.Inst.Param, inst_info.data.pl_tok.payload_index).data.name,
1918 else => unreachable,
1919 };
1920
1914 const param_ty = fn_ty_info.param_types.get(ip)[runtime_param_index];1921 const param_ty = fn_ty_info.param_types.get(ip)[runtime_param_index];
1915 runtime_param_index += 1;1922 runtime_param_index += 1;
19161923
...@@ -1931,7 +1938,10 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All...@@ -1931,7 +1938,10 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
1931 .tag = .arg,1938 .tag = .arg,
1932 .data = .{ .arg = .{1939 .data = .{ .arg = .{
1933 .ty = Air.internedToRef(param_ty),1940 .ty = Air.internedToRef(param_ty),
1934 .src_index = @intCast(src_param_index),1941 .name = if (inner_block.ownerModule().strip)
1942 .none
1943 else
1944 @enumFromInt(try sema.appendAirString(sema.code.nullTerminatedString(param_name))),
1935 } },1945 } },
1936 });1946 });
1937 }1947 }
src/arch/aarch64/CodeGen.zig+10-10
...@@ -4231,19 +4231,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {...@@ -4231,19 +4231,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
4231 while (self.args[arg_index] == .none) arg_index += 1;4231 while (self.args[arg_index] == .none) arg_index += 1;
4232 self.arg_index = arg_index + 1;4232 self.arg_index = arg_index + 1;
42334233
4234 const pt = self.pt;
4235 const mod = pt.zcu;
4236 const ty = self.typeOfIndex(inst);4234 const ty = self.typeOfIndex(inst);
4237 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];4235 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
4238 const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
4239 const name = mod.getParamName(self.func_index, src_index);
42404236
4241 try self.dbg_info_relocs.append(self.gpa, .{4237 const name_nts = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
4242 .tag = tag,4238 if (name_nts != .none) {
4243 .ty = ty,4239 const name = self.air.nullTerminatedString(@intFromEnum(name_nts));
4244 .name = name,4240 try self.dbg_info_relocs.append(self.gpa, .{
4245 .mcv = self.args[arg_index],4241 .tag = tag,
4246 });4242 .ty = ty,
4243 .name = name,
4244 .mcv = self.args[arg_index],
4245 });
4246 }
42474247
4248 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else self.args[arg_index];4248 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else self.args[arg_index];
4249 return self.finishAir(inst, result, .{ .none, .none, .none });4249 return self.finishAir(inst, result, .{ .none, .none, .none });
src/arch/arm/CodeGen.zig+10-10
...@@ -4206,19 +4206,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {...@@ -4206,19 +4206,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
4206 while (self.args[arg_index] == .none) arg_index += 1;4206 while (self.args[arg_index] == .none) arg_index += 1;
4207 self.arg_index = arg_index + 1;4207 self.arg_index = arg_index + 1;
42084208
4209 const pt = self.pt;
4210 const mod = pt.zcu;
4211 const ty = self.typeOfIndex(inst);4209 const ty = self.typeOfIndex(inst);
4212 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];4210 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
4213 const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
4214 const name = mod.getParamName(self.func_index, src_index);
42154211
4216 try self.dbg_info_relocs.append(self.gpa, .{4212 const name_nts = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
4217 .tag = tag,4213 if (name_nts != .none) {
4218 .ty = ty,4214 const name = self.air.nullTerminatedString(@intFromEnum(name_nts));
4219 .name = name,4215 try self.dbg_info_relocs.append(self.gpa, .{
4220 .mcv = self.args[arg_index],4216 .tag = tag,
4221 });4217 .ty = ty,
4218 .name = name,
4219 .mcv = self.args[arg_index],
4220 });
4221 }
42224222
4223 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else self.args[arg_index];4223 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else self.args[arg_index];
4224 return self.finishAir(inst, result, .{ .none, .none, .none });4224 return self.finishAir(inst, result, .{ .none, .none, .none });
src/arch/riscv64/CodeGen.zig+2-1
...@@ -4051,7 +4051,8 @@ fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void {...@@ -4051,7 +4051,8 @@ fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void {
4051 const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg;4051 const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg;
4052 const ty = arg.ty.toType();4052 const ty = arg.ty.toType();
4053 const owner_decl = zcu.funcOwnerDeclIndex(func.func_index);4053 const owner_decl = zcu.funcOwnerDeclIndex(func.func_index);
4054 const name = zcu.getParamName(func.func_index, arg.src_index);4054 if (arg.name == .none) return;
4055 const name = func.air.nullTerminatedString(@intFromEnum(arg.name));
40554056
4056 switch (func.debug_output) {4057 switch (func.debug_output) {
4057 .dwarf => |dw| switch (mcv) {4058 .dwarf => |dw| switch (mcv) {
src/arch/sparc64/CodeGen.zig+2-1
...@@ -3614,7 +3614,8 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {...@@ -3614,7 +3614,8 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {
3614 const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg;3614 const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg;
3615 const ty = arg.ty.toType();3615 const ty = arg.ty.toType();
3616 const owner_decl = mod.funcOwnerDeclIndex(self.func_index);3616 const owner_decl = mod.funcOwnerDeclIndex(self.func_index);
3617 const name = mod.getParamName(self.func_index, arg.src_index);3617 if (arg.name == .none) return;
3618 const name = self.air.nullTerminatedString(@intFromEnum(arg.name));
36183619
3619 switch (self.debug_output) {3620 switch (self.debug_output) {
3620 .dwarf => |dw| switch (mcv) {3621 .dwarf => |dw| switch (mcv) {
src/arch/wasm/CodeGen.zig+7-5
...@@ -2585,11 +2585,13 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2585,11 +2585,13 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
25852585
2586 switch (func.debug_output) {2586 switch (func.debug_output) {
2587 .dwarf => |dwarf| {2587 .dwarf => |dwarf| {
2588 const src_index = func.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;2588 const name_nts = func.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
2589 const name = mod.getParamName(func.func_index, src_index);2589 if (name_nts != .none) {
2590 try dwarf.genArgDbgInfo(name, arg_ty, mod.funcOwnerDeclIndex(func.func_index), .{2590 const name = func.air.nullTerminatedString(@intFromEnum(name_nts));
2591 .wasm_local = arg.local.value,2591 try dwarf.genArgDbgInfo(name, arg_ty, mod.funcOwnerDeclIndex(func.func_index), .{
2592 });2592 .wasm_local = arg.local.value,
2593 });
2594 }
2593 },2595 },
2594 else => {},2596 else => {},
2595 }2597 }
src/arch/x86_64/CodeGen.zig+5-3
...@@ -11920,9 +11920,11 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {...@@ -11920,9 +11920,11 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
11920 else => return self.fail("TODO implement arg for {}", .{src_mcv}),11920 else => return self.fail("TODO implement arg for {}", .{src_mcv}),
11921 };11921 };
1192211922
11923 const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;11923 const name_nts = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
11924 const name = mod.getParamName(self.owner.func_index, src_index);11924 switch (name_nts) {
11925 try self.genArgDbgInfo(arg_ty, name, src_mcv);11925 .none => {},
11926 _ => try self.genArgDbgInfo(arg_ty, self.air.nullTerminatedString(@intFromEnum(name_nts)), src_mcv),
11927 }
1192611928
11927 break :result dst_mcv;11929 break :result dst_mcv;
11928 };11930 };
src/codegen/llvm.zig+5-3
...@@ -8859,19 +8859,21 @@ pub const FuncGen = struct {...@@ -8859,19 +8859,21 @@ pub const FuncGen = struct {
8859 self.arg_index += 1;8859 self.arg_index += 1;
88608860
8861 // llvm does not support debug info for naked function arguments8861 // llvm does not support debug info for naked function arguments
8862 if (self.wip.strip or self.is_naked) return arg_val;8862 if (self.is_naked) return arg_val;
88638863
8864 const inst_ty = self.typeOfIndex(inst);8864 const inst_ty = self.typeOfIndex(inst);
8865 if (needDbgVarWorkaround(o)) return arg_val;8865 if (needDbgVarWorkaround(o)) return arg_val;
88668866
8867 const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;8867 const name = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
8868 if (name == .none) return arg_val;
8869
8868 const func_index = self.dg.decl.getOwnedFunctionIndex();8870 const func_index = self.dg.decl.getOwnedFunctionIndex();
8869 const func = mod.funcInfo(func_index);8871 const func = mod.funcInfo(func_index);
8870 const lbrace_line = mod.declPtr(func.owner_decl).navSrcLine(mod) + func.lbrace_line + 1;8872 const lbrace_line = mod.declPtr(func.owner_decl).navSrcLine(mod) + func.lbrace_line + 1;
8871 const lbrace_col = func.lbrace_column + 1;8873 const lbrace_col = func.lbrace_column + 1;
88728874
8873 const debug_parameter = try o.builder.debugParameter(8875 const debug_parameter = try o.builder.debugParameter(
8874 try o.builder.metadataString(mod.getParamName(func_index, src_index)),8876 try o.builder.metadataString(self.air.nullTerminatedString(@intFromEnum(name))),
8875 self.file,8877 self.file,
8876 self.scope,8878 self.scope,
8877 lbrace_line,8879 lbrace_line,
src/print_air.zig+7-1
...@@ -356,7 +356,13 @@ const Writer = struct {...@@ -356,7 +356,13 @@ const Writer = struct {
356 fn writeArg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {356 fn writeArg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
357 const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg;357 const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg;
358 try w.writeType(s, arg.ty.toType());358 try w.writeType(s, arg.ty.toType());
359 try s.print(", {d}", .{arg.src_index});359 switch (arg.name) {
360 .none => {},
361 _ => {
362 const name = w.air.nullTerminatedString(@intFromEnum(arg.name));
363 try s.print(", \"{}\"", .{std.zig.fmtEscapes(name)});
364 },
365 }
360 }366 }
361367
362 fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {368 fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {