Skip to content

Commit 35848f5

Browse files
[llvm][NFC] Add missing semantics to enum
Add missing semantics to the `Semantics` enum. Move all documentation to the header file. Rename the `EnumToSemanatics` to `getSemantics`.
1 parent 836d2dc commit 35848f5

File tree

5 files changed

+105
-122
lines changed

5 files changed

+105
-122
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ class FloatingLiteral : public Expr, private APFloatStorage {
16721672

16731673
/// Return the APFloat semantics this literal uses.
16741674
const llvm::fltSemantics &getSemantics() const {
1675-
return llvm::APFloatBase::EnumToSemantics(
1675+
return llvm::APFloatBase::getSemantics(
16761676
static_cast<llvm::APFloatBase::Semantics>(
16771677
FloatingLiteralBits.Semantics));
16781678
}

clang/include/clang/AST/PropertiesBase.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ let Class = PropertyTypeCase<APValue, "Float"> in {
282282
let Read = [{ node.getFloat().bitcastToAPInt() }];
283283
}
284284
def : Creator<[{
285-
const llvm::fltSemantics &floatSema = llvm::APFloatBase::EnumToSemantics(
285+
const llvm::fltSemantics &floatSema = llvm::APFloatBase::getSemantics(
286286
static_cast<llvm::APFloatBase::Semantics>(semantics));
287287
return APValue(llvm::APFloat(floatSema, value));
288288
}]>;
@@ -324,7 +324,7 @@ let Class = PropertyTypeCase<APValue, "ComplexFloat"> in {
324324
let Read = [{ node.getComplexFloatImag().bitcastToAPInt() }];
325325
}
326326
def : Creator<[{
327-
const llvm::fltSemantics &sema = llvm::APFloatBase::EnumToSemantics(
327+
const llvm::fltSemantics &sema = llvm::APFloatBase::getSemantics(
328328
static_cast<llvm::APFloatBase::Semantics>(semantics));
329329
return APValue(llvm::APFloat(sema, real),
330330
llvm::APFloat(sema, imag));

llvm/include/llvm/ADT/APFloat.h

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ struct APFloatBase {
155155
S_IEEEsingle,
156156
S_IEEEdouble,
157157
S_IEEEquad,
158+
// The IBM double-double semantics. Such a number consists of a pair of
159+
// IEEE 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
160+
// (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
161+
// Therefore it has two 53-bit mantissa parts that aren't necessarily
162+
// adjacent to each other, and two 11-bit exponents.
163+
//
164+
// Note: we need to make the value different from semBogus as otherwise
165+
// an unsafe optimization may collapse both values to a single address,
166+
// and we heavily rely on them having distinct addresses.
158167
S_PPCDoubleDouble,
159168
// 8-bit floating point number following IEEE-754 conventions with bit
160169
// layout S1E5M2 as described in https://arxiv.org/abs/2209.05433.
@@ -214,13 +223,41 @@ struct APFloatBase {
214223
// types, there are no infinity or NaN values. The format is detailed in
215224
// https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
216225
S_Float4E2M1FN,
217-
226+
// TODO: Documentation is missing.
218227
S_x87DoubleExtended,
219-
S_MaxSemantics = S_x87DoubleExtended,
228+
// These are legacy semantics for the fallback, inaccrurate implementation
229+
// of IBM double-double, if the accurate semPPCDoubleDouble doesn't handle
230+
// the operation. It's equivalent to having an IEEE number with consecutive
231+
// 106 bits of mantissa and 11 bits of exponent.
232+
//
233+
// It's not equivalent to IBM double-double. For example, a legit IBM
234+
// double-double, 1 + epsilon:
235+
//
236+
// 1 + epsilon = 1 + (1 >> 1076)
237+
//
238+
// is not representable by a consecutive 106 bits of mantissa.
239+
//
240+
// Currently, these semantics are used in the following way:
241+
//
242+
// semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
243+
// (64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
244+
// semPPCDoubleDoubleLegacy -> IEEE operations
245+
//
246+
// We use bitcastToAPInt() to get the bit representation (in APInt) of the
247+
// underlying IEEEdouble, then use the APInt constructor to construct the
248+
// legacy IEEE float.
249+
//
250+
// TODO: Implement all operations in semPPCDoubleDouble, and delete these
251+
// semantics.
252+
S_PPCDoubleDoubleLegacy,
253+
// A Pseudo fltsemantic used to construct APFloats that cannot conflict
254+
// with anything real.
255+
S_Bogus,
256+
S_MaxSemantics = S_Bogus,
220257
};
221258

222-
static const llvm::fltSemantics &EnumToSemantics(Semantics S);
223259
static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem);
260+
static const llvm::fltSemantics &getSemantics(Semantics S);
224261

225262
static const fltSemantics &IEEEhalf() LLVM_READNONE;
226263
static const fltSemantics &BFloat() LLVM_READNONE;

llvm/lib/Support/APFloat.cpp

Lines changed: 45 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ enum class fltNanEncoding {
101101

102102
/* Represents floating point arithmetic semantics. */
103103
struct fltSemantics {
104+
APFloat::Semantics name;
105+
104106
/* The largest E such that 2^E is representable; this matches the
105107
definition of IEEE 754. */
106108
APFloatBase::ExponentType maxExponent;
@@ -135,75 +137,54 @@ struct fltSemantics {
135137
}
136138
};
137139

138-
static constexpr fltSemantics semIEEEhalf = {15, -14, 11, 16};
139-
static constexpr fltSemantics semBFloat = {127, -126, 8, 16};
140-
static constexpr fltSemantics semIEEEsingle = {127, -126, 24, 32};
141-
static constexpr fltSemantics semIEEEdouble = {1023, -1022, 53, 64};
142-
static constexpr fltSemantics semIEEEquad = {16383, -16382, 113, 128};
143-
static constexpr fltSemantics semFloat8E5M2 = {15, -14, 3, 8};
140+
static constexpr fltSemantics semIEEEhalf = {
141+
APFloatBase::S_IEEEhalf, 15, -14, 11, 16};
142+
static constexpr fltSemantics semBFloat = {
143+
APFloatBase::S_BFloat, 127, -126, 8, 16};
144+
static constexpr fltSemantics semIEEEsingle = {
145+
APFloatBase::S_IEEEsingle, 127, -126, 24, 32};
146+
static constexpr fltSemantics semIEEEdouble = {
147+
APFloatBase::S_IEEEdouble, 1023, -1022, 53, 64};
148+
static constexpr fltSemantics semIEEEquad = {
149+
APFloatBase::S_IEEEquad, 16383, -16382, 113, 128};
150+
static constexpr fltSemantics semFloat8E5M2 = {
151+
APFloatBase::S_Float8E5M2, 15, -14, 3, 8};
144152
static constexpr fltSemantics semFloat8E5M2FNUZ = {
145-
15, -15, 3, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
146-
static constexpr fltSemantics semFloat8E4M3 = {7, -6, 4, 8};
153+
APFloatBase::S_Float8E5M2FNUZ, 15, -15, 3, 8, fltNonfiniteBehavior::NanOnly,
154+
fltNanEncoding::NegativeZero};
155+
static constexpr fltSemantics semFloat8E4M3 = {
156+
APFloatBase::S_Float8E4M3, 7, -6, 4, 8};
147157
static constexpr fltSemantics semFloat8E4M3FN = {
148-
8, -6, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::AllOnes};
158+
APFloatBase::S_Float8E4M3FN, 8, -6, 4, 8, fltNonfiniteBehavior::NanOnly,
159+
fltNanEncoding::AllOnes};
149160
static constexpr fltSemantics semFloat8E4M3FNUZ = {
150-
7, -7, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
161+
APFloatBase::S_Float8E4M3FNUZ, 7, -7, 4, 8, fltNonfiniteBehavior::NanOnly,
162+
fltNanEncoding::NegativeZero};
151163
static constexpr fltSemantics semFloat8E4M3B11FNUZ = {
152-
4, -10, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
153-
static constexpr fltSemantics semFloat8E3M4 = {3, -2, 5, 8};
154-
static constexpr fltSemantics semFloatTF32 = {127, -126, 11, 19};
164+
APFloatBase::S_Float8E4M3B11FNUZ, 4, -10, 4, 8, fltNonfiniteBehavior::NanOnly,
165+
fltNanEncoding::NegativeZero};
166+
static constexpr fltSemantics semFloat8E3M4 = {
167+
APFloatBase::S_Float8E3M4, 3, -2, 5, 8};
168+
static constexpr fltSemantics semFloatTF32 = {
169+
APFloatBase::S_FloatTF32, 127, -126, 11, 19};
155170
static constexpr fltSemantics semFloat8E8M0FNU = {
156-
127, -127, 1, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::AllOnes,
157-
false, false};
158-
171+
APFloatBase::S_Float8E8M0FNU, 127, -127, 1, 8, fltNonfiniteBehavior::NanOnly,
172+
fltNanEncoding::AllOnes, false, false};
159173
static constexpr fltSemantics semFloat6E3M2FN = {
160-
4, -2, 3, 6, fltNonfiniteBehavior::FiniteOnly};
174+
APFloatBase::S_Float6E3M2FN, 4, -2, 3, 6, fltNonfiniteBehavior::FiniteOnly};
161175
static constexpr fltSemantics semFloat6E2M3FN = {
162-
2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
176+
APFloatBase::S_Float6E2M3FN, 2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
163177
static constexpr fltSemantics semFloat4E2M1FN = {
164-
2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
165-
static constexpr fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80};
166-
static constexpr fltSemantics semBogus = {0, 0, 0, 0};
167-
168-
/* The IBM double-double semantics. Such a number consists of a pair of IEEE
169-
64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal,
170-
(double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo.
171-
Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent
172-
to each other, and two 11-bit exponents.
173-
174-
Note: we need to make the value different from semBogus as otherwise
175-
an unsafe optimization may collapse both values to a single address,
176-
and we heavily rely on them having distinct addresses. */
177-
static constexpr fltSemantics semPPCDoubleDouble = {-1, 0, 0, 128};
178-
179-
/* These are legacy semantics for the fallback, inaccrurate implementation of
180-
IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the
181-
operation. It's equivalent to having an IEEE number with consecutive 106
182-
bits of mantissa and 11 bits of exponent.
183-
184-
It's not equivalent to IBM double-double. For example, a legit IBM
185-
double-double, 1 + epsilon:
178+
APFloatBase::S_Float4E2M1FN, 2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
179+
static constexpr fltSemantics semX87DoubleExtended = {
180+
APFloatBase::S_x87DoubleExtended, 16383, -16382, 64, 80};
181+
static constexpr fltSemantics semBogus = {APFloatBase::S_Bogus, 0, 0, 0, 0};
182+
static constexpr fltSemantics semPPCDoubleDouble = {APFloatBase::S_PPCDoubleDouble, -1, 0, 0, 128};
186183

187-
1 + epsilon = 1 + (1 >> 1076)
188-
189-
is not representable by a consecutive 106 bits of mantissa.
190-
191-
Currently, these semantics are used in the following way:
192-
193-
semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) ->
194-
(64-bit APInt, 64-bit APInt) -> (128-bit APInt) ->
195-
semPPCDoubleDoubleLegacy -> IEEE operations
196-
197-
We use bitcastToAPInt() to get the bit representation (in APInt) of the
198-
underlying IEEEdouble, then use the APInt constructor to construct the
199-
legacy IEEE float.
200-
201-
TODO: Implement all operations in semPPCDoubleDouble, and delete these
202-
semantics. */
203-
static constexpr fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53,
184+
static constexpr fltSemantics semPPCDoubleDoubleLegacy = {APFloatBase::S_PPCDoubleDoubleLegacy, 1023, -1022 + 53,
204185
53 + 53, 128};
205186

206-
const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) {
187+
const llvm::fltSemantics &APFloatBase::getSemantics(Semantics S) {
207188
switch (S) {
208189
case S_IEEEhalf:
209190
return IEEEhalf();
@@ -217,6 +198,8 @@ const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) {
217198
return IEEEquad();
218199
case S_PPCDoubleDouble:
219200
return PPCDoubleDouble();
201+
case S_PPCDoubleDoubleLegacy:
202+
return semPPCDoubleDoubleLegacy;
220203
case S_Float8E5M2:
221204
return Float8E5M2();
222205
case S_Float8E5M2FNUZ:
@@ -243,52 +226,15 @@ const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) {
243226
return Float4E2M1FN();
244227
case S_x87DoubleExtended:
245228
return x87DoubleExtended();
229+
case S_Bogus:
230+
return Bogus();
246231
}
247232
llvm_unreachable("Unrecognised floating semantics");
248233
}
249234

250235
APFloatBase::Semantics
251236
APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) {
252-
if (&Sem == &llvm::APFloat::IEEEhalf())
253-
return S_IEEEhalf;
254-
else if (&Sem == &llvm::APFloat::BFloat())
255-
return S_BFloat;
256-
else if (&Sem == &llvm::APFloat::IEEEsingle())
257-
return S_IEEEsingle;
258-
else if (&Sem == &llvm::APFloat::IEEEdouble())
259-
return S_IEEEdouble;
260-
else if (&Sem == &llvm::APFloat::IEEEquad())
261-
return S_IEEEquad;
262-
else if (&Sem == &llvm::APFloat::PPCDoubleDouble())
263-
return S_PPCDoubleDouble;
264-
else if (&Sem == &llvm::APFloat::Float8E5M2())
265-
return S_Float8E5M2;
266-
else if (&Sem == &llvm::APFloat::Float8E5M2FNUZ())
267-
return S_Float8E5M2FNUZ;
268-
else if (&Sem == &llvm::APFloat::Float8E4M3())
269-
return S_Float8E4M3;
270-
else if (&Sem == &llvm::APFloat::Float8E4M3FN())
271-
return S_Float8E4M3FN;
272-
else if (&Sem == &llvm::APFloat::Float8E4M3FNUZ())
273-
return S_Float8E4M3FNUZ;
274-
else if (&Sem == &llvm::APFloat::Float8E4M3B11FNUZ())
275-
return S_Float8E4M3B11FNUZ;
276-
else if (&Sem == &llvm::APFloat::Float8E3M4())
277-
return S_Float8E3M4;
278-
else if (&Sem == &llvm::APFloat::FloatTF32())
279-
return S_FloatTF32;
280-
else if (&Sem == &llvm::APFloat::Float8E8M0FNU())
281-
return S_Float8E8M0FNU;
282-
else if (&Sem == &llvm::APFloat::Float6E3M2FN())
283-
return S_Float6E3M2FN;
284-
else if (&Sem == &llvm::APFloat::Float6E2M3FN())
285-
return S_Float6E2M3FN;
286-
else if (&Sem == &llvm::APFloat::Float4E2M1FN())
287-
return S_Float4E2M1FN;
288-
else if (&Sem == &llvm::APFloat::x87DoubleExtended())
289-
return S_x87DoubleExtended;
290-
else
291-
llvm_unreachable("Unknown floating semantics");
237+
return Sem.name;
292238
}
293239

294240
const fltSemantics &APFloatBase::IEEEhalf() { return semIEEEhalf; }
@@ -3038,7 +2984,7 @@ IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts,
30382984
unsigned sigPartCount, int exp,
30392985
roundingMode rounding_mode) {
30402986
unsigned int parts, pow5PartCount;
3041-
fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
2987+
fltSemantics calcSemantics = { APFloatBase::S_MaxSemantics, 32767, -32767, 0, 0 };
30422988
integerPart pow5Parts[maxPowerOfFiveParts];
30432989
bool isNearest;
30442990

0 commit comments

Comments
 (0)