authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-20 13:48:19-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-20 14:02:09-04:00
log52ec121469d7bf10116fb22c122cbce8ceddd028
treedc37b0986aecc60639e4b2e105c4740a14dd8c58
parentd7bd4f339c90b9ce07283600cb7ef129912ceef3

Sema: optimize callers of `indexToKey`


4 files changed, 37 insertions(+), 23 deletions(-)

src/Air.zig+1-1
......@@ -1406,7 +1406,7 @@ pub fn typeOfIndex(air: *const Air, inst: Air.Inst.Index, ip: *const InternPool)
14061406
14071407 .call, .call_always_tail, .call_never_tail, .call_never_inline => {
14081408 const callee_ty = air.typeOf(datas[inst].pl_op.operand, ip);
1409 return callee_ty.fnReturnTypeIp(ip);
1409 return ip.funcReturnType(callee_ty.toIntern()).toType();
14101410 },
14111411
14121412 .slice_elem_val, .ptr_elem_val, .array_elem_val => {
src/InternPool.zig+17-2
......@@ -5694,11 +5694,26 @@ pub fn aggregateTypeLenIncludingSentinel(ip: *const InternPool, ty: Index) u64 {
56945694 };
56955695}
56965696
5697pub fn funcReturnType(ip: *const InternPool, ty: Index) Index {
5698 const item = ip.items.get(@intFromEnum(ty));
5699 const child_item = switch (item.tag) {
5700 .type_pointer => ip.items.get(ip.extra.items[
5701 item.data + std.meta.fieldIndex(Tag.TypePointer, "child").?
5702 ]),
5703 .type_function => item,
5704 else => unreachable,
5705 };
5706 assert(child_item.tag == .type_function);
5707 return @enumFromInt(Index, ip.extra.items[
5708 child_item.data + std.meta.fieldIndex(TypeFunction, "return_type").?
5709 ]);
5710}
5711
56975712pub fn isNoReturn(ip: *const InternPool, ty: Index) bool {
56985713 return switch (ty) {
56995714 .noreturn_type => true,
5700 else => switch (ip.indexToKey(ty)) {
5701 .error_set_type => |error_set_type| error_set_type.names.len == 0,
5715 else => switch (ip.items.items(.tag)[@intFromEnum(ty)]) {
5716 .type_error_set => ip.extra.items[ip.items.items(.data)[@intFromEnum(ty)] + std.meta.fieldIndex(ErrorSet, "names_len").?] == 0,
57025717 else => false,
57035718 },
57045719 };
src/Sema.zig+18-11
......@@ -33817,18 +33817,25 @@ pub fn resolveTypeFields(sema: *Sema, ty: Type) CompileError!Type {
3381733817 .call_modifier_type => return sema.getBuiltinType("CallModifier"),
3381833818 .prefetch_options_type => return sema.getBuiltinType("PrefetchOptions"),
3381933819
33820 _ => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33821 .struct_type => |struct_type| {
33822 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return ty;
33823 try sema.resolveTypeFieldsStruct(ty, struct_obj);
33824 return ty;
33825 },
33826 .union_type => |union_type| {
33827 const union_obj = mod.unionPtr(union_type.index);
33828 try sema.resolveTypeFieldsUnion(ty, union_obj);
33829 return ty;
33820 _ => switch (mod.intern_pool.items.items(.tag)[@intFromEnum(ty.toIntern())]) {
33821 .type_struct,
33822 .type_struct_ns,
33823 .type_union_tagged,
33824 .type_union_untagged,
33825 .type_union_safety,
33826 => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33827 .struct_type => |struct_type| {
33828 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return ty;
33829 try sema.resolveTypeFieldsStruct(ty, struct_obj);
33830 return ty;
33831 },
33832 .union_type => |union_type| {
33833 const union_obj = mod.unionPtr(union_type.index);
33834 try sema.resolveTypeFieldsUnion(ty, union_obj);
33835 return ty;
33836 },
33837 else => unreachable,
3383033838 },
33831
3383233839 else => return ty,
3383333840 },
3383433841 }
src/type.zig+1-9
......@@ -2385,15 +2385,7 @@ pub const Type = struct {
23852385
23862386 /// Asserts the type is a function or a function pointer.
23872387 pub fn fnReturnType(ty: Type, mod: *Module) Type {
2388 return fnReturnTypeIp(ty, &mod.intern_pool);
2389 }
2390
2391 pub fn fnReturnTypeIp(ty: Type, ip: *const InternPool) Type {
2392 return switch (ip.indexToKey(ty.toIntern())) {
2393 .ptr_type => |ptr_type| ip.indexToKey(ptr_type.child).func_type.return_type,
2394 .func_type => |func_type| func_type.return_type,
2395 else => unreachable,
2396 }.toType();
2388 return mod.intern_pool.funcReturnType(ty.toIntern()).toType();
23972389 }
23982390
23992391 /// Asserts the type is a function.