How do I compile with "ffast-math"?

3.1k views Asked by At

I'm trying to benchmark some Rust code, but I can't figure out how to set the "ffast-math" option.

% rustc -C opt-level=3 -C llvm-args='-enable-unsafe-fp-math' unrolled.rs
rustc: Unknown command line argument '-enable-unsafe-fp-math'.  Try: 'rustc -help'
rustc: Did you mean '-enable-load-pre'?

llvm-args='-ffast-math' and llvm-args='-fast' didn't work either. What flag should I be using?

2

There are 2 answers

2
llogiq On

You can always use rustc --emit llvm-ir and compile the LLVM IR with the desired settings.

0
Shepmaster On

Rust issue #21690 talks about adding imprecise floating point operations. Linked from that issue is the addition of intrinsics that allow you to opt into looser rules on a per operation basis. For example, fadd_fast:

pub unsafe extern "rust-intrinsic" fn fadd_fast<T>(a: T, b: T) -> T

Using intrinsics requires a nightly compiler and unsafe code:

#![feature(core_intrinsics)]

use std::intrinsics::fadd_fast;

fn main() {
    let result = unsafe { fadd_fast(42.0, 31.0) };
    println!("{}", result);
}

Ultimately, this is a much better design than the all-or-nothing solution of a command line flag. Who knows if there is some floating point calculation that is critical to not use fast math, buried deep in your program? That doesn't help you when trying to compare against a C program that chose that, however!