Skip to content

Commit

Permalink
Reapply af57dbf "Add support for options -frounding-math, ftrapping-…
Browse files Browse the repository at this point in the history
…math, -ffp-model=, and -ffp-exception-behavior="

        Patch was reverted because https://bugs.llvm.org/show_bug.cgi?id=44048
        The original patch is modified to set the strictfp IR attribute
        explicitly in CodeGen instead of as a side effect of IRBuilder

        Differential Revision: https://reviews.llvm.org/D62731
  • Loading branch information
Melanie Blower committed Dec 4, 2019
1 parent 75bbbee commit cdbed2d
Show file tree
Hide file tree
Showing 25 changed files with 638 additions and 23 deletions.
54 changes: 50 additions & 4 deletions clang/docs/UsersManual.rst
Expand Up @@ -1231,10 +1231,10 @@ are listed below.

**-f[no-]trapping-math**

``-fno-trapping-math`` allows optimizations that assume that
floating point operations cannot generate traps such as divide-by-zero,
overflow and underflow. Defaults to ``-ftrapping-math``.
Currently this option has no effect.
Control floating point exception behavior. ``-fno-trapping-math`` allows optimizations that assume that floating point operations cannot generate traps such as divide-by-zero, overflow and underflow.

- The option ``-ftrapping-math`` behaves identically to ``-ffp-exception-behavior=strict``.
- The option ``-fno-trapping-math`` behaves identically to ``-ffp-exception-behavior=ignore``. This is the default.

.. option:: -ffp-contract=<value>

Expand Down Expand Up @@ -1319,6 +1319,52 @@ are listed below.

Defaults to ``-fno-finite-math``.

.. _opt_frounding-math:

**-f[no-]rounding-math**

Force floating-point operations to honor the dynamically-set rounding mode by default.

The result of a floating-point operation often cannot be exactly represented in the result type and therefore must be rounded. IEEE 754 describes different rounding modes that control how to perform this rounding, not all of which are supported by all implementations. C provides interfaces (``fesetround`` and ``fesetenv``) for dynamically controlling the rounding mode, and while it also recommends certain conventions for changing the rounding mode, these conventions are not typically enforced in the ABI. Since the rounding mode changes the numerical result of operations, the compiler must understand something about it in order to optimize floating point operations.

Note that floating-point operations performed as part of constant initialization are formally performed prior to the start of the program and are therefore not subject to the current rounding mode. This includes the initialization of global variables and local ``static`` variables. Floating-point operations in these contexts will be rounded using ``FE_TONEAREST``.

- The option ``-fno-rounding-math`` allows the compiler to assume that the rounding mode is set to ``FE_TONEAREST``. This is the default.
- The option ``-frounding-math`` forces the compiler to honor the dynamically-set rounding mode. This prevents optimizations which might affect results if the rounding mode changes or is different from the default; for example, it prevents floating-point operations from being reordered across most calls and prevents constant-folding when the result is not exactly representable.

.. option:: -ffp-model=<value>

Specify floating point behavior. ``-ffp-model`` is an umbrella
option that encompasses functionality provided by other, single
purpose, floating point options. Valid values are: ``precise``, ``strict``,
and ``fast``.
Details:

* ``precise`` Disables optimizations that are not value-safe on floating-point data, although FP contraction (FMA) is enabled (``-ffp-contract=fast``). This is the default behavior.
* ``strict`` Enables ``-frounding-math`` and ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All of the ``-ffast-math`` enablements are disabled.
* ``fast`` Behaves identically to specifying both ``-ffast-math`` and ``ffp-contract=fast``

Note: If your command line specifies multiple instances
of the ``-ffp-model`` option, or if your command line option specifies
``-ffp-model`` and later on the command line selects a floating point
option that has the effect of negating part of the ``ffp-model`` that
has been selected, then the compiler will issue a diagnostic warning
that the override has occurred.

.. option:: -ffp-exception-behavior=<value>

Specify the floating-point exception behavior.

Valid values are: ``ignore``, ``maytrap``, and ``strict``.
The default value is ``ignore``. Details:

* ``ignore`` The compiler assumes that the exception status flags will not be read and that floating point exceptions will be masked.
* ``maytrap`` The compiler avoids transformations that may raise exceptions that would not have been raised by the original code. Constant folding performed by the compiler is exempt from this option.
* ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code.




.. _controlling-code-generation:

