authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-01-20 00:39:15+01:00
committergravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-02-21 16:24:59+01:00
log7cb8813743731c81ffd551ced84a2d9dacf6d086
treee266925bcd0ff281f5af896d1a0b97ccd65d63df
parent423c2c3a27d875e907b739204cdf44beea2a4064

Added value_indices and valueIndex to Function

value_indices keeps track of the value index of each instruction in the function (i.e skips instruction which do not have a result)

1 files changed, 17 insertions(+), 0 deletions(-)

src/codegen/llvm/Builder.zig+17
...@@ -3899,6 +3899,7 @@ pub const Function = struct {...@@ -3899,6 +3899,7 @@ pub const Function = struct {
3899 blocks: []const Block = &.{},3899 blocks: []const Block = &.{},
3900 instructions: std.MultiArrayList(Instruction) = .{},3900 instructions: std.MultiArrayList(Instruction) = .{},
3901 names: [*]const String = &[0]String{},3901 names: [*]const String = &[0]String{},
3902 value_indices: [*]const u32 = &[0]u32{},
3902 metadata: ?[*]const Metadata = null,3903 metadata: ?[*]const Metadata = null,
3903 extra: []const u32 = &.{},3904 extra: []const u32 = &.{},
39043905
...@@ -4335,6 +4336,10 @@ pub const Function = struct {...@@ -4335,6 +4336,10 @@ pub const Function = struct {
4335 return function.names[@intFromEnum(self)];4336 return function.names[@intFromEnum(self)];
4336 }4337 }
43374338
4339 pub fn valueIndex(self: Instruction.Index, function: *const Function) u32 {
4340 return function.value_indices[@intFromEnum(self)];
4341 }
4342
4338 pub fn toValue(self: Instruction.Index) Value {4343 pub fn toValue(self: Instruction.Index) Value {
4339 return @enumFromInt(@intFromEnum(self));4344 return @enumFromInt(@intFromEnum(self));
4340 }4345 }
...@@ -4993,6 +4998,7 @@ pub const Function = struct {...@@ -4993,6 +4998,7 @@ pub const Function = struct {
4993 pub fn deinit(self: *Function, gpa: Allocator) void {4998 pub fn deinit(self: *Function, gpa: Allocator) void {
4994 gpa.free(self.extra);4999 gpa.free(self.extra);
4995 if (self.metadata) |metadata| gpa.free(metadata[0..self.instructions.len]);5000 if (self.metadata) |metadata| gpa.free(metadata[0..self.instructions.len]);
5001 gpa.free(self.value_indices[0..self.instructions.len]);
4996 gpa.free(self.names[0..self.instructions.len]);5002 gpa.free(self.names[0..self.instructions.len]);
4997 self.instructions.deinit(gpa);5003 self.instructions.deinit(gpa);
4998 gpa.free(self.blocks);5004 gpa.free(self.blocks);
...@@ -6382,6 +6388,9 @@ pub const WipFunction = struct {...@@ -6382,6 +6388,9 @@ pub const WipFunction = struct {
6382 const names = try gpa.alloc(String, final_instructions_len);6388 const names = try gpa.alloc(String, final_instructions_len);
6383 errdefer gpa.free(names);6389 errdefer gpa.free(names);
63846390
6391 const value_indices = try gpa.alloc(u32, final_instructions_len);
6392 errdefer gpa.free(value_indices);
6393
6385 const metadata =6394 const metadata =
6386 if (self.builder.strip) null else try gpa.alloc(Metadata, final_instructions_len);6395 if (self.builder.strip) null else try gpa.alloc(Metadata, final_instructions_len);
6387 errdefer if (metadata) |new_metadata| gpa.free(new_metadata);6396 errdefer if (metadata) |new_metadata| gpa.free(new_metadata);
...@@ -6475,12 +6484,15 @@ pub const WipFunction = struct {...@@ -6475,12 +6484,15 @@ pub const WipFunction = struct {
6475 return new_name;6484 return new_name;
6476 }6485 }
6477 } = .{};6486 } = .{};
6487 var value_index: u32 = 0;
6478 for (0..params_len) |param_index| {6488 for (0..params_len) |param_index| {
6479 const old_argument_index: Instruction.Index = @enumFromInt(param_index);6489 const old_argument_index: Instruction.Index = @enumFromInt(param_index);
6480 const new_argument_index: Instruction.Index = @enumFromInt(function.instructions.len);6490 const new_argument_index: Instruction.Index = @enumFromInt(function.instructions.len);
6481 const argument = self.instructions.get(@intFromEnum(old_argument_index));6491 const argument = self.instructions.get(@intFromEnum(old_argument_index));
6482 assert(argument.tag == .arg);6492 assert(argument.tag == .arg);
6483 assert(argument.data == param_index);6493 assert(argument.data == param_index);
6494 value_indices[function.instructions.len] = value_index;
6495 value_index += 1;
6484 function.instructions.appendAssumeCapacity(argument);6496 function.instructions.appendAssumeCapacity(argument);
6485 names[@intFromEnum(new_argument_index)] = wip_name.map(6497 names[@intFromEnum(new_argument_index)] = wip_name.map(
6486 if (self.builder.strip) .empty else self.names.items[@intFromEnum(old_argument_index)],6498 if (self.builder.strip) .empty else self.names.items[@intFromEnum(old_argument_index)],
...@@ -6488,6 +6500,7 @@ pub const WipFunction = struct {...@@ -6488,6 +6500,7 @@ pub const WipFunction = struct {
6488 }6500 }
6489 for (self.blocks.items) |current_block| {6501 for (self.blocks.items) |current_block| {
6490 const new_block_index: Instruction.Index = @enumFromInt(function.instructions.len);6502 const new_block_index: Instruction.Index = @enumFromInt(function.instructions.len);
6503 value_indices[function.instructions.len] = value_index;
6491 function.instructions.appendAssumeCapacity(.{6504 function.instructions.appendAssumeCapacity(.{
6492 .tag = .block,6505 .tag = .block,
6493 .data = current_block.incoming,6506 .data = current_block.incoming,
...@@ -6797,6 +6810,9 @@ pub const WipFunction = struct {...@@ -6797,6 +6810,9 @@ pub const WipFunction = struct {
6797 if (old_instruction_index.hasResultWip(self)) .empty else .none6810 if (old_instruction_index.hasResultWip(self)) .empty else .none
6798 else6811 else
6799 self.names.items[@intFromEnum(old_instruction_index)]);6812 self.names.items[@intFromEnum(old_instruction_index)]);
6813
6814 value_indices[@intFromEnum(new_instruction_index)] = value_index;
6815 if (old_instruction_index.hasResultWip(self)) value_index += 1;
6800 }6816 }
6801 }6817 }
68026818
...@@ -6804,6 +6820,7 @@ pub const WipFunction = struct {...@@ -6804,6 +6820,7 @@ pub const WipFunction = struct {
6804 function.extra = wip_extra.finish();6820 function.extra = wip_extra.finish();
6805 function.blocks = blocks;6821 function.blocks = blocks;
6806 function.names = names.ptr;6822 function.names = names.ptr;
6823 function.value_indices = value_indices.ptr;
6807 function.metadata = if (metadata) |new_metadata| new_metadata.ptr else null;6824 function.metadata = if (metadata) |new_metadata| new_metadata.ptr else null;
6808 }6825 }
68096826