Skip to content

Commit df69e5d

Browse files
committed
check_align: we can still check low alignments on MSVC
1 parent d4ce381 commit df69e5d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Diff for: compiler/rustc_mir_transform/src/check_alignment.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ pub(super) struct CheckAlignment;
1212

1313
impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
1414
fn is_enabled(&self, sess: &Session) -> bool {
15-
if sess.target.max_reliable_alignment() < Align::MAX {
16-
return false;
17-
}
1815
sess.ub_checks()
1916
}
2017

@@ -87,6 +84,33 @@ fn insert_alignment_check<'tcx>(
8784
))),
8885
});
8986

87+
// If this target does not have reliable alignment, further limit the mask by anding it with
88+
// the mask for the highest reliable alignment.
89+
#[allow(irrefutable_let_patterns)]
90+
if let max_align = tcx.sess.target.max_reliable_alignment()
91+
&& max_align < Align::MAX
92+
{
93+
let max_mask = max_align.bytes() - 1;
94+
let max_mask = Operand::Constant(Box::new(ConstOperand {
95+
span: source_info.span,
96+
user_ty: None,
97+
const_: Const::Val(
98+
ConstValue::Scalar(Scalar::from_target_usize(max_mask, &tcx)),
99+
tcx.types.usize,
100+
),
101+
}));
102+
stmts.push(Statement {
103+
source_info,
104+
kind: StatementKind::Assign(Box::new((
105+
alignment_mask,
106+
Rvalue::BinaryOp(
107+
BinOp::BitAnd,
108+
Box::new((Operand::Copy(alignment_mask), max_mask)),
109+
),
110+
))),
111+
});
112+
}
113+
90114
// BitAnd the alignment mask with the pointer
91115
let alignment_bits =
92116
local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into();

0 commit comments

Comments
 (0)