Skip to content

Commit 31ae4f9

Browse files
committed
Auto merge of #49249 - gnzlbg:simd_minmax, r=alexcrichton
implement minmax intrinsics This adds the `simd_{fmin,fmax}` intrinsics, which do a vertical (lane-wise) `min`/`max` for floating point vectors that's equivalent to Rust's `min`/`max` for `f32`/`f64`. It might make sense to make `{f32,f64}::{min,max}` use the `minnum` and `minmax` intrinsics as well. --- ~~HELP: I need some help with these. Either I should go to sleep or there must be something that I must be missing. AFAICT I am calling the `maxnum` builder correctly, yet rustc/LLVM seem to insert a call to `llvm.minnum` there instead...~~ EDIT: Rust's LLVM version is too old :/
2 parents 989b257 + 56aaf34 commit 31ae4f9

File tree

7 files changed

+148
-1
lines changed

7 files changed

+148
-1
lines changed

Diff for: src/librustc_llvm/ffi.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,9 @@ extern "C" {
12481248
IsNaN: bool)
12491249
-> ValueRef;
12501250

1251+
pub fn LLVMRustBuildMinNum(B: BuilderRef, LHS: ValueRef, LHS: ValueRef) -> ValueRef;
1252+
pub fn LLVMRustBuildMaxNum(B: BuilderRef, LHS: ValueRef, LHS: ValueRef) -> ValueRef;
1253+
12511254
pub fn LLVMBuildIsNull(B: BuilderRef, Val: ValueRef, Name: *const c_char) -> ValueRef;
12521255
pub fn LLVMBuildIsNotNull(B: BuilderRef, Val: ValueRef, Name: *const c_char) -> ValueRef;
12531256
pub fn LLVMBuildPtrDiff(B: BuilderRef,

Diff for: src/librustc_trans/builder.rs

+21
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
917917
}
918918
}
919919

920+
pub fn minnum(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
921+
self.count_insn("minnum");
922+
unsafe {
923+
let instr = llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs);
924+
if instr.is_null() {
925+
bug!("LLVMRustBuildMinNum is not available in LLVM version < 6.0");
926+
}
927+
instr
928+
}
929+
}
930+
pub fn maxnum(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
931+
self.count_insn("maxnum");
932+
unsafe {
933+
let instr = llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs);
934+
if instr.is_null() {
935+
bug!("LLVMRustBuildMaxNum is not available in LLVM version < 6.0");
936+
}
937+
instr
938+
}
939+
}
940+
920941
pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
921942
self.count_insn("select");
922943
unsafe {

Diff for: src/librustc_trans/intrinsic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,8 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
14321432
simd_and: TyUint, TyInt => and;
14331433
simd_or: TyUint, TyInt => or;
14341434
simd_xor: TyUint, TyInt => xor;
1435+
simd_fmax: TyFloat => maxnum;
1436+
simd_fmin: TyFloat => minnum;
14351437
}
14361438
span_bug!(span, "unknown SIMD intrinsic");
14371439
}

Diff for: src/librustc_typeck/check/intrinsic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
355355
}
356356
"simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
357357
"simd_div" | "simd_shl" | "simd_shr" |
358-
"simd_and" | "simd_or" | "simd_xor" => {
358+
"simd_and" | "simd_or" | "simd_xor" |
359+
"simd_fmin" | "simd_fmax" => {
359360
(1, vec![param(0), param(0)], param(0))
360361
}
361362
"simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),

Diff for: src/rustllvm/RustWrapper.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1503,3 +1503,23 @@ LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
15031503
return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
15041504
}
15051505
#endif
1506+
1507+
#if LLVM_VERSION_GE(6, 0)
1508+
extern "C" LLVMValueRef
1509+
LLVMRustBuildMinNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS) {
1510+
return wrap(unwrap(B)->CreateMinNum(unwrap(LHS),unwrap(RHS)));
1511+
}
1512+
extern "C" LLVMValueRef
1513+
LLVMRustBuildMaxNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS) {
1514+
return wrap(unwrap(B)->CreateMaxNum(unwrap(LHS),unwrap(RHS)));
1515+
}
1516+
#else
1517+
extern "C" LLVMValueRef
1518+
LLVMRustBuildMinNum(LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1519+
return nullptr;
1520+
}
1521+
extern "C" LLVMValueRef
1522+
LLVMRustBuildMaxNum(LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1523+
return nullptr;
1524+
}
1525+
#endif

