Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c62239a

Browse files
committedFeb 20, 2025
Auto merge of rust-lang#137058 - scottmcm:trunc-unchecked, r=nikic
Emit `trunc nuw` for unchecked shifts and `to_immediate_scalar` - For shifts this shrinks the IR by no longer needing an `assume` while still providing the UB information - Having this on the `i8`→`i1` truncations will hopefully help with some places that have to load `i8`s or pass those in LLVM structs without range information
2 parents eeb9035 + 6f9cfd6 commit c62239a

File tree

15 files changed

+241
-138
lines changed

15 files changed

+241
-138
lines changed
 

‎compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
989989
OperandValue::Ref(place.val)
990990
} else if place.layout.is_gcc_immediate() {
991991
let load = self.load(place.layout.gcc_type(self), place.val.llval, place.val.align);
992-
if let abi::BackendRepr::Scalar(ref scalar) = place.layout.backend_repr {
993-
scalar_load_metadata(self, load, scalar);
994-
}
995-
OperandValue::Immediate(self.to_immediate(load, place.layout))
992+
OperandValue::Immediate(
993+
if let abi::BackendRepr::Scalar(ref scalar) = place.layout.backend_repr {
994+
scalar_load_metadata(self, load, scalar);
995+
self.to_immediate_scalar(load, *scalar)
996+
} else {
997+
load
998+
},
999+
)
9961000
} else if let abi::BackendRepr::ScalarPair(ref a, ref b) = place.layout.backend_repr {
9971001
let b_offset = a.size(self).align_to(b.align(self).abi);
9981002

@@ -1694,7 +1698,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
16941698

16951699
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
16961700
if scalar.is_bool() {
1697-
return self.trunc(val, self.cx().type_i1());
1701+
return self.unchecked_utrunc(val, self.cx().type_i1());
16981702
}
16991703
val
17001704
}

‎compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use gccjit::FunctionType;
99
use gccjit::{ComparisonOp, Function, RValue, ToRValue, Type, UnaryOp};
1010
#[cfg(feature = "master")]
1111
use rustc_abi::ExternAbi;
12-
use rustc_abi::HasDataLayout;
12+
use rustc_abi::{BackendRepr, HasDataLayout};
1313
use rustc_codegen_ssa::MemFlags;
1414
use rustc_codegen_ssa::base::wants_msvc_seh;
1515
use rustc_codegen_ssa::common::IntPredicate;
@@ -181,14 +181,19 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
181181
sym::volatile_load | sym::unaligned_volatile_load => {
182182
let tp_ty = fn_args.type_at(0);
183183
let ptr = args[0].immediate();
184+
let layout = self.layout_of(tp_ty);
184185
let load = if let PassMode::Cast { cast: ref ty, pad_i32: _ } = fn_abi.ret.mode {
185186
let gcc_ty = ty.gcc_type(self);
186187
self.volatile_load(gcc_ty, ptr)
187188
} else {
188-
self.volatile_load(self.layout_of(tp_ty).gcc_type(self), ptr)
189+
self.volatile_load(layout.gcc_type(self), ptr)
189190
};
190191
// TODO(antoyo): set alignment.
191-
self.to_immediate(load, self.layout_of(tp_ty))
192+
if let BackendRepr::Scalar(scalar) = layout.backend_repr {
193+
self.to_immediate_scalar(load, scalar)
194+
} else {
195+
load
196+
}
192197
}
193198
sym::volatile_store => {
194199
let dst = args[0].deref(self.cx());

‎compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ use smallvec::SmallVec;
2929
use tracing::{debug, instrument};
3030

3131
use crate::abi::FnAbiLlvmExt;
32-
use crate::attributes;
3332
use crate::common::Funclet;
3433
use crate::context::{CodegenCx, SimpleCx};
3534
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, Metadata, True};
3635
use crate::type_::Type;
3736
use crate::type_of::LayoutLlvmExt;
3837
use crate::value::Value;
38+
use crate::{attributes, llvm_util};
3939

4040
#[must_use]
4141
pub(crate) struct GenericBuilder<'a, 'll, CX: Borrow<SimpleCx<'ll>>> {
@@ -606,7 +606,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
606606

