Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions compiler/rustc_codegen_gcc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2313,67 +2313,10 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
self.vector_extremum(a, b, ExtremumOperation::Min)
}

#[cfg(feature = "master")]
pub fn vector_reduce_fmin(&mut self, src: RValue<'gcc>) -> RValue<'gcc> {
let vector_type = src.get_type().unqualified().dyncast_vector().expect("vector type");
let element_count = vector_type.get_num_units();
let mut acc = self
.context
.new_vector_access(self.location, src, self.context.new_rvalue_zero(self.int_type))
.to_rvalue();
for i in 1..element_count {
let elem = self
.context
.new_vector_access(
self.location,
src,
self.context.new_rvalue_from_int(self.int_type, i as _),
)
.to_rvalue();
let cmp = self.context.new_comparison(self.location, ComparisonOp::LessThan, acc, elem);
acc = self.select(cmp, acc, elem);
}
acc
}
Copy link
Copy Markdown
Member Author

@RalfJung RalfJung Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the GCC code because it seems completely wrong under any conceivable semantics. For an input of [0.0, NaN, 1.0, 2.0] this would return 1.0!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. T_T


#[cfg(not(feature = "master"))]
pub fn vector_reduce_fmin(&mut self, _src: RValue<'gcc>) -> RValue<'gcc> {
unimplemented!();
}

pub fn vector_maximum_number_nsz(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
self.vector_extremum(a, b, ExtremumOperation::Max)
}

#[cfg(feature = "master")]
pub fn vector_reduce_fmax(&mut self, src: RValue<'gcc>) -> RValue<'gcc> {
let vector_type = src.get_type().unqualified().dyncast_vector().expect("vector type");
let element_count = vector_type.get_num_units();
let mut acc = self
.context
.new_vector_access(self.location, src, self.context.new_rvalue_zero(self.int_type))
.to_rvalue();
for i in 1..element_count {
let elem = self
.context
.new_vector_access(
self.location,
src,
self.context.new_rvalue_from_int(self.int_type, i as _),
)
.to_rvalue();
let cmp =
self.context.new_comparison(self.location, ComparisonOp::GreaterThan, acc, elem);
acc = self.select(cmp, acc, elem);
}
acc
}

#[cfg(not(feature = "master"))]
pub fn vector_reduce_fmax(&mut self, _src: RValue<'gcc>) -> RValue<'gcc> {
unimplemented!();
}

pub fn vector_select(
&mut self,
cond: RValue<'gcc>,
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_codegen_gcc/src/intrinsic/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,15 +1416,14 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
);

macro_rules! minmax_red {
($name:ident: $int_red:ident, $float_red:ident) => {
($name:ident: $int_red:ident) => {
if name == sym::$name {
require!(
ret_ty == in_elem,
InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
);
return match *in_elem.kind() {
ty::Int(_) | ty::Uint(_) => Ok(bx.$int_red(args[0].immediate())),
ty::Float(_) => Ok(bx.$float_red(args[0].immediate())),
_ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
span,
name,
Expand All @@ -1438,8 +1437,8 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
};
}

minmax_red!(simd_reduce_min: vector_reduce_min, vector_reduce_fmin);
minmax_red!(simd_reduce_max: vector_reduce_max, vector_reduce_fmax);
minmax_red!(simd_reduce_min: vector_reduce_min);
minmax_red!(simd_reduce_max: vector_reduce_max);

macro_rules! bitwise_red {
($name:ident : $op:expr, $boolean:expr) => {
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,12 +1668,6 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
pub(crate) fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
self.call_intrinsic("llvm.vector.reduce.xor", &[self.val_ty(src)], &[src])
}
pub(crate) fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
self.call_intrinsic("llvm.vector.reduce.fmin", &[self.val_ty(src)], &[src])
}
pub(crate) fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
self.call_intrinsic("llvm.vector.reduce.fmax", &[self.val_ty(src)], &[src])
}
pub(crate) fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
self.call_intrinsic(
if is_signed { "llvm.vector.reduce.smin" } else { "llvm.vector.reduce.umin" },
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
);

macro_rules! minmax_red {
($name:ident: $int_red:ident, $float_red:ident) => {
($name:ident: $int_red:ident) => {
if name == sym::$name {
require!(
ret_ty == in_elem,
Expand All @@ -2608,7 +2608,6 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
return match in_elem.kind() {
ty::Int(_i) => Ok(bx.$int_red(args[0].immediate(), true)),
ty::Uint(_u) => Ok(bx.$int_red(args[0].immediate(), false)),
ty::Float(_f) => Ok(bx.$float_red(args[0].immediate())),
_ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
span,
name,
Expand All @@ -2622,8 +2621,9 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
};
}

minmax_red!(simd_reduce_min: vector_reduce_min, vector_reduce_fmin);
minmax_red!(simd_reduce_max: vector_reduce_max, vector_reduce_fmax);
// Currently no support for float due to <https://github.com/llvm/llvm-project/issues/185827>.
minmax_red!(simd_reduce_min: vector_reduce_min);
minmax_red!(simd_reduce_max: vector_reduce_max);

macro_rules! bitwise_red {
($name:ident : $red:ident, $boolean:expr) => {
Expand Down
18 changes: 7 additions & 11 deletions library/core/src/intrinsics/simd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ pub unsafe fn simd_reduce_mul_unordered<T, U>(x: T) -> U;

/// Checks if all mask values are true.
///
/// `T` must be a vector of integer primitive types.
/// `T` must be a vector of integers.
///
/// # Safety
/// `x` must contain only `0` or `!0`.
Expand All @@ -530,7 +530,7 @@ pub const unsafe fn simd_reduce_all<T>(x: T) -> bool;

/// Checks if any mask value is true.
///
/// `T` must be a vector of integer primitive types.
/// `T` must be a vector of integers.
///
/// # Safety
/// `x` must contain only `0` or `!0`.
Expand All @@ -540,29 +540,25 @@ pub const unsafe fn simd_reduce_any<T>(x: T) -> bool;

/// Returns the maximum element of a vector.
///
/// `T` must be a vector of integers or floats.
Copy link
Copy Markdown
Member

@chenyukang chenyukang Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment below still have

/// For floating-point values, uses IEEE-754 `maxNum`.`

which maybe need to be removed?

there are several similar places.

/// `T` must be a vector of integers.
///
/// `U` must be the element type of `T`.
///
/// For floating-point values, uses IEEE-754 `maxNum`.
#[rustc_intrinsic]
#[rustc_nounwind]
pub const unsafe fn simd_reduce_max<T, U>(x: T) -> U;

/// Returns the minimum element of a vector.
///
/// `T` must be a vector of integers or floats.
/// `T` must be a vector of integers.
///
/// `U` must be the element type of `T`.
///
/// For floating-point values, uses IEEE-754 `minNum`.
#[rustc_intrinsic]
#[rustc_nounwind]
pub const unsafe fn simd_reduce_min<T, U>(x: T) -> U;

/// Logical "and"s all elements together.
///
/// `T` must be a vector of integers or floats.
/// `T` must be a vector of integers.
///
/// `U` must be the element type of `T`.
#[rustc_intrinsic]
Expand All @@ -571,7 +567,7 @@ pub const unsafe fn simd_reduce_and<T, U>(x: T) -> U;

/// Logical "ors" all elements together.
///
/// `T` must be a vector of integers or floats.
/// `T` must be a vector of integers.
///
/// `U` must be the element type of `T`.
#[rustc_intrinsic]
Expand All @@ -580,7 +576,7 @@ pub const unsafe fn simd_reduce_or<T, U>(x: T) -> U;

/// Logical "exclusive ors" all elements together.
///
/// `T` must be a vector of integers or floats.
/// `T` must be a vector of integers.
///
/// `U` must be the element type of `T`.
#[rustc_intrinsic]
Expand Down
9 changes: 5 additions & 4 deletions tests/ui/simd/intrinsic/generic-reduction-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ const fn ordered() {
let r: f32 = simd_reduce_mul_ordered(x, 2.);
assert_eq!(r, -48_f32);

let r: f32 = simd_reduce_min(x);
assert_eq!(r, -2_f32);
let r: f32 = simd_reduce_max(x);
assert_eq!(r, 4_f32);
// FIXME: re-enable when the intrinsic works on floats again.
// let r: f32 = simd_reduce_min(x);
// assert_eq!(r, -2_f32);
// let r: f32 = simd_reduce_max(x);
// assert_eq!(r, 4_f32);
}

unsafe {
Expand Down
Loading