authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-25 23:04:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:55-07:00
log66c43968546e38879a2d4c3f2264e10676deef73
tree9220a6d6f006e08bd7aa7ccd6997276dae6383bf
parent9afa97418350a51d8e27f1df903d8034507254ce

AIR: eliminate the `values` array


13 files changed, 44 insertions(+), 62 deletions(-)

src/Air.zig+9-7
......@@ -17,7 +17,6 @@ instructions: std.MultiArrayList(Inst).Slice,
1717/// The meaning of this data is determined by `Inst.Tag` value.
1818/// The first few indexes are reserved. See `ExtraIndex` for the values.
1919extra: []const u32,
20values: []const Value,
2120
2221pub const ExtraIndex = enum(u32) {
2322 /// Payload index of the main `Block` in the `extra` array.
......@@ -421,10 +420,10 @@ pub const Inst = struct {
421420 /// Marks the end of a semantic scope for debug info variables.
422421 dbg_block_end,
423422 /// Marks the start of an inline call.
424 /// Uses `ty_pl` with the payload being the index of a Value.Function in air.values.
423 /// Uses the `ty_fn` field.
425424 dbg_inline_begin,
426425 /// Marks the end of an inline call.
427 /// Uses `ty_pl` with the payload being the index of a Value.Function in air.values.
426 /// Uses the `ty_fn` field.
428427 dbg_inline_end,
429428 /// Marks the beginning of a local variable. The operand is a pointer pointing
430429 /// to the storage for the variable. The local may be a const or a var.
......@@ -967,6 +966,10 @@ pub const Inst = struct {
967966 // Index into a different array.
968967 payload: u32,
969968 },
969 ty_fn: struct {
970 ty: Ref,
971 func: Module.Fn.Index,
972 },
970973 br: struct {
971974 block_inst: Index,
972975 operand: Ref,
......@@ -1090,8 +1093,7 @@ pub const FieldParentPtr = struct {
10901093pub const Shuffle = struct {
10911094 a: Inst.Ref,
10921095 b: Inst.Ref,
1093 // index to air_values
1094 mask: u32,
1096 mask: InternPool.Index,
10951097 mask_len: u32,
10961098};
10971099
......@@ -1469,7 +1471,8 @@ pub fn extraData(air: Air, comptime T: type, index: usize) struct { data: T, end
14691471 u32 => air.extra[i],
14701472 Inst.Ref => @intToEnum(Inst.Ref, air.extra[i]),
14711473 i32 => @bitCast(i32, air.extra[i]),
1472 else => @compileError("bad field type"),
1474 InternPool.Index => @intToEnum(InternPool.Index, air.extra[i]),
1475 else => @compileError("bad field type: " ++ @typeName(field.type)),
14731476 };
14741477 i += 1;
14751478 }
......@@ -1482,7 +1485,6 @@ pub fn extraData(air: Air, comptime T: type, index: usize) struct { data: T, end
14821485pub fn deinit(air: *Air, gpa: std.mem.Allocator) void {
14831486 air.instructions.deinit(gpa);
14841487 gpa.free(air.extra);
1485 gpa.free(air.values);
14861488 air.* = undefined;
14871489}
14881490
src/Module.zig-1
......@@ -5720,7 +5720,6 @@ pub fn analyzeFnBody(mod: *Module, func_index: Fn.Index, arena: Allocator) SemaE
57205720 return Air{
57215721 .instructions = sema.air_instructions.toOwnedSlice(),
57225722 .extra = try sema.air_extra.toOwnedSlice(gpa),
5723 .values = try sema.air_values.toOwnedSlice(gpa),
57245723 };
57255724}
57265725
src/Sema.zig+9-18
......@@ -17,7 +17,6 @@ perm_arena: Allocator,
1717code: Zir,
1818air_instructions: std.MultiArrayList(Air.Inst) = .{},
1919air_extra: std.ArrayListUnmanaged(u32) = .{},
20air_values: std.ArrayListUnmanaged(Value) = .{},
2120/// Maps ZIR to AIR.
2221inst_map: InstMap = .{},
2322/// When analyzing an inline function call, owner_decl is the Decl of the caller
......@@ -772,7 +771,6 @@ pub fn deinit(sema: *Sema) void {
772771 const gpa = sema.gpa;
773772 sema.air_instructions.deinit(gpa);
774773 sema.air_extra.deinit(gpa);
775 sema.air_values.deinit(gpa);
776774 sema.inst_map.deinit(gpa);
777775 sema.decl_val_table.deinit(gpa);
778776 sema.types_to_resolve.deinit(gpa);
......@@ -2018,10 +2016,8 @@ fn resolveMaybeUndefValAllowVariablesMaybeRuntime(
20182016 }
20192017 const air_datas = sema.air_instructions.items(.data);
20202018 const val = switch (air_tags[i]) {
2021 .inferred_alloc, .inferred_alloc_comptime => val: {
2022 const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
2023 break :val sema.air_values.items[ty_pl.payload];
2024 },
2019 .inferred_alloc => unreachable,
2020 .inferred_alloc_comptime => unreachable,
20252021 .interned => air_datas[i].interned.toValue(),
20262022 else => return null,
20272023 };
......@@ -7930,20 +7926,17 @@ fn emitDbgInline(
79307926 new_func_ty: Type,
79317927 tag: Air.Inst.Tag,
79327928) CompileError!void {
7933 if (sema.mod.comp.bin_file.options.strip) return;
7929 const mod = sema.mod;
7930 if (mod.comp.bin_file.options.strip) return;
79347931
79357932 // Recursive inline call; no dbg_inline needed.
79367933 if (old_func == new_func) return;
79377934
7938 try sema.air_values.append(sema.gpa, (try sema.mod.intern(.{ .func = .{
7939 .ty = new_func_ty.toIntern(),
7940 .index = new_func,
7941 } })).toValue());
79427935 _ = try block.addInst(.{
79437936 .tag = tag,
7944 .data = .{ .ty_pl = .{
7937 .data = .{ .ty_fn = .{
79457938 .ty = try sema.addType(new_func_ty),
7946 .payload = @intCast(u32, sema.air_values.items.len - 1),
7939 .func = new_func,
79477940 } },
79487941 });
79497942}
......@@ -21724,8 +21717,6 @@ fn analyzeShuffle(
2172421717 }
2172521718 }
2172621719
21727 const mask_index = @intCast(u32, sema.air_values.items.len);
21728 try sema.air_values.append(sema.gpa, mask);
2172921720 return block.addInst(.{
2173021721 .tag = .shuffle,
2173121722 .data = .{ .ty_pl = .{
......@@ -21733,7 +21724,7 @@ fn analyzeShuffle(
2173321724 .payload = try block.sema.addExtra(Air.Shuffle{
2173421725 .a = a,
2173521726 .b = b,
21736 .mask = mask_index,
21727 .mask = mask.toIntern(),
2173721728 .mask_len = mask_len,
2173821729 }),
2173921730 } },
......@@ -33311,7 +33302,6 @@ pub fn getTmpAir(sema: Sema) Air {
3331133302 return .{
3331233303 .instructions = sema.air_instructions.slice(),
3331333304 .extra = sema.air_extra.items,
33314 .values = sema.air_values.items,
3331533305 };
3331633306}
3331733307
......@@ -33371,7 +33361,8 @@ pub fn addExtraAssumeCapacity(sema: *Sema, extra: anytype) u32 {
3337133361 u32 => @field(extra, field.name),
3337233362 Air.Inst.Ref => @enumToInt(@field(extra, field.name)),
3337333363 i32 => @bitCast(u32, @field(extra, field.name)),
33374 else => @compileError("bad field type"),
33364 InternPool.Index => @enumToInt(@field(extra, field.name)),
33365 else => @compileError("bad field type: " ++ @typeName(field.type)),
3337533366 });
3337633367 }
3337733368 return result;
src/arch/aarch64/CodeGen.zig+2-2
......@@ -4621,9 +4621,9 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
46214621}
46224622
46234623fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
4624 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
4624 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
46254625 const mod = self.bin_file.options.module.?;
4626 const function = self.air.values[ty_pl.payload].getFunction(mod).?;
4626 const function = mod.funcPtr(ty_fn.func);
46274627 // TODO emit debug info for function change
46284628 _ = function;
46294629 return self.finishAir(inst, .dead, .{ .none, .none, .none });
src/arch/arm/CodeGen.zig+2-2
......@@ -4568,9 +4568,9 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
45684568}
45694569
45704570fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
4571 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
4571 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
45724572 const mod = self.bin_file.options.module.?;
4573 const function = self.air.values[ty_pl.payload].getFunction(mod).?;
4573 const function = mod.funcPtr(ty_fn.func);
45744574 // TODO emit debug info for function change
45754575 _ = function;
45764576 return self.finishAir(inst, .dead, .{ .none, .none, .none });
src/arch/riscv64/CodeGen.zig+2-2
......@@ -1875,9 +1875,9 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
18751875}
18761876
18771877fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
1878 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
1878 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
18791879 const mod = self.bin_file.options.module.?;
1880 const function = self.air.values[ty_pl.payload].getFunction(mod).?;
1880 const function = mod.funcPtr(ty_fn.func);
18811881 // TODO emit debug info for function change
18821882 _ = function;
18831883 return self.finishAir(inst, .dead, .{ .none, .none, .none });
src/arch/sparc64/CodeGen.zig+2-2
......@@ -1660,9 +1660,9 @@ fn airDbgBlock(self: *Self, inst: Air.Inst.Index) !void {
16601660}
16611661
16621662fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
1663 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
1663 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
16641664 const mod = self.bin_file.options.module.?;
1665 const function = self.air.values[ty_pl.payload].getFunction(mod).?;
1665 const function = mod.funcPtr(ty_fn.func);
16661666 // TODO emit debug info for function change
16671667 _ = function;
16681668 return self.finishAir(inst, .dead, .{ .none, .none, .none });
src/arch/wasm/CodeGen.zig+1-1
......@@ -4947,7 +4947,7 @@ fn airShuffle(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
49474947
49484948 const a = try func.resolveInst(extra.a);
49494949 const b = try func.resolveInst(extra.b);
4950 const mask = func.air.values[extra.mask];
4950 const mask = extra.mask.toValue();
49514951 const mask_len = extra.mask_len;
49524952
49534953 const child_ty = inst_ty.childType(mod);
src/arch/x86_64/CodeGen.zig+2-2
......@@ -8541,9 +8541,9 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
85418541}
85428542
85438543fn airDbgInline(self: *Self, inst: Air.Inst.Index) !void {
8544 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
8544 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
85458545 const mod = self.bin_file.options.module.?;
8546 const function = self.air.values[ty_pl.payload].getFunction(mod).?;
8546 const function = mod.funcPtr(ty_fn.func);
85478547 // TODO emit debug info for function change
85488548 _ = function;
85498549 return self.finishAir(inst, .unreach, .{ .none, .none, .none });
src/codegen/c.zig+3-3
......@@ -4302,10 +4302,10 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
43024302}
43034303
43044304fn airDbgInline(f: *Function, inst: Air.Inst.Index) !CValue {
4305 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
4305 const ty_fn = f.air.instructions.items(.data)[inst].ty_fn;
43064306 const mod = f.object.dg.module;
43074307 const writer = f.object.writer();
4308 const function = f.air.values[ty_pl.payload].getFunction(mod).?;
4308 const function = mod.funcPtr(ty_fn.func);
43094309 try writer.print("/* dbg func:{s} */\n", .{mod.declPtr(function.owner_decl).name});
43104310 return .none;
43114311}
......@@ -6612,7 +6612,7 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue {
66126612 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
66136613 const extra = f.air.extraData(Air.Shuffle, ty_pl.payload).data;
66146614
6615 const mask = f.air.values[extra.mask];
6615 const mask = extra.mask.toValue();
66166616 const lhs = try f.resolveInst(extra.a);
66176617 const rhs = try f.resolveInst(extra.b);
66186618
src/codegen/llvm.zig+5-5
......@@ -5927,10 +5927,10 @@ pub const FuncGen = struct {
59275927
59285928 fn airDbgInlineBegin(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value {
59295929 const dib = self.dg.object.di_builder orelse return null;
5930 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
5930 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
59315931
59325932 const mod = self.dg.module;
5933 const func = self.air.values[ty_pl.payload].getFunction(mod).?;
5933 const func = mod.funcPtr(ty_fn.func);
59345934 const decl_index = func.owner_decl;
59355935 const decl = mod.declPtr(decl_index);
59365936 const di_file = try self.dg.object.getDIFile(self.gpa, mod.namespacePtr(decl.src_namespace).file_scope);
......@@ -5986,10 +5986,10 @@ pub const FuncGen = struct {
59865986
59875987 fn airDbgInlineEnd(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value {
59885988 if (self.dg.object.di_builder == null) return null;
5989 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
5989 const ty_fn = self.air.instructions.items(.data)[inst].ty_fn;
59905990
59915991 const mod = self.dg.module;
5992 const func = self.air.values[ty_pl.payload].getFunction(mod).?;
5992 const func = mod.funcPtr(ty_fn.func);
59935993 const decl = mod.declPtr(func.owner_decl);
59945994 const di_file = try self.dg.object.getDIFile(self.gpa, mod.namespacePtr(decl.src_namespace).file_scope);
59955995 self.di_file = di_file;
......@@ -8875,7 +8875,7 @@ pub const FuncGen = struct {
88758875 const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data;
88768876 const a = try self.resolveInst(extra.a);
88778877 const b = try self.resolveInst(extra.b);
8878 const mask = self.air.values[extra.mask];
8878 const mask = extra.mask.toValue();
88798879 const mask_len = extra.mask_len;
88808880 const a_len = self.typeOf(extra.a).vectorLen(mod);
88818881
src/codegen/spirv.zig+1-1
......@@ -2074,7 +2074,7 @@ pub const DeclGen = struct {
20742074 const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data;
20752075 const a = try self.resolve(extra.a);
20762076 const b = try self.resolve(extra.b);
2077 const mask = self.air.values[extra.mask];
2077 const mask = extra.mask.toValue();
20782078 const mask_len = extra.mask_len;
20792079 const a_len = self.typeOf(extra.a).vectorLen(mod);
20802080
src/print_air.zig+6-16
......@@ -15,12 +15,11 @@ pub fn write(stream: anytype, module: *Module, air: Air, liveness: ?Liveness) vo
1515 // the debug safety tag but we want to measure release size.
1616 (@sizeOf(Air.Inst.Tag) + 8);
1717 const extra_bytes = air.extra.len * @sizeOf(u32);
18 const values_bytes = air.values.len * @sizeOf(Value);
1918 const tomb_bytes = if (liveness) |l| l.tomb_bits.len * @sizeOf(usize) else 0;
2019 const liveness_extra_bytes = if (liveness) |l| l.extra.len * @sizeOf(u32) else 0;
2120 const liveness_special_bytes = if (liveness) |l| l.special.count() * 8 else 0;
2221 const total_bytes = @sizeOf(Air) + instruction_bytes + extra_bytes +
23 values_bytes + @sizeOf(Liveness) + liveness_extra_bytes +
22 @sizeOf(Liveness) + liveness_extra_bytes +
2423 liveness_special_bytes + tomb_bytes;
2524
2625 // zig fmt: off
......@@ -28,7 +27,6 @@ pub fn write(stream: anytype, module: *Module, air: Air, liveness: ?Liveness) vo
2827 \\# Total AIR+Liveness bytes: {}
2928 \\# AIR Instructions: {d} ({})
3029 \\# AIR Extra Data: {d} ({})
31 \\# AIR Values Bytes: {d} ({})
3230 \\# Liveness tomb_bits: {}
3331 \\# Liveness Extra Data: {d} ({})
3432 \\# Liveness special table: {d} ({})
......@@ -37,7 +35,6 @@ pub fn write(stream: anytype, module: *Module, air: Air, liveness: ?Liveness) vo
3735 fmtIntSizeBin(total_bytes),
3836 air.instructions.len, fmtIntSizeBin(instruction_bytes),
3937 air.extra.len, fmtIntSizeBin(extra_bytes),
40 air.values.len, fmtIntSizeBin(values_bytes),
4138 fmtIntSizeBin(tomb_bytes),
4239 if (liveness) |l| l.extra.len else 0, fmtIntSizeBin(liveness_extra_bytes),
4340 if (liveness) |l| l.special.count() else 0, fmtIntSizeBin(liveness_special_bytes),
......@@ -300,7 +297,8 @@ const Writer = struct {
300297
301298 .struct_field_ptr => try w.writeStructField(s, inst),
302299 .struct_field_val => try w.writeStructField(s, inst),
303 .inferred_alloc, .inferred_alloc_comptime => try w.writeConstant(s, inst),
300 .inferred_alloc => @panic("TODO"),
301 .inferred_alloc_comptime => @panic("TODO"),
304302 .interned => try w.writeInterned(s, inst),
305303 .assembly => try w.writeAssembly(s, inst),
306304 .dbg_stmt => try w.writeDbgStmt(s, inst),
......@@ -598,14 +596,6 @@ const Writer = struct {
598596 try s.print(", {d}", .{extra.field_index});
599597 }
600598
601 fn writeConstant(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
602 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
603 const val = w.air.values[ty_pl.payload];
604 const ty = w.air.getRefType(ty_pl.ty);
605 try w.writeType(s, ty);
606 try s.print(", {}", .{val.fmtValue(ty, w.module)});
607 }
608
609599 fn writeInterned(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
610600 const mod = w.module;
611601 const ip_index = w.air.instructions.items(.data)[inst].interned;
......@@ -693,9 +683,9 @@ const Writer = struct {
693683 }
694684
695685 fn writeDbgInline(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
696 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
697 const func_index = w.module.intern_pool.indexToFunc(w.air.values[ty_pl.payload].ip_index);
698 const owner_decl = w.module.declPtr(w.module.funcPtrUnwrap(func_index).?.owner_decl);
686 const ty_fn = w.air.instructions.items(.data)[inst].ty_fn;
687 const func_index = ty_fn.func;
688 const owner_decl = w.module.declPtr(w.module.funcPtr(func_index).owner_decl);
699689 try s.print("{s}", .{owner_decl.name});
700690 }
701691