607607
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
608608
if scalar.is_bool() {
609-
return self.trunc(val, self.cx().type_i1());
609+
return self.unchecked_utrunc(val, self.cx().type_i1());
610610
}
611611
val
612612
}
@@ -746,10 +746,12 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
746746
let load = self.load(llty, place.val.llval, place.val.align);
747747
if let abi::BackendRepr::Scalar(scalar) = place.layout.backend_repr {
748748
scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO);
749+
self.to_immediate_scalar(load, scalar)
750+
} else {
751+
load
749752
}
750-
load
751753
});
752-
OperandValue::Immediate(self.to_immediate(llval, place.layout))
754+
OperandValue::Immediate(llval)
753755
} else if let abi::BackendRepr::ScalarPair(a, b) = place.layout.backend_repr {
754756
let b_offset = a.size(self).align_to(b.align(self).abi);
755757

@@ -942,6 +944,34 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
942944
unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
943945
}
944946

947+
fn unchecked_utrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
948+
debug_assert_ne!(self.val_ty(val), dest_ty);
949+
950+
let trunc = self.trunc(val, dest_ty);
951+
if llvm_util::get_version() >= (19, 0, 0) {
952+
unsafe {
953+
if llvm::LLVMIsAInstruction(trunc).is_some() {
954+
llvm::LLVMSetNUW(trunc, True);
955+
}
956+
}
957+
}
958+
trunc
959+
}
960+
961+
fn unchecked_strunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
962+
debug_assert_ne!(self.val_ty(val), dest_ty);
963+
964+
let trunc = self.trunc(val, dest_ty);
965+
if llvm_util::get_version() >= (19, 0, 0) {
966+
unsafe {
967+
if llvm::LLVMIsAInstruction(trunc).is_some() {
968+
llvm::LLVMSetNSW(trunc, True);
969+
}
970+
}
971+
}
972+
trunc
973+
}
974+
945975
fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
946976
unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
947977
}

‎compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::query::Providers;
2424
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
2525
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2626
use rustc_session::Session;
27-
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
27+
use rustc_session::config::{self, CrateType, EntryFnType, OutputType};
2828
use rustc_span::{DUMMY_SP, Symbol, sym};
2929
use rustc_trait_selection::infer::{BoundRegionConversionTime, TyCtxtInferExt};
3030
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
@@ -364,13 +364,7 @@ pub(crate) fn build_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
364364
let rhs_sz = bx.cx().int_width(rhs_llty);
365365
let lhs_sz = bx.cx().int_width(lhs_llty);
366366
if lhs_sz < rhs_sz {
367-
if is_unchecked && bx.sess().opts.optimize != OptLevel::No {
368-
// FIXME: Use `trunc nuw` once that's available
369-
let inrange = bx.icmp(IntPredicate::IntULE, rhs, mask);
370-
bx.assume(inrange);
371-
}
372-
373-
bx.trunc(rhs, lhs_llty)
367+
if is_unchecked { bx.unchecked_utrunc(rhs, lhs_llty) } else { bx.trunc(rhs, lhs_llty) }
374368
} else if lhs_sz > rhs_sz {
375369
// We zero-extend even if the RHS is signed. So e.g. `(x: i32) << -1i8` will zero-extend the
376370
// RHS to `255i32`. But then we mask the shift amount to be within the size of the LHS

‎compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10401040
let (idx, _) = op.layout.non_1zst_field(bx).expect(
10411041
"not exactly one non-1-ZST field in a `DispatchFromDyn` type",
10421042
);
1043-
op = op.extract_field(bx, idx);
1043+
op = op.extract_field(self, bx, idx);
10441044
}
10451045

10461046
// Now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
@@ -1072,7 +1072,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10721072
let (idx, _) = op.layout.non_1zst_field(bx).expect(
10731073
"not exactly one non-1-ZST field in a `DispatchFromDyn` type",
10741074
);
1075-
op = op.extract_field(bx, idx);
1075+
op = op.extract_field(self, bx, idx);
10761076
}
10771077

10781078
// Make sure that we've actually unwrapped the rcvr down
@@ -1572,9 +1572,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15721572
if scalar.is_bool() {
15731573
bx.range_metadata(llval, WrappingRange { start: 0, end: 1 });
15741574
}
1575+
// We store bools as `i8` so we need to truncate to `i1`.
1576+
llval = bx.to_immediate_scalar(llval, scalar);
15751577
}
1576-
// We store bools as `i8` so we need to truncate to `i1`.
1577-
llval = bx.to_immediate(llval, arg.layout);
15781578
}
15791579
}
15801580

@@ -1604,7 +1604,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16041604
} else {
16051605
// If the tuple is immediate, the elements are as well.
16061606
for i in 0..tuple.layout.fields.count() {
1607-
let op = tuple.extract_field(bx, i);
1607+
let op = tuple.extract_field(self, bx, i);
16081608
self.codegen_argument(bx, op, llargs, &args[i]);
16091609
}
16101610
}

‎compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
use std::assert_matches::assert_matches;
21
use std::fmt;
32

43
use arrayvec::ArrayVec;
54
use either::Either;
65
use rustc_abi as abi;
76
use rustc_abi::{Align, BackendRepr, Size};
8-
use rustc_middle::bug;
97
use rustc_middle::mir::interpret::{Pointer, Scalar, alloc_range};
108
use rustc_middle::mir::{self, ConstValue};
119
use rustc_middle::ty::Ty;
1210
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
11+
use rustc_middle::{bug, span_bug};
1312
use tracing::debug;
1413

1514
use super::place::{PlaceRef, PlaceValue};
@@ -352,79 +351,83 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
352351