Diff for: src/test/codegen/simd-intrinsic-float-minmax.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-emscripten
12+
// min-llvm-version 6.0
13+
14+
// compile-flags: -C no-prepopulate-passes
15+
16+
#![crate_type = "lib"]
17+
18+
#![feature(repr_simd, platform_intrinsics)]
19+
#[allow(non_camel_case_types)]
20+
21+
#[repr(simd)]
22+
#[derive(Copy, Clone, PartialEq, Debug)]
23+
pub struct f32x4(pub f32, pub f32, pub f32, pub f32);
24+
25+
extern "platform-intrinsic" {
26+
fn simd_fmin<T>(x: T, y: T) -> T;
27+
fn simd_fmax<T>(x: T, y: T) -> T;
28+
}
29+
30+
// CHECK-LABEL: @fmin
31+
#[no_mangle]
32+
pub unsafe fn fmin(a: f32x4, b: f32x4) -> f32x4 {
33+
// CHECK: call <4 x float> @llvm.minnum.v4f32
34+
simd_fmin(a, b)
35+
}
36+
37+
// FIXME(49261)
38+
// // C_HECK-LABEL: @fmax
39+
// #[no_mangle]
40+
// pub unsafe fn fmax(a: f32x4, b: f32x4) -> f32x4 {
41+
// // C_HECK: call <4 x float> @llvm.maxnum.v4f32
42+
// simd_fmax(a, b)
43+
// }

Diff for: src/test/run-fail/simd-intrinsic-float-minmax.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-emscripten
12+
// min-llvm-version 6.0
13+
// error-pattern: panicked
14+
15+
// Test that the simd_f{min,max} intrinsics produce the correct results.
16+
17+
#![feature(repr_simd, platform_intrinsics)]
18+
#[allow(non_camel_case_types)]
19+
20+
#[repr(simd)]
21+
#[derive(Copy, Clone, PartialEq, Debug)]
22+
struct f32x4(pub f32, pub f32, pub f32, pub f32);
23+
24+
extern "platform-intrinsic" {
25+
fn simd_fmin<T>(x: T, y: T) -> T;
26+
fn simd_fmax<T>(x: T, y: T) -> T;
27+
}
28+
29+
fn main() {
30+
let x = f32x4(1.0, 2.0, 3.0, 4.0);
31+
let y = f32x4(2.0, 1.0, 4.0, 3.0);
32+
let nan = ::std::f32::NAN;
33+
let n = f32x4(nan, nan, nan, nan);
34+
35+
unsafe {
36+
let min0 = simd_fmin(x, y);
37+
let min1 = simd_fmin(y, x);
38+
assert_eq!(min0, min1);
39+
let e = f32x4(1.0, 1.0, 3.0, 3.0);
40+
assert_eq!(min0, e);
41+
let minn = simd_fmin(x, n);
42+
assert_eq!(minn, x);
43+
let minn = simd_fmin(y, n);
44+
assert_eq!(minn, y);
45+
46+
// FIXME(49261)
47+
let max0 = simd_fmax(x, y);
48+
let max1 = simd_fmax(y, x);
49+
assert_eq!(max0, max1);
50+
let e = f32x4(2.0, 2.0, 4.0, 4.0);
51+
assert_eq!(max0, e);
52+
let maxn = simd_fmax(x, n);
53+
assert_eq!(maxn, x);
54+
let maxn = simd_fmax(y, n);
55+
assert_eq!(maxn, y);
56+
}
57+
}

0 commit comments

Comments
 (0)