Air: store param names directly instead of referencing Zir

This commit is contained in:
mlugg 2024-07-04 10:31:59 +01:00 committed by Jacob Young
parent 3aa48bf859
commit f93a10f664
11 changed files with 87 additions and 51 deletions

View File

@ -1034,7 +1034,12 @@ pub const Inst = struct {
ty: Type,
arg: struct {
ty: Ref,
src_index: u32,
/// Index into `extra` of a null-terminated string representing the parameter name.
/// This is `.none` if debug info is stripped.
name: enum(u32) {
none = std.math.maxInt(u32),
_,
},
},
ty_op: struct {
ty: Ref,

View File

@ -6718,13 +6718,7 @@ fn addDbgVar(
if (block.need_debug_scope) |ptr| ptr.* = true;
// Add the name to the AIR.
const name_extra_index: u32 = @intCast(sema.air_extra.items.len);
const elements_used = name.len / 4 + 1;
try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements_used);
const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
@memcpy(buffer[0..name.len], name);
buffer[name.len] = 0;
sema.air_extra.items.len += elements_used;
const name_extra_index = try sema.appendAirString(name);
_ = try block.addInst(.{
.tag = air_tag,
@ -6735,6 +6729,16 @@ fn addDbgVar(
});
}
pub fn appendAirString(sema: *Sema, str: []const u8) Allocator.Error!u32 {
const str_extra_index: u32 = @intCast(sema.air_extra.items.len);
const elements_used = str.len / 4 + 1;
const elements = try sema.air_extra.addManyAsSlice(sema.gpa, elements_used);
const buffer = mem.sliceAsBytes(elements);
@memcpy(buffer[0..str.len], str);
buffer[str.len] = 0;
return str_extra_index;
}
fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
const pt = sema.pt;
const mod = pt.zcu;
@ -8354,13 +8358,6 @@ fn instantiateGenericCall(
}
} else {
// The parameter is runtime-known.
child_sema.inst_map.putAssumeCapacityNoClobber(param_inst, try child_block.addInst(.{
.tag = .arg,
.data = .{ .arg = .{
.ty = Air.internedToRef(arg_ty.toIntern()),
.src_index = @intCast(arg_index),
} },
}));
const param_name: Zir.NullTerminatedString = switch (param_tag) {
.param_anytype => fn_zir.instructions.items(.data)[@intFromEnum(param_inst)].str_tok.start,
.param => name: {
@ -8370,6 +8367,16 @@ fn instantiateGenericCall(
},
else => unreachable,
};
child_sema.inst_map.putAssumeCapacityNoClobber(param_inst, try child_block.addInst(.{
.tag = .arg,
.data = .{ .arg = .{
.ty = Air.internedToRef(arg_ty.toIntern()),
.name = if (child_block.ownerModule().strip)
.none
else
@enumFromInt(try sema.appendAirString(fn_zir.nullTerminatedString(param_name))),
} },
}));
try child_block.params.append(sema.arena, .{
.ty = arg_ty.toIntern(), // This is the type after coercion
.is_comptime = false, // We're adding only runtime args to the instantiation

View File

@ -1907,10 +1907,17 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
runtime_params_len;
var runtime_param_index: usize = 0;
for (fn_info.param_body[0..src_params_len], 0..) |inst, src_param_index| {
for (fn_info.param_body[0..src_params_len]) |inst| {
const gop = sema.inst_map.getOrPutAssumeCapacity(inst);
if (gop.found_existing) continue; // provided above by comptime arg
const inst_info = sema.code.instructions.get(@intFromEnum(inst));
const param_name: Zir.NullTerminatedString = switch (inst_info.tag) {
.param_anytype => inst_info.data.str_tok.start,
.param => sema.code.extraData(Zir.Inst.Param, inst_info.data.pl_tok.payload_index).data.name,
else => unreachable,
};
const param_ty = fn_ty_info.param_types.get(ip)[runtime_param_index];
runtime_param_index += 1;
@ -1931,7 +1938,10 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
.tag = .arg,
.data = .{ .arg = .{
.ty = Air.internedToRef(param_ty),
.src_index = @intCast(src_param_index),
.name = if (inner_block.ownerModule().strip)
.none
else
@enumFromInt(try sema.appendAirString(sema.code.nullTerminatedString(param_name))),
} },
});
}

View File

@ -4231,19 +4231,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
while (self.args[arg_index] == .none) arg_index += 1;
self.arg_index = arg_index + 1;
const pt = self.pt;
const mod = pt.zcu;
const ty = self.typeOfIndex(inst);
const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
const name = mod.getParamName(self.func_index, src_index);
try self.dbg_info_relocs.append(self.gpa, .{
.tag = tag,
.ty = ty,
.name = name,
.mcv = self.args[arg_index],
});
const name_nts = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
if (name_nts != .none) {
const name = self.air.nullTerminatedString(@intFromEnum(name_nts));
try self.dbg_info_relocs.append(self.gpa, .{
.tag = tag,
.ty = ty,
.name = name,
.mcv = self.args[arg_index],
});
}
const result: MCValue = if (self.liveness.isUnused(inst)) .dead else self.args[arg_index];
return self.finishAir(inst, result, .{ .none, .none, .none });

View File

@ -4206,19 +4206,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
while (self.args[arg_index] == .none) arg_index += 1;
self.arg_index = arg_index + 1;
const pt = self.pt;
const mod = pt.zcu;
const ty = self.typeOfIndex(inst);
const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
const name = mod.getParamName(self.func_index, src_index);
try self.dbg_info_relocs.append(self.gpa, .{
.tag = tag,
.ty = ty,
.name = name,
.mcv = self.args[arg_index],
});
const name_nts = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
if (name_nts != .none) {
const name = self.air.nullTerminatedString(@intFromEnum(name_nts));
try self.dbg_info_relocs.append(self.gpa, .{
.tag = tag,
.ty = ty,
.name = name,
.mcv = self.args[arg_index],
});
}
const result: MCValue = if (self.liveness.isUnused(inst)) .dead else self.args[arg_index];
return self.finishAir(inst, result, .{ .none, .none, .none });

View File

@ -4051,7 +4051,8 @@ fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void {
const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg;
const ty = arg.ty.toType();
const owner_decl = zcu.funcOwnerDeclIndex(func.func_index);
const name = zcu.getParamName(func.func_index, arg.src_index);
if (arg.name == .none) return;
const name = func.air.nullTerminatedString(@intFromEnum(arg.name));
switch (func.debug_output) {
.dwarf => |dw| switch (mcv) {

View File

@ -3614,7 +3614,8 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {
const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg;
const ty = arg.ty.toType();
const owner_decl = mod.funcOwnerDeclIndex(self.func_index);
const name = mod.getParamName(self.func_index, arg.src_index);
if (arg.name == .none) return;
const name = self.air.nullTerminatedString(@intFromEnum(arg.name));
switch (self.debug_output) {
.dwarf => |dw| switch (mcv) {

View File

@ -2585,11 +2585,13 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
switch (func.debug_output) {
.dwarf => |dwarf| {
const src_index = func.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
const name = mod.getParamName(func.func_index, src_index);
try dwarf.genArgDbgInfo(name, arg_ty, mod.funcOwnerDeclIndex(func.func_index), .{
.wasm_local = arg.local.value,
});
const name_nts = func.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
if (name_nts != .none) {
const name = func.air.nullTerminatedString(@intFromEnum(name_nts));
try dwarf.genArgDbgInfo(name, arg_ty, mod.funcOwnerDeclIndex(func.func_index), .{
.wasm_local = arg.local.value,
});
}
},
else => {},
}

View File

@ -11920,9 +11920,11 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
else => return self.fail("TODO implement arg for {}", .{src_mcv}),
};
const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
const name = mod.getParamName(self.owner.func_index, src_index);
try self.genArgDbgInfo(arg_ty, name, src_mcv);
const name_nts = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
switch (name_nts) {
.none => {},
_ => try self.genArgDbgInfo(arg_ty, self.air.nullTerminatedString(@intFromEnum(name_nts)), src_mcv),
}
break :result dst_mcv;
};

View File

@ -8859,19 +8859,21 @@ pub const FuncGen = struct {
self.arg_index += 1;
// llvm does not support debug info for naked function arguments
if (self.wip.strip or self.is_naked) return arg_val;
if (self.is_naked) return arg_val;
const inst_ty = self.typeOfIndex(inst);
if (needDbgVarWorkaround(o)) return arg_val;
const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index;
const name = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
if (name == .none) return arg_val;
const func_index = self.dg.decl.getOwnedFunctionIndex();
const func = mod.funcInfo(func_index);
const lbrace_line = mod.declPtr(func.owner_decl).navSrcLine(mod) + func.lbrace_line + 1;
const lbrace_col = func.lbrace_column + 1;
const debug_parameter = try o.builder.debugParameter(
try o.builder.metadataString(mod.getParamName(func_index, src_index)),
try o.builder.metadataString(self.air.nullTerminatedString(@intFromEnum(name))),
self.file,
self.scope,
lbrace_line,

View File

@ -356,7 +356,13 @@ const Writer = struct {
fn writeArg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg;
try w.writeType(s, arg.ty.toType());
try s.print(", {d}", .{arg.src_index});
switch (arg.name) {
.none => {},
_ => {
const name = w.air.nullTerminatedString(@intFromEnum(arg.name));
try s.print(", \"{}\"", .{std.zig.fmtEscapes(name)});
},
}
}
fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {