Skip to content

Commit 5ffdca7

Browse files
authored
Rollup merge of rust-lang#62828 - nikic:fadd-mul-reductions, r=eddyb
Remove vector fadd/fmul reduction workarounds The bugs that this was working around have been fixed in LLVM 9. r? @gnzlbg
2 parents 632ce74 + 6fae7db commit 5ffdca7

File tree

7 files changed

+37
-102
lines changed

7 files changed

+37
-102
lines changed

src/librustc_codegen_llvm/builder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1153,21 +1153,21 @@ impl Builder<'a, 'll, 'tcx> {
11531153
}
11541154
}
11551155

1156+
pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1157+
unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1158+
}
1159+
pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1160+
unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1161+
}
11561162
pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
11571163
unsafe {
1158-
// FIXME: add a non-fast math version once
1159-
// https://bugs.llvm.org/show_bug.cgi?id=36732
1160-
// is fixed.
11611164
let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
11621165
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
11631166
instr
11641167
}
11651168
}
11661169
pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
11671170
unsafe {
1168-
// FIXME: add a non-fast math version once
1169-
// https://bugs.llvm.org/show_bug.cgi?id=36732
1170-
// is fixed.
11711171
let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
11721172
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
11731173
instr

src/librustc_codegen_llvm/common.rs

-19
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,6 @@ impl CodegenCx<'ll, 'tcx> {
166166
r
167167
}
168168
}
169-
170-
pub fn const_get_real(&self, v: &'ll Value) -> Option<(f64, bool)> {
171-
unsafe {
172-
if self.is_const_real(v) {
173-
let mut loses_info: llvm::Bool = 0;
174-
let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
175-
let loses_info = if loses_info == 1 { true } else { false };
176-
Some((r, loses_info))
177-
} else {
178-
None
179-
}
180-
}
181-
}
182-
183-
fn is_const_real(&self, v: &'ll Value) -> bool {
184-
unsafe {
185-
llvm::LLVMIsAConstantFP(v).is_some()
186-
}
187-
}
188169
}
189170

