Skip to content

Commit 066533a

Browse files
antliljajacobly0
authored andcommitted
Builder: Emit metadata attachment for globals
1 parent 92fb05b commit 066533a

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

src/codegen/llvm/Builder.zig

+47-6
Original file line numberDiff line numberDiff line change
@@ -13853,16 +13853,12 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1385313853
try constants_block.end();
1385413854
}
1385513855

13856-
const MetadataKind = enum(u8) {
13857-
dbg = 0,
13858-
};
13859-
1386013856
// METADATA_KIND_BLOCK
1386113857
if (!self.strip) {
1386213858
const MetadataKindBlock = ir.MetadataKindBlock;
1386313859
var metadata_kind_block = try module_block.enterSubBlock(MetadataKindBlock);
1386413860

13865-
inline for (@typeInfo(MetadataKind).Enum.fields) |field| {
13861+
inline for (@typeInfo(ir.MetadataKind).Enum.fields) |field| {
1386613862
try metadata_kind_block.writeAbbrev(MetadataKindBlock.Kind{
1386713863
.id = field.value,
1386813864
.name = field.name,
@@ -14296,6 +14292,51 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1429614292
});
1429714293
}
1429814294

14295+
// Write global attached metadata
14296+
{
14297+
for (self.variables.items) |variable| {
14298+
if (variable.global.getReplacement(self) != .none) continue;
14299+
14300+
const dbg = variable.global.ptrConst(self).dbg;
14301+
14302+
if (dbg == .none) continue;
14303+
14304+
try metadata_block.writeAbbrev(MetadataBlock.GlobalDeclAttachment{
14305+
.value = @enumFromInt(constant_adapter.getConstantIndex(variable.global.toConst())),
14306+
.kind = ir.MetadataKind.dbg,
14307+
.metadata = @enumFromInt(metadata_adapter.getMetadataIndex(dbg) - 1),
14308+
});
14309+
}
14310+
14311+
for (self.functions.items) |func| {
14312+
if (func.global.getReplacement(self) != .none) continue;
14313+
14314+
const dbg = func.global.ptrConst(self).dbg;
14315+
14316+
if (dbg == .none or func.instructions.len != 0) continue;
14317+
14318+
try metadata_block.writeAbbrev(MetadataBlock.GlobalDeclAttachment{
14319+
.value = @enumFromInt(constant_adapter.getConstantIndex(func.global.toConst())),
14320+
.kind = ir.MetadataKind.dbg,
14321+
.metadata = @enumFromInt(metadata_adapter.getMetadataIndex(dbg) - 1),
14322+
});
14323+
}
14324+
14325+
for (self.aliases.items) |alias| {
14326+
if (alias.global.getReplacement(self) != .none) continue;
14327+
14328+
const dbg = alias.global.ptrConst(self).dbg;
14329+
14330+
if (dbg == .none) continue;
14331+
14332+
try metadata_block.writeAbbrev(MetadataBlock.GlobalDeclAttachment{
14333+
.value = @enumFromInt(constant_adapter.getConstantIndex(alias.global.toConst())),
14334+
.kind = ir.MetadataKind.dbg,
14335+
.metadata = @enumFromInt(metadata_adapter.getMetadataIndex(dbg) - 1),
14336+
});
14337+
}
14338+
}
14339+
1429914340
try metadata_block.end();
1430014341
}
1430114342

@@ -14909,7 +14950,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1490914950
var metadata_attach_block = try function_block.enterSubBlock(MetadataAttachmentBlock);
1491014951

1491114952
try metadata_attach_block.writeAbbrev(MetadataAttachmentBlock.AttachmentSingle{
14912-
.id = @intFromEnum(MetadataKind.dbg),
14953+
.kind = ir.MetadataKind.dbg,
1491314954
.metadata = @enumFromInt(metadata_adapter.getMetadataIndex(dbg) - 1),
1491414955
});
1491514956

src/codegen/llvm/ir.zig

+20-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const ColumnAbbrev = AbbrevOp{ .vbr = 8 };
2020

2121
const BlockAbbrev = AbbrevOp{ .vbr = 6 };
2222

23+
pub const MetadataKind = enum(u1) {
24+
dbg = 0,
25+
};
26+
2327
pub const Identification = struct {
2428
pub const id = 13;
2529

@@ -616,10 +620,10 @@ pub const MetadataAttachmentBlock = struct {
616620
pub const AttachmentSingle = struct {
617621
pub const ops = [_]AbbrevOp{
618622
.{ .literal = 11 },
619-
.{ .vbr = 4 },
623+
.{ .fixed = 1 },
620624
MetadataAbbrev,
621625
};
622-
id: u32,
626+
kind: MetadataKind,
623627
metadata: Builder.Metadata,
624628
};
625629
};
@@ -649,6 +653,7 @@ pub const MetadataBlock = struct {
649653
Constant,
650654
Name,
651655
NamedNode,
656+
GlobalDeclAttachment,
652657
};
653658

654659
pub const Strings = struct {
@@ -1045,6 +1050,19 @@ pub const MetadataBlock = struct {
10451050

10461051
elements: []const Builder.Metadata,
10471052
};
1053+
1054+
pub const GlobalDeclAttachment = struct {
1055+
pub const ops = [_]AbbrevOp{
1056+
.{ .literal = 36 },
1057+
ValueAbbrev, // value id
1058+
.{ .fixed = 1 }, // kind
1059+
MetadataAbbrev, // elements
1060+
};
1061+
1062+
value: Builder.Constant,
1063+
kind: MetadataKind,
1064+
metadata: Builder.Metadata,
1065+
};
10481066
};
10491067

10501068
pub const FunctionMetadataBlock = struct {

0 commit comments

Comments
 (0)