Skip to content

Commit

Permalink
Auto merge of rust-lang#134117 - DianQK:gep-i8, r=<try>
Browse files Browse the repository at this point in the history
[WIP] Canonicalize GEPs

Fixes rust-lang#133979.

r? ghost
  • Loading branch information
bors committed Dec 10, 2024
2 parents b597d2a + 23cc55f commit 945e153
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 12 additions & 4 deletions compiler/rustc_codegen_ssa/src/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,18 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
layout.size
};

let llval = bx.inbounds_gep(bx.cx().backend_type(self.layout), self.val.llval, &[
bx.cx().const_usize(0),
llindex,
]);
let llval = {
let pointee_layout = layout;
let bytes = pointee_layout.size.align_to(pointee_layout.align.abi).bytes();
let rhs = if bytes == 1 {
llindex
} else {
let lhs = llindex;
let rhs = bx.cx().const_usize(bytes);
bx.unchecked_umul(lhs, rhs)
};
bx.inbounds_ptradd(self.val.llval, rhs)
};
let align = self.val.align.restrict_for_offset(offset);
PlaceValue::new_sized(llval, align).with_type(layout)
}
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// so offsetting a pointer to ZST is a noop.
lhs
} else {
let llty = bx.cx().backend_type(pointee_layout);
bx.inbounds_gep(llty, lhs, &[rhs])
let bytes = pointee_layout.size.align_to(pointee_layout.align.abi).bytes();
let rhs = if bytes == 1 {
rhs
} else {
let lhs = rhs;
let rhs = bx.cx().const_usize(bytes);
bx.unchecked_umul(lhs, rhs)
};
bx.inbounds_ptradd(lhs, rhs)
}
}
mir::BinOp::Shl | mir::BinOp::ShlUnchecked => {
Expand Down

0 comments on commit 945e153

Please sign in to comment.