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?

1
  • If we manage to enable this, does this also result in exploitable UB cases being accepted by rustc? Commented Jun 16, 2015 at 13:09

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

4

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

2 Comments

How would you compile the LLVM IR after emitting the .ll file?
You can use llc to link IR (or BC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.