authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-07-15 16:18:41+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-07-16 11:38:20+01:00
logb1d3d48f687a3a73be1d9ab85035cbdc58e2bb1b
treed81f3d9705ba2bf164ed6468ae18315c7a7ffe89
parent9356cb1475606a7afd2e722af60f87ce2b39f9f8
signaturelock-open Commit is signed but in an unrecognized format.

Type,Value: mark `ResolveStrat` parameter of type queries as `comptime`

This eliminates the statically-reachable recursion loop between code generation backends and Sema. This is beneficial for optimizers (although I do not measure any performance improvement for this change), and for profilers.

3 files changed, 22 insertions(+), 22 deletions(-)

src/Type.zig+12-12
...@@ -478,7 +478,7 @@ pub fn hasRuntimeBitsAdvanced(...@@ -478,7 +478,7 @@ pub fn hasRuntimeBitsAdvanced(
478 ty: Type,478 ty: Type,
479 pt: Zcu.PerThread,479 pt: Zcu.PerThread,
480 ignore_comptime_only: bool,480 ignore_comptime_only: bool,
481 strat: ResolveStratLazy,481 comptime strat: ResolveStratLazy,
482) RuntimeBitsError!bool {482) RuntimeBitsError!bool {
483 const mod = pt.zcu;483 const mod = pt.zcu;
484 const ip = &mod.intern_pool;484 const ip = &mod.intern_pool;
...@@ -792,7 +792,7 @@ pub fn fnHasRuntimeBits(ty: Type, pt: Zcu.PerThread) bool {...@@ -792,7 +792,7 @@ pub fn fnHasRuntimeBits(ty: Type, pt: Zcu.PerThread) bool {
792/// Determines whether a function type has runtime bits, i.e. whether a792/// Determines whether a function type has runtime bits, i.e. whether a
793/// function with this type can exist at runtime.793/// function with this type can exist at runtime.
794/// Asserts that `ty` is a function type.794/// Asserts that `ty` is a function type.
795pub fn fnHasRuntimeBitsAdvanced(ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) SemaError!bool {795pub fn fnHasRuntimeBitsAdvanced(ty: Type, pt: Zcu.PerThread, comptime strat: ResolveStrat) SemaError!bool {
796 const fn_info = pt.zcu.typeToFunc(ty).?;796 const fn_info = pt.zcu.typeToFunc(ty).?;
797 if (fn_info.is_generic) return false;797 if (fn_info.is_generic) return false;
798 if (fn_info.is_var_args) return true;798 if (fn_info.is_var_args) return true;
...@@ -824,7 +824,7 @@ pub fn ptrAlignment(ty: Type, pt: Zcu.PerThread) Alignment {...@@ -824,7 +824,7 @@ pub fn ptrAlignment(ty: Type, pt: Zcu.PerThread) Alignment {
824 return ptrAlignmentAdvanced(ty, pt, .normal) catch unreachable;824 return ptrAlignmentAdvanced(ty, pt, .normal) catch unreachable;
825}825}
826826
827pub fn ptrAlignmentAdvanced(ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) !Alignment {827pub fn ptrAlignmentAdvanced(ty: Type, pt: Zcu.PerThread, comptime strat: ResolveStrat) !Alignment {
828 return switch (pt.zcu.intern_pool.indexToKey(ty.toIntern())) {828 return switch (pt.zcu.intern_pool.indexToKey(ty.toIntern())) {
829 .ptr_type => |ptr_type| {829 .ptr_type => |ptr_type| {
830 if (ptr_type.flags.alignment != .none)830 if (ptr_type.flags.alignment != .none)
...@@ -891,7 +891,7 @@ pub const ResolveStrat = enum {...@@ -891,7 +891,7 @@ pub const ResolveStrat = enum {
891 /// This should typically be used from semantic analysis.891 /// This should typically be used from semantic analysis.
892 sema,892 sema,
893893
894 pub fn toLazy(strat: ResolveStrat) ResolveStratLazy {894 pub inline fn toLazy(strat: ResolveStrat) ResolveStratLazy {
895 return switch (strat) {895 return switch (strat) {
896 .normal => .eager,896 .normal => .eager,
897 .sema => .sema,897 .sema => .sema,
...@@ -908,7 +908,7 @@ pub const ResolveStrat = enum {...@@ -908,7 +908,7 @@ pub const ResolveStrat = enum {
908pub fn abiAlignmentAdvanced(908pub fn abiAlignmentAdvanced(
909 ty: Type,909 ty: Type,
910 pt: Zcu.PerThread,910 pt: Zcu.PerThread,
911 strat: ResolveStratLazy,911 comptime strat: ResolveStratLazy,
912) SemaError!AbiAlignmentAdvanced {912) SemaError!AbiAlignmentAdvanced {
913 const mod = pt.zcu;913 const mod = pt.zcu;
914 const target = mod.getTarget();914 const target = mod.getTarget();
...@@ -1130,7 +1130,7 @@ pub fn abiAlignmentAdvanced(...@@ -1130,7 +1130,7 @@ pub fn abiAlignmentAdvanced(
1130fn abiAlignmentAdvancedErrorUnion(1130fn abiAlignmentAdvancedErrorUnion(
1131 ty: Type,1131 ty: Type,
1132 pt: Zcu.PerThread,1132 pt: Zcu.PerThread,
1133 strat: ResolveStratLazy,1133 comptime strat: ResolveStratLazy,
1134 payload_ty: Type,1134 payload_ty: Type,
1135) SemaError!AbiAlignmentAdvanced {1135) SemaError!AbiAlignmentAdvanced {
1136 // This code needs to be kept in sync with the equivalent switch prong1136 // This code needs to be kept in sync with the equivalent switch prong
...@@ -1167,7 +1167,7 @@ fn abiAlignmentAdvancedErrorUnion(...@@ -1167,7 +1167,7 @@ fn abiAlignmentAdvancedErrorUnion(
1167fn abiAlignmentAdvancedOptional(1167fn abiAlignmentAdvancedOptional(
1168 ty: Type,1168 ty: Type,
1169 pt: Zcu.PerThread,1169 pt: Zcu.PerThread,
1170 strat: ResolveStratLazy,1170 comptime strat: ResolveStratLazy,
1171) SemaError!AbiAlignmentAdvanced {1171) SemaError!AbiAlignmentAdvanced {
1172 const mod = pt.zcu;1172 const mod = pt.zcu;
1173 const target = mod.getTarget();1173 const target = mod.getTarget();
...@@ -1231,7 +1231,7 @@ const AbiSizeAdvanced = union(enum) {...@@ -1231,7 +1231,7 @@ const AbiSizeAdvanced = union(enum) {
1231pub fn abiSizeAdvanced(1231pub fn abiSizeAdvanced(
1232 ty: Type,1232 ty: Type,
1233 pt: Zcu.PerThread,1233 pt: Zcu.PerThread,
1234 strat: ResolveStratLazy,1234 comptime strat: ResolveStratLazy,
1235) SemaError!AbiSizeAdvanced {1235) SemaError!AbiSizeAdvanced {
1236 const mod = pt.zcu;1236 const mod = pt.zcu;
1237 const target = mod.getTarget();1237 const target = mod.getTarget();
...@@ -1505,7 +1505,7 @@ pub fn abiSizeAdvanced(...@@ -1505,7 +1505,7 @@ pub fn abiSizeAdvanced(
1505fn abiSizeAdvancedOptional(1505fn abiSizeAdvancedOptional(
1506 ty: Type,1506 ty: Type,
1507 pt: Zcu.PerThread,1507 pt: Zcu.PerThread,
1508 strat: ResolveStratLazy,1508 comptime strat: ResolveStratLazy,
1509) SemaError!AbiSizeAdvanced {1509) SemaError!AbiSizeAdvanced {
1510 const mod = pt.zcu;1510 const mod = pt.zcu;
1511 const child_ty = ty.optionalChild(mod);1511 const child_ty = ty.optionalChild(mod);
...@@ -1680,7 +1680,7 @@ pub fn bitSize(ty: Type, pt: Zcu.PerThread) u64 {...@@ -1680,7 +1680,7 @@ pub fn bitSize(ty: Type, pt: Zcu.PerThread) u64 {
1680pub fn bitSizeAdvanced(1680pub fn bitSizeAdvanced(
1681 ty: Type,1681 ty: Type,
1682 pt: Zcu.PerThread,1682 pt: Zcu.PerThread,
1683 strat: ResolveStrat,1683 comptime strat: ResolveStrat,
1684) SemaError!u64 {1684) SemaError!u64 {
1685 const mod = pt.zcu;1685 const mod = pt.zcu;
1686 const target = mod.getTarget();1686 const target = mod.getTarget();
...@@ -2739,7 +2739,7 @@ pub fn comptimeOnly(ty: Type, pt: Zcu.PerThread) bool {...@@ -2739,7 +2739,7 @@ pub fn comptimeOnly(ty: Type, pt: Zcu.PerThread) bool {
27392739
2740/// `generic_poison` will return false.2740/// `generic_poison` will return false.
2741/// May return false negatives when structs and unions are having their field types resolved.2741/// May return false negatives when structs and unions are having their field types resolved.
2742pub fn comptimeOnlyAdvanced(ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) SemaError!bool {2742pub fn comptimeOnlyAdvanced(ty: Type, pt: Zcu.PerThread, comptime strat: ResolveStrat) SemaError!bool {
2743 const mod = pt.zcu;2743 const mod = pt.zcu;
2744 const ip = &mod.intern_pool;2744 const ip = &mod.intern_pool;
2745 return switch (ty.toIntern()) {2745 return switch (ty.toIntern()) {
...@@ -3198,7 +3198,7 @@ pub fn structFieldAlign(ty: Type, index: usize, pt: Zcu.PerThread) Alignment {...@@ -3198,7 +3198,7 @@ pub fn structFieldAlign(ty: Type, index: usize, pt: Zcu.PerThread) Alignment {
3198 return ty.structFieldAlignAdvanced(index, pt, .normal) catch unreachable;3198 return ty.structFieldAlignAdvanced(index, pt, .normal) catch unreachable;
3199}3199}
32003200
3201pub fn structFieldAlignAdvanced(ty: Type, index: usize, pt: Zcu.PerThread, strat: ResolveStrat) !Alignment {3201pub fn structFieldAlignAdvanced(ty: Type, index: usize, pt: Zcu.PerThread, comptime strat: ResolveStrat) !Alignment {
3202 const ip = &pt.zcu.intern_pool;3202 const ip = &pt.zcu.intern_pool;
3203 switch (ip.indexToKey(ty.toIntern())) {3203 switch (ip.indexToKey(ty.toIntern())) {
3204 .struct_type => {3204 .struct_type => {
src/Value.zig+8-8
...@@ -178,7 +178,7 @@ pub fn toBigIntAdvanced(...@@ -178,7 +178,7 @@ pub fn toBigIntAdvanced(
178 val: Value,178 val: Value,
179 space: *BigIntSpace,179 space: *BigIntSpace,
180 pt: Zcu.PerThread,180 pt: Zcu.PerThread,
181 strat: ResolveStrat,181 comptime strat: ResolveStrat,
182) Module.CompileError!BigIntConst {182) Module.CompileError!BigIntConst {
183 return switch (val.toIntern()) {183 return switch (val.toIntern()) {
184 .bool_false => BigIntMutable.init(&space.limbs, 0).toConst(),184 .bool_false => BigIntMutable.init(&space.limbs, 0).toConst(),
...@@ -240,7 +240,7 @@ pub fn getUnsignedInt(val: Value, pt: Zcu.PerThread) ?u64 {...@@ -240,7 +240,7 @@ pub fn getUnsignedInt(val: Value, pt: Zcu.PerThread) ?u64 {
240240
241/// If the value fits in a u64, return it, otherwise null.241/// If the value fits in a u64, return it, otherwise null.
242/// Asserts not undefined.242/// Asserts not undefined.
243pub fn getUnsignedIntAdvanced(val: Value, pt: Zcu.PerThread, strat: ResolveStrat) !?u64 {243pub fn getUnsignedIntAdvanced(val: Value, pt: Zcu.PerThread, comptime strat: ResolveStrat) !?u64 {
244 const mod = pt.zcu;244 const mod = pt.zcu;
245 return switch (val.toIntern()) {245 return switch (val.toIntern()) {
246 .undef => unreachable,246 .undef => unreachable,
...@@ -1042,7 +1042,7 @@ pub fn orderAgainstZero(lhs: Value, pt: Zcu.PerThread) std.math.Order {...@@ -1042,7 +1042,7 @@ pub fn orderAgainstZero(lhs: Value, pt: Zcu.PerThread) std.math.Order {
1042pub fn orderAgainstZeroAdvanced(1042pub fn orderAgainstZeroAdvanced(
1043 lhs: Value,1043 lhs: Value,
1044 pt: Zcu.PerThread,1044 pt: Zcu.PerThread,
1045 strat: ResolveStrat,1045 comptime strat: ResolveStrat,
1046) Module.CompileError!std.math.Order {1046) Module.CompileError!std.math.Order {
1047 return switch (lhs.toIntern()) {1047 return switch (lhs.toIntern()) {
1048 .bool_false => .eq,1048 .bool_false => .eq,
...@@ -1081,7 +1081,7 @@ pub fn order(lhs: Value, rhs: Value, pt: Zcu.PerThread) std.math.Order {...@@ -1081,7 +1081,7 @@ pub fn order(lhs: Value, rhs: Value, pt: Zcu.PerThread) std.math.Order {
1081}1081}
10821082
1083/// Asserts the value is comparable.1083/// Asserts the value is comparable.
1084pub fn orderAdvanced(lhs: Value, rhs: Value, pt: Zcu.PerThread, strat: ResolveStrat) !std.math.Order {1084pub fn orderAdvanced(lhs: Value, rhs: Value, pt: Zcu.PerThread, comptime strat: ResolveStrat) !std.math.Order {
1085 const lhs_against_zero = try lhs.orderAgainstZeroAdvanced(pt, strat);1085 const lhs_against_zero = try lhs.orderAgainstZeroAdvanced(pt, strat);
1086 const rhs_against_zero = try rhs.orderAgainstZeroAdvanced(pt, strat);1086 const rhs_against_zero = try rhs.orderAgainstZeroAdvanced(pt, strat);
1087 switch (lhs_against_zero) {1087 switch (lhs_against_zero) {
...@@ -1119,7 +1119,7 @@ pub fn compareHeteroAdvanced(...@@ -1119,7 +1119,7 @@ pub fn compareHeteroAdvanced(
1119 op: std.math.CompareOperator,1119 op: std.math.CompareOperator,
1120 rhs: Value,1120 rhs: Value,
1121 pt: Zcu.PerThread,1121 pt: Zcu.PerThread,
1122 strat: ResolveStrat,1122 comptime strat: ResolveStrat,
1123) !bool {1123) !bool {
1124 if (lhs.pointerDecl(pt.zcu)) |lhs_decl| {1124 if (lhs.pointerDecl(pt.zcu)) |lhs_decl| {
1125 if (rhs.pointerDecl(pt.zcu)) |rhs_decl| {1125 if (rhs.pointerDecl(pt.zcu)) |rhs_decl| {
...@@ -1199,7 +1199,7 @@ pub fn compareAllWithZeroAdvancedExtra(...@@ -1199,7 +1199,7 @@ pub fn compareAllWithZeroAdvancedExtra(
1199 lhs: Value,1199 lhs: Value,
1200 op: std.math.CompareOperator,1200 op: std.math.CompareOperator,
1201 pt: Zcu.PerThread,1201 pt: Zcu.PerThread,
1202 strat: ResolveStrat,1202 comptime strat: ResolveStrat,
1203) Module.CompileError!bool {1203) Module.CompileError!bool {
1204 const mod = pt.zcu;1204 const mod = pt.zcu;
1205 if (lhs.isInf(mod)) {1205 if (lhs.isInf(mod)) {
...@@ -1505,7 +1505,7 @@ pub fn floatFromIntAdvanced(...@@ -1505,7 +1505,7 @@ pub fn floatFromIntAdvanced(
1505 int_ty: Type,1505 int_ty: Type,
1506 float_ty: Type,1506 float_ty: Type,
1507 pt: Zcu.PerThread,1507 pt: Zcu.PerThread,
1508 strat: ResolveStrat,1508 comptime strat: ResolveStrat,
1509) !Value {1509) !Value {
1510 const mod = pt.zcu;1510 const mod = pt.zcu;
1511 if (int_ty.zigTypeTag(mod) == .Vector) {1511 if (int_ty.zigTypeTag(mod) == .Vector) {
...@@ -1523,7 +1523,7 @@ pub fn floatFromIntAdvanced(...@@ -1523,7 +1523,7 @@ pub fn floatFromIntAdvanced(
1523 return floatFromIntScalar(val, float_ty, pt, strat);1523 return floatFromIntScalar(val, float_ty, pt, strat);
1524}1524}
15251525
1526pub fn floatFromIntScalar(val: Value, float_ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) !Value {1526pub fn floatFromIntScalar(val: Value, float_ty: Type, pt: Zcu.PerThread, comptime strat: ResolveStrat) !Value {
1527 const mod = pt.zcu;1527 const mod = pt.zcu;
1528 return switch (mod.intern_pool.indexToKey(val.toIntern())) {1528 return switch (mod.intern_pool.indexToKey(val.toIntern())) {
1529 .undef => try pt.undefValue(float_ty),1529 .undef => try pt.undefValue(float_ty),
src/Zcu/PerThread.zig+2-2
...@@ -3145,7 +3145,7 @@ pub fn unionFieldNormalAlignmentAdvanced(...@@ -3145,7 +3145,7 @@ pub fn unionFieldNormalAlignmentAdvanced(
3145 pt: Zcu.PerThread,3145 pt: Zcu.PerThread,
3146 loaded_union: InternPool.LoadedUnionType,3146 loaded_union: InternPool.LoadedUnionType,
3147 field_index: u32,3147 field_index: u32,
3148 strat: Type.ResolveStrat,3148 comptime strat: Type.ResolveStrat,
3149) Zcu.SemaError!InternPool.Alignment {3149) Zcu.SemaError!InternPool.Alignment {
3150 const ip = &pt.zcu.intern_pool;3150 const ip = &pt.zcu.intern_pool;
3151 assert(loaded_union.flagsUnordered(ip).layout != .@"packed");3151 assert(loaded_union.flagsUnordered(ip).layout != .@"packed");
...@@ -3173,7 +3173,7 @@ pub fn structFieldAlignmentAdvanced(...@@ -3173,7 +3173,7 @@ pub fn structFieldAlignmentAdvanced(
3173 explicit_alignment: InternPool.Alignment,3173 explicit_alignment: InternPool.Alignment,
3174 field_ty: Type,3174 field_ty: Type,
3175 layout: std.builtin.Type.ContainerLayout,3175 layout: std.builtin.Type.ContainerLayout,
3176 strat: Type.ResolveStrat,3176 comptime strat: Type.ResolveStrat,
3177) Zcu.SemaError!InternPool.Alignment {3177) Zcu.SemaError!InternPool.Alignment {
3178 assert(layout != .@"packed");3178 assert(layout != .@"packed");
3179 if (explicit_alignment != .none) return explicit_alignment;3179 if (explicit_alignment != .none) return explicit_alignment;