authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-25 18:58:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-26 12:35:14-07:00
log70746d580c7096938446eb47f57242edf17a9caf
tree61ae97613d26e33f51527fd8c9eeaf60f0e39820
parentb66cc5af41aa0cc90996a75468a84ca83b46c1fd

better codegen for `@panic` with comptime-known operand


1 files changed, 17 insertions(+), 14 deletions(-)

src/Sema.zig+17-14
......@@ -7320,10 +7320,7 @@ fn callPanic(
73207320 return;
73217321 }
73227322 const panic_cause_ty = try pt.getBuiltinType("PanicCause");
7323 const panic_cause = if (payload == .void_value)
7324 try initUnionFromEnumTag(pt, panic_cause_ty, panic_cause_ty.unionTagType(zcu).?, @intFromEnum(tag))
7325 else
7326 try block.addUnionInit(panic_cause_ty, @intFromEnum(tag), payload);
7323 const panic_cause = try unionInitFromEnumTag(sema, block, call_src, panic_cause_ty, @intFromEnum(tag), payload);
73277324 const panic_fn = try pt.getBuiltin("panic");
73287325 const err_return_trace = try sema.getErrorReturnTrace(block);
73297326 const opt_usize_ty = try pt.optionalType(.usize_type);
......@@ -18311,7 +18308,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1831118308 .undefined,
1831218309 .null,
1831318310 .enum_literal,
18314 => |type_info_tag| return initUnionFromEnumTag(pt, type_info_ty, type_info_tag_ty, @intFromEnum(type_info_tag)),
18311 => |type_info_tag| return unionInitFromEnumTag(sema, block, src, type_info_ty, @intFromEnum(type_info_tag), .void_value),
18312
1831518313 .@"fn" => {
1831618314 const fn_info_ty = try getInnerType(sema, block, src, type_info_ty, "Fn");
1831718315 const param_info_ty = try getInnerType(sema, block, src, fn_info_ty, "Param");
......@@ -20607,6 +20605,20 @@ fn unionInit(
2060720605 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_src);
2060820606 const field_ty = Type.fromInterned(zcu.typeToUnion(union_ty).?.field_types.get(ip)[field_index]);
2060920607 const init = try sema.coerce(block, field_ty, uncasted_init, init_src);
20608 _ = union_ty_src;
20609 return unionInitFromEnumTag(sema, block, init_src, union_ty, field_index, init);
20610}
20611
20612fn unionInitFromEnumTag(
20613 sema: *Sema,
20614 block: *Block,
20615 init_src: LazySrcLoc,
20616 union_ty: Type,
20617 field_index: u32,
20618 init: Air.Inst.Ref,
20619) !Air.Inst.Ref {
20620 const pt = sema.pt;
20621 const zcu = pt.zcu;
2061020622
2061120623 if (try sema.resolveValue(init)) |init_val| {
2061220624 const tag_ty = union_ty.unionTagTypeHypothetical(zcu);
......@@ -20619,7 +20631,6 @@ fn unionInit(
2061920631 }
2062020632
2062120633 try sema.requireRuntimeBlock(block, init_src, null);
20622 _ = union_ty_src;
2062320634 return block.addUnionInit(union_ty, field_index, init);
2062420635}
2062520636
......@@ -38929,11 +38940,3 @@ fn getInnerType(
3892938940 try sema.ensureNavResolved(src, nav);
3893038941 return Type.fromInterned(ip.getNav(nav).status.resolved.val);
3893138942}
38932
38933fn initUnionFromEnumTag(pt: Zcu.PerThread, union_ty: Type, union_tag_ty: Type, field_index: u32) !Air.Inst.Ref {
38934 return Air.internedToRef((try pt.internUnion(.{
38935 .ty = union_ty.toIntern(),
38936 .tag = (try pt.enumValueFieldIndex(union_tag_ty, field_index)).toIntern(),
38937 .val = .void_value,
38938 })));
38939}