authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-01-06 21:47:30-08:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-01-07 06:17:40-05:00
log40f5eac79c8491e2e9ac0861e18872d6ba5b3b0b
tree7a7fc4dfc891f1e9dad44e481587a991039f81bc
parent23281704dc3b3fedf7c215edcb38f84e7e059b2e

Sema: fix invalid AIR from array concat


1 files changed, 21 insertions(+), 19 deletions(-)

src/Sema.zig+21-19
......@@ -15155,21 +15155,26 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1515515155 try sema.requireRuntimeBlock(block, src, runtime_src);
1515615156
1515715157 if (ptr_addrspace) |ptr_as| {
15158 const alloc_ty = try pt.ptrTypeSema(.{
15158 const constant_alloc_ty = try pt.ptrTypeSema(.{
1515915159 .child = result_ty.toIntern(),
1516015160 .flags = .{
1516115161 .address_space = ptr_as,
1516215162 .is_const = true,
1516315163 },
1516415164 });
15165 const alloc = try block.addTy(.alloc, alloc_ty);
15165 const alloc_ty = try pt.ptrTypeSema(.{
15166 .child = result_ty.toIntern(),
15167 .flags = .{ .address_space = ptr_as },
15168 });
1516615169 const elem_ptr_ty = try pt.ptrTypeSema(.{
1516715170 .child = resolved_elem_ty.toIntern(),
1516815171 .flags = .{ .address_space = ptr_as },
1516915172 });
1517015173
15174 const mutable_alloc = try block.addTy(.alloc, alloc_ty);
15175
1517115176 // if both the source and destination are arrays
15172 // we can hot path via a memcpy.
15177 // we can hotpath via a memcpy.
1517315178 if (lhs_ty.zigTypeTag(zcu) == .pointer and
1517415179 rhs_ty.zigTypeTag(zcu) == .pointer)
1517515180 {
......@@ -15180,27 +15185,24 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1518015185 .address_space = ptr_as,
1518115186 },
1518215187 });
15183 const many_ty = try pt.ptrTypeSema(.{
15184 .child = resolved_elem_ty.toIntern(),
15185 .flags = .{
15186 .size = .Many,
15187 .address_space = ptr_as,
15188 },
15189 });
15190 const slice_ty_ref = Air.internedToRef(slice_ty.toIntern());
15188
15189 const many_ty = slice_ty.slicePtrFieldType(zcu);
15190 const many_alloc = try block.addTyOp(.bitcast, many_ty, mutable_alloc);
1519115191
1519215192 // lhs_dest_slice = dest[0..lhs.len]
15193 const slice_ty_ref = Air.internedToRef(slice_ty.toIntern());
1519315194 const lhs_len_ref = try pt.intRef(Type.usize, lhs_len);
1519415195 const lhs_dest_slice = try block.addInst(.{
1519515196 .tag = .slice,
1519615197 .data = .{ .ty_pl = .{
1519715198 .ty = slice_ty_ref,
1519815199 .payload = try sema.addExtra(Air.Bin{
15199 .lhs = alloc,
15200 .lhs = many_alloc,
1520015201 .rhs = lhs_len_ref,
1520115202 }),
1520215203 } },
1520315204 });
15205
1520415206 _ = try block.addBinOp(.memcpy, lhs_dest_slice, lhs);
1520515207
1520615208 // rhs_dest_slice = dest[lhs.len..][0..rhs.len]
......@@ -15210,7 +15212,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1521015212 .data = .{ .ty_pl = .{
1521115213 .ty = Air.internedToRef(many_ty.toIntern()),
1521215214 .payload = try sema.addExtra(Air.Bin{
15213 .lhs = alloc,
15215 .lhs = many_alloc,
1521415216 .rhs = lhs_len_ref,
1521515217 }),
1521615218 } },
......@@ -15230,18 +15232,18 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1523015232
1523115233 if (res_sent_val) |sent_val| {
1523215234 const elem_index = try pt.intRef(Type.usize, result_len);
15233 const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty);
15235 const elem_ptr = try block.addPtrElemPtr(mutable_alloc, elem_index, elem_ptr_ty);
1523415236 const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern());
1523515237 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);
1523615238 }
1523715239
15238 return alloc;
15240 return block.addTyOp(.bitcast, constant_alloc_ty, mutable_alloc);
1523915241 }
1524015242
1524115243 var elem_i: u32 = 0;
1524215244 while (elem_i < lhs_len) : (elem_i += 1) {
1524315245 const elem_index = try pt.intRef(Type.usize, elem_i);
15244 const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty);
15246 const elem_ptr = try block.addPtrElemPtr(mutable_alloc, elem_index, elem_ptr_ty);
1524515247 const operand_src = block.src(.{ .array_cat_lhs = .{
1524615248 .array_cat_offset = inst_data.src_node,
1524715249 .elem_index = elem_i,
......@@ -15253,7 +15255,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1525315255 const rhs_elem_i = elem_i - lhs_len;
1525415256 const elem_index = try pt.intRef(Type.usize, elem_i);
1525515257 const rhs_index = try pt.intRef(Type.usize, rhs_elem_i);
15256 const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty);
15258 const elem_ptr = try block.addPtrElemPtr(mutable_alloc, elem_index, elem_ptr_ty);
1525715259 const operand_src = block.src(.{ .array_cat_rhs = .{
1525815260 .array_cat_offset = inst_data.src_node,
1525915261 .elem_index = @intCast(rhs_elem_i),
......@@ -15263,12 +15265,12 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1526315265 }
1526415266 if (res_sent_val) |sent_val| {
1526515267 const elem_index = try pt.intRef(Type.usize, result_len);
15266 const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty);
15268 const elem_ptr = try block.addPtrElemPtr(mutable_alloc, elem_index, elem_ptr_ty);
1526715269 const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern());
1526815270 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);
1526915271 }
1527015272
15271 return alloc;
15273 return block.addTyOp(.bitcast, constant_alloc_ty, mutable_alloc);
1527215274 }
1527315275
1527415276 const element_refs = try sema.arena.alloc(Air.Inst.Ref, result_len);