Skip to content

Commit 02310fd

Browse files
committed
Auto merge of #32362 - bluss:float-fast-math, r=eddyb
Fix floating point fast-math intrinsics The implementation did not handle the case where both operands were constants, which caused an llvm assertion: ``` rustc: //buildslave//rust-buildbot//slave//nightly-dist-rustc-musl-linux//build//src//llvm//include/llvm/Support/Casting.h:237: typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X = llvm::Instruction; Y = llvm::Value; typename llvm::cast_retty<X, Y*>::ret_type = llvm::Instruction*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed. ```
2 parents 8eeb506 + ba89b25 commit 02310fd

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/rustllvm/RustWrapper.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ extern "C" void LLVMRemoveFunctionAttrString(LLVMValueRef fn, unsigned index, co
165165
}
166166

167167
// enable fpmath flag UnsafeAlgebra
168-
extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef Instr) {
169-
unwrap<Instruction>(Instr)->setHasUnsafeAlgebra(true);
168+
extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef V) {
169+
if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
170+
I->setHasUnsafeAlgebra(true);
171+
}
170172
}
171173

172174
extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B,

src/test/run-pass/float_math.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212

1313
use std::intrinsics::{fadd_fast, fsub_fast, fmul_fast, fdiv_fast, frem_fast};
1414

15-
fn main() {
15+
#[inline(never)]
16+
pub fn test_operations(a: f64, b: f64) {
1617
// make sure they all map to the correct operation
1718
unsafe {
18-
assert_eq!(fadd_fast(1., 2.), 1. + 2.);
19-
assert_eq!(fsub_fast(1., 2.), 1. - 2.);
20-
assert_eq!(fmul_fast(2., 3.), 2. * 3.);
21-
assert_eq!(fdiv_fast(10., 5.), 10. / 5.);
22-
assert_eq!(frem_fast(10., 5.), 10. % 5.);
19+
assert_eq!(fadd_fast(a, b), a + b);
20+
assert_eq!(fsub_fast(a, b), a - b);
21+
assert_eq!(fmul_fast(a, b), a * b);
22+
assert_eq!(fdiv_fast(a, b), a / b);
23+
assert_eq!(frem_fast(a, b), a % b);
2324
}
2425
}
26+
27+
fn main() {
28+
test_operations(1., 2.);
29+
test_operations(10., 5.);
30+
}

0 commit comments

Comments
 (0)