authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2023-07-06 19:47:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-12 15:35:57-07:00
log4bce7b1db964098e4a9163201fa3adcb26af6d97
tree5ba1b96590d1ef9ea4c6b72eaccef36dc4e9c58d
parentd78517f4f0f540f0d0254a78e47a7b4a30e98c74

Remove len parameter from splat builtin function

Resolve the result type of the splat builtin instead of requiring a length parameter.

3 files changed, 35 insertions(+), 29 deletions(-)

src/AstGen.zig+10-3
......@@ -8591,10 +8591,17 @@ fn builtinCall(
85918591 },
85928592
85938593 .splat => {
8594 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
8595 const scalar = try expr(gz, scope, .{ .rl = .none }, params[1]);
8594 const result_type = try ri.rl.resultType(gz, node, "@splat");
8595 const elem_type = try gz.add(.{
8596 .tag = .elem_type_index,
8597 .data = .{ .bin = .{
8598 .lhs = result_type,
8599 .rhs = @as(Zir.Inst.Ref, @enumFromInt(0)),
8600 } },
8601 });
8602 const scalar = try expr(gz, scope, .{ .rl = .{ .ty = elem_type } }, params[0]);
85968603 const result = try gz.addPlNode(.splat, node, Zir.Inst.Bin{
8597 .lhs = len,
8604 .lhs = result_type,
85988605 .rhs = scalar,
85998606 });
86008607 return rvalue(gz, ri, result, node);
src/BuiltinFn.zig+1-1
......@@ -792,7 +792,7 @@ pub const list = list: {
792792 "@splat",
793793 .{
794794 .tag = .splat,
795 .param_count = 2,
795 .param_count = 1,
796796 },
797797 },
798798 .{
src/Sema.zig+24-25
......@@ -1820,7 +1820,7 @@ pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Ins
18201820 return ty;
18211821}
18221822
1823fn resolveCastDestType(
1823fn resolveDestType(
18241824 sema: *Sema,
18251825 block: *Block,
18261826 src: LazySrcLoc,
......@@ -8337,7 +8337,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
83378337 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
83388338 const src = inst_data.src();
83398339 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8340 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@enumFromInt");
8340 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@enumFromInt");
83418341 const operand = try sema.resolveInst(extra.rhs);
83428342
83438343 if (dest_ty.zigTypeTag(mod) != .Enum) {
......@@ -9666,7 +9666,7 @@ fn zirIntCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
96669666 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
96679667 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
96689668
9669 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@intCast");
9669 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@intCast");
96709670 const operand = try sema.resolveInst(extra.rhs);
96719671
96729672 return sema.intCast(block, inst_data.src(), dest_ty, src, operand, operand_src, true);
......@@ -9827,7 +9827,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
98279827 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
98289828 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
98299829
9830 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@bitCast");
9830 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@bitCast");
98319831 const operand = try sema.resolveInst(extra.rhs);
98329832 const operand_ty = sema.typeOf(operand);
98339833 switch (dest_ty.zigTypeTag(mod)) {
......@@ -9970,7 +9970,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
99709970 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
99719971 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
99729972
9973 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@floatCast");
9973 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@floatCast");
99749974 const operand = try sema.resolveInst(extra.rhs);
99759975
99769976 const target = mod.getTarget();
......@@ -20783,7 +20783,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2078320783 const src = inst_data.src();
2078420784 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2078520785 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
20786 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@intFromFloat");
20786 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@intFromFloat");
2078720787 const operand = try sema.resolveInst(extra.rhs);
2078820788 const operand_ty = sema.typeOf(operand);
2078920789
......@@ -20823,7 +20823,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2082320823 const src = inst_data.src();
2082420824 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2082520825 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
20826 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@floatFromInt");
20826 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@floatFromInt");
2082720827 const operand = try sema.resolveInst(extra.rhs);
2082820828 const operand_ty = sema.typeOf(operand);
2082920829
......@@ -20852,7 +20852,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
2085220852 const operand_res = try sema.resolveInst(extra.rhs);
2085320853 const operand_coerced = try sema.coerce(block, Type.usize, operand_res, operand_src);
2085420854
20855 const ptr_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu, "@ptrFromInt");
20855 const ptr_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu, "@ptrFromInt");
2085620856 try sema.checkPtrType(block, src, ptr_ty);
2085720857 const elem_ty = ptr_ty.elemType2(mod);
2085820858 const ptr_align = try ptr_ty.ptrAlignmentAdvanced(mod, sema);
......@@ -20910,7 +20910,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2091020910 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
2091120911 const src = LazySrcLoc.nodeOffset(extra.node);
2091220912 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
20913 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@errSetCast");
20913 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@errSetCast");
2091420914 const operand = try sema.resolveInst(extra.rhs);
2091520915 const operand_ty = sema.typeOf(operand);
2091620916 try sema.checkErrorSetType(block, src, dest_ty);
......@@ -20997,7 +20997,7 @@ fn zirPtrCastFull(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDa
2099720997 const src = LazySrcLoc.nodeOffset(extra.node);
2099820998 const operand_src: LazySrcLoc = .{ .node_offset_ptrcast_operand = extra.node };
2099920999 const operand = try sema.resolveInst(extra.rhs);
21000 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu, flags.needResultTypeBuiltinName());
21000 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu, flags.needResultTypeBuiltinName());
2100121001 return sema.ptrCastFull(
2100221002 block,
2100321003 flags,
......@@ -21013,7 +21013,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2101321013 const src = inst_data.src();
2101421014 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2101521015 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
21016 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu, "@ptrCast");
21016 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu, "@ptrCast");
2101721017 const operand = try sema.resolveInst(extra.rhs);
2101821018
2101921019 return sema.ptrCastFull(
......@@ -21426,7 +21426,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
2142621426 const src = inst_data.src();
2142721427 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2142821428 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
21429 const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@truncate");
21429 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@truncate");
2143021430 const dest_scalar_ty = try sema.checkIntOrVectorAllowComptime(block, dest_ty, src);
2143121431 const operand = try sema.resolveInst(extra.rhs);
2143221432 const operand_ty = sema.typeOf(operand);
......@@ -22358,23 +22358,22 @@ fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
2235822358 const mod = sema.mod;
2235922359 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
2236022360 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
22361 const len_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
22362 const scalar_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
22363 const len = @as(u32, @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, "vector splat destination length must be comptime-known")));
22364 const scalar = try sema.resolveInst(extra.rhs);
22365 const scalar_ty = sema.typeOf(scalar);
22366 try sema.checkVectorElemType(block, scalar_src, scalar_ty);
22367 const vector_ty = try mod.vectorType(.{
22368 .len = len,
22369 .child = scalar_ty.toIntern(),
22370 });
22361 const src = inst_data.src();
22362 const scalar_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
22363 const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@splat");
22364
22365 if (!dest_ty.isVector(mod)) return sema.fail(block, src, "expected vector type, found '{}'", .{dest_ty.fmt(mod)});
22366
22367 const operand = try sema.resolveInst(extra.rhs);
22368 const scalar_ty = dest_ty.childType(mod);
22369 const scalar = try sema.coerce(block, scalar_ty, operand, scalar_src);
2237122370 if (try sema.resolveMaybeUndefVal(scalar)) |scalar_val| {
22372 if (scalar_val.isUndef(mod)) return sema.addConstUndef(vector_ty);
22373 return sema.addConstant(try sema.splat(vector_ty, scalar_val));
22371 if (scalar_val.isUndef(mod)) return sema.addConstUndef(dest_ty);
22372 return sema.addConstant(try sema.splat(dest_ty, scalar_val));
2237422373 }
2237522374
2237622375 try sema.requireRuntimeBlock(block, inst_data.src(), scalar_src);
22377 return block.addTyOp(.splat, vector_ty, scalar);
22376 return block.addTyOp(.splat, dest_ty, scalar);
2237822377}
2237922378
2238022379fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {