[PATCH] D66078: Added RAII object for authomatic restore of fp state
Serge Pavlov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 12 03:38:24 PDT 2019
sepavloff created this revision.
sepavloff added reviewers: rjmccall, kpn, andrew.w.kaylor.
Herald added a project: LLVM.
Class IRBuilderBase has now RAII class that helps to restore floating
point options, including default rounding and exceptions modes.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D66078
Files:
llvm/include/llvm/IR/IRBuilder.h
llvm/include/llvm/IR/IntrinsicInst.h
Index: llvm/include/llvm/IR/IntrinsicInst.h
===================================================================
--- llvm/include/llvm/IR/IntrinsicInst.h
+++ llvm/include/llvm/IR/IntrinsicInst.h
@@ -212,11 +212,11 @@
/// when constrained floating point is enabled. See the LLVM Language
/// Reference Manual for details.
enum RoundingMode : uint8_t {
- rmDynamic, ///< This corresponds to "fpround.dynamic".
rmToNearest, ///< This corresponds to "fpround.tonearest".
rmDownward, ///< This corresponds to "fpround.downward".
rmUpward, ///< This corresponds to "fpround.upward".
- rmTowardZero ///< This corresponds to "fpround.tozero".
+ rmTowardZero, ///< This corresponds to "fpround.tozero".
+ rmDynamic ///< This corresponds to "fpround.dynamic".
};
/// Specifies the required exception behavior. This is only used when
Index: llvm/include/llvm/IR/IRBuilder.h
===================================================================
--- llvm/include/llvm/IR/IRBuilder.h
+++ llvm/include/llvm/IR/IRBuilder.h
@@ -96,8 +96,14 @@
MDNode *DefaultFPMathTag;
FastMathFlags FMF;
+ /// If set, builder creates constrained floating point operations.
bool IsFPConstrained;
+
+ /// Exception behavior used by default for constrained floating point
+ /// operations.
ConstrainedFPIntrinsic::ExceptionBehavior DefaultConstrainedExcept;
+
+ /// Rounding mode used by default for constrained floating point operations.
ConstrainedFPIntrinsic::RoundingMode DefaultConstrainedRounding;
ArrayRef<OperandBundleDef> DefaultOperandBundles;
@@ -301,6 +307,36 @@
}
};
+ // RAII object that stores constrained floating point settings and
+ // restores them when the object is destroyed.
+ class FloatingPointStateSaver {
+ IRBuilderBase &Builder;
+ bool IsFPConstrainted;
+ ConstrainedFPIntrinsic::ExceptionBehavior DefaultConstrainedExcept;
+ ConstrainedFPIntrinsic::RoundingMode DefaultConstrainedRounding;
+
+ public:
+ FloatingPointStateSaver(IRBuilderBase &Builder)
+ : Builder(Builder),
+ IsFPConstrainted(Builder.getIsFPConstrained()),
+ DefaultConstrainedExcept(Builder.getDefaultConstrainedExcept()),
+ DefaultConstrainedRounding(Builder.getDefaultConstrainedRounding()) {
+ }
+ FloatingPointStateSaver(IRBuilderBase &Builder, bool NewIsFPConstrained,
+ ConstrainedFPIntrinsic::ExceptionBehavior NewDefaultConstrainedExcept,
+ ConstrainedFPIntrinsic::RoundingMode NewDefaultConstrainedRounding)
+ : FloatingPointStateSaver(Builder) {
+ Builder.setIsFPConstrained(NewIsFPConstrained);
+ Builder.setDefaultConstrainedExcept(NewDefaultConstrainedExcept);
+ Builder.setDefaultConstrainedRounding(NewDefaultConstrainedRounding);
+ }
+ ~FloatingPointStateSaver() {
+ Builder.setDefaultConstrainedRounding(DefaultConstrainedRounding);
+ Builder.setDefaultConstrainedExcept(DefaultConstrainedExcept);
+ Builder.setIsFPConstrained(IsFPConstrainted);
+ }
+ };
+
//===--------------------------------------------------------------------===//
// Miscellaneous creation methods.
//===--------------------------------------------------------------------===//
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66078.214595.patch
Type: text/x-patch
Size: 3309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190812/1e4f948d/attachment.bin>
More information about the llvm-commits
mailing list