Merge pull request #17691 from mlugg/no-interned-runtime-value

Remove `InternPool.Key.runtime_value`, clean up Sema value resolution functions
This commit is contained in:
Andrew Kelley 2023-10-24 14:57:55 -04:00 committed by GitHub
commit 22a6a5d93f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 333 additions and 504 deletions

View File

@ -3666,7 +3666,13 @@ fn ReverseIterator(comptime T: type) type {
@compileError("expected slice or pointer to array, found '" ++ @typeName(T) ++ "'");
};
const Element = std.meta.Elem(Pointer);
const ElementPointer = @TypeOf(&@as(Pointer, undefined)[0]);
const ElementPointer = @Type(.{ .Pointer = ptr: {
var ptr = @typeInfo(Pointer).Pointer;
ptr.size = .One;
ptr.child = Element;
ptr.sentinel = null;
break :ptr ptr;
} });
return struct {
ptr: Pointer,
index: usize,

View File

@ -237,7 +237,6 @@ pub const Key = union(enum) {
/// Typed `undefined`. This will never be `none`; untyped `undefined` is represented
/// via `simple_value` and has a named `Index` tag for it.
undef: Index,
runtime_value: TypeValue,
simple_value: SimpleValue,
variable: Variable,
extern_func: ExternFunc,
@ -1181,8 +1180,6 @@ pub const Key = union(enum) {
.payload => |y| Hash.hash(seed + 1, asBytes(&x.ty) ++ asBytes(&y)),
},
.runtime_value => |x| Hash.hash(seed, asBytes(&x.val)),
inline .opaque_type,
.enum_type,
.variable,
@ -1413,10 +1410,6 @@ pub const Key = union(enum) {
const b_info = b.undef;
return a_info == b_info;
},
.runtime_value => |a_info| {
const b_info = b.runtime_value;
return a_info.val == b_info.val;
},
.opt => |a_info| {
const b_info = b.opt;
return std.meta.eql(a_info, b_info);
@ -1703,8 +1696,7 @@ pub const Key = union(enum) {
.func_type,
=> .type_type,
inline .runtime_value,
.ptr,
inline .ptr,
.int,
.float,
.opt,
@ -2138,7 +2130,6 @@ pub const Index = enum(u32) {
},
undef: DataIsIndex,
runtime_value: struct { data: *Tag.TypeValue },
simple_value: struct { data: SimpleValue },
ptr_decl: struct { data: *PtrDecl },
ptr_mut_decl: struct { data: *PtrMutDecl },
@ -2580,10 +2571,6 @@ pub const Tag = enum(u8) {
/// `data` is `Index` of the type.
/// Untyped `undefined` is stored instead via `simple_value`.
undef,
/// A wrapper for values which are comptime-known but should
/// semantically be runtime-known.
/// data is extra index of `TypeValue`.
runtime_value,
/// A value that can be represented with only an enum tag.
/// data is SimpleValue enum value.
simple_value,
@ -2795,7 +2782,6 @@ pub const Tag = enum(u8) {
.type_function => TypeFunction,
.undef => unreachable,
.runtime_value => TypeValue,
.simple_value => unreachable,
.ptr_decl => PtrDecl,
.ptr_mut_decl => PtrMutDecl,
@ -3730,7 +3716,6 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
.type_function => .{ .func_type = ip.extraFuncType(data) },
.undef => .{ .undef = @as(Index, @enumFromInt(data)) },
.runtime_value => .{ .runtime_value = ip.extraData(Tag.TypeValue, data) },
.opt_null => .{ .opt = .{
.ty = @as(Index, @enumFromInt(data)),
.val = .none,
@ -4556,13 +4541,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
.data = @intFromEnum(ty),
});
},
.runtime_value => |runtime_value| {
assert(runtime_value.ty == ip.typeOf(runtime_value.val));
ip.items.appendAssumeCapacity(.{
.tag = .runtime_value,
.data = try ip.addExtra(gpa, runtime_value),
});
},
.struct_type => unreachable, // use getStructType() instead
.anon_struct_type => unreachable, // use getAnonStructType() instead
@ -7237,7 +7215,6 @@ fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
},
.undef => 0,
.runtime_value => @sizeOf(Tag.TypeValue),
.simple_type => 0,
.simple_value => 0,
.ptr_decl => @sizeOf(PtrDecl),
@ -7370,7 +7347,6 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void {
.type_union,
.type_function,
.undef,
.runtime_value,
.ptr_decl,
.ptr_mut_decl,
.ptr_anon_decl,
@ -7793,7 +7769,6 @@ pub fn typeOf(ip: *const InternPool, index: Index) Index {
.ptr_slice,
.opt_payload,
.error_union_payload,
.runtime_value,
.int_small,
.int_lazy_align,
.int_lazy_size,
@ -7906,10 +7881,6 @@ pub fn isUndef(ip: *const InternPool, val: Index) bool {
return val == .undef or ip.items.items(.tag)[@intFromEnum(val)] == .undef;
}
pub fn isRuntimeValue(ip: *const InternPool, val: Index) bool {
return ip.items.items(.tag)[@intFromEnum(val)] == .runtime_value;
}
pub fn isVariable(ip: *const InternPool, val: Index) bool {
return ip.items.items(.tag)[@intFromEnum(val)] == .variable;
}
@ -8116,7 +8087,6 @@ pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPois
// values, not types
.undef,
.runtime_value,
.simple_value,
.ptr_decl,
.ptr_mut_decl,

View File

@ -3757,7 +3757,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };
const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };
const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, .{
const decl_tv = try sema.resolveInstValueAllowVariables(&block_scope, init_src, result_ref, .{
.needed_comptime_reason = "global variable initializer must be comptime-known",
});

File diff suppressed because it is too large Load Diff

View File

@ -206,7 +206,6 @@ pub fn print(
.inferred_error_set_type,
=> return Type.print(val.toType(), writer, mod),
.undef => return writer.writeAll("undefined"),
.runtime_value => return writer.writeAll("(runtime value)"),
.simple_value => |simple_value| switch (simple_value) {
.void => return writer.writeAll("{}"),
.empty_struct => return printAggregate(ty, val, writer, level, mod),

View File

@ -3224,16 +3224,11 @@ fn toTwosComplement(value: anytype, bits: u7) std.meta.Int(.unsigned, @typeInfo(
/// This function is intended to assert that `isByRef` returns `false` for `ty`.
/// However such an assertion fails on the behavior tests currently.
fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
const mod = func.bin_file.base.options.module.?;
// TODO: enable this assertion
//assert(!isByRef(ty, mod));
const ip = &mod.intern_pool;
var val = arg_val;
switch (ip.indexToKey(val.ip_index)) {
.runtime_value => |rt| val = rt.val.toValue(),
else => {},
}
if (val.isUndefDeep(mod)) return func.emitUndefined(ty);
switch (ip.indexToKey(val.ip_index)) {
@ -3255,7 +3250,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
.inferred_error_set_type,
=> unreachable, // types, not values
.undef, .runtime_value => unreachable, // handled above
.undef => unreachable, // handled above
.simple_value => |simple_value| switch (simple_value) {
.undefined,
.void,

View File

@ -167,11 +167,7 @@ pub fn generateSymbol(
const mod = bin_file.options.module.?;
const ip = &mod.intern_pool;
var typed_value = arg_tv;
switch (ip.indexToKey(typed_value.val.toIntern())) {
.runtime_value => |rt| typed_value.val = rt.val.toValue(),
else => {},
}
const typed_value = arg_tv;
const target = mod.getTarget();
const endian = target.cpu.arch.endian();
@ -206,7 +202,7 @@ pub fn generateSymbol(
.inferred_error_set_type,
=> unreachable, // types, not values
.undef, .runtime_value => unreachable, // handled above
.undef => unreachable, // handled above
.simple_value => |simple_value| switch (simple_value) {
.undefined,
.void,
@ -970,11 +966,7 @@ pub fn genTypedValue(
owner_decl_index: Module.Decl.Index,
) CodeGenError!GenResult {
const mod = bin_file.options.module.?;
var typed_value = arg_tv;
switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
.runtime_value => |rt| typed_value.val = rt.val.toValue(),
else => {},
}
const typed_value = arg_tv;
log.debug("genTypedValue: ty = {}, val = {}", .{
typed_value.ty.fmt(mod),

View File

@ -772,17 +772,12 @@ pub const DeclGen = struct {
dg: *DeclGen,
writer: anytype,
ty: Type,
arg_val: Value,
val: Value,
location: ValueRenderLocation,
) error{ OutOfMemory, AnalysisFail }!void {
const mod = dg.module;
const ip = &mod.intern_pool;
var val = arg_val;
switch (ip.indexToKey(val.ip_index)) {
.runtime_value => |rt| val = rt.val.toValue(),
else => {},
}
const target = mod.getTarget();
const initializer_type: ValueRenderLocation = switch (location) {
.StaticInitializer => .StaticInitializer,
@ -1005,7 +1000,7 @@ pub const DeclGen = struct {
.memoized_call,
=> unreachable,
.undef, .runtime_value => unreachable, // handled above
.undef => unreachable, // handled above
.simple_value => |simple_value| switch (simple_value) {
// non-runtime values
.undefined => unreachable,

View File

@ -3560,7 +3560,6 @@ pub const Object = struct {
.error_set_type, .inferred_error_set_type => try o.errorIntType(),
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -3672,17 +3671,13 @@ pub const Object = struct {
const ip = &mod.intern_pool;
const target = mod.getTarget();
var val = arg_val.toValue();
const arg_val_key = ip.indexToKey(arg_val);
switch (arg_val_key) {
.runtime_value => |rt| val = rt.val.toValue(),
else => {},
}
const val = arg_val.toValue();
const val_key = ip.indexToKey(val.toIntern());
if (val.isUndefDeep(mod)) {
return o.builder.undefConst(try o.lowerType(arg_val_key.typeOf().toType()));
return o.builder.undefConst(try o.lowerType(val_key.typeOf().toType()));
}
const val_key = ip.indexToKey(val.toIntern());
const ty = val_key.typeOf().toType();
return switch (val_key) {
.int_type,
@ -3703,7 +3698,7 @@ pub const Object = struct {
.inferred_error_set_type,
=> unreachable, // types, not values
.undef, .runtime_value => unreachable, // handled above
.undef => unreachable, // handled above
.simple_value => |simple_value| switch (simple_value) {
.undefined,
.void,

View File

@ -644,11 +644,7 @@ const DeclGen = struct {
const result_ty_ref = try self.resolveType(ty, repr);
const ip = &mod.intern_pool;
var val = arg_val;
switch (ip.indexToKey(val.toIntern())) {
.runtime_value => |rt| val = rt.val.toValue(),
else => {},
}
const val = arg_val;
log.debug("constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(ty, mod) });
if (val.isUndefDeep(mod)) {
@ -674,7 +670,7 @@ const DeclGen = struct {
.inferred_error_set_type,
=> unreachable, // types, not values
.undef, .runtime_value => unreachable, // handled above
.undef => unreachable, // handled above
.variable,
.extern_func,

View File

@ -414,7 +414,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -633,7 +632,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -741,7 +739,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -1103,7 +1100,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -1461,7 +1457,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -1681,7 +1676,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -2217,7 +2211,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -2560,7 +2553,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -2754,7 +2746,6 @@ pub const Type = struct {
// values, not types
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,

View File

@ -364,7 +364,6 @@ pub const Value = struct {
.inferred_error_set_type,
.undef,
.runtime_value,
.simple_value,
.variable,
.extern_func,
@ -464,7 +463,6 @@ pub const Value = struct {
.bool_true => BigIntMutable.init(&space.limbs, 1).toConst(),
.null_value => BigIntMutable.init(&space.limbs, 0).toConst(),
else => switch (mod.intern_pool.indexToKey(val.toIntern())) {
.runtime_value => |runtime_value| runtime_value.val.toValue().toBigIntAdvanced(space, mod, opt_sema),
.int => |int| switch (int.storage) {
.u64, .i64, .big_int => int.storage.toBigInt(space),
.lazy_align, .lazy_size => |ty| {
@ -1657,34 +1655,6 @@ pub const Value = struct {
};
}
pub fn isRuntimeValue(val: Value, mod: *Module) bool {
return mod.intern_pool.isRuntimeValue(val.toIntern());
}
/// Returns true if a Value is backed by a variable
pub fn isVariable(val: Value, mod: *Module) bool {
return val.ip_index != .none and switch (mod.intern_pool.indexToKey(val.toIntern())) {
.variable => true,
.ptr => |ptr| switch (ptr.addr) {
.decl => |decl_index| {
const decl = mod.declPtr(decl_index);
assert(decl.has_tv);
return decl.val.isVariable(mod);
},
.mut_decl => |mut_decl| {
const decl = mod.declPtr(mut_decl.decl);
assert(decl.has_tv);
return decl.val.isVariable(mod);
},
.int => false,
.eu_payload, .opt_payload => |base_ptr| base_ptr.toValue().isVariable(mod),
.comptime_field => |comptime_field| comptime_field.toValue().isVariable(mod),
.elem, .field => |base_index| base_index.base.toValue().isVariable(mod),
},
else => false,
};
}
pub fn isPtrToThreadLocal(val: Value, mod: *Module) bool {
const backing_decl = mod.intern_pool.getBackingDecl(val.toIntern()).unwrap() orelse return false;
const variable = mod.declPtr(backing_decl).getOwnedVariable(mod) orelse return false;