Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/ConstantFPRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ class [[nodiscard]] ConstantFPRange {
/// Get the range without infinities. It is useful when we apply ninf flag to
/// range of operands/results.
LLVM_ABI ConstantFPRange getWithoutInf() const;

/// Return a new range in the specified format with the specified rounding
/// mode.
LLVM_ABI ConstantFPRange
cast(const fltSemantics &DstSem,
APFloat::roundingMode RM = APFloat::rmNearestTiesToEven) const;
};

inline raw_ostream &operator<<(raw_ostream &OS, const ConstantFPRange &CR) {
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/IR/ConstantFPRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ std::optional<bool> ConstantFPRange::getSignBit() const {
}

bool ConstantFPRange::operator==(const ConstantFPRange &CR) const {
assert(&getSemantics() == &CR.getSemantics() &&
"Should only use the same semantics");
if (MayBeSNaN != CR.MayBeSNaN || MayBeQNaN != CR.MayBeQNaN)
return false;
return Lower.bitwiseIsEqual(CR.Lower) && Upper.bitwiseIsEqual(CR.Upper);
Expand Down Expand Up @@ -425,3 +427,20 @@ ConstantFPRange ConstantFPRange::getWithoutInf() const {
return ConstantFPRange(std::move(NewLower), std::move(NewUpper), MayBeQNaN,
MayBeSNaN);
}

ConstantFPRange ConstantFPRange::cast(const fltSemantics &DstSem,
APFloat::roundingMode RM) const {
bool LosesInfo;
APFloat NewLower = Lower;
APFloat NewUpper = Upper;
// For conservative, return full range if conversion is invalid.
if (NewLower.convert(DstSem, RM, &LosesInfo) == APFloat::opInvalidOp ||
NewLower.isNaN())
return getFull(DstSem);
if (NewUpper.convert(DstSem, RM, &LosesInfo) == APFloat::opInvalidOp ||
NewUpper.isNaN())
return getFull(DstSem);
return ConstantFPRange(std::move(NewLower), std::move(NewUpper),
/*MayBeQNaNVal=*/MayBeQNaN || MayBeSNaN,
/*MayBeSNaNVal=*/false);
}
107 changes: 107 additions & 0 deletions llvm/unittests/IR/ConstantFPRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/ConstantFPRange.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -818,4 +819,110 @@ TEST_F(ConstantFPRangeTest, getWithout) {
APFloat::getLargest(Sem, /*Negative=*/true), APFloat(3.0)));
}

