Skip to content

Commit 3dc631a

Browse files
committed
make simd_reduce_{mul,add}_unordered use only the 'reassoc' flag, not all fast-math flags
1 parent 25fe3cc commit 3dc631a

File tree

7 files changed

+30
-22
lines changed

7 files changed

+30
-22
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
17521752
self.vector_reduce(src, |a, b, context| context.new_binary_op(None, op, a.get_type(), a, b))
17531753
}
17541754

1755-
pub fn vector_reduce_fadd_fast(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
1755+
pub fn vector_reduce_fadd_reassoc(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
17561756
unimplemented!();
17571757
}
17581758

@@ -1772,7 +1772,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
17721772
unimplemented!();
17731773
}
17741774

1775-
pub fn vector_reduce_fmul_fast(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
1775+
pub fn vector_reduce_fmul_reassoc(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
17761776
unimplemented!();
17771777
}
17781778

compiler/rustc_codegen_gcc/src/intrinsic/simd.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -989,14 +989,14 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
989989

990990
arith_red!(
991991
simd_reduce_add_unordered: BinaryOp::Plus,
992-
vector_reduce_fadd_fast,
992+
vector_reduce_fadd_reassoc,
993993
false,
994994
add,
995995
0.0 // TODO: Use this argument.
996996
);
997997
arith_red!(
998998
simd_reduce_mul_unordered: BinaryOp::Mult,
999-
vector_reduce_fmul_fast,
999+
vector_reduce_fmul_reassoc,
10001000
false,
10011001
mul,
10021002
1.0

compiler/rustc_codegen_llvm/src/builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1367,17 +1367,17 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13671367
pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
13681368
unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
13691369
}
1370-
pub fn vector_reduce_fadd_algebraic(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1370+
pub fn vector_reduce_fadd_reassoc(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
13711371
unsafe {
13721372
let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1373-
llvm::LLVMRustSetAlgebraicMath(instr);
1373+
llvm::LLVMRustSetAllowReassoc(instr);
13741374
instr
13751375
}
13761376
}
1377-
pub fn vector_reduce_fmul_algebraic(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1377+
pub fn vector_reduce_fmul_reassoc(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
13781378
unsafe {
13791379
let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1380-
llvm::LLVMRustSetAlgebraicMath(instr);
1380+
llvm::LLVMRustSetAllowReassoc(instr);
13811381
instr
13821382
}
13831383
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1880,14 +1880,14 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
18801880
arith_red!(simd_reduce_mul_ordered: vector_reduce_mul, vector_reduce_fmul, true, mul, 1.0);
18811881
arith_red!(
18821882
simd_reduce_add_unordered: vector_reduce_add,
1883-
vector_reduce_fadd_algebraic,
1883+
vector_reduce_fadd_reassoc,
18841884
false,
18851885
add,
18861886
0.0
18871887
);
18881888
arith_red!(
18891889
simd_reduce_mul_unordered: vector_reduce_mul,
1890-
vector_reduce_fmul_algebraic,
1890+
vector_reduce_fmul_reassoc,
18911891
false,
18921892
mul,
18931893
1.0

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ extern "C" {
16191619

16201620
pub fn LLVMRustSetFastMath(Instr: &Value);
16211621
pub fn LLVMRustSetAlgebraicMath(Instr: &Value);
1622+
pub fn LLVMRustSetAllowReassoc(Instr: &Value);
16221623

16231624
// Miscellaneous instructions
16241625
pub fn LLVMRustGetInstrProfIncrementIntrinsic(M: &Module) -> &Value;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,17 @@ extern "C" LLVMAttributeRef LLVMRustCreateMemoryEffectsAttr(LLVMContextRef C,
418418
}
419419
}
420420

421+
<<<<<<< HEAD
421422
// Enable all fast-math flags, including those which will cause floating-point operations
422423
// to return poison for some well-defined inputs. This function can only be used to build
423424
// unsafe Rust intrinsics. That unsafety does permit additional optimizations, but at the
424425
// time of writing, their value is not well-understood relative to those enabled by
425426
// LLVMRustSetAlgebraicMath.
427+
||||||| parent of 019019d83e2 (make simd_reduce_{mul,add}_unordered use only the 'reassoc' flag, not all fast-math flags)
428+
// Enable a fast-math flag
429+
=======
430+
// Enable all fast-math flags
431+
>>>>>>> 019019d83e2 (make simd_reduce_{mul,add}_unordered use only the 'reassoc' flag, not all fast-math flags)
426432
//
427433
// https://llvm.org/docs/LangRef.html#fast-math-flags
428434
extern "C" void LLVMRustSetFastMath(LLVMValueRef V) {
@@ -450,6 +456,15 @@ extern "C" void LLVMRustSetAlgebraicMath(LLVMValueRef V) {
450456
}
451457
}
452458

459+
// Enable the reassoc fast-math flag
460+
//
461+
// https://llvm.org/docs/LangRef.html#fast-math-flags
462+
extern "C" void LLVMRustSetAllowReassoc(LLVMValueRef V) {
463+
if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
464+
I->setHasAllowReassoc(true);
465+
}
466+
}
467+
453468
extern "C" LLVMValueRef
454469
LLVMRustBuildAtomicLoad(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Source,
455470
const char *Name, LLVMAtomicOrdering Order) {

library/core/src/intrinsics/simd.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,12 @@ extern "platform-intrinsic" {
335335
/// Starting with the value `y`, add the elements of `x` and accumulate.
336336
pub fn simd_reduce_add_ordered<T, U>(x: T, y: U) -> U;
337337

338-
/// Add elements within a vector in arbitrary order, and without regard
339-
/// for signed zeros.
338+
/// Add elements within a vector in arbitrary order. May also be re-associated with
339+
/// unordered additions on the inputs/outputs.
340340
///
341341
/// `T` must be a vector of integer or floating-point primitive types.
342342
///
343343
/// `U` must be the element type of `T`.
344-
///
345-
/// # Safety
346-
///
347-
/// All input elements must be finite (i.e., not NAN and not +/- INF).
348344
pub fn simd_reduce_add_unordered<T, U>(x: T) -> U;
349345

350346
/// Multiply elements within a vector from left to right.
@@ -356,16 +352,12 @@ extern "platform-intrinsic" {
356352
/// Starting with the value `y`, multiply the elements of `x` and accumulate.
357353
pub fn simd_reduce_mul_ordered<T, U>(x: T, y: U) -> U;
358354

359-
/// Multiply elements within a vector in arbitrary order, and without regard
360-
/// for signed zeros.
355+
/// Add elements within a vector in arbitrary order. May also be re-associated with
356+
/// unordered additions on the inputs/outputs.
361357
///
362358
/// `T` must be a vector of integer or floating-point primitive types.
363359
///
364360
/// `U` must be the element type of `T`.
365-
///
366-
/// # Safety
367-
///
368-
/// All input elements must be finite (i.e., not NAN and not +/- INF).
369361
pub fn simd_reduce_mul_unordered<T, U>(x: T) -> U;
370362

371363
/// Check if all mask values are true.

0 commit comments

Comments
 (0)