authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-21 21:46:04+01:00
committergravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-21 21:46:04+01:00
loge57f553c07cff62871927146e072be43f13432e3
tree8619261c86ef0a929f95619d0a99fde59fad2a84
parent48bd0ed7f3f5d6c1c0f2046a791e7367ba0672ad

LLVM Builder: Rework MetadataString to not rely on String

This fixes a problem where empty strings where not emitted as null. This should also emit a smaller stringtab as all metadata strings were emitted in both the strtab and in the strings block inside the metadata block.

2 files changed, 193 insertions(+), 145 deletions(-)

src/codegen/llvm.zig+56-56
......@@ -872,8 +872,8 @@ pub const Object = struct {
872872 };
873873
874874 const debug_file = try builder.debugFile(
875 try builder.string(compile_unit_dir),
876 try builder.string(comp.root_name),
875 try builder.metadataString(compile_unit_dir),
876 try builder.metadataString(comp.root_name),
877877 );
878878
879879 const debug_enums_fwd_ref = try builder.debugForwardReference();
......@@ -883,7 +883,7 @@ pub const Object = struct {
883883 debug_file,
884884 // Don't use the version string here; LLVM misparses it when it
885885 // includes the git revision.
886 try builder.fmt("zig {d}.{d}.{d}", .{
886 try builder.metadataStringFmt("zig {d}.{d}.{d}", .{
887887 build_options.semver.major,
888888 build_options.semver.minor,
889889 build_options.semver.patch,
......@@ -896,7 +896,7 @@ pub const Object = struct {
896896 if (!builder.strip) {
897897 const debug_info_version = try builder.debugModuleFlag(
898898 try builder.debugConstant(try builder.intConst(.i32, 2)),
899 try builder.string("Debug Info Version"),
899 try builder.metadataString("Debug Info Version"),
900900 try builder.debugConstant(try builder.intConst(.i32, 3)),
901901 );
902902
......@@ -904,12 +904,12 @@ pub const Object = struct {
904904 .dwarf => |f| {
905905 const dwarf_version = try builder.debugModuleFlag(
906906 try builder.debugConstant(try builder.intConst(.i32, 2)),
907 try builder.string("Dwarf Version"),
907 try builder.metadataString("Dwarf Version"),
908908 try builder.debugConstant(try builder.intConst(.i32, 4)),
909909 );
910910 switch (f) {
911911 .@"32" => {
912 try builder.debugNamed(try builder.string("llvm.module.flags"), &.{
912 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{
913913 debug_info_version,
914914 dwarf_version,
915915 });
......@@ -917,10 +917,10 @@ pub const Object = struct {
917917 .@"64" => {
918918 const dwarf64 = try builder.debugModuleFlag(
919919 try builder.debugConstant(try builder.intConst(.i32, 2)),
920 try builder.string("DWARF64"),
920 try builder.metadataString("DWARF64"),
921921 try builder.debugConstant(try builder.intConst(.i32, 1)),
922922 );
923 try builder.debugNamed(try builder.string("llvm.module.flags"), &.{
923 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{
924924 debug_info_version,
925925 dwarf_version,
926926 dwarf64,
......@@ -931,10 +931,10 @@ pub const Object = struct {
931931 .code_view => {
932932 const code_view = try builder.debugModuleFlag(
933933 try builder.debugConstant(try builder.intConst(.i32, 2)),
934 try builder.string("CodeView"),
934 try builder.metadataString("CodeView"),
935935 try builder.debugConstant(try builder.intConst(.i32, 1)),
936936 );
937 try builder.debugNamed(try builder.string("llvm.module.flags"), &.{
937 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{
938938 debug_info_version,
939939 code_view,
940940 });
......@@ -942,7 +942,7 @@ pub const Object = struct {
942942 .strip => unreachable,
943943 }
944944
945 try builder.debugNamed(try builder.string("llvm.dbg.cu"), &.{debug_compile_unit});
945 try builder.debugNamed(try builder.metadataString("llvm.dbg.cu"), &.{debug_compile_unit});
946946 }
947947
948948 const obj = try arena.create(Object);
......@@ -1654,8 +1654,8 @@ pub const Object = struct {
16541654
16551655 break :blk try o.builder.debugSubprogram(
16561656 file,
1657 try o.builder.string(ip.stringToSlice(decl.name)),
1658 function_index.name(&o.builder),
1657 try o.builder.metadataString(ip.stringToSlice(decl.name)),
1658 try o.builder.metadataStringFromString(function_index.name(&o.builder)),
16591659 line_number,
16601660 line_number + func.lbrace_line,
16611661 debug_decl_type,
......@@ -1914,8 +1914,8 @@ pub const Object = struct {
19141914
19151915 fn getDebugFile(o: *Object, file: *const Module.File) Allocator.Error!Builder.Metadata {
19161916 return try o.builder.debugFile(
1917 if (std.fs.path.dirname(file.sub_file_path)) |dirname| try o.builder.string(dirname) else .empty,
1918 try o.builder.string(std.fs.path.basename(file.sub_file_path)),
1917 if (std.fs.path.dirname(file.sub_file_path)) |dirname| try o.builder.metadataString(dirname) else .none,
1918 try o.builder.metadataString(std.fs.path.basename(file.sub_file_path)),
19191919 );
19201920 }
19211921
......@@ -1936,7 +1936,7 @@ pub const Object = struct {
19361936 .NoReturn,
19371937 => {
19381938 const debug_void_type = try o.builder.debugSignedType(
1939 try o.builder.string("void"),
1939 try o.builder.metadataString("void"),
19401940 0,
19411941 );
19421942 try o.debug_type_map.put(gpa, ty, debug_void_type);
......@@ -1947,7 +1947,7 @@ pub const Object = struct {
19471947 assert(info.bits != 0);
19481948 const name = try o.allocTypeName(ty);
19491949 defer gpa.free(name);
1950 const builder_name = try o.builder.string(name);
1950 const builder_name = try o.builder.metadataString(name);
19511951 const debug_bits = ty.abiSize(mod) * 8; // lldb cannot handle non-byte sized types
19521952 const debug_int_type = switch (info.signedness) {
19531953 .signed => try o.builder.debugSignedType(builder_name, debug_bits),
......@@ -1984,7 +1984,7 @@ pub const Object = struct {
19841984 std.math.big.int.Mutable.init(&bigint_space.limbs, i).toConst();
19851985
19861986 enumerators[i] = try o.builder.debugEnumerator(
1987 try o.builder.string(ip.stringToSlice(field_name_ip)),
1987 try o.builder.metadataString(ip.stringToSlice(field_name_ip)),
19881988 int_ty.isUnsignedInt(mod),
19891989 int_info.bits,
19901990 bigint,
......@@ -1998,7 +1998,7 @@ pub const Object = struct {
19981998 defer gpa.free(name);
19991999
20002000 const debug_enum_type = try o.builder.debugEnumerationType(
2001 try o.builder.string(name),
2001 try o.builder.metadataString(name),
20022002 file,
20032003 scope,
20042004 owner_decl.src_node + 1, // Line
......@@ -2017,7 +2017,7 @@ pub const Object = struct {
20172017 const name = try o.allocTypeName(ty);
20182018 defer gpa.free(name);
20192019 const debug_float_type = try o.builder.debugFloatType(
2020 try o.builder.string(name),
2020 try o.builder.metadataString(name),
20212021 bits,
20222022 );
20232023 try o.debug_type_map.put(gpa, ty, debug_float_type);
......@@ -2025,7 +2025,7 @@ pub const Object = struct {
20252025 },
20262026 .Bool => {
20272027 const debug_bool_type = try o.builder.debugBoolType(
2028 try o.builder.string("bool"),
2028 try o.builder.metadataString("bool"),
20292029 8, // lldb cannot handle non-byte sized types
20302030 );
20312031 try o.debug_type_map.put(gpa, ty, debug_bool_type);
......@@ -2085,7 +2085,7 @@ pub const Object = struct {
20852085 const len_offset = len_align.forward(ptr_size);
20862086
20872087 const debug_ptr_type = try o.builder.debugMemberType(
2088 try o.builder.string("ptr"),
2088 try o.builder.metadataString("ptr"),
20892089 Builder.Metadata.none, // File
20902090 debug_fwd_ref,
20912091 0, // Line
......@@ -2096,7 +2096,7 @@ pub const Object = struct {
20962096 );
20972097
20982098 const debug_len_type = try o.builder.debugMemberType(
2099 try o.builder.string("len"),
2099 try o.builder.metadataString("len"),
21002100 Builder.Metadata.none, // File
21012101 debug_fwd_ref,
21022102 0, // Line
......@@ -2107,7 +2107,7 @@ pub const Object = struct {
21072107 );
21082108
21092109 const debug_slice_type = try o.builder.debugStructType(
2110 try o.builder.string(name),
2110 try o.builder.metadataString(name),
21112111 Builder.Metadata.none, // File
21122112 o.debug_compile_unit, // Scope
21132113 line,
......@@ -2135,7 +2135,7 @@ pub const Object = struct {
21352135 defer gpa.free(name);
21362136
21372137 const debug_ptr_type = try o.builder.debugPointerType(
2138 try o.builder.string(name),
2138 try o.builder.metadataString(name),
21392139 Builder.Metadata.none, // File
21402140 Builder.Metadata.none, // Scope
21412141 0, // Line
......@@ -2156,7 +2156,7 @@ pub const Object = struct {
21562156 .Opaque => {
21572157 if (ty.toIntern() == .anyopaque_type) {
21582158 const debug_opaque_type = try o.builder.debugSignedType(
2159 try o.builder.string("anyopaque"),
2159 try o.builder.metadataString("anyopaque"),
21602160 0,
21612161 );
21622162 try o.debug_type_map.put(gpa, ty, debug_opaque_type);
......@@ -2168,7 +2168,7 @@ pub const Object = struct {
21682168 const owner_decl_index = ty.getOwnerDecl(mod);
21692169 const owner_decl = o.module.declPtr(owner_decl_index);
21702170 const debug_opaque_type = try o.builder.debugStructType(
2171 try o.builder.string(name),
2171 try o.builder.metadataString(name),
21722172 try o.getDebugFile(mod.namespacePtr(owner_decl.src_namespace).file_scope),
21732173 try o.namespaceToDebugScope(owner_decl.src_namespace),
21742174 owner_decl.src_node + 1, // Line
......@@ -2182,7 +2182,7 @@ pub const Object = struct {
21822182 },
21832183 .Array => {
21842184 const debug_array_type = try o.builder.debugArrayType(
2185 Builder.String.empty, // Name
2185 Builder.MetadataString.none, // Name
21862186 Builder.Metadata.none, // File
21872187 Builder.Metadata.none, // Scope
21882188 0, // Line
......@@ -2211,21 +2211,21 @@ pub const Object = struct {
22112211 assert(info.bits != 0);
22122212 const name = try o.allocTypeName(ty);
22132213 defer gpa.free(name);
2214 const builder_name = try o.builder.string(name);
2214 const builder_name = try o.builder.metadataString(name);
22152215 break :blk switch (info.signedness) {
22162216 .signed => try o.builder.debugSignedType(builder_name, info.bits),
22172217 .unsigned => try o.builder.debugUnsignedType(builder_name, info.bits),
22182218 };
22192219 },
22202220 .Bool => try o.builder.debugBoolType(
2221 try o.builder.string("bool"),
2221 try o.builder.metadataString("bool"),
22222222 1,
22232223 ),
22242224 else => try o.lowerDebugType(ty.childType(mod)),
22252225 };
22262226
22272227 const debug_vector_type = try o.builder.debugArrayType(
2228 Builder.String.empty, // Name
2228 Builder.MetadataString.none, // Name
22292229 Builder.Metadata.none, // File
22302230 Builder.Metadata.none, // Scope
22312231 0, // Line
......@@ -2249,7 +2249,7 @@ pub const Object = struct {
22492249 const child_ty = ty.optionalChild(mod);
22502250 if (!child_ty.hasRuntimeBitsIgnoreComptime(mod)) {
22512251 const debug_bool_type = try o.builder.debugBoolType(
2252 try o.builder.string(name),
2252 try o.builder.metadataString(name),
22532253 8,
22542254 );
22552255 try o.debug_type_map.put(gpa, ty, debug_bool_type);
......@@ -2281,7 +2281,7 @@ pub const Object = struct {
22812281 const non_null_offset = non_null_align.forward(payload_size);
22822282
22832283 const debug_data_type = try o.builder.debugMemberType(
2284 try o.builder.string("data"),
2284 try o.builder.metadataString("data"),
22852285 Builder.Metadata.none, // File
22862286 debug_fwd_ref,
22872287 0, // Line
......@@ -2292,7 +2292,7 @@ pub const Object = struct {
22922292 );
22932293
22942294 const debug_some_type = try o.builder.debugMemberType(
2295 try o.builder.string("some"),
2295 try o.builder.metadataString("some"),
22962296 Builder.Metadata.none,
22972297 debug_fwd_ref,
22982298 0,
......@@ -2303,7 +2303,7 @@ pub const Object = struct {
23032303 );
23042304
23052305 const debug_optional_type = try o.builder.debugStructType(
2306 try o.builder.string(name),
2306 try o.builder.metadataString(name),
23072307 Builder.Metadata.none, // File
23082308 o.debug_compile_unit, // Scope
23092309 0, // Line
......@@ -2361,7 +2361,7 @@ pub const Object = struct {
23612361
23622362 var fields: [2]Builder.Metadata = undefined;
23632363 fields[error_index] = try o.builder.debugMemberType(
2364 try o.builder.string("tag"),
2364 try o.builder.metadataString("tag"),
23652365 Builder.Metadata.none, // File
23662366 debug_fwd_ref,
23672367 0, // Line
......@@ -2371,7 +2371,7 @@ pub const Object = struct {
23712371 error_offset * 8,
23722372 );
23732373 fields[payload_index] = try o.builder.debugMemberType(
2374 try o.builder.string("value"),
2374 try o.builder.metadataString("value"),
23752375 Builder.Metadata.none, // File
23762376 debug_fwd_ref,
23772377 0, // Line
......@@ -2382,7 +2382,7 @@ pub const Object = struct {
23822382 );
23832383
23842384 const debug_error_union_type = try o.builder.debugStructType(
2385 try o.builder.string(name),
2385 try o.builder.metadataString(name),
23862386 Builder.Metadata.none, // File
23872387 o.debug_compile_unit, // Sope
23882388 0, // Line
......@@ -2399,7 +2399,7 @@ pub const Object = struct {
23992399 },
24002400 .ErrorSet => {
24012401 const debug_error_set = try o.builder.debugUnsignedType(
2402 try o.builder.string("anyerror"),
2402 try o.builder.metadataString("anyerror"),
24032403 16,
24042404 );
24052405 try o.debug_type_map.put(gpa, ty, debug_error_set);
......@@ -2413,7 +2413,7 @@ pub const Object = struct {
24132413 const backing_int_ty = struct_type.backingIntType(ip).*;
24142414 if (backing_int_ty != .none) {
24152415 const info = Type.fromInterned(backing_int_ty).intInfo(mod);
2416 const builder_name = try o.builder.string(name);
2416 const builder_name = try o.builder.metadataString(name);
24172417 const debug_int_type = switch (info.signedness) {
24182418 .signed => try o.builder.debugSignedType(builder_name, ty.abiSize(mod) * 8),
24192419 .unsigned => try o.builder.debugUnsignedType(builder_name, ty.abiSize(mod) * 8),
......@@ -2450,7 +2450,7 @@ pub const Object = struct {
24502450 defer if (tuple.names.len == 0) gpa.free(field_name);
24512451
24522452 fields.appendAssumeCapacity(try o.builder.debugMemberType(
2453 try o.builder.string(field_name),
2453 try o.builder.metadataString(field_name),
24542454 Builder.Metadata.none, // File
24552455 debug_fwd_ref,
24562456 0,
......@@ -2462,7 +2462,7 @@ pub const Object = struct {
24622462 }
24632463
24642464 const debug_struct_type = try o.builder.debugStructType(
2465 try o.builder.string(name),
2465 try o.builder.metadataString(name),
24662466 Builder.Metadata.none, // File
24672467 o.debug_compile_unit, // Scope
24682468 0, // Line
......@@ -2531,7 +2531,7 @@ pub const Object = struct {
25312531 try ip.getOrPutStringFmt(gpa, "{d}", .{field_index});
25322532
25332533 fields.appendAssumeCapacity(try o.builder.debugMemberType(
2534 try o.builder.string(ip.stringToSlice(field_name)),
2534 try o.builder.metadataString(ip.stringToSlice(field_name)),
25352535 Builder.Metadata.none, // File
25362536 debug_fwd_ref,
25372537 0, // Line
......@@ -2543,7 +2543,7 @@ pub const Object = struct {
25432543 }
25442544
25452545 const debug_struct_type = try o.builder.debugStructType(
2546 try o.builder.string(name),
2546 try o.builder.metadataString(name),
25472547 Builder.Metadata.none, // File
25482548 o.debug_compile_unit, // Scope
25492549 0, // Line
......@@ -2584,7 +2584,7 @@ pub const Object = struct {
25842584
25852585 if (layout.payload_size == 0) {
25862586 const debug_union_type = try o.builder.debugStructType(
2587 try o.builder.string(name),
2587 try o.builder.metadataString(name),
25882588 Builder.Metadata.none, // File
25892589 o.debug_compile_unit, // Scope
25902590 0, // Line
......@@ -2622,7 +2622,7 @@ pub const Object = struct {
26222622
26232623 const field_name = union_obj.field_names.get(ip)[field_index];
26242624 fields.appendAssumeCapacity(try o.builder.debugMemberType(
2625 try o.builder.string(ip.stringToSlice(field_name)),
2625 try o.builder.metadataString(ip.stringToSlice(field_name)),
26262626 Builder.Metadata.none, // File
26272627 debug_union_fwd_ref,
26282628 0, // Line
......@@ -2641,7 +2641,7 @@ pub const Object = struct {
26412641 };
26422642
26432643 const debug_union_type = try o.builder.debugUnionType(
2644 try o.builder.string(union_name),
2644 try o.builder.metadataString(union_name),
26452645 Builder.Metadata.none, // File
26462646 o.debug_compile_unit, // Scope
26472647 0, // Line
......@@ -2672,7 +2672,7 @@ pub const Object = struct {
26722672 }
26732673
26742674 const debug_tag_type = try o.builder.debugMemberType(
2675 try o.builder.string("tag"),
2675 try o.builder.metadataString("tag"),
26762676 Builder.Metadata.none, // File
26772677 debug_fwd_ref,
26782678 0, // Line
......@@ -2683,7 +2683,7 @@ pub const Object = struct {
26832683 );
26842684
26852685 const debug_payload_type = try o.builder.debugMemberType(
2686 try o.builder.string("payload"),
2686 try o.builder.metadataString("payload"),
26872687 Builder.Metadata.none, // File
26882688 debug_fwd_ref,
26892689 0, // Line
......@@ -2700,7 +2700,7 @@ pub const Object = struct {
27002700 .{ debug_payload_type, debug_tag_type };
27012701
27022702 const debug_tagged_union_type = try o.builder.debugStructType(
2703 try o.builder.string(name),
2703 try o.builder.metadataString(name),
27042704 Builder.Metadata.none, // File
27052705 o.debug_compile_unit, // Scope
27062706 0, // Line
......@@ -2794,7 +2794,7 @@ pub const Object = struct {
27942794 const mod = o.module;
27952795 const decl = mod.declPtr(decl_index);
27962796 return o.builder.debugStructType(
2797 try o.builder.string(mod.intern_pool.stringToSlice(decl.name)), // TODO use fully qualified name
2797 try o.builder.metadataString(mod.intern_pool.stringToSlice(decl.name)), // TODO use fully qualified name
27982798 try o.getDebugFile(mod.namespacePtr(decl.src_namespace).file_scope),
27992799 try o.namespaceToDebugScope(decl.src_namespace),
28002800 decl.src_line + 1,
......@@ -4699,8 +4699,8 @@ pub const DeclGen = struct {
46994699 const debug_file = try o.getDebugFile(mod.namespacePtr(decl.src_namespace).file_scope);
47004700
47014701 const debug_global_var = try o.builder.debugGlobalVar(
4702 try o.builder.string(mod.intern_pool.stringToSlice(decl.name)), // Name
4703 variable_index.name(&o.builder), // Linkage name
4702 try o.builder.metadataString(mod.intern_pool.stringToSlice(decl.name)), // Name
4703 try o.builder.metadataStringFromString(variable_index.name(&o.builder)), // Linkage name
47044704 debug_file, // File
47054705 debug_file, // Scope
47064706 line_number,
......@@ -6678,7 +6678,7 @@ pub const FuncGen = struct {
66786678 const ptr_ty = self.typeOf(pl_op.operand);
66796679
66806680 const debug_local_var = try o.builder.debugLocalVar(
6681 try o.builder.string(name),
6681 try o.builder.metadataString(name),
66826682 self.file,
66836683 self.current_scope,
66846684 self.prev_dbg_line,
......@@ -6712,7 +6712,7 @@ pub const FuncGen = struct {
67126712 if (needDbgVarWorkaround(o)) return .none;
67136713
67146714 const debug_local_var = try o.builder.debugLocalVar(
6715 try o.builder.string(name),
6715 try o.builder.metadataString(name),
67166716 self.file,
67176717 self.current_scope,
67186718 self.prev_dbg_line,
......@@ -8796,7 +8796,7 @@ pub const FuncGen = struct {
87968796 const lbrace_col = func.lbrace_column + 1;
87978797
87988798 const debug_parameter = try o.builder.debugParameter(
8799 try o.builder.string(mod.getParamName(func_index, src_index)),
8799 try o.builder.metadataString(mod.getParamName(func_index, src_index)),
88008800 self.file,
88018801 self.current_scope,
88028802 lbrace_line,
src/codegen/llvm/Builder.zig+137-89
......@@ -40,14 +40,17 @@ constant_limbs: std.ArrayListUnmanaged(std.math.big.Limb),
4040metadata_map: std.AutoArrayHashMapUnmanaged(void, void),
4141metadata_items: std.MultiArrayList(Metadata.Item),
4242metadata_extra: std.ArrayListUnmanaged(u32),
43metadata_strings: std.AutoArrayHashMapUnmanaged(String, void),
4443metadata_limbs: std.ArrayListUnmanaged(std.math.big.Limb),
4544metadata_forward_references: std.ArrayListUnmanaged(Metadata),
46metadata_named: std.AutoArrayHashMapUnmanaged(String, struct {
45metadata_named: std.AutoArrayHashMapUnmanaged(MetadataString, struct {
4746 len: u32,
4847 index: Metadata.Item.ExtraIndex,
4948}),
5049
50metadata_string_map: std.AutoArrayHashMapUnmanaged(void, void),
51metadata_string_indices: std.ArrayListUnmanaged(u32),
52metadata_string_bytes: std.ArrayListUnmanaged(u8),
53
5154pub const expected_args_len = 16;
5255pub const expected_attrs_len = 16;
5356pub const expected_fields_len = 32;
......@@ -7553,7 +7556,26 @@ pub const Value = enum(u32) {
75537556};
75547557
75557558pub const MetadataString = enum(u32) {
7559 none = 0,
75567560 _,
7561
7562 pub fn slice(self: MetadataString, b: *const Builder) []const u8 {
7563 const index = @intFromEnum(self);
7564 const start = b.metadata_string_indices.items[index];
7565 const end = b.metadata_string_indices.items[index + 1];
7566 return b.metadata_string_bytes.items[start..end];
7567 }
7568
7569 const Adapter = struct {
7570 builder: *const Builder,
7571 pub fn hash(_: Adapter, key: []const u8) u32 {
7572 return @truncate(std.hash.Wyhash.hash(0, key));
7573 }
7574 pub fn eql(ctx: Adapter, lhs_key: []const u8, _: void, rhs_index: usize) bool {
7575 const rhs_metadata_string: MetadataString = @enumFromInt(rhs_index);
7576 return std.mem.eql(u8, lhs_key, rhs_metadata_string.slice(ctx.builder));
7577 }
7578 };
75577579};
75587580
75597581pub const Metadata = enum(u32) {
......@@ -7829,10 +7851,12 @@ pub fn init(options: Options) InitError!Builder {
78297851 .metadata_map = .{},
78307852 .metadata_items = .{},
78317853 .metadata_extra = .{},
7832 .metadata_strings = .{},
78337854 .metadata_limbs = .{},
78347855 .metadata_forward_references = .{},
78357856 .metadata_named = .{},
7857 .metadata_string_map = .{},
7858 .metadata_string_indices = .{},
7859 .metadata_string_bytes = .{},
78367860 };
78377861 errdefer self.deinit();
78387862
......@@ -7876,6 +7900,9 @@ pub fn init(options: Options) InitError!Builder {
78767900 assert(try self.noneConst(.token) == .none);
78777901 assert(try self.debugNone() == .none);
78787902
7903 try self.metadata_string_indices.append(self.gpa, 0);
7904 assert(try self.metadataString("") == .none);
7905
78797906 return self;
78807907}
78817908
......@@ -7914,11 +7941,14 @@ pub fn deinit(self: *Builder) void {
79147941 self.metadata_map.deinit(self.gpa);
79157942 self.metadata_items.deinit(self.gpa);
79167943 self.metadata_extra.deinit(self.gpa);
7917 self.metadata_strings.deinit(self.gpa);
79187944 self.metadata_limbs.deinit(self.gpa);
79197945 self.metadata_forward_references.deinit(self.gpa);
79207946 self.metadata_named.deinit(self.gpa);
79217947
7948 self.metadata_string_map.deinit(self.gpa);
7949 self.metadata_string_indices.deinit(self.gpa);
7950 self.metadata_string_bytes.deinit(self.gpa);
7951
79227952 self.* = undefined;
79237953}
79247954
......@@ -10913,14 +10943,51 @@ fn metadataExtraData(self: *const Builder, comptime T: type, index: Metadata.Ite
1091310943 return self.metadataExtraDataTrail(T, index).data;
1091410944}
1091510945
10916fn metadataString(self: *Builder, str: String) Allocator.Error!MetadataString {
10917 const gop = try self.metadata_strings.getOrPut(self.gpa, str);
10918 if (!gop.found_existing) gop.key_ptr.* = str;
10946pub fn metadataString(self: *Builder, bytes: []const u8) Allocator.Error!MetadataString {
10947 try self.metadata_string_bytes.ensureUnusedCapacity(self.gpa, bytes.len);
10948 try self.metadata_string_indices.ensureUnusedCapacity(self.gpa, 1);
10949 try self.metadata_string_map.ensureUnusedCapacity(self.gpa, 1);
10950
10951 const gop = self.metadata_string_map.getOrPutAssumeCapacityAdapted(
10952 bytes,
10953 MetadataString.Adapter{ .builder = self },
10954 );
10955 if (!gop.found_existing) {
10956 self.metadata_string_bytes.appendSliceAssumeCapacity(bytes);
10957 self.metadata_string_indices.appendAssumeCapacity(@intCast(self.metadata_string_bytes.items.len));
10958 }
10959 return @enumFromInt(gop.index);
10960}
10961
10962pub fn metadataStringFromString(self: *Builder, str: String) Allocator.Error!MetadataString {
10963 if (str == .none or str == .empty) return MetadataString.none;
10964
10965 const slice = str.slice(self) orelse unreachable;
10966 return try self.metadataString(slice);
10967}
10968
10969pub fn metadataStringFmt(self: *Builder, comptime fmt_str: []const u8, fmt_args: anytype) Allocator.Error!MetadataString {
10970 try self.metadata_string_map.ensureUnusedCapacity(self.gpa, 1);
10971 try self.metadata_string_bytes.ensureUnusedCapacity(self.gpa, @intCast(std.fmt.count(fmt_str, fmt_args)));
10972 try self.metadata_string_indices.ensureUnusedCapacity(self.gpa, 1);
10973 return self.metadataStringFmtAssumeCapacity(fmt_str, fmt_args);
10974}
1091910975
10976pub fn metadataStringFmtAssumeCapacity(self: *Builder, comptime fmt_str: []const u8, fmt_args: anytype) MetadataString {
10977 const start = self.metadata_string_bytes.items.len;
10978 self.metadata_string_bytes.writer(self.gpa).print(fmt_str, fmt_args) catch unreachable;
10979 const bytes: []const u8 = self.metadata_string_bytes.items[start..self.metadata_string_bytes.items.len];
10980
10981 const gop = self.metadata_string_map.getOrPutAssumeCapacityAdapted(bytes, String.Adapter{ .builder = self });
10982 if (gop.found_existing) {
10983 self.metadata_string_bytes.shrinkRetainingCapacity(start);
10984 } else {
10985 self.metadata_string_indices.appendAssumeCapacity(@intCast(self.metadata_string_bytes.items.len));
10986 }
1092010987 return @enumFromInt(gop.index);
1092110988}
1092210989
10923pub fn debugNamed(self: *Builder, name: String, operands: []const Metadata) Allocator.Error!void {
10990pub fn debugNamed(self: *Builder, name: MetadataString, operands: []const Metadata) Allocator.Error!void {
1092410991 try self.metadata_extra.ensureUnusedCapacity(
1092510992 self.gpa,
1092610993 operands.len * @sizeOf(Metadata),
......@@ -10934,31 +11001,28 @@ fn debugNone(self: *Builder) Allocator.Error!Metadata {
1093411001 return self.debugNoneAssumeCapacity();
1093511002}
1093611003
10937pub fn debugFile(self: *Builder, path: String, name: String) Allocator.Error!Metadata {
11004pub fn debugFile(self: *Builder, path: MetadataString, name: MetadataString) Allocator.Error!Metadata {
1093811005 try self.ensureUnusedMetadataCapacity(1, Metadata.File, 0);
10939 const metadata_path = try self.metadataString(path);
10940 const metadata_name = try self.metadataString(name);
10941 return self.debugFileAssumeCapacity(metadata_path, metadata_name);
11006 return self.debugFileAssumeCapacity(path, name);
1094211007}
1094311008
1094411009pub fn debugCompileUnit(
1094511010 self: *Builder,
1094611011 file: Metadata,
10947 producer: String,
11012 producer: MetadataString,
1094811013 enums: Metadata,
1094911014 globals: Metadata,
1095011015 flags: Metadata.CompileUnit.Flags,
1095111016) Allocator.Error!Metadata {
1095211017 try self.ensureUnusedMetadataCapacity(1, Metadata.CompileUnit, 0);
10953 const metadata_producer = try self.metadataString(producer);
10954 return self.debugCompileUnitAssumeCapacity(file, metadata_producer, enums, globals, flags);
11018 return self.debugCompileUnitAssumeCapacity(file, producer, enums, globals, flags);
1095511019}
1095611020
1095711021pub fn debugSubprogram(
1095811022 self: *Builder,
1095911023 file: Metadata,
10960 name: String,
10961 linkage_name: String,
11024 name: MetadataString,
11025 linkage_name: MetadataString,
1096211026 line: u32,
1096311027 scope_line: u32,
1096411028 ty: Metadata,
......@@ -10966,12 +11030,10 @@ pub fn debugSubprogram(
1096611030 compile_unit: Metadata,
1096711031) Allocator.Error!Metadata {
1096811032 try self.ensureUnusedMetadataCapacity(1, Metadata.Subprogram, 0);
10969 const metadata_name = try self.metadataString(name);
10970 const metadata_linkage_name = try self.metadataString(linkage_name);
1097111033 return self.debugSubprogramAssumeCapacity(
1097211034 file,
10973 metadata_name,
10974 metadata_linkage_name,
11035 name,
11036 linkage_name,
1097511037 line,
1097611038 scope_line,
1097711039 ty,
......@@ -10990,28 +11052,24 @@ pub fn debugLocation(self: *Builder, scope: Metadata, line: u32, column: u32, in
1099011052 return self.debugLocationAssumeCapacity(scope, line, column, inlined_at);
1099111053}
1099211054
10993pub fn debugBoolType(self: *Builder, name: String, size_in_bits: u64) Allocator.Error!Metadata {
11055pub fn debugBoolType(self: *Builder, name: MetadataString, size_in_bits: u64) Allocator.Error!Metadata {
1099411056 try self.ensureUnusedMetadataCapacity(1, Metadata.BasicType, 0);
10995 const metadata_name = try self.metadataString(name);
10996 return self.debugBoolTypeAssumeCapacity(metadata_name, size_in_bits);
11057 return self.debugBoolTypeAssumeCapacity(name, size_in_bits);
1099711058}
1099811059
10999pub fn debugUnsignedType(self: *Builder, name: String, size_in_bits: u64) Allocator.Error!Metadata {
11060pub fn debugUnsignedType(self: *Builder, name: MetadataString, size_in_bits: u64) Allocator.Error!Metadata {
1100011061 try self.ensureUnusedMetadataCapacity(1, Metadata.BasicType, 0);
11001 const metadata_name = try self.metadataString(name);
11002 return self.debugUnsignedTypeAssumeCapacity(metadata_name, size_in_bits);
11062 return self.debugUnsignedTypeAssumeCapacity(name, size_in_bits);
1100311063}
1100411064
11005pub fn debugSignedType(self: *Builder, name: String, size_in_bits: u64) Allocator.Error!Metadata {
11065pub fn debugSignedType(self: *Builder, name: MetadataString, size_in_bits: u64) Allocator.Error!Metadata {
1100611066 try self.ensureUnusedMetadataCapacity(1, Metadata.BasicType, 0);
11007 const metadata_name = try self.metadataString(name);
11008 return self.debugSignedTypeAssumeCapacity(metadata_name, size_in_bits);
11067 return self.debugSignedTypeAssumeCapacity(name, size_in_bits);
1100911068}
1101011069
11011pub fn debugFloatType(self: *Builder, name: String, size_in_bits: u64) Allocator.Error!Metadata {
11070pub fn debugFloatType(self: *Builder, name: MetadataString, size_in_bits: u64) Allocator.Error!Metadata {
1101211071 try self.ensureUnusedMetadataCapacity(1, Metadata.BasicType, 0);
11013 const metadata_name = try self.metadataString(name);
11014 return self.debugFloatTypeAssumeCapacity(metadata_name, size_in_bits);
11072 return self.debugFloatTypeAssumeCapacity(name, size_in_bits);
1101511073}
1101611074
1101711075pub fn debugForwardReference(self: *Builder) Allocator.Error!Metadata {
......@@ -11021,7 +11079,7 @@ pub fn debugForwardReference(self: *Builder) Allocator.Error!Metadata {
1102111079
1102211080pub fn debugStructType(
1102311081 self: *Builder,
11024 name: String,
11082 name: MetadataString,
1102511083 file: Metadata,
1102611084 scope: Metadata,
1102711085 line: u32,
......@@ -11031,9 +11089,8 @@ pub fn debugStructType(
1103111089 fields_tuple: Metadata,
1103211090) Allocator.Error!Metadata {
1103311091 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
11034 const metadata_name = try self.metadataString(name);
1103511092 return self.debugStructTypeAssumeCapacity(
11036 metadata_name,
11093 name,
1103711094 file,
1103811095 scope,
1103911096 line,
......@@ -11046,7 +11103,7 @@ pub fn debugStructType(
1104611103
1104711104pub fn debugUnionType(
1104811105 self: *Builder,
11049 name: String,
11106 name: MetadataString,
1105011107 file: Metadata,
1105111108 scope: Metadata,
1105211109 line: u32,
......@@ -11056,9 +11113,8 @@ pub fn debugUnionType(
1105611113 fields_tuple: Metadata,
1105711114) Allocator.Error!Metadata {
1105811115 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
11059 const metadata_name = try self.metadataString(name);
1106011116 return self.debugUnionTypeAssumeCapacity(
11061 metadata_name,
11117 name,
1106211118 file,
1106311119 scope,
1106411120 line,
......@@ -11071,7 +11127,7 @@ pub fn debugUnionType(
1107111127
1107211128pub fn debugEnumerationType(
1107311129 self: *Builder,
11074 name: String,
11130 name: MetadataString,
1107511131 file: Metadata,
1107611132 scope: Metadata,
1107711133 line: u32,
......@@ -11081,9 +11137,8 @@ pub fn debugEnumerationType(
1108111137 fields_tuple: Metadata,
1108211138) Allocator.Error!Metadata {
1108311139 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
11084 const metadata_name = try self.metadataString(name);
1108511140 return self.debugEnumerationTypeAssumeCapacity(
11086 metadata_name,
11141 name,
1108711142 file,
1108811143 scope,
1108911144 line,
......@@ -11096,7 +11151,7 @@ pub fn debugEnumerationType(
1109611151
1109711152pub fn debugArrayType(
1109811153 self: *Builder,
11099 name: String,
11154 name: MetadataString,
1110011155 file: Metadata,
1110111156 scope: Metadata,
1110211157 line: u32,
......@@ -11106,9 +11161,8 @@ pub fn debugArrayType(
1110611161 fields_tuple: Metadata,
1110711162) Allocator.Error!Metadata {
1110811163 try self.ensureUnusedMetadataCapacity(1, Metadata.CompositeType, 0);
11109 const metadata_name = try self.metadataString(name);
1111011164 return self.debugArrayTypeAssumeCapacity(
11111 metadata_name,
11165 name,
1111211166 file,
1111311167 scope,
1111411168 line,
......@@ -11121,7 +11175,7 @@ pub fn debugArrayType(
1112111175
1112211176pub fn debugPointerType(
1112311177 self: *Builder,
11124 name: String,
11178 name: MetadataString,
1112511179 file: Metadata,
1112611180 scope: Metadata,
1112711181 line: u32,
......@@ -11131,9 +11185,8 @@ pub fn debugPointerType(
1113111185 offset_in_bits: u64,
1113211186) Allocator.Error!Metadata {
1113311187 try self.ensureUnusedMetadataCapacity(1, Metadata.DerivedType, 0);
11134 const metadata_name = try self.metadataString(name);
1113511188 return self.debugPointerTypeAssumeCapacity(
11136 metadata_name,
11189 name,
1113711190 file,
1113811191 scope,
1113911192 line,
......@@ -11146,7 +11199,7 @@ pub fn debugPointerType(
1114611199
1114711200pub fn debugMemberType(
1114811201 self: *Builder,
11149 name: String,
11202 name: MetadataString,
1115011203 file: Metadata,
1115111204 scope: Metadata,
1115211205 line: u32,
......@@ -11156,9 +11209,8 @@ pub fn debugMemberType(
1115611209 offset_in_bits: u64,
1115711210) Allocator.Error!Metadata {
1115811211 try self.ensureUnusedMetadataCapacity(1, Metadata.DerivedType, 0);
11159 const metadata_name = try self.metadataString(name);
1116011212 return self.debugMemberTypeAssumeCapacity(
11161 metadata_name,
11213 name,
1116211214 file,
1116311215 scope,
1116411216 line,
......@@ -11179,7 +11231,7 @@ pub fn debugSubroutineType(
1117911231
1118011232pub fn debugEnumerator(
1118111233 self: *Builder,
11182 name: String,
11234 name: MetadataString,
1118311235 unsigned: bool,
1118411236 bit_width: u32,
1118511237 value: std.math.big.int.Const,
......@@ -11187,8 +11239,7 @@ pub fn debugEnumerator(
1118711239 std.debug.assert(!(unsigned and !value.positive));
1118811240 try self.ensureUnusedMetadataCapacity(1, Metadata.Enumerator, 0);
1118911241 try self.metadata_limbs.ensureUnusedCapacity(self.gpa, value.limbs.len);
11190 const metadata_name = try self.metadataString(name);
11191 return self.debugEnumeratorAssumeCapacity(metadata_name, unsigned, bit_width, value);
11242 return self.debugEnumeratorAssumeCapacity(name, unsigned, bit_width, value);
1119211243}
1119311244
1119411245pub fn debugSubrange(
......@@ -11219,30 +11270,28 @@ pub fn debugTuple(
1121911270pub fn debugModuleFlag(
1122011271 self: *Builder,
1122111272 behaviour: Metadata,
11222 name: String,
11273 name: MetadataString,
1122311274 constant: Metadata,
1122411275) Allocator.Error!Metadata {
1122511276 try self.ensureUnusedMetadataCapacity(1, Metadata.ModuleFlag, 0);
11226 const metadata_name = try self.metadataString(name);
11227 return self.debugModuleFlagAssumeCapacity(behaviour, metadata_name, constant);
11277 return self.debugModuleFlagAssumeCapacity(behaviour, name, constant);
1122811278}
1122911279
1123011280pub fn debugLocalVar(
1123111281 self: *Builder,
11232 name: String,
11282 name: MetadataString,
1123311283 file: Metadata,
1123411284 scope: Metadata,
1123511285 line: u32,
1123611286 ty: Metadata,
1123711287) Allocator.Error!Metadata {
1123811288 try self.ensureUnusedMetadataCapacity(1, Metadata.LocalVar, 0);
11239 const metadata_name = try self.metadataString(name);
11240 return self.debugLocalVarAssumeCapacity(metadata_name, file, scope, line, ty);
11289 return self.debugLocalVarAssumeCapacity(name, file, scope, line, ty);
1124111290}
1124211291
1124311292pub fn debugParameter(
1124411293 self: *Builder,
11245 name: String,
11294 name: MetadataString,
1124611295 file: Metadata,
1124711296 scope: Metadata,
1124811297 line: u32,
......@@ -11250,14 +11299,13 @@ pub fn debugParameter(
1125011299 arg_no: u32,
1125111300) Allocator.Error!Metadata {
1125211301 try self.ensureUnusedMetadataCapacity(1, Metadata.Parameter, 0);
11253 const metadata_name = try self.metadataString(name);
11254 return self.debugParameterAssumeCapacity(metadata_name, file, scope, line, ty, arg_no);
11302 return self.debugParameterAssumeCapacity(name, file, scope, line, ty, arg_no);
1125511303}
1125611304
1125711305pub fn debugGlobalVar(
1125811306 self: *Builder,
11259 name: String,
11260 linkage_name: String,
11307 name: MetadataString,
11308 linkage_name: MetadataString,
1126111309 file: Metadata,
1126211310 scope: Metadata,
1126311311 line: u32,
......@@ -11266,11 +11314,9 @@ pub fn debugGlobalVar(
1126611314 flags: Metadata.GlobalVar.Flags,
1126711315) Allocator.Error!Metadata {
1126811316 try self.ensureUnusedMetadataCapacity(1, Metadata.GlobalVar, 0);
11269 const metadata_name = try self.metadataString(name);
11270 const metadata_linkage_name = try self.metadataString(linkage_name);
1127111317 return self.debugGlobalVarAssumeCapacity(
11272 metadata_name,
11273 metadata_linkage_name,
11318 name,
11319 linkage_name,
1127411320 file,
1127511321 scope,
1127611322 line,
......@@ -11347,7 +11393,8 @@ fn metadataSimpleAssumeCapacity(self: *Builder, tag: Metadata.Tag, value: anytyp
1134711393 return @enumFromInt(gop.index);
1134811394}
1134911395
11350fn debugNamedAssumeCapacity(self: *Builder, name: String, operands: []const Metadata) void {
11396fn debugNamedAssumeCapacity(self: *Builder, name: MetadataString, operands: []const Metadata) void {
11397 std.debug.assert(name != .none);
1135111398 const extra_index: u32 = @intCast(self.metadata_extra.items.len);
1135211399 self.metadata_extra.appendSliceAssumeCapacity(@ptrCast(operands));
1135311400
......@@ -12953,12 +13000,12 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1295313000
1295413001 pub fn getMetadataIndex(adapter: @This(), metadata: Metadata) u32 {
1295513002 if (metadata == .none) return 0;
12956 return @intCast(adapter.builder.metadata_strings.count() +
12957 @intFromEnum(metadata.unwrap(adapter.builder)));
13003 return @intCast(adapter.builder.metadata_string_map.count() +
13004 @intFromEnum(metadata.unwrap(adapter.builder)) - 1);
1295813005 }
1295913006
1296013007 pub fn getMetadataStringIndex(_: @This(), metadata_string: MetadataString) u32 {
12961 return @intFromEnum(metadata_string) + 1;
13008 return @intFromEnum(metadata_string);
1296213009 }
1296313010 };
1296413011
......@@ -12976,11 +13023,11 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1297613023 const strings_offset, const strings_size = blk: {
1297713024 var strings_offset: u32 = 0;
1297813025 var strings_size: u32 = 0;
12979 for (self.metadata_strings.keys()) |metadata_string| {
12980 if (metadata_string.slice(self)) |slice| {
12981 strings_offset += bitcode.bitsVBR(@as(u32, @intCast(slice.len)), 6);
12982 strings_size += @intCast(slice.len * 8);
12983 }
13026 for (1..self.metadata_string_map.count()) |metadata_string_index| {
13027 const metadata_string: MetadataString = @enumFromInt(metadata_string_index);
13028 const slice = metadata_string.slice(self);
13029 strings_offset += bitcode.bitsVBR(@as(u32, @intCast(slice.len)), 6);
13030 strings_size += @intCast(slice.len * 8);
1298413031 }
1298513032 break :blk .{
1298613033 std.mem.alignForward(u32, strings_offset, 32) / 8,
......@@ -12993,25 +13040,26 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1299313040 MetadataBlockWriter.abbrev_len,
1299413041 );
1299513042
12996 try bitcode.writeVBR(@as(u32, @intCast(self.metadata_strings.count())), 6);
13043 try bitcode.writeVBR(@as(u32, @intCast(self.metadata_string_map.count() - 1)), 6);
1299713044 try bitcode.writeVBR(strings_offset, 6);
1299813045
1299913046 try bitcode.writeVBR(strings_size + strings_offset, 6);
1300013047
1300113048 try bitcode.alignTo32();
1300213049
13003 for (self.metadata_strings.keys()) |metadata_string| {
13004 if (metadata_string.slice(self)) |slice|
13005 try bitcode.writeVBR(@as(u32, @intCast(slice.len)), 6);
13050 for (1..self.metadata_string_map.count()) |metadata_string_index| {
13051 const metadata_string: MetadataString = @enumFromInt(metadata_string_index);
13052 const slice = metadata_string.slice(self);
13053 try bitcode.writeVBR(@as(u32, @intCast(slice.len)), 6);
1300613054 }
1300713055
1300813056 try bitcode.alignTo32();
1300913057
13010 for (self.metadata_strings.keys()) |metadata_string| {
13011 if (metadata_string.slice(self)) |slice| {
13012 for (slice) |c| {
13013 try bitcode.writeBits(c, 8);
13014 }
13058 for (1..self.metadata_string_map.count()) |metadata_string_index| {
13059 const metadata_string: MetadataString = @enumFromInt(metadata_string_index);
13060 const slice = metadata_string.slice(self);
13061 for (slice) |c| {
13062 try bitcode.writeBits(c, 8);
1301513063 }
1301613064 }
1301713065
......@@ -13335,7 +13383,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1333513383
1333613384 // Write named metadata
1333713385 for (self.metadata_named.keys(), self.metadata_named.values()) |name, operands| {
13338 const slice = name.slice(self).?;
13386 const slice = name.slice(self);
1333913387 try metadata_block.writeAbbrev(MetadataBlock.Name{
1334013388 .name = slice,
1334113389 });
......@@ -13396,7 +13444,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1339613444
1339713445 return @intCast(@intFromEnum(metadata) -
1339813446 Metadata.first_local_metadata +
13399 adapter.metadata_adapter.builder.metadata_strings.count() +
13447 adapter.metadata_adapter.builder.metadata_string_map.count() - 1 +
1340013448 adapter.metadata_adapter.builder.metadata_map.count() - 1);
1340113449 } else unreachable,
1340213450 });