authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-04-28 13:13:40-04:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-01 18:30:31+01:00
log49ad51b2feacad394e05d7b5c87c5020c3bc0f5e
tree336508d1623e4c35e52cddd7f924996ff54d4962
parent28383d4d985cd04c897f6b6a63bd2107d8e2a8e9
signature Commit is signed but in an unrecognized format.

Builder: add `indirectbr` llvm instruction


2 files changed, 96 insertions(+), 15 deletions(-)

src/codegen/llvm/Builder.zig+82-15
......@@ -4157,6 +4157,7 @@ pub const Function = struct {
41574157 @"icmp ugt",
41584158 @"icmp ule",
41594159 @"icmp ult",
4160 indirectbr,
41604161 insertelement,
41614162 insertvalue,
41624163 inttoptr,
......@@ -4367,6 +4368,7 @@ pub const Function = struct {
43674368 return switch (wip.instructions.items(.tag)[@intFromEnum(self)]) {
43684369 .br,
43694370 .br_cond,
4371 .indirectbr,
43704372 .ret,
43714373 .@"ret void",
43724374 .@"switch",
......@@ -4381,6 +4383,7 @@ pub const Function = struct {
43814383 .br,
43824384 .br_cond,
43834385 .fence,
4386 .indirectbr,
43844387 .ret,
43854388 .@"ret void",
43864389 .store,
......@@ -4471,6 +4474,7 @@ pub const Function = struct {
44714474 .br,
44724475 .br_cond,
44734476 .fence,
4477 .indirectbr,
44744478 .ret,
44754479 .@"ret void",
44764480 .store,
......@@ -4657,6 +4661,7 @@ pub const Function = struct {
46574661 .br,
46584662 .br_cond,
46594663 .fence,
4664 .indirectbr,
46604665 .ret,
46614666 .@"ret void",
46624667 .store,
......@@ -4837,6 +4842,12 @@ pub const Function = struct {
48374842 //case_blocks: [cases_len]Block.Index,
48384843 };
48394844
4845 pub const IndirectBr = struct {
4846 addr: Value,
4847 targets_len: u32,
4848 //targets: [targets_len]Block.Index,
4849 };
4850
48404851 pub const Binary = struct {
48414852 lhs: Value,
48424853 rhs: Value,
......@@ -5294,10 +5305,27 @@ pub const WipFunction = struct {
52945305 return .{ .index = 0, .instruction = instruction };
52955306 }
52965307
5308 pub fn indirectbr(
5309 self: *WipFunction,
5310 addr: Value,
5311 targets: []const Block.Index,
5312 ) Allocator.Error!Instruction.Index {
5313 try self.ensureUnusedExtraCapacity(1, Instruction.IndirectBr, targets.len);
5314 const instruction = try self.addInst(null, .{
5315 .tag = .indirectbr,
5316 .data = self.addExtraAssumeCapacity(Instruction.IndirectBr{
5317 .addr = addr,
5318 .targets_len = @intCast(targets.len),
5319 }),
5320 });
5321 _ = self.extra.appendSliceAssumeCapacity(@ptrCast(targets));
5322 for (targets) |target| target.ptr(self).branches += 1;
5323 return instruction;
5324 }
5325
52975326 pub fn @"unreachable"(self: *WipFunction) Allocator.Error!Instruction.Index {
52985327 try self.ensureUnusedExtraCapacity(1, NoExtra, 0);
5299 const instruction = try self.addInst(null, .{ .tag = .@"unreachable", .data = undefined });
5300 return instruction;
5328 return try self.addInst(null, .{ .tag = .@"unreachable", .data = undefined });
53015329 }
53025330
53035331 pub fn un(
......@@ -6299,8 +6327,7 @@ pub const WipFunction = struct {
62996327 });
63006328 names[@intFromEnum(new_block_index)] = try wip_name.map(current_block.name, "");
63016329 for (current_block.instructions.items) |old_instruction_index| {
6302 const new_instruction_index: Instruction.Index =
6303 @enumFromInt(function.instructions.len);
6330 const new_instruction_index: Instruction.Index = @enumFromInt(function.instructions.len);
63046331 var instruction = self.instructions.get(@intFromEnum(old_instruction_index));
63056332 switch (instruction.tag) {
63066333 .add,
......@@ -6509,6 +6536,15 @@ pub const WipFunction = struct {
65096536 });
65106537 wip_extra.appendMappedValues(indices, instructions);
65116538 },
6539 .indirectbr => {
6540 var extra = self.extraDataTrail(Instruction.IndirectBr, instruction.data);
6541 const targets = extra.trail.next(extra.data.targets_len, Block.Index, self);
6542 instruction.data = wip_extra.addExtra(Instruction.IndirectBr{
6543 .addr = instructions.map(extra.data.addr),
6544 .targets_len = extra.data.targets_len,
6545 });
6546 wip_extra.appendSlice(targets);
6547 },
65126548 .insertelement => {
65136549 const extra = self.extraData(Instruction.InsertElement, instruction.data);
65146550 instruction.data = wip_extra.addExtra(Instruction.InsertElement{
......@@ -7555,10 +7591,10 @@ pub const Constant = enum(u32) {
75557591 .blockaddress => |tag| {
75567592 const extra = data.builder.constantExtraData(BlockAddress, item.data);
75577593 const function = extra.function.ptrConst(data.builder);
7558 try writer.print("{s}({}, %{d})", .{
7594 try writer.print("{s}({}, {})", .{
75597595 @tagName(tag),
75607596 function.global.fmt(data.builder),
7561 @intFromEnum(extra.block), // TODO
7597 extra.block.toInst(function).fmt(extra.function, data.builder),
75627598 });
75637599 },
75647600 .dso_local_equivalent,
......@@ -9902,6 +9938,23 @@ pub fn printUnbuffered(
99029938 index.fmt(function_index, self),
99039939 });
99049940 },
9941 .indirectbr => |tag| {
9942 var extra =
9943 function.extraDataTrail(Function.Instruction.IndirectBr, instruction.data);
9944 const targets =
9945 extra.trail.next(extra.data.targets_len, Function.Block.Index, &function);
9946 try writer.print(" {s} {%}, [", .{
9947 @tagName(tag),
9948 extra.data.addr.fmt(function_index, self),
9949 });
9950 for (0.., targets) |target_index, target| {
9951 if (target_index > 0) try writer.writeAll(", ");
9952 try writer.print("{%}", .{
9953 target.toInst(&function).fmt(function_index, self),
9954 });
9955 }
9956 try writer.writeByte(']');
9957 },
99059958 .insertelement => |tag| {
99069959 const extra =
99079960 function.extraData(Function.Instruction.InsertElement, instruction.data);
......@@ -14777,15 +14830,6 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1477714830 .indices = indices,
1477814831 });
1477914832 },
14780 .insertvalue => {
14781 var extra = func.extraDataTrail(Function.Instruction.InsertValue, data);
14782 const indices = extra.trail.next(extra.data.indices_len, u32, &func);
14783 try function_block.writeAbbrev(FunctionBlock.InsertValue{
14784 .val = adapter.getOffsetValueIndex(extra.data.val),
14785 .elem = adapter.getOffsetValueIndex(extra.data.elem),
14786 .indices = indices,
14787 });
14788 },
1478914833 .extractelement => {
1479014834 const extra = func.extraData(Function.Instruction.ExtractElement, data);
1479114835 try function_block.writeAbbrev(FunctionBlock.ExtractElement{
......@@ -14793,6 +14837,20 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1479314837 .index = adapter.getOffsetValueIndex(extra.index),
1479414838 });
1479514839 },
14840 .indirectbr => {
14841 var extra =
14842 func.extraDataTrail(Function.Instruction.IndirectBr, datas[instr_index]);
14843 const targets =
14844 extra.trail.next(extra.data.targets_len, Function.Block.Index, &func);
14845 try function_block.writeAbbrevAdapted(
14846 FunctionBlock.IndirectBr{
14847 .ty = extra.data.addr.typeOf(@enumFromInt(func_index), self),
14848 .addr = extra.data.addr,
14849 .targets = targets,
14850 },
14851 adapter,
14852 );
14853 },
1479614854 .insertelement => {
1479714855 const extra = func.extraData(Function.Instruction.InsertElement, data);
1479814856 try function_block.writeAbbrev(FunctionBlock.InsertElement{
......@@ -14801,6 +14859,15 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1480114859 .index = adapter.getOffsetValueIndex(extra.index),
1480214860 });
1480314861 },
14862 .insertvalue => {
14863 var extra = func.extraDataTrail(Function.Instruction.InsertValue, datas[instr_index]);
14864 const indices = extra.trail.next(extra.data.indices_len, u32, &func);
14865 try function_block.writeAbbrev(FunctionBlock.InsertValue{
14866 .val = adapter.getOffsetValueIndex(extra.data.val),
14867 .elem = adapter.getOffsetValueIndex(extra.data.elem),
14868 .indices = indices,
14869 });
14870 },
1480414871 .select => {
1480514872 const extra = func.extraData(Function.Instruction.Select, data);
1480614873 try function_block.writeAbbrev(FunctionBlock.Select{
src/codegen/llvm/ir.zig+14
......@@ -19,6 +19,7 @@ const LineAbbrev = AbbrevOp{ .vbr = 8 };
1919const ColumnAbbrev = AbbrevOp{ .vbr = 8 };
2020
2121const BlockAbbrev = AbbrevOp{ .vbr = 6 };
22const BlockArrayAbbrev = AbbrevOp{ .array_vbr = 6 };
2223
2324/// Unused tags are commented out so that they are omitted in the generated
2425/// bitcode, which scans over this enum using reflection.
......@@ -1294,6 +1295,7 @@ pub const FunctionBlock = struct {
12941295 DebugLoc,
12951296 DebugLocAgain,
12961297 ColdOperandBundle,
1298 IndirectBr,
12971299 };
12981300
12991301 pub const DeclareBlocks = struct {
......@@ -1813,6 +1815,18 @@ pub const FunctionBlock = struct {
18131815 .{ .literal = 0 },
18141816 };
18151817 };
1818
1819 pub const IndirectBr = struct {
1820 pub const ops = [_]AbbrevOp{
1821 .{ .literal = 31 },
1822 .{ .fixed_runtime = Builder.Type },
1823 ValueAbbrev,
1824 BlockArrayAbbrev,
1825 };
1826 ty: Builder.Type,
1827 addr: Builder.Value,
1828 targets: []const Builder.Function.Block.Index,
1829 };
18161830};
18171831
18181832pub const FunctionValueSymbolTable = struct {