Controlling Code Generation
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/Decl.h
Expand Up @@ -2196,6 +2196,10 @@ class FunctionDecl : public DeclaratorDecl,
bool usesSEHTry() const { return FunctionDeclBits.UsesSEHTry; }
void setUsesSEHTry(bool UST) { FunctionDeclBits.UsesSEHTry = UST; }

/// Indicates the function uses Floating Point constrained intrinsics
bool usesFPIntrin() const { return FunctionDeclBits.UsesFPIntrin; }
void setUsesFPIntrin(bool Val) { FunctionDeclBits.UsesFPIntrin = Val; }

/// Whether this function has been deleted.
///
/// A function that is "deleted" (via the C++0x "= delete" syntax)
Expand Down
7 changes: 5 additions & 2 deletions clang/include/clang/AST/DeclBase.h
Expand Up @@ -1534,10 +1534,13 @@ class DeclContext {

/// Store the ODRHash after first calculation.
uint64_t HasODRHash : 1;

/// Indicates if the function uses Floating Point Constrained Intrinsics
uint64_t UsesFPIntrin : 1;
};

/// Number of non-inherited bits in FunctionDeclBitfields.
enum { NumFunctionDeclBits = 25 };
enum { NumFunctionDeclBits = 26 };

/// Stores the bits used by CXXConstructorDecl. If modified
/// NumCXXConstructorDeclBits and the accessor
Expand All @@ -1554,7 +1557,7 @@ class DeclContext {
/// exactly 64 bits and thus the width of NumCtorInitializers
/// will need to be shrunk if some bit is added to NumDeclContextBitfields,
/// NumFunctionDeclBitfields or CXXConstructorDeclBitfields.
uint64_t NumCtorInitializers : 23;
uint64_t NumCtorInitializers : 22;
uint64_t IsInheritingConstructor : 1;

/// Whether this constructor has a trail-allocated explicit specifier.
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Expand Up @@ -441,6 +441,10 @@ def warn_drv_experimental_isel_incomplete_opt : Warning<
"-fexperimental-isel support is incomplete for this architecture at the current optimization level">,
InGroup<ExperimentalISel>;

def warn_drv_experimental_fp_control_incomplete_opt : Warning<
"Support for floating point control option %0 is incomplete and experimental">,
InGroup<ExperimentalFloatControl>;

def warn_drv_moutline_unsupported_opt : Warning<
"The '%0' architecture does not support -moutline; flag ignored">,
InGroup<OptionIgnored>;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticGroups.td
Expand Up @@ -1109,6 +1109,9 @@ def SpirCompat : DiagGroup<"spir-compat">;
// Warning for the experimental-isel options.
def ExperimentalISel : DiagGroup<"experimental-isel">;

// Warning for the experimental float control options.
def ExperimentalFloatControl : DiagGroup<"experimental-float-control">;

// A warning group specifically for warnings related to function
// multiversioning.
def FunctionMultiVersioning : DiagGroup<"function-multiversion">;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/LangOptions.def
Expand Up @@ -255,6 +255,8 @@ LANGOPT(SinglePrecisionConstants , 1, 0, "treating double-precision floating poi
LANGOPT(FastRelaxedMath , 1, 0, "OpenCL fast relaxed math")
/// FP_CONTRACT mode (on/off/fast).
ENUM_LANGOPT(DefaultFPContractMode, FPContractModeKind, 2, FPC_Off, "FP contraction type")
ENUM_LANGOPT(FPRoundingMode, FPRoundingModeKind, 3, FPR_ToNearest, "FP Rounding Mode type")
ENUM_LANGOPT(FPExceptionMode, FPExceptionModeKind, 2, FPE_Ignore, "FP Exception Behavior Mode type")
LANGOPT(NoBitFieldTypeAlign , 1, 0, "bit-field type alignment")
LANGOPT(HexagonQdsp6Compat , 1, 0, "hexagon-qdsp6 backward compatibility")
LANGOPT(ObjCAutoRefCount , 1, 0, "Objective-C automated reference counting")
Expand Down
28 changes: 28 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Expand Up @@ -190,6 +190,34 @@ class LangOptions : public LangOptionsBase {
FEA_On
};

// Values of the following enumerations correspond to metadata arguments
// specified for constrained floating-point intrinsics:
// http://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics.

/// Possible rounding modes.
enum FPRoundingModeKind {
/// Rounding to nearest, corresponds to "round.tonearest".
FPR_ToNearest,
/// Rounding toward -Inf, corresponds to "round.downward".
FPR_Downward,
/// Rounding toward +Inf, corresponds to "round.upward".
FPR_Upward,
/// Rounding toward zero, corresponds to "round.towardzero".
FPR_TowardZero,
/// Is determined by runtime environment, corresponds to "round.dynamic".
FPR_Dynamic
};

/// Possible floating point exception behavior.
enum FPExceptionModeKind {
/// Assume that floating-point exceptions are masked.
FPE_Ignore,
/// Transformations do not cause new exceptions but may hide some.
FPE_MayTrap,
/// Strictly preserve the floating-point exception semantics.
FPE_Strict
};

enum class LaxVectorConversionKind {
/// Permit no implicit vector bitcasts.
None,
Expand Down
7 changes: 6 additions & 1 deletion clang/include/clang/Driver/Options.td
Expand Up @@ -929,6 +929,10 @@ def : Flag<["-"], "fextended-identifiers">, Group<clang_ignored_f_Group>;
def : Flag<["-"], "fno-extended-identifiers">, Group<f_Group>, Flags<[Unsupported]>;
def fhosted : Flag<["-"], "fhosted">, Group<f_Group>;
def fdenormal_fp_math_EQ : Joined<["-"], "fdenormal-fp-math=">, Group<f_Group>, Flags<[CC1Option]>;
def ffp_model_EQ : Joined<["-"], "ffp-model=">, Group<f_Group>, Flags<[DriverOption]>,
HelpText<"Controls the semantics of floating-point calculations.">;
def ffp_exception_behavior_EQ : Joined<["-"], "ffp-exception-behavior=">, Group<f_Group>, Flags<[CC1Option]>,
HelpText<"Specifies the exception behavior of floating-point operations.">;
def ffast_math : Flag<["-"], "ffast-math">, Group<f_Group>, Flags<[CC1Option]>,
HelpText<"Allow aggressive, lossy floating-point optimizations">;
def fno_fast_math : Flag<["-"], "fno-fast-math">, Group<f_Group>;
Expand Down Expand Up @@ -1154,6 +1158,8 @@ def fno_honor_infinities : Flag<["-"], "fno-honor-infinities">, Group<f_Group>;
// This option was originally misspelt "infinites" [sic].
def : Flag<["-"], "fhonor-infinites">, Alias<fhonor_infinities>;
def : Flag<["-"], "fno-honor-infinites">, Alias<fno_honor_infinities>;
def frounding_math : Flag<["-"], "frounding-math">, Group<f_Group>, Flags<[CC1Option]>;
def fno_rounding_math : Flag<["-"], "fno-rounding-math">, Group<f_Group>, Flags<[CC1Option]>;
def ftrapping_math : Flag<["-"], "ftrapping-math">, Group<f_Group>, Flags<[CC1Option]>;
def fno_trapping_math : Flag<["-"], "fno-trapping-math">, Group<f_Group>, Flags<[CC1Option]>;
def ffp_contract : Joined<["-"], "ffp-contract=">, Group<f_Group>,
Expand Down Expand Up @@ -3242,7 +3248,6 @@ defm profile_values : BooleanFFlag<"profile-values">, Group<clang_ignored_gcc_op
defm regs_graph : BooleanFFlag<"regs-graph">, Group<clang_ignored_f_Group>;
defm rename_registers : BooleanFFlag<"rename-registers">, Group<clang_ignored_gcc_optimization_f_Group>;
defm ripa : BooleanFFlag<"ripa">, Group<clang_ignored_f_Group>;
defm rounding_math : BooleanFFlag<"rounding-math">, Group<clang_ignored_gcc_optimization_f_Group>;
defm schedule_insns : BooleanFFlag<"schedule-insns">, Group<clang_ignored_gcc_optimization_f_Group>;
defm schedule_insns2 : BooleanFFlag<"schedule-insns2">, Group<clang_ignored_gcc_optimization_f_Group>;
defm see : BooleanFFlag<"see">, Group<clang_ignored_f_Group>;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/Decl.cpp
Expand Up @@ -2793,6 +2793,7 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
FunctionDeclBits.ConstexprKind = ConstexprKind;
FunctionDeclBits.InstantiationIsPending = false;
FunctionDeclBits.UsesSEHTry = false;
FunctionDeclBits.UsesFPIntrin = false;
FunctionDeclBits.HasSkippedBody = false;
FunctionDeclBits.WillHaveBody = false;
FunctionDeclBits.IsMultiVersion = false;
Expand Down
14 changes: 14 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Expand Up @@ -4336,6 +4336,13 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Callee.getAbstractInfo(), Attrs, CallingConv,
/*AttrOnCallSite=*/true);

if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
if (FD->usesFPIntrin())
// All calls within a strictfp function are marked strictfp
Attrs =
Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
llvm::Attribute::StrictFP);

// Apply some call-site-specific attributes.
// TODO: work this into building the attribute set.

Expand Down Expand Up @@ -4385,6 +4392,13 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
SmallVector<llvm::OperandBundleDef, 1> BundleList =
getBundlesForFunclet(CalleePtr);

if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
if (FD->usesFPIntrin())
// All calls within a strictfp function are marked strictfp
Attrs =
Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
llvm::Attribute::StrictFP);

// Emit the actual call/invoke instruction.
llvm::CallBase *CI;
if (!InvokeDest) {
Expand Down
52 changes: 52 additions & 0 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Expand Up @@ -33,6 +33,8 @@
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/FPEnv.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Operator.h"
Expand Down Expand Up @@ -87,6 +89,7 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
FMF.setAllowReassoc();
}
Builder.setFastMathFlags(FMF);
SetFPModel();
}

CodeGenFunction::~CodeGenFunction() {
Expand All @@ -102,6 +105,51 @@ CodeGenFunction::~CodeGenFunction() {
CGM.getOpenMPRuntime().functionFinished(*this);
}

// Map the LangOption for rounding mode into
// the corresponding enum in the IR.
static llvm::fp::RoundingMode ToConstrainedRoundingMD(
LangOptions::FPRoundingModeKind Kind) {

switch (Kind) {
case LangOptions::FPR_ToNearest: return llvm::fp::rmToNearest;
case LangOptions::FPR_Downward: return llvm::fp::rmDownward;
case LangOptions::FPR_Upward: return llvm::fp::rmUpward;
case LangOptions::FPR_TowardZero: return llvm::fp::rmTowardZero;
case LangOptions::FPR_Dynamic: return llvm::fp::rmDynamic;
}
llvm_unreachable("Unsupported FP RoundingMode");
}

// Map the LangOption for exception behavior into
// the corresponding enum in the IR.
static llvm::fp::ExceptionBehavior ToConstrainedExceptMD(
LangOptions::FPExceptionModeKind Kind) {

switch (Kind) {
case LangOptions::FPE_Ignore: return llvm::fp::ebIgnore;
case LangOptions::FPE_MayTrap: return llvm::fp::ebMayTrap;
case LangOptions::FPE_Strict: return llvm::fp::ebStrict;
}
llvm_unreachable("Unsupported FP Exception Behavior");
}

void CodeGenFunction::SetFPModel() {
auto fpRoundingMode = ToConstrainedRoundingMD(
getLangOpts().getFPRoundingMode());
auto fpExceptionBehavior = ToConstrainedExceptMD(
getLangOpts().getFPExceptionMode());

if (fpExceptionBehavior == llvm::fp::ebIgnore &&
fpRoundingMode == llvm::fp::rmToNearest)
// Constrained intrinsics are not used.
;
else {
Builder.setIsFPConstrained(true);
Builder.setDefaultConstrainedRounding(fpRoundingMode);
Builder.setDefaultConstrainedExcept(fpExceptionBehavior);
}
}

CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T,
LValueBaseInfo *BaseInfo,
TBAAAccessInfo *TBAAInfo) {
Expand Down Expand Up @@ -823,6 +871,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (FD->isMain())
Fn->addFnAttr(llvm::Attribute::NoRecurse);

if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
if (FD->usesFPIntrin())
Fn->addFnAttr(llvm::Attribute::StrictFP);

// If a custom alignment is used, force realigning to this alignment on
// any main function which certainly will need it.
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CodeGenFunction.h
Expand Up @@ -4169,6 +4169,9 @@ class CodeGenFunction : public CodeGenTypeCache {
/// point operation, expressed as the maximum relative error in ulp.
void SetFPAccuracy(llvm::Value *Val, float Accuracy);

/// SetFPModel - Control floating point behavior via fp-model settings.
void SetFPModel();

private:
llvm::MDNode *getRangeForLoadFromType(QualType Ty);
void EmitReturnOfRValue(RValue RV, QualType Ty);
Expand Down

0 comments on commit cdbed2d

Please sign in to comment.