190171
impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {

src/librustc_codegen_llvm/intrinsic.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -1640,29 +1640,11 @@ fn generic_simd_intrinsic(
16401640
}
16411641
},
16421642
ty::Float(f) => {
1643-
// ordered arithmetic reductions take an accumulator
16441643
let acc = if $ordered {
1645-
let acc = args[1].immediate();
1646-
// FIXME: https://bugs.llvm.org/show_bug.cgi?id=36734
1647-
// * if the accumulator of the fadd isn't 0, incorrect
1648-
// code is generated
1649-
// * if the accumulator of the fmul isn't 1, incorrect
1650-
// code is generated
1651-
match bx.const_get_real(acc) {
1652-
None => return_error!("accumulator of {} is not a constant", $name),
1653-
Some((v, loses_info)) => {
1654-
if $name.contains("mul") && v != 1.0_f64 {
1655-
return_error!("accumulator of {} is not 1.0", $name);
1656-
} else if $name.contains("add") && v != 0.0_f64 {
1657-
return_error!("accumulator of {} is not 0.0", $name);
1658-
} else if loses_info {
1659-
return_error!("accumulator of {} loses information", $name);
1660-
}
1661-
}
1662-
}
1663-
acc
1644+
// ordered arithmetic reductions take an accumulator
1645+
args[1].immediate()
16641646
} else {
1665-
// unordered arithmetic reductions do not:
1647+
// unordered arithmetic reductions use the identity accumulator
16661648
let identity_acc = if $name.contains("mul") { 1.0 } else { 0.0 };
16671649
match f.bit_width() {
16681650
32 => bx.const_real(bx.type_f32(), identity_acc),
@@ -1688,8 +1670,8 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
16881670
}
16891671
}
16901672

1691-
arith_red!("simd_reduce_add_ordered": vector_reduce_add, vector_reduce_fadd_fast, true);
1692-
arith_red!("simd_reduce_mul_ordered": vector_reduce_mul, vector_reduce_fmul_fast, true);
1673+
arith_red!("simd_reduce_add_ordered": vector_reduce_add, vector_reduce_fadd, true);
1674+
arith_red!("simd_reduce_mul_ordered": vector_reduce_mul, vector_reduce_fmul, true);
16931675
arith_red!("simd_reduce_add_unordered": vector_reduce_add, vector_reduce_fadd_fast, false);
16941676
arith_red!("simd_reduce_mul_unordered": vector_reduce_mul, vector_reduce_fmul_fast, false);
16951677

src/librustc_codegen_llvm/llvm/ffi.rs

-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,6 @@ extern "C" {
719719
pub fn LLVMConstIntGetZExtValue(ConstantVal: &Value) -> c_ulonglong;
720720
pub fn LLVMRustConstInt128Get(ConstantVal: &Value, SExt: bool,
721721
high: &mut u64, low: &mut u64) -> bool;
722-
pub fn LLVMConstRealGetDouble (ConstantVal: &Value, losesInfo: &mut Bool) -> f64;
723722

724723

725724
// Operations on composite constants
@@ -1663,7 +1662,6 @@ extern "C" {
16631662
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
16641663

16651664
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
1666-
pub fn LLVMIsAConstantFP(value_ref: &Value) -> Option<&Value>;
16671665

16681666
pub fn LLVMRustPassKind(Pass: &Pass) -> PassKind;
16691667
pub fn LLVMRustFindAndCreatePass(Pass: *const c_char) -> Option<&'static mut Pass>;

src/test/run-pass/simd/simd-intrinsic-generic-reduction.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(non_camel_case_types)]
33

44
// ignore-emscripten
5-
// ignore-aarch64 FIXME: https://github.com/rust-lang/rust/issues/54510
5+
// min-system-llvm-version: 9.0
66

77
// Test that the simd_reduce_{op} intrinsics produce the correct results.
88

@@ -124,14 +124,14 @@ fn main() {
124124
assert_eq!(r, 6_f32);
125125
let r: f32 = simd_reduce_mul_unordered(x);
126126
assert_eq!(r, -24_f32);
127-
// FIXME: only works correctly for accumulator, 0:
128-
// https://bugs.llvm.org/show_bug.cgi?id=36734
129127
let r: f32 = simd_reduce_add_ordered(x, 0.);
130128
assert_eq!(r, 6_f32);
131-
// FIXME: only works correctly for accumulator, 1:
132-
// https://bugs.llvm.org/show_bug.cgi?id=36734
133129
let r: f32 = simd_reduce_mul_ordered(x, 1.);
134130
assert_eq!(r, -24_f32);
131+
let r: f32 = simd_reduce_add_ordered(x, 1.);
132+
assert_eq!(r, 7_f32);
133+
let r: f32 = simd_reduce_mul_ordered(x, 2.);
134+
assert_eq!(r, -48_f32);
135135

136136
let r: f32 = simd_reduce_min(x);
137137
assert_eq!(r, -2_f32);

src/test/ui/simd-intrinsic/simd-intrinsic-generic-reduction.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ fn main() {
3030
let z = f32x4(0.0, 0.0, 0.0, 0.0);
3131

3232
unsafe {
33-
simd_reduce_add_ordered(z, 0_f32);
34-
simd_reduce_mul_ordered(z, 1_f32);
35-
36-
simd_reduce_add_ordered(z, 2_f32);
37-
//~^ ERROR accumulator of simd_reduce_add_ordered is not 0.0
38-
simd_reduce_mul_ordered(z, 3_f32);
39-
//~^ ERROR accumulator of simd_reduce_mul_ordered is not 1.0
33+
simd_reduce_add_ordered(z, 0);
34+
//~^ ERROR expected return type `f32` (element of input `f32x4`), found `i32`
35+
simd_reduce_mul_ordered(z, 1);
36+
//~^ ERROR expected return type `f32` (element of input `f32x4`), found `i32`
4037

4138
let _: f32 = simd_reduce_and(x);
4239
//~^ ERROR expected return type `u32` (element of input `u32x4`), found `f32`
@@ -56,16 +53,5 @@ fn main() {
5653
//~^ ERROR unsupported simd_reduce_all from `f32x4` with element `f32` to `bool`
5754
let _: bool = simd_reduce_any(z);
5855
//~^ ERROR unsupported simd_reduce_any from `f32x4` with element `f32` to `bool`
59-
60-
foo(0_f32);
6156
}
6257
}
63-
64-
#[inline(never)]
65-
unsafe fn foo(x: f32) {
66-
let z = f32x4(0.0, 0.0, 0.0, 0.0);
67-
simd_reduce_add_ordered(z, x);
68-
//~^ ERROR accumulator of simd_reduce_add_ordered is not a constant
69-
simd_reduce_mul_ordered(z, x);
70-
//~^ ERROR accumulator of simd_reduce_mul_ordered is not a constant
71-
}
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,62 @@
1-
error[E0511]: invalid monomorphization of `simd_reduce_add_ordered` intrinsic: accumulator of simd_reduce_add_ordered is not 0.0
2-
--> $DIR/simd-intrinsic-generic-reduction.rs:36:9
1+
error[E0511]: invalid monomorphization of `simd_reduce_add_ordered` intrinsic: expected return type `f32` (element of input `f32x4`), found `i32`
2+
--> $DIR/simd-intrinsic-generic-reduction.rs:33:9
33
|
4-
LL | simd_reduce_add_ordered(z, 2_f32);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | simd_reduce_add_ordered(z, 0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0511]: invalid monomorphization of `simd_reduce_mul_ordered` intrinsic: accumulator of simd_reduce_mul_ordered is not 1.0
8-
--> $DIR/simd-intrinsic-generic-reduction.rs:38:9
7+
error[E0511]: invalid monomorphization of `simd_reduce_mul_ordered` intrinsic: expected return type `f32` (element of input `f32x4`), found `i32`
8+
--> $DIR/simd-intrinsic-generic-reduction.rs:35:9
99
|
10-
LL | simd_reduce_mul_ordered(z, 3_f32);
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | simd_reduce_mul_ordered(z, 1);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error[E0511]: invalid monomorphization of `simd_reduce_and` intrinsic: expected return type `u32` (element of input `u32x4`), found `f32`
14-
--> $DIR/simd-intrinsic-generic-reduction.rs:41:22
14+
--> $DIR/simd-intrinsic-generic-reduction.rs:38:22
1515
|
1616
LL | let _: f32 = simd_reduce_and(x);
1717
| ^^^^^^^^^^^^^^^^^^
1818

1919
error[E0511]: invalid monomorphization of `simd_reduce_or` intrinsic: expected return type `u32` (element of input `u32x4`), found `f32`
20-
--> $DIR/simd-intrinsic-generic-reduction.rs:43:22
20+
--> $DIR/simd-intrinsic-generic-reduction.rs:40:22
2121
|
2222
LL | let _: f32 = simd_reduce_or(x);
2323
| ^^^^^^^^^^^^^^^^^
2424

2525
error[E0511]: invalid monomorphization of `simd_reduce_xor` intrinsic: expected return type `u32` (element of input `u32x4`), found `f32`
26-
--> $DIR/simd-intrinsic-generic-reduction.rs:45:22
26+
--> $DIR/simd-intrinsic-generic-reduction.rs:42:22
2727
|
2828
LL | let _: f32 = simd_reduce_xor(x);
2929
| ^^^^^^^^^^^^^^^^^^
3030

3131
error[E0511]: invalid monomorphization of `simd_reduce_and` intrinsic: unsupported simd_reduce_and from `f32x4` with element `f32` to `f32`
32-
--> $DIR/simd-intrinsic-generic-reduction.rs:48:22
32+
--> $DIR/simd-intrinsic-generic-reduction.rs:45:22
3333
|
3434
LL | let _: f32 = simd_reduce_and(z);
3535
| ^^^^^^^^^^^^^^^^^^
3636

3737
error[E0511]: invalid monomorphization of `simd_reduce_or` intrinsic: unsupported simd_reduce_or from `f32x4` with element `f32` to `f32`
38-
--> $DIR/simd-intrinsic-generic-reduction.rs:50:22
38+
--> $DIR/simd-intrinsic-generic-reduction.rs:47:22
3939
|
4040
LL | let _: f32 = simd_reduce_or(z);
4141
| ^^^^^^^^^^^^^^^^^
4242

4343
error[E0511]: invalid monomorphization of `simd_reduce_xor` intrinsic: unsupported simd_reduce_xor from `f32x4` with element `f32` to `f32`
44-
--> $DIR/simd-intrinsic-generic-reduction.rs:52:22
44+
--> $DIR/simd-intrinsic-generic-reduction.rs:49:22
4545
|
4646
LL | let _: f32 = simd_reduce_xor(z);
4747
| ^^^^^^^^^^^^^^^^^^
4848

4949
error[E0511]: invalid monomorphization of `simd_reduce_all` intrinsic: unsupported simd_reduce_all from `f32x4` with element `f32` to `bool`
50-
--> $DIR/simd-intrinsic-generic-reduction.rs:55:23
50+
--> $DIR/simd-intrinsic-generic-reduction.rs:52:23
5151
|
5252
LL | let _: bool = simd_reduce_all(z);
5353
| ^^^^^^^^^^^^^^^^^^
5454

5555
error[E0511]: invalid monomorphization of `simd_reduce_any` intrinsic: unsupported simd_reduce_any from `f32x4` with element `f32` to `bool`
56-
--> $DIR/simd-intrinsic-generic-reduction.rs:57:23
56+
--> $DIR/simd-intrinsic-generic-reduction.rs:54:23
5757
|
5858
LL | let _: bool = simd_reduce_any(z);
5959
| ^^^^^^^^^^^^^^^^^^
6060

61-
error[E0511]: invalid monomorphization of `simd_reduce_add_ordered` intrinsic: accumulator of simd_reduce_add_ordered is not a constant
62-
--> $DIR/simd-intrinsic-generic-reduction.rs:67:5
63-
|
64-
LL | simd_reduce_add_ordered(z, x);
65-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66-
67-
error[E0511]: invalid monomorphization of `simd_reduce_mul_ordered` intrinsic: accumulator of simd_reduce_mul_ordered is not a constant
68-
--> $DIR/simd-intrinsic-generic-reduction.rs:69:5
69-
|
70-
LL | simd_reduce_mul_ordered(z, x);
71-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72-
73-
error: aborting due to 12 previous errors
61+
error: aborting due to 10 previous errors
7462

0 commit comments

Comments
 (0)