Skip to content

Commit 45bca00

Browse files
committed
Handle alignment of the load instruction
1 parent 218575a commit 45bca00

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/builder.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -729,17 +729,25 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
729729
unimplemented!();
730730
}
731731

732-
fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
732+
fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
733733
let block = self.llbb();
734734
let function = block.get_function();
735735
// NOTE: instead of returning the dereference here, we have to assign it to a variable in
736736
// the current basic block. Otherwise, it could be used in another basic block, causing a
737737
// dereference after a drop, for instance.
738-
// TODO(antoyo): handle align of the load instruction.
739-
let ptr = self.context.new_cast(None, ptr, pointee_ty.make_pointer());
738+
// FIXME(antoyo): this check that we don't call get_aligned() a second time on a type.
739+
// Ideally, we shouldn't need to do this check.
740+
let aligned_type =
741+
if pointee_ty == self.cx.u128_type || pointee_ty == self.cx.i128_type {
742+
pointee_ty
743+
}
744+
else {
745+
pointee_ty.get_aligned(align.bytes())
746+
};
747+
let ptr = self.context.new_cast(None, ptr, aligned_type.make_pointer());
740748
let deref = ptr.dereference(None).to_rvalue();
741749
unsafe { RETURN_VALUE_COUNT += 1 };
742-
let loaded_value = function.new_local(None, pointee_ty, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
750+
let loaded_value = function.new_local(None, aligned_type, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
743751
block.add_assignment(None, loaded_value, deref);
744752
loaded_value.to_rvalue()
745753
}
@@ -1857,7 +1865,8 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
18571865

18581866
#[cfg(feature="master")]
18591867
let (cond, element_type) = {
1860-
let then_val_vector_type = then_val.get_type().dyncast_vector().expect("vector type");
1868+
// TODO(antoyo): dyncast_vector should not require a call to unqualified.
1869+
let then_val_vector_type = then_val.get_type().unqualified().dyncast_vector().expect("vector type");
18611870
let then_val_element_type = then_val_vector_type.get_element_type();
18621871
let then_val_element_size = then_val_element_type.get_size();
18631872

src/intrinsic/simd.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
346346
// endian and MSB-first for big endian.
347347

348348
let vector = args[0].immediate();
349-
let vector_type = vector.get_type().dyncast_vector().expect("vector type");
349+
// TODO(antoyo): dyncast_vector should not require a call to unqualified.
350+
let vector_type = vector.get_type().unqualified().dyncast_vector().expect("vector type");
350351
let elem_type = vector_type.get_element_type();
351352

352353
let expected_int_bits = in_len.max(8);
@@ -853,7 +854,8 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
853854
(true, true) => {
854855
// Algorithm from: https://codereview.stackexchange.com/questions/115869/saturated-signed-addition
855856
// TODO(antoyo): improve using conditional operators if possible.
856-
let arg_type = lhs.get_type();
857+
// TODO(antoyo): dyncast_vector should not require a call to unqualified.
858+
let arg_type = lhs.get_type().unqualified();
857859
// TODO(antoyo): convert lhs and rhs to unsigned.
858860
let sum = lhs + rhs;
859861
let vector_type = arg_type.dyncast_vector().expect("vector type");
@@ -883,7 +885,8 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
883885
res & cmp
884886
},
885887
(true, false) => {
886-
let arg_type = lhs.get_type();
888+
// TODO(antoyo): dyncast_vector should not require a call to unqualified.
889+
let arg_type = lhs.get_type().unqualified();
887890
// TODO(antoyo): this uses the same algorithm from saturating add, but add the
888891
// negative of the right operand. Find a proper subtraction algorithm.
889892
let rhs = bx.context.new_unary_op(None, UnaryOp::Minus, arg_type, rhs);

0 commit comments

Comments
 (0)