authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2025-08-07 13:32:59+02:00
committergravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2025-08-12 16:33:57+02:00
log7756fa66411d88f8e280d6d5e91d809ea536cbff
tree1b6aab2c46fa10b679db550db5c0a7d23838e873
parent05762ca02ff97e7abfe1b52c090663dbf99bd4fc

Sema: replace most aggregate interns with pt.aggregateValue


1 files changed, 23 insertions(+), 56 deletions(-)

src/Sema.zig+23-56
...@@ -15154,13 +15154,10 @@ fn addDivIntOverflowSafety(...@@ -15154,13 +15154,10 @@ fn addDivIntOverflowSafety(
15154 const elem_val = try lhs_val.elemValue(pt, elem_idx);15154 const elem_val = try lhs_val.elemValue(pt, elem_idx);
15155 elem_ok.* = if (elem_val.eqlScalarNum(min_int_scalar, zcu)) .bool_false else .bool_true;15155 elem_ok.* = if (elem_val.eqlScalarNum(min_int_scalar, zcu)) .bool_false else .bool_true;
15156 }15156 }
15157 break :ok Air.internedToRef(try pt.intern(.{ .aggregate = .{15157 break :ok .fromValue(try pt.aggregateValue(try pt.vectorType(.{
15158 .ty = (try pt.vectorType(.{15158 .len = vec_len,
15159 .len = vec_len,15159 .child = .bool_type,
15160 .child = .bool_type,15160 }), elems_ok));
15161 })).toIntern(),
15162 .storage = .{ .elems = elems_ok },
15163 } }));
15164 } else ok: {15161 } else ok: {
15165 // The operand isn't comptime-known; add a runtime comparison.15162 // The operand isn't comptime-known; add a runtime comparison.
15166 const min_int_ref = Air.internedToRef(min_int.toIntern());15163 const min_int_ref = Air.internedToRef(min_int.toIntern());
...@@ -15175,13 +15172,10 @@ fn addDivIntOverflowSafety(...@@ -15175,13 +15172,10 @@ fn addDivIntOverflowSafety(
15175 const elem_val = try rhs_val.elemValue(pt, elem_idx);15172 const elem_val = try rhs_val.elemValue(pt, elem_idx);
15176 elem_ok.* = if (elem_val.eqlScalarNum(neg_one_scalar, zcu)) .bool_false else .bool_true;15173 elem_ok.* = if (elem_val.eqlScalarNum(neg_one_scalar, zcu)) .bool_false else .bool_true;
15177 }15174 }
15178 break :ok Air.internedToRef(try pt.intern(.{ .aggregate = .{15175 break :ok .fromValue(try pt.aggregateValue(try pt.vectorType(.{
15179 .ty = (try pt.vectorType(.{15176 .len = vec_len,
15180 .len = vec_len,15177 .child = .bool_type,
15181 .child = .bool_type,15178 }), elems_ok));
15182 })).toIntern(),
15183 .storage = .{ .elems = elems_ok },
15184 } }));
15185 } else ok: {15179 } else ok: {
15186 // The operand isn't comptime-known; add a runtime comparison.15180 // The operand isn't comptime-known; add a runtime comparison.
15187 const neg_one_ref = Air.internedToRef(neg_one.toIntern());15181 const neg_one_ref = Air.internedToRef(neg_one.toIntern());
...@@ -19057,10 +19051,7 @@ fn arrayInitEmpty(sema: *Sema, block: *Block, src: LazySrcLoc, obj_ty: Type) Com...@@ -19057,10 +19051,7 @@ fn arrayInitEmpty(sema: *Sema, block: *Block, src: LazySrcLoc, obj_ty: Type) Com
19057 return sema.fail(block, src, "expected {d} vector elements; found 0", .{arr_len});19051 return sema.fail(block, src, "expected {d} vector elements; found 0", .{arr_len});
19058 }19052 }
19059 }19053 }
19060 return Air.internedToRef((try pt.intern(.{ .aggregate = .{19054 return .fromValue(try pt.aggregateValue(obj_ty, &.{}));
19061 .ty = obj_ty.toIntern(),
19062 .storage = .{ .elems = &.{} },
19063 } })));
19064}19055}
1906519056
19066fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {19057fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -23478,40 +23469,23 @@ fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -23478,40 +23469,23 @@ fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
23478 }23469 }
2347923470
23480 // We also need this case because `[0:s]T` is not OPV.23471 // We also need this case because `[0:s]T` is not OPV.
23481 if (len == 0) {23472 if (len == 0) return .fromValue(try pt.aggregateValue(dest_ty, &.{}));
23482 const empty_aggregate = try pt.intern(.{ .aggregate = .{
23483 .ty = dest_ty.toIntern(),
23484 .storage = .{ .elems = &.{} },
23485 } });
23486 return Air.internedToRef(empty_aggregate);
23487 }
2348823473
23489 const maybe_sentinel = dest_ty.sentinel(zcu);23474 const maybe_sentinel = dest_ty.sentinel(zcu);
2349023475
23491 if (try sema.resolveValue(scalar)) |scalar_val| {23476 if (try sema.resolveValue(scalar)) |scalar_val| {
23492 if (scalar_val.isUndef(zcu) and maybe_sentinel == null) {23477 full: {
23493 return pt.undefRef(dest_ty);23478 if (dest_ty.zigTypeTag(zcu) == .vector) break :full;
23479 const sentinel = maybe_sentinel orelse break :full;
23480 if (sentinel.toIntern() == scalar_val.toIntern()) break :full;
23481 // This is a array with non-zero length and a sentinel which does not match the element.
23482 // We have to use the full `elems` representation.
23483 const elems = try sema.arena.alloc(InternPool.Index, len + 1);
23484 @memset(elems[0..len], scalar_val.toIntern());
23485 elems[len] = sentinel.toIntern();
23486 return .fromValue(try pt.aggregateValue(dest_ty, elems));
23494 }23487 }
23495 // TODO: I didn't want to put `.aggregate` on a separate line here; `zig fmt` bugs have forced my hand23488 return .fromValue(try pt.aggregateSplatValue(dest_ty, scalar_val));
23496 return Air.internedToRef(try pt.intern(.{
23497 .aggregate = .{
23498 .ty = dest_ty.toIntern(),
23499 .storage = s: {
23500 full: {
23501 if (dest_ty.zigTypeTag(zcu) == .vector) break :full;
23502 const sentinel = maybe_sentinel orelse break :full;
23503 if (sentinel.toIntern() == scalar_val.toIntern()) break :full;
23504 // This is a array with non-zero length and a sentinel which does not match the element.
23505 // We have to use the full `elems` representation.
23506 const elems = try sema.arena.alloc(InternPool.Index, len + 1);
23507 @memset(elems[0..len], scalar_val.toIntern());
23508 elems[len] = sentinel.toIntern();
23509 break :s .{ .elems = elems };
23510 }
23511 break :s .{ .repeated_elem = scalar_val.toIntern() };
23512 },
23513 },
23514 }));
23515 }23489 }
2351623490
23517 try sema.requireRuntimeBlock(block, src, scalar_src);23491 try sema.requireRuntimeBlock(block, src, scalar_src);
...@@ -35923,11 +35897,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {...@@ -35923,11 +35897,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
35923 => switch (ip.indexToKey(ty.toIntern())) {35897 => switch (ip.indexToKey(ty.toIntern())) {
35924 inline .array_type, .vector_type => |seq_type, seq_tag| {35898 inline .array_type, .vector_type => |seq_type, seq_tag| {
35925 const has_sentinel = seq_tag == .array_type and seq_type.sentinel != .none;35899 const has_sentinel = seq_tag == .array_type and seq_type.sentinel != .none;
35926 if (seq_type.len + @intFromBool(has_sentinel) == 0) return Value.fromInterned(try pt.intern(.{ .aggregate = .{35900 if (seq_type.len + @intFromBool(has_sentinel) == 0) return try pt.aggregateValue(ty, &.{});
35927 .ty = ty.toIntern(),
35928 .storage = .{ .elems = &.{} },
35929 } }));
35930
35931 if (try sema.typeHasOnePossibleValue(.fromInterned(seq_type.child))) |opv| {35901 if (try sema.typeHasOnePossibleValue(.fromInterned(seq_type.child))) |opv| {
35932 return try pt.aggregateSplatValue(ty, opv);35902 return try pt.aggregateSplatValue(ty, opv);
35933 }35903 }
...@@ -35944,10 +35914,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {...@@ -35944,10 +35914,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
35944 if (struct_type.field_types.len == 0) {35914 if (struct_type.field_types.len == 0) {
35945 // In this case the struct has no fields at all and35915 // In this case the struct has no fields at all and
35946 // therefore has one possible value.35916 // therefore has one possible value.
35947 return Value.fromInterned(try pt.intern(.{ .aggregate = .{35917 return try pt.aggregateValue(ty, &.{});
35948 .ty = ty.toIntern(),
35949 .storage = .{ .elems = &.{} },
35950 } }));
35951 }35918 }
3595235919
35953 const field_vals = try sema.arena.alloc(35920 const field_vals = try sema.arena.alloc(