353352
pub(crate) fn extract_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
354353
&self,
354+
fx: &mut FunctionCx<'a, 'tcx, Bx>,
355355
bx: &mut Bx,
356356
i: usize,
357357
) -> Self {
358358
let field = self.layout.field(bx.cx(), i);
359359
let offset = self.layout.fields.offset(i);
360360

361-
let mut val = match (self.val, self.layout.backend_repr) {
362-
// If the field is ZST, it has no data.
363-
_ if field.is_zst() => OperandValue::ZeroSized,
364-
365-
// Newtype of a scalar, scalar pair or vector.
366-
(OperandValue::Immediate(_) | OperandValue::Pair(..), _)
367-
if field.size == self.layout.size =>
368-
{
369-
assert_eq!(offset.bytes(), 0);
370-
self.val
361+
let val = if field.is_zst() {
362+
OperandValue::ZeroSized
363+
} else if field.size == self.layout.size {
364+
assert_eq!(offset.bytes(), 0);
365+
if let Some(field_val) = fx.codegen_transmute_operand(bx, *self, field) {
366+
field_val
367+
} else {
368+
// we have to go through memory for things like
369+
// Newtype vector of array, e.g. #[repr(simd)] struct S([i32; 4]);
370+
let place = PlaceRef::alloca(bx, field);
371+
self.val.store(bx, place.val.with_type(self.layout));
372+
bx.load_operand(place).val
371373
}
372-
373-
// Extract a scalar component from a pair.
374-
(OperandValue::Pair(a_llval, b_llval), BackendRepr::ScalarPair(a, b)) => {
375-
if offset.bytes() == 0 {
376-
assert_eq!(field.size, a.size(bx.cx()));
377-
OperandValue::Immediate(a_llval)
378-
} else {
379-
assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
380-
assert_eq!(field.size, b.size(bx.cx()));
381-
OperandValue::Immediate(b_llval)
374+
} else {
375+
let (in_scalar, imm) = match (self.val, self.layout.backend_repr) {
376+
// Extract a scalar component from a pair.
377+
(OperandValue::Pair(a_llval, b_llval), BackendRepr::ScalarPair(a, b)) => {
378+
if offset.bytes() == 0 {
379+
assert_eq!(field.size, a.size(bx.cx()));
380+
(Some(a), a_llval)
381+
} else {
382+
assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
383+
assert_eq!(field.size, b.size(bx.cx()));
384+
(Some(b), b_llval)
385+
}
382386
}
383-
}
384387

385-
// `#[repr(simd)]` types are also immediate.
386-
(OperandValue::Immediate(llval), BackendRepr::Vector { .. }) => {
387-
OperandValue::Immediate(bx.extract_element(llval, bx.cx().const_usize(i as u64)))
388-
}
388+
// `#[repr(simd)]` types are also immediate.
389+
(OperandValue::Immediate(llval), BackendRepr::Vector { .. }) => {
390+
(None, bx.extract_element(llval, bx.cx().const_usize(i as u64)))
391+
}
389392

390-
_ => bug!("OperandRef::extract_field({:?}): not applicable", self),
393+
_ => {
394+
span_bug!(fx.mir.span, "OperandRef::extract_field({:?}): not applicable", self)
395+
}
396+
};
397+
OperandValue::Immediate(match field.backend_repr {
398+
BackendRepr::Vector { .. } => imm,
399+
BackendRepr::Scalar(out_scalar) => {
400+
let Some(in_scalar) = in_scalar else {
401+
span_bug!(
402+
fx.mir.span,
403+
"OperandRef::extract_field({:?}): missing input scalar for output scalar",
404+
self
405+
)
406+
};
407+
if in_scalar != out_scalar {
408+
// If the backend and backend_immediate types might differ,
409+
// flip back to the backend type then to the new immediate.
410+
// This avoids nop truncations, but still handles things like
411+
// Bools in union fields needs to be truncated.
412+
let backend = bx.from_immediate(imm);
413+
bx.to_immediate_scalar(backend, out_scalar)
414+
} else {
415+
imm
416+
}
417+
}
418+
BackendRepr::Memory { sized: true } => {
419+
span_bug!(
420+
fx.mir.span,
421+
"Projecting into a simd type with padding doesn't work; \
422+
See <https://github.com/rust-lang/rust/issues/137108>",
423+
);
424+
}
425+
BackendRepr::Uninhabited
426+
| BackendRepr::ScalarPair(_, _)
427+
| BackendRepr::Memory { sized: false } => bug!(),
428+
})
391429
};
392430

393-
match (&mut val, field.backend_repr) {
394-
(OperandValue::ZeroSized, _) => {}
395-
(
396-
OperandValue::Immediate(llval),
397-
BackendRepr::Scalar(_) | BackendRepr::ScalarPair(..) | BackendRepr::Vector { .. },
398-
) => {
399-
// Bools in union fields needs to be truncated.
400-
*llval = bx.to_immediate(*llval, field);
401-
}
402-
(OperandValue::Pair(a, b), BackendRepr::ScalarPair(a_abi, b_abi)) => {
403-
// Bools in union fields needs to be truncated.
404-
*a = bx.to_immediate_scalar(*a, a_abi);
405-
*b = bx.to_immediate_scalar(*b, b_abi);
406-
}
407-
// Newtype vector of array, e.g. #[repr(simd)] struct S([i32; 4]);
408-
(OperandValue::Immediate(llval), BackendRepr::Memory { sized: true }) => {
409-
assert_matches!(self.layout.backend_repr, BackendRepr::Vector { .. });
410-
411-
let llfield_ty = bx.cx().backend_type(field);
412-
413-
// Can't bitcast an aggregate, so round trip through memory.
414-
let llptr = bx.alloca(field.size, field.align.abi);
415-
bx.store(*llval, llptr, field.align.abi);
416-
*llval = bx.load(llfield_ty, llptr, field.align.abi);
417-
}
418-
(
419-
OperandValue::Immediate(_),
420-
BackendRepr::Uninhabited | BackendRepr::Memory { sized: false },
421-
) => {
422-
bug!()
423-
}
424-
(OperandValue::Pair(..), _) => bug!(),
425-
(OperandValue::Ref(..), _) => bug!(),
426-
}
427-
428431
OperandRef { val, layout: field }
429432
}
430433
}
@@ -587,7 +590,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
587590
"Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
588591
but tried to access field {f:?} of pointer {o:?}",
589592
);
590-
o = o.extract_field(bx, f.index());
593+
o = o.extract_field(self, bx, f.index());
591594
}
592595
mir::ProjectionElem::Index(_)
593596
| mir::ProjectionElem::ConstantIndex { .. } => {

‎compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
231231
///
232232
/// Returns `None` for cases that can't work in that framework, such as for
233233
/// `Immediate`->`Ref` that needs an `alloc` to get the location.
234-
fn codegen_transmute_operand(
234+
pub(crate) fn codegen_transmute_operand(
235235
&mut self,
236236
bx: &mut Bx,
237237
operand: OperandRef<'tcx, Bx::Value>,
@@ -260,6 +260,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
260260
OperandValue::Ref(source_place_val) => {
261261
assert_eq!(source_place_val.llextra, None);
262262
assert_matches!(operand_kind, OperandValueKind::Ref);
263+
// The existing alignment is part of `source_place_val`,
264+
// so that alignment will be used, not `cast`'s.
263265
Some(bx.load_operand(source_place_val.with_type(cast)).val)
264266
}
265267
OperandValue::ZeroSized => {

‎compiler/rustc_codegen_ssa/src/traits/builder.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::assert_matches::assert_matches;
22
use std::ops::Deref;
33

4-
use rustc_abi::{Align, BackendRepr, Scalar, Size, WrappingRange};
4+
use rustc_abi::{Align, Scalar, Size, WrappingRange};
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
66
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
77
use rustc_middle::ty::{Instance, Ty};
@@ -223,13 +223,6 @@ pub trait BuilderMethods<'a, 'tcx>:
223223
) -> (Self::Value, Self::Value);
224224

225225
fn from_immediate(&mut self, val: Self::Value) -> Self::Value;
226-
fn to_immediate(&mut self, val: Self::Value, layout: TyAndLayout<'_>) -> Self::Value {
227-
if let BackendRepr::Scalar(scalar) = layout.backend_repr {
228-
self.to_immediate_scalar(val, scalar)
229-
} else {
230-
val
231-
}
232-
}
233226
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: Scalar) -> Self::Value;
234227

235228
fn alloca(&mut self, size: Size, align: Align) -> Self::Value;
@@ -340,6 +333,17 @@ pub trait BuilderMethods<'a, 'tcx>:
340333
}
341334

342335
fn trunc(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
336+
/// Produces the same value as [`Self::trunc`] (and defaults to that),
337+
/// but is UB unless the *zero*-extending the result can reproduce `val`.
338+
fn unchecked_utrunc(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value {
339+
self.trunc(val, dest_ty)
340+
}
341+
/// Produces the same value as [`Self::trunc`] (and defaults to that),
342+
/// but is UB unless the *sign*-extending the result can reproduce `val`.
343+
fn unchecked_strunc(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value {
344+
self.trunc(val, dest_ty)
345+
}
346+
343347
fn sext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
344348
fn fptoui_sat(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
345349
fn fptosi_sat(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;

‎tests/codegen/intrinsics/transmute-niched.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub unsafe fn check_bool_from_ordering(x: std::cmp::Ordering) -> bool {
170170
// OPT: call void @llvm.assume(i1 %2)
171171
// CHECK-NOT: icmp
172172
// CHECK-NOT: assume
173-
// CHECK: %[[R:.+]] = trunc i8 %x to i1
173+
// CHECK: %[[R:.+]] = trunc{{( nuw)?}} i8 %x to i1
174174
// CHECK: ret i1 %[[R]]
175175

176176
transmute(x)

‎tests/codegen/intrinsics/transmute.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use std::intrinsics::mir::*;
1111
use std::intrinsics::{transmute, transmute_unchecked};
1212
use std::mem::MaybeUninit;
1313

14+
// FIXME(LLVM18REMOVED): `trunc nuw` doesn't exist in LLVM 18, so once we no
15+
// longer support it the optional flag checks can be changed to required.
16+
1417
pub enum ZstNever {}
1518

1619
#[repr(align(2))]
@@ -153,7 +156,7 @@ pub unsafe fn check_from_newtype(x: Scalar64) -> u64 {
153156
pub unsafe fn check_aggregate_to_bool(x: Aggregate8) -> bool {
154157
// CHECK: %x = alloca [1 x i8], align 1
155158
// CHECK: %[[BYTE:.+]] = load i8, ptr %x, align 1
156-
// CHECK: %[[BOOL:.+]] = trunc i8 %[[BYTE]] to i1
159+
// CHECK: %[[BOOL:.+]] = trunc{{( nuw)?}} i8 %[[BYTE]] to i1
157160
// CHECK: ret i1 %[[BOOL]]
158161
transmute(x)
159162
}
@@ -171,7 +174,7 @@ pub unsafe fn check_aggregate_from_bool(x: bool) -> Aggregate8 {
171174
#[no_mangle]
172175
pub unsafe fn check_byte_to_bool(x: u8) -> bool {
173176
// CHECK-NOT: alloca
174-
// CHECK: %[[R:.+]] = trunc i8 %x to i1
177+
// CHECK: %[[R:.+]] = trunc{{( nuw)?}} i8 %x to i1
175178
// CHECK: ret i1 %[[R]]
176179
transmute(x)
177180
}
@@ -284,7 +287,7 @@ pub unsafe fn check_long_array_more_aligned(x: [u8; 100]) -> [u32; 25] {
284287
#[no_mangle]
285288
pub unsafe fn check_pair_with_bool(x: (u8, bool)) -> (bool, i8) {
286289
// CHECK-NOT: alloca
287-
// CHECK: trunc i8 %x.0 to i1
290+
// CHECK: trunc{{( nuw)?}} i8 %x.0 to i1
288291
// CHECK: zext i1 %x.1 to i8
289292
transmute(x)
290293
}
@@ -338,7 +341,7 @@ pub unsafe fn check_heterogeneous_integer_pair(x: (i32, bool)) -> (bool, u32) {
338341
// CHECK: store i8 %[[WIDER]]
339342

340343
// CHECK: %[[BYTE:.+]] = load i8
341-
// CHECK: trunc i8 %[[BYTE:.+]] to i1
344+
// CHECK: trunc{{( nuw)?}} i8 %[[BYTE:.+]] to i1
342345
// CHECK: load i32
343346
transmute(x)
344347
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@compile-flags: -Copt-level=3
2+
3+
#![crate_type = "lib"]
4+
#![feature(repr_simd, core_intrinsics)]
5+
6+
#[allow(non_camel_case_types)]
7+
#[derive(Clone, Copy)]
8+
#[repr(simd)]
9+
struct i32x4([i32; 4]);
10+
11+
#[inline(always)]
12+
fn to_array4(a: i32x4) -> [i32; 4] {
13+
a.0
14+
}
15+
16+
// CHECK-LABEL: simd_add_self_then_return_array(
17+
// CHECK-SAME: ptr{{.+}}sret{{.+}}%[[RET:.+]],
18+
// CHECK-SAME: ptr{{.+}}%a)
19+
#[no_mangle]
20+
pub fn simd_add_self_then_return_array(a: &i32x4) -> [i32; 4] {
21+
// It would be nice to just ban `.0` into simd types,
22+
// but until we do this has to keep working.
23+
// See also <https://github.com/rust-lang/rust/issues/105439>
24+
25+
// CHECK: %[[T1:.+]] = load <4 x i32>, ptr %a
26+
// CHECK: %[[T2:.+]] = shl <4 x i32> %[[T1]], {{splat \(i32 1\)|<i32 1, i32 1, i32 1, i32 1>}}
27+
// CHECK: store <4 x i32> %[[T2]], ptr %[[RET]]
28+
let a = *a;
29+
let b = unsafe { core::intrinsics::simd::simd_add(a, a) };
30+
to_array4(b)
31+
}

‎tests/codegen/transmute-scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn bool_to_byte(b: bool) -> u8 {
2626
}
2727

2828
// CHECK-LABEL: define{{.*}}zeroext i1 @byte_to_bool(i8{{.*}} %byte)
29-
// CHECK: %_0 = trunc i8 %byte to i1
29+
// CHECK: %_0 = trunc{{( nuw)?}} i8 %byte to i1
3030
// CHECK-NEXT: ret i1 %_0
3131
#[no_mangle]
3232
pub unsafe fn byte_to_bool(byte: u8) -> bool {

‎tests/codegen/unchecked_shifts.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
//@ compile-flags: -Copt-level=3
1+
//@ revisions: LLVM18 LLVM19PLUS
2+
//@ compile-flags: -Copt-level=3 -C no-prepopulate-passes
3+
//@[LLVM18] exact-llvm-major-version: 18
4+
//@[LLVM19PLUS] min-llvm-version: 19
5+
6+
// This runs mir-opts to inline the standard library call, but doesn't run LLVM
7+
// optimizations so it doesn't need to worry about them adding more flags.
28

39
#![crate_type = "lib"]
410
#![feature(unchecked_shifts)]
@@ -17,21 +23,18 @@ pub unsafe fn unchecked_shl_unsigned_same(a: u32, b: u32) -> u32 {
1723
// CHECK-LABEL: @unchecked_shl_unsigned_smaller
1824
#[no_mangle]
1925
pub unsafe fn unchecked_shl_unsigned_smaller(a: u16, b: u32) -> u16 {
20-
// This uses -DAG to avoid failing on irrelevant reorderings,
21-
// like emitting the truncation earlier.
22-
23-
// CHECK-DAG: %[[INRANGE:.+]] = icmp ult i32 %b, 16
24-
// CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
25-
// CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i32 %b to i16
26-
// CHECK-DAG: shl i16 %a, %[[TRUNC]]
26+
// CHECK-NOT: assume
27+
// LLVM18: %[[TRUNC:.+]] = trunc i32 %b to i16
28+
// LLVM19PLUS: %[[TRUNC:.+]] = trunc nuw i32 %b to i16
29+
// CHECK: shl i16 %a, %[[TRUNC]]
2730
a.unchecked_shl(b)
2831
}
2932

3033
// CHECK-LABEL: @unchecked_shl_unsigned_bigger
3134
#[no_mangle]
3235
pub unsafe fn unchecked_shl_unsigned_bigger(a: u64, b: u32) -> u64 {
3336
// CHECK-NOT: assume
34-
// CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i32 %b to i64
37+
// CHECK: %[[EXT:.+]] = zext i32 %b to i64
3538
// CHECK: shl i64 %a, %[[EXT]]
3639
a.unchecked_shl(b)
3740
}
@@ -49,21 +52,18 @@ pub unsafe fn unchecked_shr_signed_same(a: i32, b: u32) -> i32 {
4952
// CHECK-LABEL: @unchecked_shr_signed_smaller
5053
#[no_mangle]
5154
pub unsafe fn unchecked_shr_signed_smaller(a: i16, b: u32) -> i16 {
52-
// This uses -DAG to avoid failing on irrelevant reorderings,
53-
// like emitting the truncation earlier.
54-
55-
// CHECK-DAG: %[[INRANGE:.+]] = icmp ult i32 %b, 16
56-
// CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
57-
// CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i32 %b to i16
58-
// CHECK-DAG: ashr i16 %a, %[[TRUNC]]
55+
// CHECK-NOT: assume
56+
// LLVM18: %[[TRUNC:.+]] = trunc i32 %b to i16
57+
// LLVM19PLUS: %[[TRUNC:.+]] = trunc nuw i32 %b to i16
58+
// CHECK: ashr i16 %a, %[[TRUNC]]
5959
a.unchecked_shr(b)
6060
}
6161

6262
// CHECK-LABEL: @unchecked_shr_signed_bigger
6363
#[no_mangle]
6464
pub unsafe fn unchecked_shr_signed_bigger(a: i64, b: u32) -> i64 {
6565
// CHECK-NOT: assume
66-
// CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i32 %b to i64
66+
// CHECK: %[[EXT:.+]] = zext i32 %b to i64
6767
// CHECK: ashr i64 %a, %[[EXT]]
6868
a.unchecked_shr(b)
6969
}
@@ -72,7 +72,7 @@ pub unsafe fn unchecked_shr_signed_bigger(a: i64, b: u32) -> i64 {
7272
#[no_mangle]
7373
pub unsafe fn unchecked_shr_u128_i8(a: u128, b: i8) -> u128 {
7474
// CHECK-NOT: assume
75-
// CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i8 %b to i128
75+
// CHECK: %[[EXT:.+]] = zext i8 %b to i128
7676
// CHECK: lshr i128 %a, %[[EXT]]
7777
std::intrinsics::unchecked_shr(a, b)
7878
}
@@ -81,33 +81,27 @@ pub unsafe fn unchecked_shr_u128_i8(a: u128, b: i8) -> u128 {
8181
#[no_mangle]
8282
pub unsafe fn unchecked_shl_i128_u8(a: i128, b: u8) -> i128 {
8383
// CHECK-NOT: assume
84-
// CHECK: %[[EXT:.+]] = zext{{( nneg)?}} i8 %b to i128
84+
// CHECK: %[[EXT:.+]] = zext i8 %b to i128
8585
// CHECK: shl i128 %a, %[[EXT]]
8686
std::intrinsics::unchecked_shl(a, b)
8787
}
8888

8989
// CHECK-LABEL: @unchecked_shl_u8_i128
9090
#[no_mangle]
9191
pub unsafe fn unchecked_shl_u8_i128(a: u8, b: i128) -> u8 {
92-
// This uses -DAG to avoid failing on irrelevant reorderings,
93-
// like emitting the truncation earlier.
94-
95-
// CHECK-DAG: %[[INRANGE:.+]] = icmp ult i128 %b, 8
96-
// CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
97-
// CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i128 %b to i8
98-
// CHECK-DAG: shl i8 %a, %[[TRUNC]]
92+
// CHECK-NOT: assume
93+
// LLVM18: %[[TRUNC:.+]] = trunc i128 %b to i8
94+
// LLVM19PLUS: %[[TRUNC:.+]] = trunc nuw i128 %b to i8
95+
// CHECK: shl i8 %a, %[[TRUNC]]
9996
std::intrinsics::unchecked_shl(a, b)
10097
}
10198

10299
// CHECK-LABEL: @unchecked_shr_i8_u128
103100
#[no_mangle]
104101
pub unsafe fn unchecked_shr_i8_u128(a: i8, b: u128) -> i8 {
105-
// This uses -DAG to avoid failing on irrelevant reorderings,
106-
// like emitting the truncation earlier.
107-
108-
// CHECK-DAG: %[[INRANGE:.+]] = icmp ult i128 %b, 8
109-
// CHECK-DAG: tail call void @llvm.assume(i1 %[[INRANGE]])
110-
// CHECK-DAG: %[[TRUNC:.+]] = trunc{{( nuw)?( nsw)?}} i128 %b to i8
111-
// CHECK-DAG: ashr i8 %a, %[[TRUNC]]
102+
// CHECK-NOT: assume
103+
// LLVM18: %[[TRUNC:.+]] = trunc i128 %b to i8
104+
// LLVM19PLUS: %[[TRUNC:.+]] = trunc nuw i128 %b to i8
105+
// CHECK: ashr i8 %a, %[[TRUNC]]
112106
std::intrinsics::unchecked_shr(a, b)
113107
}

‎tests/codegen/union-abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ pub union UnionBool {
142142
pub fn test_UnionBool(b: UnionBool) -> bool {
143143
unsafe { b.b }
144144
}
145-
// CHECK: %_0 = trunc i8 %b to i1
145+
// CHECK: %_0 = trunc{{( nuw)?}} i8 %b to i1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ known-bug: #137108
2+
//@compile-flags: -Copt-level=3
3+
4+
// If you fix this, put it in the corresponding codegen test,
5+
// not in a UI test like the readme says.
6+
7+
#![crate_type = "lib"]
8+
9+
#![feature(repr_simd, core_intrinsics)]
10+
11+
#[allow(non_camel_case_types)]
12+
#[derive(Clone, Copy)]
13+
#[repr(simd)]
14+
struct i32x3([i32; 3]);
15+
16+
const _: () = { assert!(size_of::<i32x3>() == 16) };
17+
18+
#[inline(always)]
19+
fn to_array3(a: i32x3) -> [i32; 3] {
20+
a.0
21+
}
22+
23+
// CHECK-LABEL: simd_add_self_then_return_array_packed(
24+
// CHECK-SAME: ptr{{.+}}sret{{.+}}%[[RET:.+]],
25+
// CHECK-SAME: ptr{{.+}}%a)
26+
#[no_mangle]
27+
pub fn simd_add_self_then_return_array_packed(a: i32x3) -> [i32; 3] {
28+
// CHECK: %[[T1:.+]] = load <3 x i32>, ptr %a
29+
// CHECK: %[[T2:.+]] = shl <3 x i32> %[[T1]], <i32 1, i32 1, i32 1>
30+
// CHECK: store <3 x i32> %[[T2]], ptr %[[RET]]
31+
let b = unsafe { core::intrinsics::simd::simd_add(a, a) };
32+
to_array3(b)
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.