Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/cuda_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ pub struct CudaBuilder {
/// Enable FMA (fused multiply-add) contraction.
/// `true` by default.
pub fma_contraction: bool,
/// Enable fast math approximations globally (equivalent to NVCC's --use_fast_math).
/// This implies ftz=true, prec-div=false, prec-sqrt=false, and fmad=true.
/// `false` by default.
pub fast_math: bool,
/// Whether to emit a certain IR. Emitting LLVM IR is useful to debug any codegen
/// issues. If you are submitting a bug report try to include the LLVM IR file of
/// the program that contains the offending function.
Expand Down Expand Up @@ -206,6 +210,7 @@ impl CudaBuilder {
nvvm_opts: true,
arch: NvvmArch::default(),
ftz: false,
fast_math: false,
fast_sqrt: false,
fast_div: false,
fma_contraction: true,
Expand Down Expand Up @@ -266,6 +271,19 @@ impl CudaBuilder {
self
}

/// Enable fast math approximations globally (equivalent to NVCC's --use_fast_math).
/// This implies ftz=true, prec-div=false, prec-sqrt=false, and fmad=true.
pub fn fast_math(mut self, fast_math: bool) -> Self {
self.fast_math = fast_math;
if fast_math {
self.ftz = true;
self.fast_sqrt = true;
self.fast_div = true;
self.fma_contraction = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a redundant representation. You're storing fast_math and also the individual flags. It should do one or the other.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, nothing happens if fast_math is false. Is that intended?

}
self
}

/// Use a fast approximation for single-precision floating point square root.
pub fn fast_sqrt(mut self, fast_sqrt: bool) -> Self {
self.fast_sqrt = fast_sqrt;
Expand Down Expand Up @@ -725,6 +743,10 @@ fn invoke_rustc(builder: &CudaBuilder) -> Result<PathBuf, CudaBuilderError> {
llvm_args.push("-ftz=1".to_string());
}

if builder.fast_math {
llvm_args.push("--use_fast_math".to_string());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be plumbed through to NVCC, or should it be handled by CudaBuilder which then specifies the individual flags?


if builder.fast_sqrt {
llvm_args.push("-prec-sqrt=0".to_string());
}
Expand Down