authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-29 09:46:12+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-29 08:43:27-08:00
logf93a36c091a151ed02602d2f330f7206eb9f95a3
tree1e3e822ff0e9cc5c6b55b47039fa9545fa04349e
parent9c7fa358c11bbb49f2e624cf81c5003e39fbcd92

llvm: revert bad array access optimization

Closes #18723

2 files changed, 13 insertions(+), 28 deletions(-)

src/codegen/llvm.zig-28
...@@ -6382,34 +6382,6 @@ pub const FuncGen = struct {...@@ -6382,34 +6382,6 @@ pub const FuncGen = struct {
6382 const elem_alignment = elem_ty.abiAlignment(mod).toLlvm();6382 const elem_alignment = elem_ty.abiAlignment(mod).toLlvm();
6383 return self.loadByRef(elem_ptr, elem_ty, elem_alignment, .normal);6383 return self.loadByRef(elem_ptr, elem_ty, elem_alignment, .normal);
6384 } else {6384 } else {
6385 if (bin_op.lhs.toIndex()) |lhs_index| {
6386 if (self.air.instructions.items(.tag)[@intFromEnum(lhs_index)] == .load) {
6387 const load_data = self.air.instructions.items(.data)[@intFromEnum(lhs_index)];
6388 const load_ptr = load_data.ty_op.operand;
6389 if (load_ptr.toIndex()) |load_ptr_index| {
6390 const load_ptr_tag = self.air.instructions.items(.tag)[@intFromEnum(load_ptr_index)];
6391 switch (load_ptr_tag) {
6392 .struct_field_ptr,
6393 .struct_field_ptr_index_0,
6394 .struct_field_ptr_index_1,
6395 .struct_field_ptr_index_2,
6396 .struct_field_ptr_index_3,
6397 => {
6398 const load_ptr_inst = try self.resolveInst(load_ptr);
6399 const gep = try self.wip.gep(
6400 .inbounds,
6401 array_llvm_ty,
6402 load_ptr_inst,
6403 &indices,
6404 "",
6405 );
6406 return self.loadTruncate(.normal, elem_ty, gep, .default);
6407 },
6408 else => {},
6409 }
6410 }
6411 }
6412 }
6413 const elem_ptr =6385 const elem_ptr =
6414 try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, "");6386 try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, "");
6415 return self.loadTruncate(.normal, elem_ty, elem_ptr, .default);6387 return self.loadTruncate(.normal, elem_ty, elem_ptr, .default);
test/behavior/basic.zig+13
...@@ -1400,3 +1400,16 @@ test "allocation and looping over 3-byte integer" {...@@ -1400,3 +1400,16 @@ test "allocation and looping over 3-byte integer" {
1400 try expect(x[0] == 0x00);1400 try expect(x[0] == 0x00);
1401 try expect(x[1] == 0x00);1401 try expect(x[1] == 0x00);
1402}1402}
1403
1404test "loading array from struct is not optimized away" {
1405 const S = struct {
1406 arr: [1]u32 = .{0},
1407 fn doTheTest(self: *@This()) !void {
1408 const o = self.arr;
1409 self.arr[0] = 1;
1410 try expect(o[0] == 0);
1411 }
1412 };
1413 var s = S{};
1414 try s.doTheTest();
1415}