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...@@ -15155,21 +15155,26 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15155 try sema.requireRuntimeBlock(block, src, runtime_src);15155 try sema.requireRuntimeBlock(block, src, runtime_src);
1515615156
15157 if (ptr_addrspace) |ptr_as| {15157 if (ptr_addrspace) |ptr_as| {
15158 const alloc_ty = try pt.ptrTypeSema(.{15158 const constant_alloc_ty = try pt.ptrTypeSema(.{
15159 .child = result_ty.toIntern(),15159 .child = result_ty.toIntern(),
15160 .flags = .{15160 .flags = .{
15161 .address_space = ptr_as,15161 .address_space = ptr_as,
15162 .is_const = true,15162 .is_const = true,
15163 },15163 },
15164 });15164 });
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 });
15166 const elem_ptr_ty = try pt.ptrTypeSema(.{15169 const elem_ptr_ty = try pt.ptrTypeSema(.{
15167 .child = resolved_elem_ty.toIntern(),15170 .child = resolved_elem_ty.toIntern(),
15168 .flags = .{ .address_space = ptr_as },15171 .flags = .{ .address_space = ptr_as },
15169 });15172 });
1517015173
15174 const mutable_alloc = try block.addTy(.alloc, alloc_ty);
15175
15171 // if both the source and destination are arrays15176 // if both the source and destination are arrays
15172 // we can hot path via a memcpy.15177 // we can hotpath via a memcpy.
15173 if (lhs_ty.zigTypeTag(zcu) == .pointer and15178 if (lhs_ty.zigTypeTag(zcu) == .pointer and
15174 rhs_ty.zigTypeTag(zcu) == .pointer)15179 rhs_ty.zigTypeTag(zcu) == .pointer)
15175 {15180 {
...@@ -15180,27 +15185,24 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15180,27 +15185,24 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15180 .address_space = ptr_as,15185 .address_space = ptr_as,
15181 },15186 },
15182 });15187 });
15183 const many_ty = try pt.ptrTypeSema(.{15188
15184 .child = resolved_elem_ty.toIntern(),15189 const many_ty = slice_ty.slicePtrFieldType(zcu);
15185 .flags = .{15190 const many_alloc = try block.addTyOp(.bitcast, many_ty, mutable_alloc);
15186 .size = .Many,
15187 .address_space = ptr_as,
15188 },
15189 });
15190 const slice_ty_ref = Air.internedToRef(slice_ty.toIntern());
1519115191
15192 // lhs_dest_slice = dest[0..lhs.len]15192 // lhs_dest_slice = dest[0..lhs.len]
15193 const slice_ty_ref = Air.internedToRef(slice_ty.toIntern());
15193 const lhs_len_ref = try pt.intRef(Type.usize, lhs_len);15194 const lhs_len_ref = try pt.intRef(Type.usize, lhs_len);
15194 const lhs_dest_slice = try block.addInst(.{15195 const lhs_dest_slice = try block.addInst(.{
15195 .tag = .slice,15196 .tag = .slice,
15196 .data = .{ .ty_pl = .{15197 .data = .{ .ty_pl = .{
15197 .ty = slice_ty_ref,15198 .ty = slice_ty_ref,
15198 .payload = try sema.addExtra(Air.Bin{15199 .payload = try sema.addExtra(Air.Bin{
15199 .lhs = alloc,15200 .lhs = many_alloc,
15200 .rhs = lhs_len_ref,15201 .rhs = lhs_len_ref,
15201 }),15202 }),
15202 } },15203 } },
15203 });15204 });
15205
15204 _ = try block.addBinOp(.memcpy, lhs_dest_slice, lhs);15206 _ = try block.addBinOp(.memcpy, lhs_dest_slice, lhs);
1520515207
15206 // rhs_dest_slice = dest[lhs.len..][0..rhs.len]15208 // 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...@@ -15210,7 +15212,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15210 .data = .{ .ty_pl = .{15212 .data = .{ .ty_pl = .{
15211 .ty = Air.internedToRef(many_ty.toIntern()),15213 .ty = Air.internedToRef(many_ty.toIntern()),
15212 .payload = try sema.addExtra(Air.Bin{15214 .payload = try sema.addExtra(Air.Bin{
15213 .lhs = alloc,15215 .lhs = many_alloc,
15214 .rhs = lhs_len_ref,15216 .rhs = lhs_len_ref,
15215 }),15217 }),
15216 } },15218 } },
...@@ -15230,18 +15232,18 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15230,18 +15232,18 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1523015232
15231 if (res_sent_val) |sent_val| {15233 if (res_sent_val) |sent_val| {
15232 const elem_index = try pt.intRef(Type.usize, result_len);15234 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);
15234 const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern());15236 const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern());
15235 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);15237 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);
15236 }15238 }
1523715239
15238 return alloc;15240 return block.addTyOp(.bitcast, constant_alloc_ty, mutable_alloc);
15239 }15241 }
1524015242
15241 var elem_i: u32 = 0;15243 var elem_i: u32 = 0;
15242 while (elem_i < lhs_len) : (elem_i += 1) {15244 while (elem_i < lhs_len) : (elem_i += 1) {
15243 const elem_index = try pt.intRef(Type.usize, elem_i);15245 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);
15245 const operand_src = block.src(.{ .array_cat_lhs = .{15247 const operand_src = block.src(.{ .array_cat_lhs = .{
15246 .array_cat_offset = inst_data.src_node,15248 .array_cat_offset = inst_data.src_node,
15247 .elem_index = elem_i,15249 .elem_index = elem_i,
...@@ -15253,7 +15255,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15253,7 +15255,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15253 const rhs_elem_i = elem_i - lhs_len;15255 const rhs_elem_i = elem_i - lhs_len;
15254 const elem_index = try pt.intRef(Type.usize, elem_i);15256 const elem_index = try pt.intRef(Type.usize, elem_i);
15255 const rhs_index = try pt.intRef(Type.usize, rhs_elem_i);15257 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);
15257 const operand_src = block.src(.{ .array_cat_rhs = .{15259 const operand_src = block.src(.{ .array_cat_rhs = .{
15258 .array_cat_offset = inst_data.src_node,15260 .array_cat_offset = inst_data.src_node,
15259 .elem_index = @intCast(rhs_elem_i),15261 .elem_index = @intCast(rhs_elem_i),
...@@ -15263,12 +15265,12 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15263,12 +15265,12 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15263 }15265 }
15264 if (res_sent_val) |sent_val| {15266 if (res_sent_val) |sent_val| {
15265 const elem_index = try pt.intRef(Type.usize, result_len);15267 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);
15267 const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern());15269 const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern());
15268 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);15270 try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store);
15269 }15271 }
1527015272
15271 return alloc;15273 return block.addTyOp(.bitcast, constant_alloc_ty, mutable_alloc);
15272 }15274 }
1527315275
15274 const element_refs = try sema.arena.alloc(Air.Inst.Ref, result_len);15276 const element_refs = try sema.arena.alloc(Air.Inst.Ref, result_len);