Skip to content

Commit

Permalink
llvm: revert bad array access optimization
Browse files Browse the repository at this point in the history
Closes #18723
  • Loading branch information
Vexu authored and andrewrk committed Jan 29, 2024
1 parent 9c7fa35 commit f93a36c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
28 changes: 0 additions & 28 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6382,34 +6382,6 @@ pub const FuncGen = struct {
const elem_alignment = elem_ty.abiAlignment(mod).toLlvm();
return self.loadByRef(elem_ptr, elem_ty, elem_alignment, .normal);
} else {
if (bin_op.lhs.toIndex()) |lhs_index| {
if (self.air.instructions.items(.tag)[@intFromEnum(lhs_index)] == .load) {
const load_data = self.air.instructions.items(.data)[@intFromEnum(lhs_index)];
const load_ptr = load_data.ty_op.operand;
if (load_ptr.toIndex()) |load_ptr_index| {
const load_ptr_tag = self.air.instructions.items(.tag)[@intFromEnum(load_ptr_index)];
switch (load_ptr_tag) {
.struct_field_ptr,
.struct_field_ptr_index_0,
.struct_field_ptr_index_1,
.struct_field_ptr_index_2,
.struct_field_ptr_index_3,
=> {
const load_ptr_inst = try self.resolveInst(load_ptr);
const gep = try self.wip.gep(
.inbounds,
array_llvm_ty,
load_ptr_inst,
&indices,
"",
);
return self.loadTruncate(.normal, elem_ty, gep, .default);
},
else => {},
}
}
}
}
const elem_ptr =
try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, "");
return self.loadTruncate(.normal, elem_ty, elem_ptr, .default);
Expand Down
13 changes: 13 additions & 0 deletions test/behavior/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1400,3 +1400,16 @@ test "allocation and looping over 3-byte integer" {
try expect(x[0] == 0x00);
try expect(x[1] == 0x00);
}

test "loading array from struct is not optimized away" {
const S = struct {
arr: [1]u32 = .{0},
fn doTheTest(self: *@This()) !void {
const o = self.arr;
self.arr[0] = 1;
try expect(o[0] == 0);
}
};
var s = S{};
try s.doTheTest();
}

0 comments on commit f93a36c

Please sign in to comment.