TEST_F(ConstantFPRangeTest, cast) {
const fltSemantics &F16Sem = APFloat::IEEEhalf();
const fltSemantics &BF16Sem = APFloat::BFloat();
const fltSemantics &F32Sem = APFloat::IEEEsingle();
const fltSemantics &F8NanOnlySem = APFloat::Float8E4M3FN();
// normal -> normal (exact)
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(1.0), APFloat(2.0)).cast(F32Sem),
ConstantFPRange::getNonNaN(APFloat(1.0f), APFloat(2.0f)));
EXPECT_EQ(
ConstantFPRange::getNonNaN(APFloat(-2.0f), APFloat(-1.0f)).cast(Sem),
ConstantFPRange::getNonNaN(APFloat(-2.0), APFloat(-1.0)));
// normal -> normal (inexact)
EXPECT_EQ(
ConstantFPRange::getNonNaN(APFloat(3.141592653589793),
APFloat(6.283185307179586))
.cast(F32Sem),
ConstantFPRange::getNonNaN(APFloat(3.14159274f), APFloat(6.28318548f)));
// normal -> subnormal
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(-5e-8), APFloat(5e-8))
.cast(F16Sem)
.classify(),
fcSubnormal | fcZero);
// normal -> zero
EXPECT_EQ(ConstantFPRange::getNonNaN(
APFloat::getSmallestNormalized(Sem, /*Negative=*/true),
APFloat::getSmallestNormalized(Sem, /*Negative=*/false))
.cast(F32Sem)
.classify(),
fcZero);
// normal -> inf
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(-65536.0), APFloat(65536.0))
.cast(F16Sem),
ConstantFPRange::getNonNaN(F16Sem));
// nan -> qnan
EXPECT_EQ(
ConstantFPRange::getNaNOnly(Sem, /*MayBeQNaN=*/true, /*MayBeSNaN=*/false)
.cast(F32Sem),
ConstantFPRange::getNaNOnly(F32Sem, /*MayBeQNaN=*/true,
/*MayBeSNaN=*/false));
EXPECT_EQ(
ConstantFPRange::getNaNOnly(Sem, /*MayBeQNaN=*/false, /*MayBeSNaN=*/true)
.cast(F32Sem),
ConstantFPRange::getNaNOnly(F32Sem, /*MayBeQNaN=*/true,
/*MayBeSNaN=*/false));
EXPECT_EQ(
ConstantFPRange::getNaNOnly(Sem, /*MayBeQNaN=*/true, /*MayBeSNaN=*/true)
.cast(F32Sem),
ConstantFPRange::getNaNOnly(F32Sem, /*MayBeQNaN=*/true,
/*MayBeSNaN=*/false));
// For BF16 -> F32, signaling bit is still lost.
EXPECT_EQ(ConstantFPRange::getNaNOnly(BF16Sem, /*MayBeQNaN=*/true,
/*MayBeSNaN=*/true)
.cast(F32Sem),
ConstantFPRange::getNaNOnly(F32Sem, /*MayBeQNaN=*/true,
/*MayBeSNaN=*/false));
// inf -> nan only (return full set for now)
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat::getInf(Sem, /*Negative=*/true),
APFloat::getInf(Sem, /*Negative=*/false))
.cast(F8NanOnlySem),
ConstantFPRange::getFull(F8NanOnlySem));
// other rounding modes
EXPECT_EQ(
ConstantFPRange::getNonNaN(APFloat::getSmallest(Sem, /*Negative=*/true),
APFloat::getSmallest(Sem, /*Negative=*/false))
.cast(F32Sem, APFloat::rmTowardNegative),
ConstantFPRange::getNonNaN(
APFloat::getSmallest(F32Sem, /*Negative=*/true),
APFloat::getZero(F32Sem, /*Negative=*/false)));
EXPECT_EQ(
ConstantFPRange::getNonNaN(APFloat::getSmallest(Sem, /*Negative=*/true),
APFloat::getSmallest(Sem, /*Negative=*/false))
.cast(F32Sem, APFloat::rmTowardPositive),
ConstantFPRange::getNonNaN(
APFloat::getZero(F32Sem, /*Negative=*/true),
APFloat::getSmallest(F32Sem, /*Negative=*/false)));
EXPECT_EQ(
ConstantFPRange::getNonNaN(
APFloat::getSmallestNormalized(Sem, /*Negative=*/true),
APFloat::getSmallestNormalized(Sem, /*Negative=*/false))
.cast(F32Sem, APFloat::rmTowardZero),
ConstantFPRange::getNonNaN(APFloat::getZero(F32Sem, /*Negative=*/true),
APFloat::getZero(F32Sem, /*Negative=*/false)));

EnumerateValuesInConstantFPRange(
ConstantFPRange::getFull(APFloat::Float8E4M3()),
[&](const APFloat &V) {
bool LosesInfo = false;

APFloat DoubleV = V;
DoubleV.convert(Sem, APFloat::rmNearestTiesToEven, &LosesInfo);
ConstantFPRange DoubleCR = ConstantFPRange(V).cast(Sem);
EXPECT_TRUE(DoubleCR.contains(DoubleV))
<< "Casting " << V << " to double failed. " << DoubleCR
<< " doesn't contain " << DoubleV;

auto &FP4Sem = APFloat::Float4E2M1FN();
APFloat FP4V = V;
FP4V.convert(FP4Sem, APFloat::rmNearestTiesToEven, &LosesInfo);
ConstantFPRange FP4CR = ConstantFPRange(V).cast(FP4Sem);
EXPECT_TRUE(FP4CR.contains(FP4V))
<< "Casting " << V << " to FP4E2M1FN failed. " << FP4CR
<< " doesn't contain " << FP4V;
},
/*IgnoreNaNPayload=*/true);
}

} // anonymous namespace
Loading