Skip to content

Commit e6584b2

Browse files
committed
Move floating point related entities to namespace level
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 0df4a8f commit e6584b2

File tree

9 files changed

+211
-177
lines changed

9 files changed

+211
-177
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "clang/Frontend/FrontendDiagnostic.h"
3434
#include "llvm/IR/DataLayout.h"
3535
#include "llvm/IR/Dominators.h"
36+
#include "llvm/IR/FPEnv.h"
3637
#include "llvm/IR/IntrinsicInst.h"
3738
#include "llvm/IR/Intrinsics.h"
3839
#include "llvm/IR/MDBuilder.h"
@@ -106,36 +107,28 @@ CodeGenFunction::~CodeGenFunction() {
106107

107108
// Map the LangOption for rounding mode into
108109
// the corresponding enum in the IR.
109-
static llvm::ConstrainedFPIntrinsic::RoundingMode ToConstrainedRoundingMD(
110+
static llvm::fp::RoundingMode ToConstrainedRoundingMD(
110111
LangOptions::FPRoundingModeKind Kind) {
111112

112113
switch (Kind) {
113-
case LangOptions::FPR_ToNearest:
114-
return llvm::ConstrainedFPIntrinsic::rmToNearest;
115-
case LangOptions::FPR_Downward:
116-
return llvm::ConstrainedFPIntrinsic::rmDownward;
117-
case LangOptions::FPR_Upward:
118-
return llvm::ConstrainedFPIntrinsic::rmUpward;
119-
case LangOptions::FPR_TowardZero:
120-
return llvm::ConstrainedFPIntrinsic::rmTowardZero;
121-
case LangOptions::FPR_Dynamic:
122-
return llvm::ConstrainedFPIntrinsic::rmDynamic;
114+
case LangOptions::FPR_ToNearest: return llvm::fp::rmToNearest;
115+
case LangOptions::FPR_Downward: return llvm::fp::rmDownward;
116+
case LangOptions::FPR_Upward: return llvm::fp::rmUpward;
117+
case LangOptions::FPR_TowardZero: return llvm::fp::rmTowardZero;
118+
case LangOptions::FPR_Dynamic: return llvm::fp::rmDynamic;
123119
}
124120
llvm_unreachable("Unsupported FP RoundingMode");
125121
}
126122

127123
// Map the LangOption for exception behavior into
128124
// the corresponding enum in the IR.
129-
static llvm::ConstrainedFPIntrinsic::ExceptionBehavior ToConstrainedExceptMD(
125+
static llvm::fp::ExceptionBehavior ToConstrainedExceptMD(
130126
LangOptions::FPExceptionModeKind Kind) {
131127

132128
switch (Kind) {
133-
case LangOptions::FPE_Ignore:
134-
return llvm::ConstrainedFPIntrinsic::ebIgnore;
135-
case LangOptions::FPE_MayTrap:
136-
return llvm::ConstrainedFPIntrinsic::ebMayTrap;
137-
case LangOptions::FPE_Strict:
138-
return llvm::ConstrainedFPIntrinsic::ebStrict;
129+
case LangOptions::FPE_Ignore: return llvm::fp::ebIgnore;
130+
case LangOptions::FPE_MayTrap: return llvm::fp::ebMayTrap;
131+
case LangOptions::FPE_Strict: return llvm::fp::ebStrict;
139132
}
140133
llvm_unreachable("Unsupported FP Exception Behavior");
141134
}
@@ -146,8 +139,8 @@ void CodeGenFunction::SetFPModel() {
146139
auto fpExceptionBehavior = ToConstrainedExceptMD(
147140
getLangOpts().getFPExceptionMode());
148141

149-
if (fpExceptionBehavior == llvm::ConstrainedFPIntrinsic::ebIgnore &&
150-
fpRoundingMode == llvm::ConstrainedFPIntrinsic::rmToNearest)
142+
if (fpExceptionBehavior == llvm::fp::ebIgnore &&
143+
fpRoundingMode == llvm::fp::rmToNearest)
151144
// Constrained intrinsics are not used.
152145
;
153146
else {

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
@@ -7020,8 +7020,7 @@ void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
70207020
SDVTList VTs = DAG.getVTList(ValueVTs);
70217021
SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers);
70227022

7023-
if (FPI.getExceptionBehavior() !=
7024-
ConstrainedFPIntrinsic::ExceptionBehavior::ebIgnore) {
7023+
if (FPI.getExceptionBehavior() != fp::ExceptionBehavior::ebIgnore) {
70257024
SDNodeFlags Flags;
70267025
Flags.setFPExcept(true);
70277026
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

0 commit comments

Comments
 (0)