Skip to content

Commit c091e27

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 9a7d28f commit c091e27

File tree

21 files changed

+210
-2442
lines changed

21 files changed

+210
-2442
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: 27 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15919,26 +15919,27 @@ fn zirOverflowArithmetic(
1591915919
},
1592015920
.mul_with_overflow => {
1592115921
// If either of the arguments is zero, the result is zero and no overflow occured.
15922+
if (maybe_lhs_val) |lhs_val| {
15923+
if (!lhs_val.isUndef(zcu) and try lhs_val.compareAllWithZeroSema(.eq, pt)) {
15924+
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = lhs };
15925+
}
15926+
}
15927+
if (maybe_rhs_val) |rhs_val| {
15928+
if (!rhs_val.isUndef(zcu) and try rhs_val.compareAllWithZeroSema(.eq, pt)) {
15929+
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = rhs };
15930+
}
15931+
}
1592215932
// If either of the arguments is one, the result is the other and no overflow occured.
15923-
// Otherwise, if either of the arguments is undefined, both results are undefined.
1592415933
const scalar_one = try pt.intValue(dest_ty.scalarType(zcu), 1);
15934+
const vec_one = try sema.splat(dest_ty, scalar_one);
1592515935
if (maybe_lhs_val) |lhs_val| {
15926-
if (!lhs_val.isUndef(zcu)) {
15927-
if (try lhs_val.compareAllWithZeroSema(.eq, pt)) {
15928-
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = lhs };
15929-
} else if (try sema.compareAll(lhs_val, .eq, try sema.splat(dest_ty, scalar_one), dest_ty)) {
15930-
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = rhs };
15931-
}
15936+
if (!lhs_val.isUndef(zcu) and try sema.compareAll(lhs_val, .eq, vec_one, dest_ty)) {
15937+
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = rhs };
1593215938
}
1593315939
}
15934-
1593515940
if (maybe_rhs_val) |rhs_val| {
15936-
if (!rhs_val.isUndef(zcu)) {
15937-
if (try rhs_val.compareAllWithZeroSema(.eq, pt)) {
15938-
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = rhs };
15939-
} else if (try sema.compareAll(rhs_val, .eq, try sema.splat(dest_ty, scalar_one), dest_ty)) {
15940-
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = lhs };
15941-
}
15941+
if (!rhs_val.isUndef(zcu) and try sema.compareAll(rhs_val, .eq, vec_one, dest_ty)) {
15942+
break :result .{ .overflow_bit = try sema.splat(overflow_ty, .zero_u1), .inst = lhs };
1594215943
}
1594315944
}
1594415945

@@ -15947,7 +15948,6 @@ fn zirOverflowArithmetic(
1594715948
if (lhs_val.isUndef(zcu) or rhs_val.isUndef(zcu)) {
1594815949
break :result .{ .overflow_bit = .undef, .wrapped = .undef };
1594915950
}
15950-
1595115951
const result = try arith.mulWithOverflow(sema, dest_ty, lhs_val, rhs_val);
1595215952
break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result };
1595315953
}
@@ -17751,10 +17751,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1775117751
try ty.resolveStructFieldInits(pt);
1775217752

1775317753
for (struct_field_vals, 0..) |*field_val, field_index| {
17754-
const field_name = if (struct_type.fieldName(ip, field_index).unwrap()) |field_name|
17755-
field_name
17756-
else
17757-
try ip.getOrPutStringFmt(gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls);
17754+
const field_name = struct_type.fieldName(ip, field_index);
1775817755
const field_name_len = field_name.length(ip);
1775917756
const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]);
1776017757
const field_init = struct_type.fieldInit(ip, field_index);
@@ -28347,6 +28344,10 @@ fn elemPtrArray(
2834728344
break :o index;
2834828345
} else null;
2834928346

28347+
if (offset == null and array_ty.zigTypeTag(zcu) == .vector) {
28348+
return sema.fail(block, elem_index_src, "vector index not comptime known", .{});
28349+
}
28350+
2835028351
const elem_ptr_ty = try array_ptr_ty.elemPtrType(offset, pt);
2835128352

