Skip to content

Commit ea8678d

Browse files
committed
Move floating point related entities to namespace level
This is recommit of commit e6584b2, which was reverted in 30e7ee3 together with af57dbf. Original message is below. Enumerations that describe rounding mode and exception behavior were defined inside ConstrainedFPIntrinsic. It makes sense to use the same definitions to represent the same properties in other cases, not only in constrained intrinsics. It was however inconvenient as required to include constrained intrinsics definitions even if they were not needed. Also using long scope prefix reduced readability. This change moves these definitioins to the namespace llvm::fp. No functional changes. Differential Revision: https://reviews.llvm.org/D69552
1 parent c502bae commit ea8678d

File tree

8 files changed

+198
-157
lines changed

8 files changed

+198
-157
lines changed

llvm/include/llvm/IR/FPEnv.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===- FPEnv.h ---- FP Environment ------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// @file
10+
/// This file contains the declarations of entities that describe floating
11+
/// point environment and related functions.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_IR_FLOATINGPOINT_H
16+
#define LLVM_IR_FLOATINGPOINT_H
17+
18+
#include "llvm/ADT/Optional.h"
19+
#include "llvm/ADT/StringRef.h"
20+
#include <stdint.h>
21+
22+
namespace llvm {
23+
24+
namespace fp {
25+
26+
/// Rounding mode used for floating point operations.
27+
///
28+
/// Each of these values correspond to some metadata argument value of a
29+
/// constrained floating point intrinsic. See the LLVM Language Reference Manual
30+
/// for details.
31+
enum RoundingMode : uint8_t {
32+
rmDynamic, ///< This corresponds to "fpround.dynamic".
33+
rmToNearest, ///< This corresponds to "fpround.tonearest".
34+
rmDownward, ///< This corresponds to "fpround.downward".
35+
rmUpward, ///< This corresponds to "fpround.upward".
36+
rmTowardZero ///< This corresponds to "fpround.tozero".
37+
};
38+
39+
/// Exception behavior used for floating point operations.
40+
///
41+
/// Each of these values correspond to some metadata argument value of a
42+
/// constrained floating point intrinsic. See the LLVM Language Reference Manual
43+
/// for details.
44+
enum ExceptionBehavior : uint8_t {
45+
ebIgnore, ///< This corresponds to "fpexcept.ignore".
46+
ebMayTrap, ///< This corresponds to "fpexcept.maytrap".
47+
ebStrict ///< This corresponds to "fpexcept.strict".
48+
};
49+
50+
}
51+
52+
/// Returns a valid RoundingMode enumerator when given a string
53+
/// that is valid as input in constrained intrinsic rounding mode
54+
/// metadata.
55+
Optional<fp::RoundingMode> StrToRoundingMode(StringRef);
56+
57+
/// For any RoundingMode enumerator, returns a string valid as input in
58+
/// constrained intrinsic rounding mode metadata.
59+
Optional<StringRef> RoundingModeToStr(fp::RoundingMode);
60+
61+
/// Returns a valid ExceptionBehavior enumerator when given a string
62+
/// valid as input in constrained intrinsic exception behavior metadata.
63+
Optional<fp::ExceptionBehavior> StrToExceptionBehavior(StringRef);
64+
65+
/// For any ExceptionBehavior enumerator, returns a string valid as
66+
/// input in constrained intrinsic exception behavior metadata.
67+
Optional<StringRef> ExceptionBehaviorToStr(fp::ExceptionBehavior);
68+
69+
}
70+
#endif

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ class IRBuilderBase {
9797
FastMathFlags FMF;
9898

9999
bool IsFPConstrained;
100-
ConstrainedFPIntrinsic::ExceptionBehavior DefaultConstrainedExcept;
101-
ConstrainedFPIntrinsic::RoundingMode DefaultConstrainedRounding;
100+
fp::ExceptionBehavior DefaultConstrainedExcept;
101+
fp::RoundingMode DefaultConstrainedRounding;
102102

103103
ArrayRef<OperandBundleDef> DefaultOperandBundles;
104104

105105
public:
106106
IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
107107
ArrayRef<OperandBundleDef> OpBundles = None)
108108
: Context(context), DefaultFPMathTag(FPMathTag), IsFPConstrained(false),
109-
DefaultConstrainedExcept(ConstrainedFPIntrinsic::ebStrict),
110-
DefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDynamic),
109+
DefaultConstrainedExcept(fp::ebStrict),
110+
DefaultConstrainedRounding(fp::rmDynamic),
111111
DefaultOperandBundles(OpBundles) {
112112
ClearInsertionPoint();
113113
}
@@ -234,24 +234,22 @@ class IRBuilderBase {
234234
bool getIsFPConstrained() { return IsFPConstrained; }
235235

236236
/// Set the exception handling to be used with constrained floating point
237-
void setDefaultConstrainedExcept(
238-
ConstrainedFPIntrinsic::ExceptionBehavior NewExcept) {
237+
void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
239238
DefaultConstrainedExcept = NewExcept;
240239
}
241240

242241
/// Set the rounding mode handling to be used with constrained floating point
243-
void setDefaultConstrainedRounding(
244-
ConstrainedFPIntrinsic::RoundingMode NewRounding) {
242+
void setDefaultConstrainedRounding(fp::RoundingMode NewRounding) {
245243
DefaultConstrainedRounding = NewRounding;
246244
}
247245

248246
/// Get the exception handling used with constrained floating point
249-
ConstrainedFPIntrinsic::ExceptionBehavior getDefaultConstrainedExcept() {
247+
fp::ExceptionBehavior getDefaultConstrainedExcept() {
250248
return DefaultConstrainedExcept;
251249
}
252250

253251
/// Get the rounding mode handling used with constrained floating point
254-
ConstrainedFPIntrinsic::RoundingMode getDefaultConstrainedRounding() {
252+
fp::RoundingMode getDefaultConstrainedRounding() {
255253
return DefaultConstrainedRounding;
256254
}
257255

@@ -1097,32 +1095,26 @@ class IRBuilder : public IRBuilderBase, public Inserter {
10971095
return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
10981096
}
10991097

1100-
Value *getConstrainedFPRounding(
1101-
Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding) {
1102-
ConstrainedFPIntrinsic::RoundingMode UseRounding =
1103-
DefaultConstrainedRounding;
1098+
Value *getConstrainedFPRounding(Optional<fp::RoundingMode> Rounding) {
1099+
fp::RoundingMode UseRounding = DefaultConstrainedRounding;
11041100

11051101
if (Rounding.hasValue())
11061102
UseRounding = Rounding.getValue();
11071103

1108-
Optional<StringRef> RoundingStr =
1109-
ConstrainedFPIntrinsic::RoundingModeToStr(UseRounding);
1104+
Optional<StringRef> RoundingStr = RoundingModeToStr(UseRounding);
11101105
assert(RoundingStr.hasValue() && "Garbage strict rounding mode!");
11111106
auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue());
11121107

11131108
return MetadataAsValue::get(Context, RoundingMDS);
11141109
}
11151110

1116-
Value *getConstrainedFPExcept(
1117-
Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except) {
1118-
ConstrainedFPIntrinsic::ExceptionBehavior UseExcept =
1119-
DefaultConstrainedExcept;
1111+
Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) {
1112+
fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept;
11201113

11211114
if (Except.hasValue())
11221115
UseExcept = Except.getValue();
11231116

1124-
Optional<StringRef> ExceptStr =
1125-
ConstrainedFPIntrinsic::ExceptionBehaviorToStr(UseExcept);
1117+
Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(UseExcept);
11261118
assert(ExceptStr.hasValue() && "Garbage strict exception behavior!");
11271119
auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue());
11281120

@@ -1483,8 +1475,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
14831475
CallInst *CreateConstrainedFPBinOp(
14841476
Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
14851477
const Twine &Name = "", MDNode *FPMathTag = nullptr,
1486-
Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding = None,
1487-
Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except = None) {
1478+
Optional<fp::RoundingMode> Rounding = None,
1479+
Optional<fp::ExceptionBehavior> Except = None) {
14881480
Value *RoundingV = getConstrainedFPRounding(Rounding);
14891481
Value *ExceptV = getConstrainedFPExcept(Except);
14901482

@@ -2078,8 +2070,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
20782070
Intrinsic::ID ID, Value *V, Type *DestTy,
20792071
Instruction *FMFSource = nullptr, const Twine &Name = "",
20802072
MDNode *FPMathTag = nullptr,
2081-
Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding = None,
2082-
Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except = None) {
2073+
Optional<fp::RoundingMode> Rounding = None,
2074+
Optional<fp::ExceptionBehavior> Except = None) {
20832075
Value *ExceptV = getConstrainedFPExcept(Except);
20842076

20852077
FastMathFlags UseFMF = FMF;

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "llvm/IR/Constants.h"
2727
#include "llvm/IR/DerivedTypes.h"
28+
#include "llvm/IR/FPEnv.h"
2829
#include "llvm/IR/Function.h"
2930
#include "llvm/IR/GlobalVariable.h"
3031
#include "llvm/IR/Instructions.h"
@@ -208,47 +209,10 @@ namespace llvm {
208209
/// This is the common base class for constrained floating point intrinsics.
209210
class ConstrainedFPIntrinsic : public IntrinsicInst {
210211
public:
211-
/// Specifies the rounding mode to be assumed. This is only used when
212-
/// when constrained floating point is enabled. See the LLVM Language
213-
/// Reference Manual for details.
214-
enum RoundingMode : uint8_t {
215-
rmDynamic, ///< This corresponds to "fpround.dynamic".
216-
rmToNearest, ///< This corresponds to "fpround.tonearest".
217-
rmDownward, ///< This corresponds to "fpround.downward".
218-
rmUpward, ///< This corresponds to "fpround.upward".
219-
rmTowardZero ///< This corresponds to "fpround.tozero".
220-
};
221-
222-
/// Specifies the required exception behavior. This is only used when
223-
/// when constrained floating point is used. See the LLVM Language
224-
/// Reference Manual for details.
225-
enum ExceptionBehavior : uint8_t {
226-
ebIgnore, ///< This corresponds to "fpexcept.ignore".
227-
ebMayTrap, ///< This corresponds to "fpexcept.maytrap".
228-
ebStrict ///< This corresponds to "fpexcept.strict".
229-
};
230-
231212
bool isUnaryOp() const;
232213
bool isTernaryOp() const;
233-
Optional<RoundingMode> getRoundingMode() const;
234-
Optional<ExceptionBehavior> getExceptionBehavior() const;
235-
236-
/// Returns a valid RoundingMode enumerator when given a string
237-
/// that is valid as input in constrained intrinsic rounding mode
238-
/// metadata.
239-
static Optional<RoundingMode> StrToRoundingMode(StringRef);
240-
241-
/// For any RoundingMode enumerator, returns a string valid as input in
242-
/// constrained intrinsic rounding mode metadata.
243-
static Optional<StringRef> RoundingModeToStr(RoundingMode);
244-
245-
/// Returns a valid ExceptionBehavior enumerator when given a string
246-
/// valid as input in constrained intrinsic exception behavior metadata.
247-
static Optional<ExceptionBehavior> StrToExceptionBehavior(StringRef);
248-
249-
/// For any ExceptionBehavior enumerator, returns a string valid as
250-
/// input in constrained intrinsic exception behavior metadata.
251-
static Optional<StringRef> ExceptionBehaviorToStr(ExceptionBehavior);
214+
Optional<fp::RoundingMode> getRoundingMode() const;
215+
Optional<fp::ExceptionBehavior> getExceptionBehavior() const;
252216

253217
// Methods for support type inquiry through isa, cast, and dyn_cast:
254218
static bool classof(const IntrinsicInst *I);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6914,8 +6914,7 @@ void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
69146914
SDVTList VTs = DAG.getVTList(ValueVTs);
69156915
SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers);
69166916

6917-
if (FPI.getExceptionBehavior() !=
6918-
ConstrainedFPIntrinsic::ExceptionBehavior::ebIgnore) {
6917+
if (FPI.getExceptionBehavior() != fp::ExceptionBehavior::ebIgnore) {
69196918
SDNodeFlags Flags;
69206919
Flags.setFPExcept(true);
69216920
Result->setFlags(Flags);

llvm/lib/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_llvm_library(LLVMCore
2222
DiagnosticInfo.cpp
2323
DiagnosticPrinter.cpp
2424
Dominators.cpp
25+
FPEnv.cpp
2526
Function.cpp
2627
GVMaterializer.cpp
2728
Globals.cpp

llvm/lib/IR/FPEnv.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===-- FPEnv.cpp ---- FP Environment -------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// @file
10+
/// This file contains the implementations of entities that describe floating
11+
/// point environment.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "llvm/ADT/StringSwitch.h"
16+
#include "llvm/IR/FPEnv.h"
17+
18+
namespace llvm {
19+
20+
Optional<fp::RoundingMode> StrToRoundingMode(StringRef RoundingArg) {
21+
// For dynamic rounding mode, we use round to nearest but we will set the
22+
// 'exact' SDNodeFlag so that the value will not be rounded.
23+
return StringSwitch<Optional<fp::RoundingMode>>(RoundingArg)
24+
.Case("round.dynamic", fp::rmDynamic)
25+
.Case("round.tonearest", fp::rmToNearest)
26+
.Case("round.downward", fp::rmDownward)
27+
.Case("round.upward", fp::rmUpward)
28+
.Case("round.towardzero", fp::rmTowardZero)
29+
.Default(None);
30+
}
31+
32+
Optional<StringRef> RoundingModeToStr(fp::RoundingMode UseRounding) {
33+
Optional<StringRef> RoundingStr = None;
34+
switch (UseRounding) {
35+
case fp::rmDynamic:
36+
RoundingStr = "round.dynamic";
37+
break;
38+
case fp::rmToNearest:
39+
RoundingStr = "round.tonearest";
40+
break;
41+
case fp::rmDownward:
42+
RoundingStr = "round.downward";
43+
break;
44+
case fp::rmUpward:
45+
RoundingStr = "round.upward";
46+
break;
47+
case fp::rmTowardZero:
48+
RoundingStr = "round.towardzero";
49+
break;
50+
}
51+
return RoundingStr;
52+
}
53+
54+
Optional<fp::ExceptionBehavior> StrToExceptionBehavior(StringRef ExceptionArg) {
55+
return StringSwitch<Optional<fp::ExceptionBehavior>>(ExceptionArg)
56+
.Case("fpexcept.ignore", fp::ebIgnore)
57+
.Case("fpexcept.maytrap", fp::ebMayTrap)
58+
.Case("fpexcept.strict", fp::ebStrict)
59+
.Default(None);
60+
}
61+
62+
Optional<StringRef> ExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) {
63+
Optional<StringRef> ExceptStr = None;
64+
switch (UseExcept) {
65+
case fp::ebStrict:
66+
ExceptStr = "fpexcept.strict";
67+
break;
68+
case fp::ebIgnore:
69+
ExceptStr = "fpexcept.ignore";
70+
break;
71+
case fp::ebMayTrap:
72+
ExceptStr = "fpexcept.maytrap";
73+
break;
74+
}
75+
return ExceptStr;
76+
}
77+
78+
}

0 commit comments

Comments
 (0)