Skip to content

Commit 76f0d34

Browse files
committed
llvm: revert bad array access optimization
Closes #18723
1 parent abb8e74 commit 76f0d34

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

src/codegen/llvm.zig

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6382,34 +6382,6 @@ pub const FuncGen = struct {
63826382
const elem_alignment = elem_ty.abiAlignment(mod).toLlvm();
63836383
return self.loadByRef(elem_ptr, elem_ty, elem_alignment, .normal);
63846384
} 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-
}
64136385
const elem_ptr =
64146386
try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, "");
64156387
return self.loadTruncate(.normal, elem_ty, elem_ptr, .default);

test/behavior/basic.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,3 +1400,16 @@ test "allocation and looping over 3-byte integer" {
14001400
try expect(x[0] == 0x00);
14011401
try expect(x[1] == 0x00);
14021402
}
1403+
1404+
test "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+
}

0 commit comments

Comments
 (0)