2835228353
if (maybe_undef_array_ptr_val) |array_ptr_val| {
@@ -28364,10 +28365,6 @@ fn elemPtrArray(
2836428365
try sema.validateRuntimeValue(block, array_ptr_src, array_ptr);
2836528366
}
2836628367

28367-
if (offset == null and array_ty.zigTypeTag(zcu) == .vector) {
28368-
return sema.fail(block, elem_index_src, "vector index not comptime known", .{});
28369-
}
28370-
2837128368
// Runtime check is only needed if unable to comptime check.
2837228369
if (oob_safety and block.wantSafety() and offset == null) {
2837328370
const len_inst = try pt.intRef(.usize, array_len);
@@ -30399,22 +30396,6 @@ fn storePtr2(
3039930396

3040030397
const is_ret = air_tag == .ret_ptr;
3040130398

30402-
// Detect if we are storing an array operand to a bitcasted vector pointer.
30403-
// If so, we instead reach through the bitcasted pointer to the vector pointer,
30404-
// bitcast the array operand to a vector, and then lower this as a store of
30405-
// a vector value to a vector pointer. This generally results in better code,
30406-
// as well as working around an LLVM bug:
30407-
// https://github.com/ziglang/zig/issues/11154
30408-
if (sema.obtainBitCastedVectorPtr(ptr)) |vector_ptr| {
30409-
const vector_ty = sema.typeOf(vector_ptr).childType(zcu);
30410-
const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
30411-
error.NotCoercible => unreachable,
30412-
else => |e| return e,
30413-
};
30414-
try sema.storePtr2(block, src, vector_ptr, ptr_src, vector, operand_src, .store);
30415-
return;
30416-
}
30417-
3041830399
const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, .{ .is_ret = is_ret }) catch |err| switch (err) {
3041930400
error.NotCoercible => unreachable,
3042030401
else => |e| return e,
@@ -30447,29 +30428,6 @@ fn storePtr2(
3044730428

3044830429
try sema.requireRuntimeBlock(block, src, runtime_src);
3044930430

30450-
if (ptr_ty.ptrInfo(zcu).flags.vector_index == .runtime) {
30451-
const ptr_inst = ptr.toIndex().?;
30452-
const air_tags = sema.air_instructions.items(.tag);
30453-
if (air_tags[@intFromEnum(ptr_inst)] == .ptr_elem_ptr) {
30454-
const ty_pl = sema.air_instructions.items(.data)[@intFromEnum(ptr_inst)].ty_pl;
30455-
const bin_op = sema.getTmpAir().extraData(Air.Bin, ty_pl.payload).data;
30456-
_ = try block.addInst(.{
30457-
.tag = .vector_store_elem,
30458-
.data = .{ .vector_store_elem = .{
30459-
.vector_ptr = bin_op.lhs,
30460-
.payload = try block.sema.addExtra(Air.Bin{
30461-
.lhs = bin_op.rhs,
30462-
.rhs = operand,
30463-
}),
30464-
} },
30465-
});
30466-
return;
30467-
}
30468-
return sema.fail(block, ptr_src, "unable to determine vector element index of type '{f}'", .{
30469-
ptr_ty.fmt(pt),
30470-
});
30471-
}
30472-
3047330431
const store_inst = if (is_ret)
3047430432
try block.addBinOp(.store, ptr, operand)
3047530433
else
@@ -30569,37 +30527,6 @@ fn markMaybeComptimeAllocRuntime(sema: *Sema, block: *Block, alloc_inst: Air.Ins
3056930527
}
3057030528
}
3057130529

30572-
/// Traverse an arbitrary number of bitcasted pointers and return the underyling vector
30573-
/// pointer. Only if the final element type matches the vector element type, and the
30574-
/// lengths match.
30575-
fn obtainBitCastedVectorPtr(sema: *Sema, ptr: Air.Inst.Ref) ?Air.Inst.Ref {
30576-
const pt = sema.pt;
30577-
const zcu = pt.zcu;
30578-
const array_ty = sema.typeOf(ptr).childType(zcu);
30579-
if (array_ty.zigTypeTag(zcu) != .array) return null;
30580-
var ptr_ref = ptr;
30581-
var ptr_inst = ptr_ref.toIndex() orelse return null;
30582-
const air_datas = sema.air_instructions.items(.data);
30583-
const air_tags = sema.air_instructions.items(.tag);
30584-
const vector_ty = while (air_tags[@intFromEnum(ptr_inst)] == .bitcast) {
30585-
ptr_ref = air_datas[@intFromEnum(ptr_inst)].ty_op.operand;
30586-
if (!sema.isKnownZigType(ptr_ref, .pointer)) return null;
30587-
const child_ty = sema.typeOf(ptr_ref).childType(zcu);
30588-
if (child_ty.zigTypeTag(zcu) == .vector) break child_ty;
30589-
ptr_inst = ptr_ref.toIndex() orelse return null;
30590-
} else return null;
30591-
30592-
// We have a pointer-to-array and a pointer-to-vector. If the elements and
30593-
// lengths match, return the result.
30594-
if (array_ty.childType(zcu).eql(vector_ty.childType(zcu), zcu) and
30595-
array_ty.arrayLen(zcu) == vector_ty.vectorLen(zcu))
30596-
{
30597-
return ptr_ref;
30598-
} else {
30599-
return null;
30600-
}
30601-
}
30602-
3060330530
/// Call when you have Value objects rather than Air instructions, and you want to
3060430531
/// assert the store must be done at comptime.
3060530532
fn storePtrVal(
@@ -35579,8 +35506,13 @@ fn structFieldInits(
3557935506
const default_val = try sema.resolveConstValue(&block_scope, init_src, coerced, null);
3558035507

3558135508
if (default_val.canMutateComptimeVarState(zcu)) {
35582-
const field_name = struct_type.fieldName(ip, field_i).unwrap().?;
35583-
return sema.failWithContainsReferenceToComptimeVar(&block_scope, init_src, field_name, "field default value", default_val);
35509+
return sema.failWithContainsReferenceToComptimeVar(
35510+
&block_scope,
35511+
init_src,
35512+
struct_type.fieldName(ip, field_i),
35513+
"field default value",
35514+
default_val,
35515+
);
3558435516
}
3558535517
struct_type.field_inits.get(ip)[field_i] = default_val.toIntern();
3558635518
}

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)