[llvm] 9eb8040 - [ConstantRange] Support checking optimality for subset of inputs (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 15 13:48:16 PDT 2021


Author: Nikita Popov
Date: 2021-10-15T22:48:07+02:00
New Revision: 9eb8040a28b90c3d7ec8f2cae593f1b3509ce2de

URL: https://github.com/llvm/llvm-project/commit/9eb8040a28b90c3d7ec8f2cae593f1b3509ce2de
DIFF: https://github.com/llvm/llvm-project/commit/9eb8040a28b90c3d7ec8f2cae593f1b3509ce2de.diff

LOG: [ConstantRange] Support checking optimality for subset of inputs (NFC)

We always want to check correctness, but for some operations we
can only guarantee optimality for a subset of inputs. Accept an
additional predicate that determines whether optimality for a
given pair of ranges should be checked.

Added: 
    

Modified: 
    llvm/unittests/IR/ConstantRangeTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index 45d50cc9830f..d220b70daf71 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -111,7 +111,8 @@ testing::AssertionResult rangeContains(const ConstantRange &CR, const APInt &N,
 // Elems under the given PreferenceFn. The preference function should return
 // true if the first range argument is strictly preferred to the second one.
 static void TestRange(const ConstantRange &CR, const SmallBitVector &Elems,
-                      PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs) {
+                      PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs,
+                      bool CheckOptimality = true) {
   unsigned BitWidth = CR.getBitWidth();
 
   // Check conservative correctness.
@@ -119,6 +120,9 @@ static void TestRange(const ConstantRange &CR, const SmallBitVector &Elems,
     EXPECT_TRUE(rangeContains(CR, APInt(BitWidth, Elem), Inputs));
   }
 
+  if (!CheckOptimality)
+    return;
+
   // Make sure we have at least one element for the code below.
   if (Elems.none()) {
     EXPECT_TRUE(CR.isEmptySet());
@@ -182,9 +186,23 @@ using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &,
                                                        const ConstantRange &)>;
 using BinaryIntFn = llvm::function_ref<Optional<APInt>(const APInt &,
                                                        const APInt &)>;
+using BinaryCheckFn = llvm::function_ref<bool(const ConstantRange &,
+                                              const ConstantRange &)>;
+
+static bool CheckAll(const ConstantRange &, const ConstantRange &) {
+  return true;
+}
+
+static bool CheckSingleElementsOnly(const ConstantRange &CR1,
+                                    const ConstantRange &CR2) {
+  return CR1.isSingleElement() && CR2.isSingleElement();
+}
 
+// CheckFn determines whether optimality is checked for a given range pair.
+// Correctness is always checked.
 static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
-                                   PreferFn PreferenceFn = PreferSmallest) {
+                                   PreferFn PreferenceFn = PreferSmallest,
+                                   BinaryCheckFn CheckFn = CheckAll) {
   unsigned Bits = 4;
   EnumerateTwoConstantRanges(
       Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
@@ -195,23 +213,8 @@ static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
               Elems.set(ResultN->getZExtValue());
           });
         });
-        TestRange(RangeFn(CR1, CR2), Elems, PreferenceFn, {CR1, CR2});
-      });
-}
-
-static void TestBinaryOpExhaustiveCorrectnessOnly(BinaryRangeFn RangeFn,
-                                                  BinaryIntFn IntFn) {
-  unsigned Bits = 4;
-  EnumerateTwoConstantRanges(
-      Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
-        ConstantRange ResultCR = RangeFn(CR1, CR2);
-        ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
-          ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
-            if (Optional<APInt> ResultN = IntFn(N1, N2)) {
-              EXPECT_TRUE(rangeContains(ResultCR, *ResultN, {CR1, CR2}));
-            }
-          });
-        });
+        TestRange(RangeFn(CR1, CR2), Elems, PreferenceFn, {CR1, CR2},
+                  CheckFn(CR1, CR2));
       });
 }
 
@@ -1303,7 +1306,7 @@ TEST_F(ConstantRangeTest, URem) {
                 .urem(ConstantRange(APInt(16, 10))),
             ConstantRange(APInt(16, 0), APInt(16, 10)));
 
-  TestBinaryOpExhaustiveCorrectnessOnly(
+  TestBinaryOpExhaustive(
       [](const ConstantRange &CR1, const ConstantRange &CR2) {
         return CR1.urem(CR2);
       },
@@ -1311,7 +1314,9 @@ TEST_F(ConstantRangeTest, URem) {
         if (N2.isZero())
           return None;
         return N1.urem(N2);
-      });
+      },
+      PreferSmallest,
+      CheckSingleElementsOnly);
 }
 
 TEST_F(ConstantRangeTest, SRem) {
@@ -1377,7 +1382,7 @@ TEST_F(ConstantRangeTest, SRem) {
                 .srem(ConstantRange(APInt(16, 10))),
             ConstantRange(APInt(16, 0), APInt(16, 10)));
 
-  TestBinaryOpExhaustiveCorrectnessOnly(
+  TestBinaryOpExhaustive(
       [](const ConstantRange &CR1, const ConstantRange &CR2) {
         return CR1.srem(CR2);
       },
@@ -1385,7 +1390,9 @@ TEST_F(ConstantRangeTest, SRem) {
         if (N2.isZero())
           return None;
         return N1.srem(N2);
-      });
+      },
+      PreferSmallest,
+      CheckSingleElementsOnly);
 }
 
 TEST_F(ConstantRangeTest, Shl) {


        


More information about the llvm-commits mailing list