Commit 177b3359a1

Tau <jonathan.haehne@hotmail.com>
2024-06-28 18:01:55
llvm: Do not generate static member definitions
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.
1 parent 52e4cdb
Changed files (3)
src
src/codegen/llvm/Builder.zig
@@ -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,
@@ -7666,7 +7665,6 @@ pub const Metadata = enum(u32) {
         parameter,
         global_var,
         @"global_var local",
-        @"global_var decl",
         global_var_expression,
         constant,
 
@@ -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,
@@ -7714,7 +7711,6 @@ pub const Metadata = enum(u32) {
                 .parameter,
                 .global_var,
                 .@"global_var local",
-                .@"global_var decl",
                 .global_var_expression,
                 => false,
             };
@@ -8008,12 +8004,6 @@ pub const Metadata = enum(u32) {
     };
 
     pub const GlobalVar = struct {
-        pub const Options = enum {
-            internal,
-            internal_decl,
-            external,
-        };
-
         name: MetadataString,
         linkage_name: MetadataString,
         file: Metadata,
@@ -8021,7 +8011,6 @@ pub const Metadata = enum(u32) {
         line: u32,
         ty: Metadata,
         variable: Variable.Index,
-        declaration: Metadata,
     };
 
     pub const GlobalVarExpression = struct {
@@ -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);
@@ -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,
                         }),
@@ -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,
@@ -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, .{
@@ -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,
@@ -11991,28 +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,
     name: MetadataString,
@@ -12034,7 +11999,6 @@ pub fn debugMemberType(
         size_in_bits,
         align_in_bits,
         offset_in_bits,
-        false,
     );
 }
 
@@ -12175,8 +12139,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(
@@ -12187,8 +12150,7 @@ pub fn debugGlobalVar(
         line,
         ty,
         variable,
-        declaration,
-        options,
+        internal,
     );
 }
 
@@ -12617,10 +12579,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,
@@ -12888,16 +12849,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,
@@ -12906,7 +12862,6 @@ fn debugGlobalVarAssumeCapacity(
             .line = line,
             .ty = ty,
             .variable = variable,
-            .declaration = declaration,
         },
     );
 }
@@ -14024,14 +13979,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,
                             },
@@ -14044,7 +13998,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);
                     },
@@ -14184,7 +14138,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{
@@ -14195,8 +14148,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 => {
src/codegen/llvm/ir.zig
@@ -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
@@ -1040,8 +1040,6 @@ pub const MetadataBlock = struct {
         line: u32,
         ty: Builder.Metadata,
         local: bool,
-        defined: bool,
-        declaration: Builder.Metadata,
     };
 
     pub const GlobalVarExpression = struct {
src/codegen/llvm.zig
@@ -2444,6 +2444,8 @@ pub const Object = struct {
                     if (decl.kind != .named) continue;
                     if (decl.analysis != .complete) continue;
 
+                    const decl_line = 0;
+
                     if (decl.val.typeOf(zcu).ip_index == .type_type) {
                         const nested_type = decl.val.toType();
                         // If this decl is the owner of the type, it will
@@ -2465,52 +2467,10 @@ pub const Object = struct {
                             try o.builder.metadataString(decl_name),
                             try o.getDebugFile(namespace.fileScope(zcu)),
                             fwd_ref,
-                            0, // Line
+                            decl_line,
                             try o.lowerDebugType(nested_type, false),
                             0, // Align
                         ));
-                    } else if (decl.val.getVariable(zcu)) |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.fileScope(zcu));
-                        const debug_line = decl.navSrcLine(zcu) + 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);
                     }
                 }
             }
@@ -4778,32 +4738,42 @@ pub const DeclGen = struct {
                 const debug_file = try o.getDebugFile(file_scope);
 
                 const linkage_name = try o.builder.metadataStringFromStrtabString(variable_index.name(&o.builder));
+                const is_internal_linkage = !decl.isExtern(zcu);
+
+                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) {
+                    const name = try o.builder.metadataString(decl.name.toSlice(ip));
+                    const debug_scope = try o.namespaceToDebugScope(decl.src_namespace);
 
-                if (!decl.isExtern(zcu)) {
-                    // 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 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);
             }
         }