21

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?

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
yong
  • 3,583
  • 16
  • 32
  • If we manage to enable this, does this also result in exploitable UB cases being accepted by rustc? – bluss Jun 16 '15 at 13:09

2 Answers2

11

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!

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
4

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

llogiq
  • 13,815
  • 8
  • 40
  • 72