[llvm] b8b054a - Reland "Ensure that InstructionCost actually implements a total ordering"
Christopher Tetreault via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 4 10:07:01 PST 2021
Author: Christopher Tetreault
Date: 2021-02-04T10:04:10-08:00
New Revision: b8b054aa8aacfd01005054f5ec9e2342ed152516
URL: https://github.com/llvm/llvm-project/commit/b8b054aa8aacfd01005054f5ec9e2342ed152516
DIFF: https://github.com/llvm/llvm-project/commit/b8b054aa8aacfd01005054f5ec9e2342ed152516.diff
LOG: Reland "Ensure that InstructionCost actually implements a total ordering"
The operator< in the previous attempt was incorrect. It is unfortunate
that this was only caught by the expensive checks.
This reverts commit ff1147c3635685ba6aefbdc9394300adb5404595.
Added:
Modified:
llvm/include/llvm/Support/InstructionCost.h
llvm/unittests/Support/InstructionCostTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/InstructionCost.h b/llvm/include/llvm/Support/InstructionCost.h
index fbc898b878bb..90621f290785 100644
--- a/llvm/include/llvm/Support/InstructionCost.h
+++ b/llvm/include/llvm/Support/InstructionCost.h
@@ -36,8 +36,8 @@ class InstructionCost {
enum CostState { Valid, Invalid };
private:
- CostType Value;
- CostState State;
+ CostType Value = 0;
+ CostState State = Valid;
void propagateState(const InstructionCost &RHS) {
if (RHS.State == Invalid)
@@ -45,6 +45,7 @@ class InstructionCost {
}
public:
+ // A default constructed InstructionCost is a valid zero cost
InstructionCost() = default;
InstructionCost(CostState) = delete;
@@ -146,31 +147,32 @@ class InstructionCost {
return Copy;
}
+ /// For the comparison operators we have chosen to use lexicographical
+ /// ordering where valid costs are always considered to be less than invalid
+ /// costs. This avoids having to add asserts to the comparison operators that
+ /// the states are valid and users can test for validity of the cost
+ /// explicitly.
+ bool operator<(const InstructionCost &RHS) const {
+ if (State != RHS.State)
+ return State < RHS.State;
+ return Value < RHS.Value;
+ }
+
+ // Implement in terms of operator< to ensure that the two comparisons stay in
+ // sync
bool operator==(const InstructionCost &RHS) const {
- return State == RHS.State && Value == RHS.Value;
+ return !(*this < RHS) && !(RHS < *this);
}
bool operator!=(const InstructionCost &RHS) const { return !(*this == RHS); }
bool operator==(const CostType RHS) const {
- return State == Valid && Value == RHS;
+ InstructionCost RHS2(RHS);
+ return *this == RHS2;
}
bool operator!=(const CostType RHS) const { return !(*this == RHS); }
- /// For the comparison operators we have chosen to use total ordering with
- /// the following rules:
- /// 1. If either of the states != Valid then a lexicographical order is
- /// applied based upon the state.
- /// 2. If both states are valid then order based upon value.
- /// This avoids having to add asserts the comparison operators that the states
- /// are valid and users can test for validity of the cost explicitly.
- bool operator<(const InstructionCost &RHS) const {
- if (State != Valid || RHS.State != Valid)
- return State < RHS.State;
- return Value < RHS.Value;
- }
-
bool operator>(const InstructionCost &RHS) const { return RHS < *this; }
bool operator<=(const InstructionCost &RHS) const { return !(RHS < *this); }
diff --git a/llvm/unittests/Support/InstructionCostTest.cpp b/llvm/unittests/Support/InstructionCostTest.cpp
index 8ba9f990f027..6c8a9151e18d 100644
--- a/llvm/unittests/Support/InstructionCostTest.cpp
+++ b/llvm/unittests/Support/InstructionCostTest.cpp
@@ -19,12 +19,21 @@ struct CostTest : public testing::Test {
} // namespace
+TEST_F(CostTest, DefaultCtor) {
+ InstructionCost DefaultCost;
+
+ ASSERT_TRUE(DefaultCost.isValid());
+ EXPECT_EQ(*(DefaultCost.getValue()), 0);
+}
+
TEST_F(CostTest, Operators) {
+
InstructionCost VThree = 3;
InstructionCost VNegTwo = -2;
InstructionCost VSix = 6;
InstructionCost IThreeA = InstructionCost::getInvalid(3);
InstructionCost IThreeB = InstructionCost::getInvalid(3);
+ InstructionCost ITwo = InstructionCost::getInvalid(2);
InstructionCost TmpCost;
EXPECT_NE(VThree, VNegTwo);
@@ -33,10 +42,15 @@ TEST_F(CostTest, Operators) {
EXPECT_EQ(IThreeA, IThreeB);
EXPECT_GE(IThreeA, VNegTwo);
EXPECT_LT(VSix, IThreeA);
+ EXPECT_LT(VThree, ITwo);
+ EXPECT_GE(ITwo, VThree);
EXPECT_EQ(VSix - IThreeA, IThreeB);
EXPECT_EQ(VThree - VNegTwo, 5);
EXPECT_EQ(VThree * VNegTwo, -6);
EXPECT_EQ(VSix / VThree, 2);
+ EXPECT_NE(IThreeA, ITwo);
+ EXPECT_LT(ITwo, IThreeA);
+ EXPECT_GT(IThreeA, ITwo);
EXPECT_FALSE(IThreeA.isValid());
EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
More information about the llvm-commits
mailing list