Skip to content

Commit 405778a

Browse files
authored
[SYCL][FE][Driver] Implement floating point accuracy control (#8280)
This patch implements the accuracy controls for floating-point math functions in DPC++. Using the -ffp-accuracy command line option, the user can request an accuracy level for all math functions or for specific ones. Calls to fpbuiltin intrinsics llvm.fpbuilin.* are then generated. Syntax: Linux: -ffp-accuracy=[default|value][:funclist] Windows: /Qfp-accuracy:[default|value][:funclist] funclist is an optional comma separated list of math library functions. -ffp-accuracy=[default|value] default: Use the implementation defined accuracy for all math library functions. This is equivalent to not using this option. value: Use the defined standard accuracy for what each accuracy value means for all math library functions. -ffp-accuracy=[default|value][:funclist] default: Use the implementation defined accuracy for the math library functions in funclist. This is equivalent to not using this option. value: Use the defined standard accuracy for what each accuracy value means for the math library functions in funclist. value is one of the following values denoting the library function accuracy. high This is equivalent to max-error = 1.0. medium This is equivalent to max-error = 4. low This is equivalent to accuracy-bits = 11 for single-precision functions. accuracy-bits = 26 for double-precision functions. sycl Determined by the OpenCL specification for math function accuracy: https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_C.html#relative-error-as-ulps cuda Determined by standard https://docs.nvidia.com/cuda/cuda-c-programming-guide/#mathematical-functions-appendix
1 parent 054ed1c commit 405778a

File tree

17 files changed

+907
-44
lines changed

17 files changed

+907
-44
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ CODEGENOPT(VirtualFunctionElimination, 1, 0) ///< Whether to apply the dead
373373
/// virtual function elimination
374374
/// optimization.
375375

376+
/// Whether accuracy levels for math library functions are requested by the
377+
/// user. These accuracy levels will then be expressed in terms of ULPs.
378+
CODEGENOPT(FPAccuracy, 1, 0)
379+
376380
/// Whether to use public LTO visibility for entities in std and stdext
377381
/// namespaces. This is enabled by clang-cl's /MT and /MTd flags.
378382
CODEGENOPT(LTOVisibilityPublicStd, 1, 0)

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ def warn_stack_clash_protection_inline_asm : Warning<
301301
def warn_slh_does_not_support_asm_goto : Warning<
302302
"speculative load hardening does not protect functions with asm goto">,
303303
InGroup<DiagGroup<"slh-asm-goto">>;
304+
305+
def err_drv_incompatible_options : Error<
306+
"the combination of '%0' and '%1' is incompatible">;
304307
}
305308

306309
// Sema && Serialization

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def err_drv_no_cuda_libdevice : Error<
6363
"via '--cuda-path', or pass '-nocudalib' to build without linking with "
6464
"libdevice">;
6565

66+
def warn_function_fp_accuracy_already_set : Warning <
67+
"floating point accuracy value of '%0' has already been assigned to "
68+
"function '%1'">,
69+
InGroup<DiagGroup<"fp-accuracy-already-set">>;
6670
def err_drv_no_rocm_device_lib : Error<
6771
"cannot find ROCm device library%select{| for %1|for ABI version %1}0; provide its path via "
6872
"'--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build "
@@ -141,8 +145,9 @@ def err_drv_invalid_unwindlib_name : Error<
141145
"invalid unwind library name in argument '%0'">;
142146
def err_drv_incompatible_unwindlib : Error<
143147
"--rtlib=libgcc requires --unwindlib=libgcc">;
144-
def err_drv_incompatible_options : Error<
145-
"the combination of '%0' and '%1' is incompatible">;
148+
def err_drv_incompatible_fp_accuracy_options : Error<
149+
"floating point accuracy requirements cannot be guaranteed when '-fmath-errno' "
150+
"is enabled; use '-fno-math-errno' to enable floating point accuracy control">;
146151
def err_drv_invalid_stdlib_name : Error<
147152
"invalid library name in argument '%0'">;
148153
def err_drv_invalid_output_with_multiple_archs : Error<

