Skip to content

Commit 1b6f963

Browse files
committed
compiler: spring cleaning
I started this diff trying to remove a little dead code from the C backend, but ended up finding a bunch of dead code sprinkled all over the place: * `packed` handling in the C backend which was made dead by `Legalize` * Representation of pointers to runtime-known vector indices * Handling for the `vector_store_elem` AIR instruction (now removed) * Old tuple handling from when they used the InternPool repr of structs * Straightforward unused functions * TODOs in the LLVM backend for features which Zig just does not support
1 parent dd0cd3e commit 1b6f963

File tree

21 files changed

+173
-2424
lines changed

21 files changed

+173
-2424
lines changed

src/Air.zig

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,6 @@ pub const Inst = struct {
874874
/// Uses the `ty_pl` field.
875875
save_err_return_trace_index,
876876

877-
/// Store an element to a vector pointer at an index.
878-
/// Uses the `vector_store_elem` field.
879-
vector_store_elem,
880-
881877
/// Compute a pointer to a `Nav` at runtime, always one of:
882878
///
883879
/// * `threadlocal var`
@@ -1220,11 +1216,6 @@ pub const Inst = struct {
12201216
operand: Ref,
12211217
operation: std.builtin.ReduceOp,
12221218
},
1223-
vector_store_elem: struct {
1224-
vector_ptr: Ref,
1225-
// Index into a different array.
1226-
payload: u32,
1227-
},
12281219
ty_nav: struct {
12291220
ty: InternPool.Index,
12301221
nav: InternPool.Nav.Index,
@@ -1689,7 +1680,6 @@ pub fn typeOfIndex(air: *const Air, inst: Air.Inst.Index, ip: *const InternPool)
16891680
.set_union_tag,
16901681
.prefetch,
16911682
.set_err_return_trace,
1692-
.vector_store_elem,
16931683
.c_va_end,
16941684
=> return .void,
16951685

@@ -1857,7 +1847,6 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
18571847
.prefetch,
18581848
.wasm_memory_grow,
18591849
.set_err_return_trace,
1860-
.vector_store_elem,
18611850
.c_va_arg,
18621851
.c_va_copy,
18631852
.c_va_end,

src/Air/Liveness.zig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,6 @@ fn analyzeInst(
463463
return analyzeOperands(a, pass, data, inst, .{ o.lhs, o.rhs, .none });
464464
},
465465

466-
.vector_store_elem => {
467-
const o = inst_datas[@intFromEnum(inst)].vector_store_elem;
468-
const extra = a.air.extraData(Air.Bin, o.payload).data;
469-
return analyzeOperands(a, pass, data, inst, .{ o.vector_ptr, extra.lhs, extra.rhs });
470-
},
471-
472466
.arg,
473467
.alloc,
474468
.ret_ptr,

src/Air/Liveness/Verify.zig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,6 @@ fn verifyBody(self: *Verify, body: []const Air.Inst.Index) Error!void {
322322
const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
323323
try self.verifyInstOperands(inst, .{ extra.lhs, extra.rhs, pl_op.operand });
324324
},
325-
.vector_store_elem => {
326-
const vector_store_elem = data[@intFromEnum(inst)].vector_store_elem;
327-
const extra = self.air.extraData(Air.Bin, vector_store_elem.payload).data;
328-
try self.verifyInstOperands(inst, .{ vector_store_elem.vector_ptr, extra.lhs, extra.rhs });
329-
},
330325
.cmpxchg_strong,
331326
.cmpxchg_weak,
332327
=> {

src/Air/print.zig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ const Writer = struct {
330330
.shuffle_two => try w.writeShuffleTwo(s, inst),
331331
.reduce, .reduce_optimized => try w.writeReduce(s, inst),
332332
.cmp_vector, .cmp_vector_optimized => try w.writeCmpVector(s, inst),
333-
.vector_store_elem => try w.writeVectorStoreElem(s, inst),
334333
.runtime_nav_ptr => try w.writeRuntimeNavPtr(s, inst),
335334

336335
.work_item_id,
@@ -576,17 +575,6 @@ const Writer = struct {
576575
try w.writeOperand(s, inst, 1, extra.rhs);
577576
}
578577

579-
fn writeVectorStoreElem(w: *Writer, s: *std.Io.Writer, inst: Air.Inst.Index) Error!void {
580-
const data = w.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem;
581-
const extra = w.air.extraData(Air.VectorCmp, data.payload).data;
582-
583-
try w.writeOperand(s, inst, 0, data.vector_ptr);
584-
try s.writeAll(", ");
585-
try w.writeOperand(s, inst, 1, extra.lhs);
586-
try s.writeAll(", ");
587-
try w.writeOperand(s, inst, 2, extra.rhs);
588-
}
589-
590578
fn writeRuntimeNavPtr(w: *Writer, s: *std.Io.Writer, inst: Air.Inst.Index) Error!void {
591579
const ip = &w.pt.zcu.intern_pool;
592580
const ty_nav = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;

src/Air/types_resolved.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,6 @@ fn checkBody(air: Air, body: []const Air.Inst.Index, zcu: *Zcu) bool {
316316
if (!checkRef(data.prefetch.ptr, zcu)) return false;
317317
},
318318

319-
.vector_store_elem => {
320-
const bin = air.extraData(Air.Bin, data.vector_store_elem.payload).data;
321-
if (!checkRef(data.vector_store_elem.vector_ptr, zcu)) return false;
322-
if (!checkRef(bin.lhs, zcu)) return false;
323-
if (!checkRef(bin.rhs, zcu)) return false;
324-
},
325-
326319
.runtime_nav_ptr => {
327320
if (!checkType(.fromInterned(data.ty_nav.ty), zcu)) return false;
328321
},

src/InternPool.zig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,6 @@ pub const Key = union(enum) {
21042104

21052105
pub const VectorIndex = enum(u16) {
21062106
none = std.math.maxInt(u16),
2107-
runtime = std.math.maxInt(u16) - 1,
21082107
_,
21092108
};
21102109

@@ -3739,10 +3738,8 @@ pub const LoadedStructType = struct {
37393738
return s.field_inits.get(ip)[i];
37403739
}
37413740

3742-
/// Returns `none` in the case the struct is a tuple.
3743-
pub fn fieldName(s: LoadedStructType, ip: *const InternPool, i: usize) OptionalNullTerminatedString {
3744-
if (s.field_names.len == 0) return .none;
3745-
return s.field_names.get(ip)[i].toOptional();
3741+
pub fn fieldName(s: LoadedStructType, ip: *const InternPool, i: usize) NullTerminatedString {
3742+
return s.field_names.get(ip)[i];
37463743
}
37473744

37483745
pub fn fieldIsComptime(s: LoadedStructType, ip: *const InternPool, i: usize) bool {

src/Sema.zig

Lines changed: 12 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17732,10 +17732,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1773217732
try ty.resolveStructFieldInits(pt);
1773317733

1773417734
for (struct_field_vals, 0..) |*field_val, field_index| {
17735-
const field_name = if (struct_type.fieldName(ip, field_index).unwrap()) |field_name|
17736-
field_name
17737-
else
17738-
try ip.getOrPutStringFmt(gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls);
17735+
const field_name = struct_type.fieldName(ip, field_index);
1773917736
const field_name_len = field_name.length(ip);
1774017737
const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]);
1774117738
const field_init = struct_type.fieldInit(ip, field_index);
@@ -28334,6 +28331,10 @@ fn elemPtrArray(
2833428331
break :o index;
2833528332
} else null;
2833628333

28334+
if (offset == null and array_ty.zigTypeTag(zcu) == .vector) {
28335+
return sema.fail(block, elem_index_src, "vector index not comptime known", .{});
28336+
}
28337+
2833728338
const elem_ptr_ty = try array_ptr_ty.elemPtrType(offset, pt);
2833828339

2833928340
if (maybe_undef_array_ptr_val) |array_ptr_val| {
@@ -28351,10 +28352,6 @@ fn elemPtrArray(
2835128352
try sema.validateRuntimeValue(block, array_ptr_src, array_ptr);
2835228353
}
2835328354

28354-
if (offset == null and array_ty.zigTypeTag(zcu) == .vector) {
28355-
return sema.fail(block, elem_index_src, "vector index not comptime known", .{});
28356-
}
28357-
2835828355
// Runtime check is only needed if unable to comptime check.
2835928356
if (oob_safety and block.wantSafety() and offset == null) {
2836028357
const len_inst = try pt.intRef(.usize, array_len);
@@ -30386,22 +30383,6 @@ fn storePtr2(
3038630383

3038730384
const is_ret = air_tag == .ret_ptr;
3038830385

30389-
// Detect if we are storing an array operand to a bitcasted vector pointer.
30390-
// If so, we instead reach through the bitcasted pointer to the vector pointer,
30391-
// bitcast the array operand to a vector, and then lower this as a store of
30392-
// a vector value to a vector pointer. This generally results in better code,
30393-
// as well as working around an LLVM bug:
30394-
// https://github.com/ziglang/zig/issues/11154
30395-
if (sema.obtainBitCastedVectorPtr(ptr)) |vector_ptr| {
30396-
const vector_ty = sema.typeOf(vector_ptr).childType(zcu);
30397-
const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
30398-
error.NotCoercible => unreachable,
30399-
else => |e| return e,
30400-
};
30401-
try sema.storePtr2(block, src, vector_ptr, ptr_src, vector, operand_src, .store);
30402-
return;
30403-
}
30404-
3040530386
const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
3040630387
error.NotCoercible => unreachable,
3040730388
else => |e| return e,
@@ -30434,29 +30415,6 @@ fn storePtr2(
3043430415

3043530416
try sema.requireRuntimeBlock(block, src, runtime_src);
3043630417

30437-
if (ptr_ty.ptrInfo(zcu).flags.vector_index == .runtime) {
30438-
const ptr_inst = ptr.toIndex().?;
30439-
const air_tags = sema.air_instructions.items(.tag);
30440-
if (air_tags[@intFromEnum(ptr_inst)] == .ptr_elem_ptr) {
30441-
const ty_pl = sema.air_instructions.items(.data)[@intFromEnum(ptr_inst)].ty_pl;
30442-
const bin_op = sema.getTmpAir().extraData(Air.Bin, ty_pl.payload).data;
30443-
_ = try block.addInst(.{
30444-
.tag = .vector_store_elem,
30445-
.data = .{ .vector_store_elem = .{
30446-
.vector_ptr = bin_op.lhs,
30447-
.payload = try block.sema.addExtra(Air.Bin{
30448-
.lhs = bin_op.rhs,
30449-
.rhs = operand,
30450-
}),
30451-
} },
30452-
});
30453-
return;
30454-
}
30455-
return sema.fail(block, ptr_src, "unable to determine vector element index of type '{f}'", .{
30456-
ptr_ty.fmt(pt),
30457-
});
30458-
}
30459-
3046030418
const store_inst = if (is_ret)
3046130419
try block.addBinOp(.store, ptr, operand)
3046230420
else
@@ -30556,37 +30514,6 @@ fn markMaybeComptimeAllocRuntime(sema: *Sema, block: *Block, alloc_inst: Air.Ins
3055630514
}
3055730515
}
3055830516

30559-
/// Traverse an arbitrary number of bitcasted pointers and return the underyling vector
30560-
/// pointer. Only if the final element type matches the vector element type, and the
30561-
/// lengths match.
30562-
fn obtainBitCastedVectorPtr(sema: *Sema, ptr: Air.Inst.Ref) ?Air.Inst.Ref {
30563-
const pt = sema.pt;
30564-
const zcu = pt.zcu;
30565-
const array_ty = sema.typeOf(ptr).childType(zcu);
30566-
if (array_ty.zigTypeTag(zcu) != .array) return null;
30567-
var ptr_ref = ptr;
30568-
var ptr_inst = ptr_ref.toIndex() orelse return null;
30569-
const air_datas = sema.air_instructions.items(.data);
30570-
const air_tags = sema.air_instructions.items(.tag);
30571-
const vector_ty = while (air_tags[@intFromEnum(ptr_inst)] == .bitcast) {
30572-
ptr_ref = air_datas[@intFromEnum(ptr_inst)].ty_op.operand;
30573-
if (!sema.isKnownZigType(ptr_ref, .pointer)) return null;
30574-
const child_ty = sema.typeOf(ptr_ref).childType(zcu);
30575-
if (child_ty.zigTypeTag(zcu) == .vector) break child_ty;
30576-
ptr_inst = ptr_ref.toIndex() orelse return null;
30577-
} else return null;
30578-
30579-
// We have a pointer-to-array and a pointer-to-vector. If the elements and
30580-
// lengths match, return the result.
30581-
if (array_ty.childType(zcu).eql(vector_ty.childType(zcu), zcu) and
30582-
array_ty.arrayLen(zcu) == vector_ty.vectorLen(zcu))
30583-
{
30584-
return ptr_ref;
30585-
} else {
30586-
return null;
30587-
}
30588-
}
30589-
3059030517
/// Call when you have Value objects rather than Air instructions, and you want to
3059130518
/// assert the store must be done at comptime.
3059230519
fn storePtrVal(
@@ -35566,8 +35493,13 @@ fn structFieldInits(
3556635493
const default_val = try sema.resolveConstValue(&block_scope, init_src, coerced, null);
3556735494

3556835495
if (default_val.canMutateComptimeVarState(zcu)) {
35569-
const field_name = struct_type.fieldName(ip, field_i).unwrap().?;
35570-
return sema.failWithContainsReferenceToComptimeVar(&block_scope, init_src, field_name, "field default value", default_val);
35496+
return sema.failWithContainsReferenceToComptimeVar(
35497+
&block_scope,
35498+
init_src,
35499+
struct_type.fieldName(ip, field_i),
35500+
"field default value",
35501+
default_val,
35502+
);
3557135503
}
3557235504
struct_type.field_inits.get(ip)[field_i] = default_val.toIntern();
3557335505
}

src/Sema/comptime_ptr_access.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub fn loadComptimePtr(sema: *Sema, block: *Block, src: LazySrcLoc, ptr: Value)
2424
const child_bits = Type.fromInterned(ptr_info.child).bitSize(zcu);
2525
const bit_offset = ptr_info.packed_offset.bit_offset + switch (ptr_info.flags.vector_index) {
2626
.none => 0,
27-
.runtime => return .runtime_load,
2827
else => |idx| switch (pt.zcu.getTarget().cpu.arch.endian()) {
2928
.little => child_bits * @intFromEnum(idx),
3029
.big => host_bits - child_bits * (@intFromEnum(idx) + 1), // element order reversed on big endian
@@ -81,7 +80,6 @@ pub fn storeComptimePtr(
8180
};
8281
const bit_offset = ptr_info.packed_offset.bit_offset + switch (ptr_info.flags.vector_index) {
8382
.none => 0,
84-
.runtime => return .runtime_store,
8583
else => |idx| switch (zcu.getTarget().cpu.arch.endian()) {
8684
.little => Type.fromInterned(ptr_info.child).bitSize(zcu) * @intFromEnum(idx),
8785
.big => host_bits - Type.fromInterned(ptr_info.child).bitSize(zcu) * (@intFromEnum(idx) + 1), // element order reversed on big endian

src/Type.zig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ pub fn print(ty: Type, writer: *std.Io.Writer, pt: Zcu.PerThread) std.Io.Writer.
198198
info.packed_offset.bit_offset, info.packed_offset.host_size,
199199
});
200200
}
201-
if (info.flags.vector_index == .runtime) {
202-
try writer.writeAll(":?");
203-
} else if (info.flags.vector_index != .none) {
201+
if (info.flags.vector_index != .none) {
204202
try writer.print(":{d}", .{@intFromEnum(info.flags.vector_index)});
205203
}
206204
try writer.writeAll(") ");
@@ -3113,7 +3111,7 @@ pub fn enumTagFieldIndex(ty: Type, enum_tag: Value, zcu: *const Zcu) ?u32 {
31133111
pub fn structFieldName(ty: Type, index: usize, zcu: *const Zcu) InternPool.OptionalNullTerminatedString {
31143112
const ip = &zcu.intern_pool;
31153113
return switch (ip.indexToKey(ty.toIntern())) {
3116-
.struct_type => ip.loadStructType(ty.toIntern()).fieldName(ip, index),
3114+
.struct_type => ip.loadStructType(ty.toIntern()).fieldName(ip, index).toOptional(),
31173115
.tuple_type => .none,
31183116
else => unreachable,
31193117
};
@@ -3985,7 +3983,7 @@ pub fn elemPtrType(ptr_ty: Type, offset: ?usize, pt: Zcu.PerThread) !Type {
39853983
break :blk .{
39863984
.host_size = @intCast(parent_ty.arrayLen(zcu)),
39873985
.alignment = parent_ty.abiAlignment(zcu),
3988-
.vector_index = if (offset) |some| @enumFromInt(some) else .runtime,
3986+
.vector_index = @enumFromInt(offset.?),
39893987
};
39903988
} else .{};
39913989

0 commit comments

Comments
 (0)