Skip to content

Commit

Permalink
[ConstantFPRange] Implement ConstantFPRange::makeExactFCmpRegion (#…
Browse files Browse the repository at this point in the history
…111490)

Note: The current implementation doesn't return optimal result for `fcmp
one/une x, +/-inf` since we don't handle this case in
#110891. Maybe we can make it
optimal after seeing some real-world cases.
  • Loading branch information
dtcxzyw authored Oct 8, 2024
1 parent a11509c commit a3a253d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion llvm/lib/IR/ConstantFPRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ ConstantFPRange::makeSatisfyingFCmpRegion(FCmpInst::Predicate Pred,
std::optional<ConstantFPRange>
ConstantFPRange::makeExactFCmpRegion(FCmpInst::Predicate Pred,
const APFloat &Other) {
return std::nullopt;
if ((Pred == FCmpInst::FCMP_UNE || Pred == FCmpInst::FCMP_ONE) &&
!Other.isNaN())
return std::nullopt;
return makeSatisfyingFCmpRegion(Pred, ConstantFPRange(Other));
}

bool ConstantFPRange::fcmp(FCmpInst::Predicate Pred,
Expand Down
24 changes: 24 additions & 0 deletions llvm/unittests/IR/ConstantFPRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,4 +743,28 @@ TEST_F(ConstantFPRangeTest, fcmp) {
}
}

TEST_F(ConstantFPRangeTest, makeExactFCmpRegion) {
for (auto Pred : FCmpInst::predicates()) {
EnumerateValuesInConstantFPRange(
ConstantFPRange::getFull(APFloat::Float8E4M3()),
[Pred](const APFloat &V) {
std::optional<ConstantFPRange> Res =
ConstantFPRange::makeExactFCmpRegion(Pred, V);
ConstantFPRange Allowed =
ConstantFPRange::makeAllowedFCmpRegion(Pred, ConstantFPRange(V));
ConstantFPRange Satisfying =
ConstantFPRange::makeSatisfyingFCmpRegion(Pred,
ConstantFPRange(V));
if (Allowed == Satisfying)
EXPECT_EQ(Res, Allowed) << "Wrong result for makeExactFCmpRegion("
<< Pred << ", " << V << ").";
else
EXPECT_FALSE(Res.has_value())
<< "Wrong result for makeExactFCmpRegion(" << Pred << ", " << V
<< ").";
},
/*IgnoreNaNPayload=*/true);
}
}

} // anonymous namespace

0 comments on commit a3a253d

Please sign in to comment.