clang/include/clang/Basic/FPOptions.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ OPTION(AllowReciprocal, bool, 1, NoSignedZero)
2626
OPTION(AllowApproxFunc, bool, 1, AllowReciprocal)
2727
OPTION(FPEvalMethod, LangOptions::FPEvalMethodKind, 2, AllowApproxFunc)
2828
OPTION(Float16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, FPEvalMethod)
29-
OPTION(BFloat16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, FPEvalMethod)
29+
OPTION(BFloat16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, Float16ExcessPrecision)
30+
OPTION(FPAccuracy, LangOptions::FPAccuracyKind, 3, BFloat16ExcessPrecision)
3031
#undef OPTION

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ BENIGN_ENUM_LANGOPT(FPExceptionMode, FPExceptionModeKind, 2, FPE_Default, "FP Ex
338338
BENIGN_ENUM_LANGOPT(FPEvalMethod, FPEvalMethodKind, 2, FEM_UnsetOnCommandLine, "FP type used for floating point arithmetic")
339339
ENUM_LANGOPT(Float16ExcessPrecision, ExcessPrecisionKind, 2, FPP_Standard, "Intermediate truncation behavior for Float16 arithmetic")
340340
ENUM_LANGOPT(BFloat16ExcessPrecision, ExcessPrecisionKind, 2, FPP_Standard, "Intermediate truncation behavior for BFloat16 arithmetic")
341+
BENIGN_ENUM_LANGOPT(FPAccuracy, FPAccuracyKind, 3, FPA_Default, "Accuracy for floating point operations and library functions")
341342
LANGOPT(NoBitFieldTypeAlign , 1, 0, "bit-field type alignment")
342343
LANGOPT(HexagonQdsp6Compat , 1, 0, "hexagon-qdsp6 backward compatibility")
343344
LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated reference counting")

clang/include/clang/Basic/LangOptions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ class LangOptions : public LangOptionsBase {
303303

304304
enum ExcessPrecisionKind { FPP_Standard, FPP_Fast, FPP_None };
305305

306+
enum FPAccuracyKind {
307+
FPA_Default,
308+
FPA_High,
309+
FPA_Medium,
310+
FPA_Low,
311+
FPA_Sycl,
312+
FPA_Cuda,
313+
};
314+
306315
/// Possible exception handling behavior.
307316
enum class ExceptionHandlingKind { None, SjLj, WinEH, DwarfCFI, Wasm };
308317

@@ -509,6 +518,10 @@ class LangOptions : public LangOptionsBase {
509518
/// records.
510519
std::string OptRecordFile;
511520

521+
std::string FPAccuracyVal;
522+
using FPAccuracyFuncMapTy = std::map<std::string, std::string>;
523+
FPAccuracyFuncMapTy FPAccuracyFuncMap;
524+
512525
LangOptions();
513526

514527
/// Set language defaults for the given input language and

clang/include/clang/Driver/Options.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,13 @@ def ffp_exception_behavior_EQ : Joined<["-"], "ffp-exception-behavior=">, Group<
17291729
Values<"ignore,maytrap,strict">, NormalizedValuesScope<"LangOptions">,
17301730
NormalizedValues<["FPE_Ignore", "FPE_MayTrap", "FPE_Strict"]>,
17311731
MarshallingInfoEnum<LangOpts<"FPExceptionMode">, "FPE_Default">;
1732+
def ffp_accuracy_EQ : Joined<["-"], "ffp-accuracy=">, Group<f_Group>, Flags<[CC1Option]>,
1733+
HelpText<"Specifies the required accuracy for floating-point operations and library calls.">,
1734+
Values<"default,high,medium,low,sycl,cuda">, NormalizedValuesScope<"LangOptions">,
1735+
NormalizedValues<["FPA_Default", "FPA_High", "FPA_Medium", "FPA_Low", "FPA_Sycl", "FPA_Cuda"]>,
1736+
MarshallingInfoEnum<LangOpts<"FPAccuracy">, "FPA_Default">;
1737+
def ffp_builtin_accuracy_EQ : Joined<["-"], "ffp-builtin-accuracy=">, Group<f_Group>, Flags<[CC1Option]>;
1738+
17321739
defm fast_math : BoolFOption<"fast-math",
17331740
LangOpts<"FastMath">, DefaultFalse,
17341741
PosFlag<SetTrue, [CC1Option, CoreOption, FC1Option, FlangOption], "Allow aggressive, lossy floating-point optimizations",
@@ -7020,6 +7027,14 @@ class CLRemainingArgsJoined<string name> : Option<["/", "-"], name,
70207027
// (We don't put any of these in cl_compile_Group as the options they alias are
70217028
// already in the right group.)
70227029

7030+
// INTEL_CUSTOMIZATION
7031+
def _SLASH_Qfp_accuracy_EQ : CLJoined<"Qfp-accuracy=">,
7032+
Alias<ffp_accuracy_EQ>;
7033+
def _SLASH_Qfp_accuracy_COL : CLJoined<"Qfp-accuracy:">,
7034+
Alias<ffp_accuracy_EQ>,HelpText<"Specifies the required accuracy for "
7035+
"floating-point operations and library calls.">;
7036+
// END INTEL_CUSTOMIZATION
7037+
70237038
def _SLASH_Brepro : CLFlag<"Brepro">,
70247039
HelpText<"Do not write current time into COFF output (breaks link.exe /incremental)">,
70257040
Alias<mno_incremental_linker_compatible>;

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ class CompilerInvocation : public CompilerInvocationRefBase,
276276
std::vector<std::string> &Includes,
277277
DiagnosticsEngine &Diags);
278278

279+
static void ParseFpAccuracyArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
280+
DiagnosticsEngine &Diags);
281+
279282
/// Generate command line options from LangOptions.
280283
static void GenerateLangArgs(const LangOptions &Opts,
281284
SmallVectorImpl<const char *> &Args,

0 commit comments

Comments
 (0)