authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-05 14:57:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:42:28-07:00
log2f05b1482a6f62658cc70c252d48a24a40404aa8
tree17384a9a13b9cb993918ded8c83b8d7a123e6b47
parentac07ddadeb36019c262b4f28a4fa3884f6f50b32

Sema: update core comptime detection logic to be InternPool aware

* Add some assertions to make sure instructions are not none. I tested all these with master branch as well and made sure the behavior tests still passed with the assertions intact (along with a handful of callsite updates). * Fix Sema.resolveMaybeUndefValAllowVariablesMaybeRuntime not noticing that interned values are comptime-known. This was causing all kinds of chaos. * Fix print_air writeType calling tag() without checking for ip_index

6 files changed, 47 insertions(+), 28 deletions(-)

src/Air.zig+11-5
......@@ -925,7 +925,7 @@ pub const Inst = struct {
925925
926926 /// This Ref does not correspond to any AIR instruction or constant
927927 /// value and may instead be used as a sentinel to indicate null.
928 none = std.math.maxInt(u32),
928 none = @enumToInt(InternPool.Index.none),
929929 _,
930930 };
931931
......@@ -1461,11 +1461,12 @@ pub fn deinit(air: *Air, gpa: std.mem.Allocator) void {
14611461
14621462pub const ref_start_index: u32 = InternPool.static_len;
14631463
1464pub fn indexToRef(inst: Air.Inst.Index) Air.Inst.Ref {
1465 return @intToEnum(Air.Inst.Ref, ref_start_index + inst);
1464pub fn indexToRef(inst: Inst.Index) Inst.Ref {
1465 return @intToEnum(Inst.Ref, ref_start_index + inst);
14661466}
14671467
1468pub fn refToIndex(inst: Air.Inst.Ref) ?Air.Inst.Index {
1468pub fn refToIndex(inst: Inst.Ref) ?Inst.Index {
1469 assert(inst != .none);
14691470 const ref_int = @enumToInt(inst);
14701471 if (ref_int >= ref_start_index) {
14711472 return ref_int - ref_start_index;
......@@ -1474,8 +1475,13 @@ pub fn refToIndex(inst: Air.Inst.Ref) ?Air.Inst.Index {
14741475 }
14751476}
14761477
1478pub fn refToIndexAllowNone(inst: Inst.Ref) ?Inst.Index {
1479 if (inst == .none) return null;
1480 return refToIndex(inst);
1481}
1482
14771483/// Returns `null` if runtime-known.
1478pub fn value(air: Air, inst: Air.Inst.Ref, mod: *const Module) ?Value {
1484pub fn value(air: Air, inst: Inst.Ref, mod: *const Module) ?Value {
14791485 const ref_int = @enumToInt(inst);
14801486 if (ref_int < ref_start_index) {
14811487 const ip_index = @intToEnum(InternPool.Index, ref_int);
src/Liveness.zig+2-2
......@@ -1268,7 +1268,7 @@ fn analyzeOperands(
12681268 _ = data.live_set.remove(inst);
12691269
12701270 for (operands) |op_ref| {
1271 const operand = Air.refToIndex(op_ref) orelse continue;
1271 const operand = Air.refToIndexAllowNone(op_ref) orelse continue;
12721272
12731273 // Don't compute any liveness for constants
12741274 switch (inst_tags[operand]) {
......@@ -1304,7 +1304,7 @@ fn analyzeOperands(
13041304 while (i > 0) {
13051305 i -= 1;
13061306 const op_ref = operands[i];
1307 const operand = Air.refToIndex(op_ref) orelse continue;
1307 const operand = Air.refToIndexAllowNone(op_ref) orelse continue;
13081308
13091309 // Don't compute any liveness for constants
13101310 switch (inst_tags[operand]) {
src/Liveness/Verify.zig+1-1
......@@ -555,7 +555,7 @@ fn verifyDeath(self: *Verify, inst: Air.Inst.Index, operand: Air.Inst.Index) Err
555555}
556556
557557fn verifyOperand(self: *Verify, inst: Air.Inst.Index, op_ref: Air.Inst.Ref, dies: bool) Error!void {
558 const operand = Air.refToIndex(op_ref) orelse return;
558 const operand = Air.refToIndexAllowNone(op_ref) orelse return;
559559 switch (self.air.instructions.items(.tag)[operand]) {
560560 .constant, .const_ty, .interned => {},
561561 else => {
src/Sema.zig+19-14
......@@ -968,7 +968,7 @@ fn analyzeBodyInner(
968968 .int_big => try sema.zirIntBig(block, inst),
969969 .float => try sema.zirFloat(block, inst),
970970 .float128 => try sema.zirFloat128(block, inst),
971 .int_type => try sema.zirIntType(block, inst),
971 .int_type => try sema.zirIntType(inst),
972972 .is_non_err => try sema.zirIsNonErr(block, inst),
973973 .is_non_err_ptr => try sema.zirIsNonErrPtr(block, inst),
974974 .ret_is_non_err => try sema.zirRetIsNonErr(block, inst),
......@@ -1694,7 +1694,7 @@ fn analyzeBodyInner(
16941694 const extra = sema.code.extraData(Zir.Inst.DeferErrCode, inst_data.payload_index).data;
16951695 const defer_body = sema.code.extra[extra.index..][0..extra.len];
16961696 const err_code = try sema.resolveInst(inst_data.err_code);
1697 sema.inst_map.putAssumeCapacity(extra.remapped_err_code, err_code);
1697 map.putAssumeCapacity(extra.remapped_err_code, err_code);
16981698 const break_inst = sema.analyzeBodyInner(block, defer_body) catch |err| switch (err) {
16991699 error.ComptimeBreak => sema.comptime_break_inst,
17001700 else => |e| return e,
......@@ -1730,7 +1730,16 @@ fn analyzeBodyInner(
17301730 return result;
17311731}
17321732
1733pub fn resolveInstAllowNone(sema: *Sema, zir_ref: Zir.Inst.Ref) !Air.Inst.Ref {
1734 if (zir_ref == .none) {
1735 return .none;
1736 } else {
1737 return resolveInst(sema, zir_ref);
1738 }
1739}
1740
17331741pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) !Air.Inst.Ref {
1742 assert(zir_ref != .none);
17341743 const i = @enumToInt(zir_ref);
17351744 // First section of indexes correspond to a set number of constant values.
17361745 // We intentionally map the same indexes to the same values between ZIR and AIR.
......@@ -1969,6 +1978,7 @@ fn resolveMaybeUndefValAllowVariablesMaybeRuntime(
19691978 inst: Air.Inst.Ref,
19701979 make_runtime: *bool,
19711980) CompileError!?Value {
1981 assert(inst != .none);
19721982 // First section of indexes correspond to a set number of constant values.
19731983 const int = @enumToInt(inst);
19741984 if (int < InternPool.static_len) {
......@@ -1985,17 +1995,17 @@ fn resolveMaybeUndefValAllowVariablesMaybeRuntime(
19851995 }
19861996 return opv;
19871997 }
1998 const air_datas = sema.air_instructions.items(.data);
19881999 switch (air_tags[i]) {
19892000 .constant => {
1990 const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
2001 const ty_pl = air_datas[i].ty_pl;
19912002 const val = sema.air_values.items[ty_pl.payload];
19922003 if (val.tag() == .runtime_value) make_runtime.* = true;
19932004 if (val.isPtrToThreadLocal(sema.mod)) make_runtime.* = true;
19942005 return val;
19952006 },
1996 .const_ty => {
1997 return try sema.air_instructions.items(.data)[i].ty.toValue(sema.arena);
1998 },
2007 .const_ty => return try air_datas[i].ty.toValue(sema.arena),
2008 .interned => return air_datas[i].interned.toValue(),
19992009 else => return null,
20002010 }
20012011}
......@@ -7913,15 +7923,10 @@ fn emitDbgInline(
79137923 });
79147924}
79157925
7916fn zirIntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7917 _ = block;
7918 const tracy = trace(@src());
7919 defer tracy.end();
7920
7926fn zirIntType(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
79217927 const mod = sema.mod;
79227928 const int_type = sema.code.instructions.items(.data)[inst].int_type;
79237929 const ty = try mod.intType(int_type.signedness, int_type.bit_count);
7924
79257930 return sema.addType(ty);
79267931}
79277932
......@@ -17509,7 +17514,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1750917514 const tracy = trace(@src());
1751017515 defer tracy.end();
1751117516
17512 const saved_index = if (Zir.refToIndex(inst_data.block)) |zir_block| b: {
17517 const saved_index = if (Zir.refToIndexAllowNone(inst_data.block)) |zir_block| b: {
1751317518 var block = start_block;
1751417519 while (true) {
1751517520 if (block.label) |label| {
......@@ -17535,7 +17540,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1753517540
1753617541 assert(saved_index != .none); // The .error_return_trace_index field was dropped somewhere
1753717542
17538 const operand = try sema.resolveInst(inst_data.operand);
17543 const operand = try sema.resolveInstAllowNone(inst_data.operand);
1753917544 return sema.popErrorReturnTrace(start_block, src, operand, saved_index);
1754017545}
1754117546
src/Zir.zig+8-2
......@@ -2132,7 +2132,7 @@ pub const Inst = struct {
21322132
21332133 /// This Ref does not correspond to any ZIR instruction or constant
21342134 /// value and may instead be used as a sentinel to indicate null.
2135 none = std.math.maxInt(u32),
2135 none = @enumToInt(InternPool.Index.none),
21362136 _,
21372137 };
21382138
......@@ -3814,13 +3814,14 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
38143814 };
38153815}
38163816
3817const ref_start_index: u32 = InternPool.static_len;
3817pub const ref_start_index: u32 = InternPool.static_len;
38183818
38193819pub fn indexToRef(inst: Inst.Index) Inst.Ref {
38203820 return @intToEnum(Inst.Ref, ref_start_index + inst);
38213821}
38223822
38233823pub fn refToIndex(inst: Inst.Ref) ?Inst.Index {
3824 assert(inst != .none);
38243825 const ref_int = @enumToInt(inst);
38253826 if (ref_int >= ref_start_index) {
38263827 return ref_int - ref_start_index;
......@@ -3828,3 +3829,8 @@ pub fn refToIndex(inst: Inst.Ref) ?Inst.Index {
38283829 return null;
38293830 }
38303831}
3832
3833pub fn refToIndexAllowNone(inst: Inst.Ref) ?Inst.Index {
3834 if (inst == .none) return null;
3835 return refToIndex(inst);
3836}
src/print_air.zig+6-4
......@@ -366,10 +366,12 @@ const Writer = struct {
366366 }
367367
368368 fn writeType(w: *Writer, s: anytype, ty: Type) !void {
369 const t = ty.tag();
370 switch (t) {
371 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),
372 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),
369 switch (ty.ip_index) {
370 .none => switch (ty.tag()) {
371 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),
372 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),
373 else => try ty.print(s, w.module),
374 },
373375 else => try ty.print(s, w.module),
374376 }
375377 }