Skip to content

Commit

Permalink
llvm: Do not generate static member definitions
Browse files Browse the repository at this point in the history
They were not helping LLDB and actively throwing off GDB.
Also: clean up some llvm.Builder and llvm.ir definitions that are no
longer necessary.
  • Loading branch information
tau-dev committed Jun 28, 2024
1 parent fdcbb79 commit 1d2ea78
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 124 deletions.
93 changes: 31 additions & 62 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,8 @@ pub const Object = struct {
if (decl.kind != .named) continue;
if (decl.analysis != .complete) continue;

const decl_line = 0;

if (decl.val.typeOf(o.module).ip_index == .type_type) {
const nested_type = decl.val.toType();
// If this decl is the owner of the type, it will
Expand All @@ -2538,52 +2540,10 @@ pub const Object = struct {
try o.builder.metadataString(decl_name),
try o.getDebugFile(namespace.file_scope),
fwd_ref,
0, // Line
decl_line,
try o.lowerDebugType(nested_type, false),
0, // Align
));
} else if (decl.val.getVariable(mod)) |v| {
// Imitate a C++ static member variable since neither
// GDB or LLDB can really cope with regular variables
// directly inside a struct type.

const vglobal = (o.decl_map.get(decl_id) orelse continue).ptr(&o.builder);

const linkage_name = try o.builder.metadataStringFromStrtabString(vglobal.kind.variable.name(&o.builder));
const var_name = try o.builder.metadataString(decl.name.toSlice(ip));
const var_type = try o.lowerDebugType(Type.fromInterned(v.ty), false);
const debug_file = try o.getDebugFile(namespace.file_scope);
const debug_line = decl.navSrcLine(mod) + 1;

const static_member = try o.builder.debugStaticMemberType(
var_name,
debug_file,
fwd_ref,
debug_line,
var_type,
);
fields.appendAssumeCapacity(static_member);

const debug_global_var = try o.builder.debugGlobalVar(
var_name,
linkage_name,
debug_file,
debug_file,
debug_line,
var_type,
vglobal.kind.variable,
static_member,
.internal,
);

const debug_expression = try o.builder.debugExpression(&.{});

const resolved_var = try o.builder.debugGlobalVarExpression(
debug_global_var,
debug_expression,
);
vglobal.dbg = resolved_var;
try o.debug_globals.append(o.gpa, resolved_var);
}
}
}
Expand Down Expand Up @@ -4834,31 +4794,40 @@ pub const DeclGen = struct {

const linkage_name = try o.builder.metadataStringFromStrtabString(variable_index.name(&o.builder));

const ty = try o.lowerDebugType(decl.typeOf(zcu), true);
const debug_global_var = try o.builder.debugGlobalVar(
linkage_name,
linkage_name,
debug_file,
debug_file,
line_number,
ty,
variable_index,
is_internal_linkage,
);
if (is_internal_linkage) {
// Make it a static member variable, which is resolved later in genNamespaces.
_ = try o.namespaceToDebugScope(decl.src_namespace);
} else {
const debug_global_var = try o.builder.debugGlobalVar(
linkage_name,
linkage_name,
debug_file,
const name = try o.builder.metadataString(decl.name.toSlice(ip));
const debug_scope = try o.namespaceToDebugScope(decl.src_namespace);

const import = try o.builder.debugImportDeclaration(
name,
debug_file,
debug_scope,
line_number,
try o.lowerDebugType(decl.typeOf(zcu), true),
variable_index,
.none,
.external,
);

const debug_expression = try o.builder.debugExpression(&.{});

const debug_global_var_expression = try o.builder.debugGlobalVarExpression(
debug_global_var,
debug_expression,
);
variable_index.setGlobalVariableExpression(debug_global_var_expression, &o.builder);
try o.debug_globals.append(o.gpa, debug_global_var_expression);
try o.debug_imports.append(o.gpa, import);
}

const debug_expression = try o.builder.debugExpression(&.{});

const debug_global_var_expression = try o.builder.debugGlobalVarExpression(
debug_global_var,
debug_expression,
);

variable_index.setGlobalVariableExpression(debug_global_var_expression, &o.builder);
try o.debug_globals.append(o.gpa, debug_global_var_expression);
}
}
};
Expand Down
68 changes: 10 additions & 58 deletions src/codegen/llvm/Builder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7651,7 +7651,6 @@ pub const Metadata = enum(u32) {
composite_vector_type,
derived_pointer_type,
derived_member_type,
derived_static_member_type,
derived_typedef,
imported_declaration,
subroutine_type,
Expand All @@ -7666,7 +7665,6 @@ pub const Metadata = enum(u32) {
parameter,
global_var,
@"global_var local",
@"global_var decl",
global_var_expression,
constant,

Expand Down Expand Up @@ -7700,7 +7698,6 @@ pub const Metadata = enum(u32) {
.composite_vector_type,
.derived_pointer_type,
.derived_member_type,
.derived_static_member_type,
.derived_typedef,
.imported_declaration,
.subroutine_type,
Expand All @@ -7714,7 +7711,6 @@ pub const Metadata = enum(u32) {
.parameter,
.global_var,
.@"global_var local",
.@"global_var decl",
.global_var_expression,
=> false,
};
Expand Down Expand Up @@ -8008,20 +8004,13 @@ pub const Metadata = enum(u32) {
};

pub const GlobalVar = struct {
pub const Options = enum {
internal,
internal_decl,
external,
};

name: MetadataString,
linkage_name: MetadataString,
file: Metadata,
scope: Metadata,
line: u32,
ty: Metadata,
variable: Variable.Index,
declaration: Metadata,
};

pub const GlobalVarExpression = struct {
Expand Down Expand Up @@ -10123,7 +10112,6 @@ pub fn printUnbuffered(
},
.derived_pointer_type,
.derived_member_type,
.derived_static_member_type,
.derived_typedef,
=> |kind| {
const extra = self.metadataExtraData(Metadata.DerivedType, metadata_item.data);
Expand All @@ -10134,7 +10122,7 @@ pub fn printUnbuffered(
DW_TAG_typedef,
}, switch (kind) {
.derived_pointer_type => .DW_TAG_pointer_type,
.derived_member_type, .derived_static_member_type => .DW_TAG_member,
.derived_member_type => .DW_TAG_member,
.derived_typedef => .DW_TAG_typedef,
else => unreachable,
}),
Expand All @@ -10152,7 +10140,7 @@ pub fn printUnbuffered(
0 => null,
else => |bit_offset| bit_offset,
},
.flags = null, // TODO staticness
.flags = null,
.extraData = null,
.dwarfAddressSpace = null,
.annotations = null,
Expand Down Expand Up @@ -10288,7 +10276,6 @@ pub fn printUnbuffered(
},
.global_var,
.@"global_var local",
.@"global_var decl",
=> |kind| {
const extra = self.metadataExtraData(Metadata.GlobalVar, metadata_item.data);
try metadata_formatter.specialized(.@"distinct !", .DIGlobalVariable, .{
Expand All @@ -10299,7 +10286,7 @@ pub fn printUnbuffered(
.line = extra.line,
.type = extra.ty,
.isLocal = kind != .global_var,
.isDefinition = kind != .@"global_var decl",
.isDefinition = true,
.declaration = null,
.templateParams = null,
.@"align" = null,
Expand Down Expand Up @@ -11991,27 +11978,6 @@ pub fn debugPointerType(
);
}

pub fn debugStaticMemberType(
self: *Builder,
name: MetadataString,
file: Metadata,
scope: Metadata,
line: u32,
underlying_type: Metadata,
) Allocator.Error!Metadata {
try self.ensureUnusedMetadataCapacity(1, Metadata.DerivedType, 0);
return self.debugMemberTypeAssumeCapacity(
name,
file,
scope,
line,
underlying_type,
0,
0,
0,
true,
);
}

pub fn debugMemberType(
self: *Builder,
Expand All @@ -12034,7 +12000,6 @@ pub fn debugMemberType(
size_in_bits,
align_in_bits,
offset_in_bits,
false,
);
}

Expand Down Expand Up @@ -12175,8 +12140,7 @@ pub fn debugGlobalVar(
line: u32,
ty: Metadata,
variable: Variable.Index,
declaration: Metadata,
options: Metadata.GlobalVar.Options,
internal: bool,
) Allocator.Error!Metadata {
try self.ensureUnusedMetadataCapacity(1, Metadata.GlobalVar, 0);
return self.debugGlobalVarAssumeCapacity(
Expand All @@ -12187,8 +12151,7 @@ pub fn debugGlobalVar(
line,
ty,
variable,
declaration,
options,
internal,
);
}

Expand Down Expand Up @@ -12617,10 +12580,9 @@ fn debugMemberTypeAssumeCapacity(
size_in_bits: u64,
align_in_bits: u64,
offset_in_bits: u64,
static: bool,
) Metadata {
assert(!self.strip);
return self.metadataSimpleAssumeCapacity(if (static) .derived_static_member_type else .derived_member_type, Metadata.DerivedType{
return self.metadataSimpleAssumeCapacity(.derived_member_type, Metadata.DerivedType{
.name = name,
.file = file,
.scope = scope,
Expand Down Expand Up @@ -12888,16 +12850,11 @@ fn debugGlobalVarAssumeCapacity(
line: u32,
ty: Metadata,
variable: Variable.Index,
declaration: Metadata,
options: Metadata.GlobalVar.Options,
internal: bool,
) Metadata {
assert(!self.strip);
return self.metadataDistinctAssumeCapacity(
switch (options) {
.internal => .@"global_var local",
.internal_decl => .@"global_var decl",
.external => .global_var,
},
if (internal) .@"global_var local" else .global_var,
Metadata.GlobalVar{
.name = name,
.linkage_name = linkage_name,
Expand All @@ -12906,7 +12863,6 @@ fn debugGlobalVarAssumeCapacity(
.line = line,
.ty = ty,
.variable = variable,
.declaration = declaration,
},
);
}
Expand Down Expand Up @@ -14024,14 +13980,13 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
},
.derived_pointer_type,
.derived_member_type,
.derived_static_member_type,
.derived_typedef,
=> |kind| {
const extra = self.metadataExtraData(Metadata.DerivedType, data);
try metadata_block.writeAbbrevAdapted(MetadataBlock.DerivedType{
.tag = switch (kind) {
.derived_pointer_type => DW.TAG.pointer_type,
.derived_member_type, .derived_static_member_type => DW.TAG.member,
.derived_member_type => DW.TAG.member,
.derived_typedef => DW.TAG.typedef,
else => unreachable,
},
Expand All @@ -14044,7 +13999,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
.align_in_bits = extra.bitAlign(),
.offset_in_bits = extra.bitOffset(),
.flags = .{
.StaticMember = kind == .derived_static_member_type,
.StaticMember = false,
},
}, metadata_adapter);
},
Expand Down Expand Up @@ -14184,7 +14139,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
},
.global_var,
.@"global_var local",
.@"global_var decl",
=> |kind| {
const extra = self.metadataExtraData(Metadata.GlobalVar, data);
try metadata_block.writeAbbrevAdapted(MetadataBlock.GlobalVar{
Expand All @@ -14195,8 +14149,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
.line = extra.line,
.ty = extra.ty,
.local = kind == .@"global_var local",
.defined = kind != .@"global_var decl",
.declaration = extra.declaration,
}, metadata_adapter);
},
.global_var_expression => {
Expand Down
6 changes: 2 additions & 4 deletions src/codegen/llvm/ir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,8 @@ pub const MetadataBlock = struct {
LineAbbrev, // line
MetadataAbbrev, // type
.{ .fixed = 1 }, // local
.{ .fixed = 1 }, // defined
MetadataAbbrev, // static data members declaration
.{ .literal = 1 }, // defined
.{ .literal = 0 }, // static data members declaration
.{ .literal = 0 }, // template params
.{ .literal = 0 }, // align in bits
.{ .literal = 0 }, // annotations
Expand All @@ -1040,8 +1040,6 @@ pub const MetadataBlock = struct {
line: u32,
ty: Builder.Metadata,
local: bool,
defined: bool,
declaration: Builder.Metadata,
};

pub const GlobalVarExpression = struct {
Expand Down

0 comments on commit 1d2ea78

Please sign in to comment.