/home/arjun/llvm-project/mlir/unittests/Analysis/AffineStructuresTest.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | //===- AffineStructuresTest.cpp - Tests for AffineStructures ----*- C++ -*-===// | 
| 2 |  | // | 
| 3 |  | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
| 4 |  | // See https://llvm.org/LICENSE.txt for license information. | 
| 5 |  | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| 6 |  | // | 
| 7 |  | //===----------------------------------------------------------------------===// | 
| 8 |  |  | 
| 9 |  | #include "mlir/Analysis/AffineStructures.h" | 
| 10 |  |  | 
| 11 |  | #include <gmock/gmock.h> | 
| 12 |  | #include <gtest/gtest.h> | 
| 13 |  |  | 
| 14 |  | #include <numeric> | 
| 15 |  |  | 
| 16 |  | namespace mlir { | 
| 17 |  |  | 
| 18 |  | /// Evaluate the value of the given affine expression at the specified point. | 
| 19 |  | /// The expression is a list of coefficients for the dimensions followed by the | 
| 20 |  | /// constant term. | 
| 21 | 51 | int64_t valueAt(ArrayRef<int64_t> expr, ArrayRef<int64_t> point) { | 
| 22 | 51 |   assert(expr.size() == 1 + point.size()); | 
| 23 | 51 |   int64_t value = expr.back(); | 
| 24 | 195 |   for (unsigned i = 0; i < point.size(); ++i) | 
| 25 | 144 |     value += expr[i] * point[i]; | 
| 26 | 51 |   return value; | 
| 27 | 51 | } | 
| 28 |  |  | 
| 29 |  | /// If hasValue is true, check that findIntegerSample returns a valid sample | 
| 30 |  | /// for the FlatAffineConstraints fac. | 
| 31 |  | /// | 
| 32 |  | /// If hasValue is false, check that findIntegerSample does not return None. | 
| 33 | 25 | void checkSample(bool hasValue, const FlatAffineConstraints &fac) { | 
| 34 | 25 |   Optional<SmallVector<int64_t, 8>> maybeSample = fac.findIntegerSample(); | 
| 35 | 25 |   if (!hasValue) { | 
| 36 | 11 |     EXPECT_FALSE(maybeSample.hasValue()); | 
| 37 | 11 |     if (maybeSample.hasValue()) { | 
| 38 | 0 |       for (auto x : *maybeSample) | 
| 39 | 0 |         llvm::errs() << x << ' '; | 
| 40 | 0 |       llvm::errs() << '\n'; | 
| 41 | 0 |     } | 
| 42 | 14 |   } else { | 
| 43 | 14 |     ASSERT_TRUE(maybeSample.hasValue()); | 
| 44 | 19 |     for (unsigned i = 0; i < fac.getNumEqualities(); ++i) | 
| 45 | 14 |       EXPECT_EQ(valueAt(fac.getEquality(i), *maybeSample), 0); | 
| 46 | 60 |     for (unsigned i = 0; i < fac.getNumInequalities(); ++i) | 
| 47 | 14 |       EXPECT_GE(valueAt(fac.getInequality(i), *maybeSample), 0); | 
| 48 | 14 |   } | 
| 49 | 25 | } | 
| 50 |  |  | 
| 51 |  | /// Construct a FlatAffineConstraints from a set of inequality and | 
| 52 |  | /// equality constraints. | 
| 53 |  | FlatAffineConstraints | 
| 54 |  | makeFACFromConstraints(unsigned dims, | 
| 55 |  |                        SmallVector<SmallVector<int64_t, 4>, 4> ineqs, | 
| 56 | 31 |                        SmallVector<SmallVector<int64_t, 4>, 4> eqs) { | 
| 57 | 31 |   FlatAffineConstraints fac(ineqs.size(), eqs.size(), dims + 1, dims); | 
| 58 | 31 |   for (const auto &eq : eqs) | 
| 59 | 16 |     fac.addEquality(eq); | 
| 60 | 31 |   for (const auto &ineq : ineqs) | 
| 61 | 102 |     fac.addInequality(ineq); | 
| 62 | 31 |   return fac; | 
| 63 | 31 | } | 
| 64 |  |  | 
| 65 |  | /// Check sampling for all the permutations of the dimensions for the given | 
| 66 |  | /// constraint set. Since the GBR algorithm progresses dimension-wise, different | 
| 67 |  | /// orderings may cause the algorithm to proceed differently. At lesast some of | 
| 68 |  | ///.these permutations should make it past the heuristics and test the | 
| 69 |  | /// implementation of the GBR algorithm itself. | 
| 70 |  | void checkPermutationsSample(bool hasValue, unsigned nDim, | 
| 71 |  |                              SmallVector<SmallVector<int64_t, 4>, 4> ineqs, | 
| 72 | 2 |                              SmallVector<SmallVector<int64_t, 4>, 4> eqs) { | 
| 73 | 2 |   SmallVector<unsigned, 4> perm(nDim); | 
| 74 | 2 |   std::iota(perm.begin(), perm.end(), 0); | 
| 75 | 48 |   auto permute = [&perm](ArrayRef<int64_t> coeffs) { | 
| 76 | 48 |     SmallVector<int64_t, 4> permuted; | 
| 77 | 48 |     for (unsigned id : perm) | 
| 78 | 144 |       permuted.push_back(coeffs[id]); | 
| 79 | 48 |     permuted.push_back(coeffs.back()); | 
| 80 | 48 |     return permuted; | 
| 81 | 48 |   }; | 
| 82 | 12 |   do { | 
| 83 | 12 |     SmallVector<SmallVector<int64_t, 4>, 4> permutedIneqs, permutedEqs; | 
| 84 | 12 |     for (const auto &ineq : ineqs) | 
| 85 | 48 |       permutedIneqs.push_back(permute(ineq)); | 
| 86 | 12 |     for (const auto &eq : eqs) | 
| 87 | 0 |       permutedEqs.push_back(permute(eq)); | 
| 88 | 12 |     checkSample(hasValue, | 
| 89 | 12 |                 makeFACFromConstraints(nDim, permutedIneqs, permutedEqs)); | 
| 90 | 12 |   } while (std::next_permutation(perm.begin(), perm.end())); | 
| 91 | 2 | } | 
| 92 |  |  | 
| 93 | 1 | TEST(FlatAffineConstraintsTest, FindSampleTest) { | 
| 94 | 1 |   // Bounded sets with only inequalities. | 
| 95 | 1 |  | 
| 96 | 1 |   // 0 <= 7x <= 5 | 
| 97 | 1 |   checkSample(true, makeFACFromConstraints(1, {{7, 0}, {-7, 5}}, {})); | 
| 98 | 1 |  | 
| 99 | 1 |   // 1 <= 5x and 5x <= 4 (no solution). | 
| 100 | 1 |   checkSample(false, makeFACFromConstraints(1, {{5, -1}, {-5, 4}}, {})); | 
| 101 | 1 |  | 
| 102 | 1 |   // 1 <= 5x and 5x <= 9 (solution: x = 1). | 
| 103 | 1 |   checkSample(true, makeFACFromConstraints(1, {{5, -1}, {-5, 9}}, {})); | 
| 104 | 1 |  | 
| 105 | 1 |   // Bounded sets with equalities. | 
| 106 | 1 |   // x >= 8 and 40 >= y and x = y. | 
| 107 | 1 |   checkSample( | 
| 108 | 1 |       true, makeFACFromConstraints(2, {{1, 0, -8}, {0, -1, 40}}, {{1, -1, 0}})); | 
| 109 | 1 |  | 
| 110 | 1 |   // x <= 10 and y <= 10 and 10 <= z and x + 2y = 3z. | 
| 111 | 1 |   // solution: x = y = z = 10. | 
| 112 | 1 |   checkSample(true, makeFACFromConstraints( | 
| 113 | 1 |                         3, {{-1, 0, 0, 10}, {0, -1, 0, 10}, {0, 0, 1, -10}}, | 
| 114 | 1 |                         {{1, 2, -3, 0}})); | 
| 115 | 1 |  | 
| 116 | 1 |   // x <= 10 and y <= 10 and 11 <= z and x + 2y = 3z. | 
| 117 | 1 |   // This implies x + 2y >= 33 and x + 2y <= 30, which has no solution. | 
| 118 | 1 |   checkSample(false, makeFACFromConstraints( | 
| 119 | 1 |                          3, {{-1, 0, 0, 10}, {0, -1, 0, 10}, {0, 0, 1, -11}}, | 
| 120 | 1 |                          {{1, 2, -3, 0}})); | 
| 121 | 1 |  | 
| 122 | 1 |   // 0 <= r and r <= 3 and 4q + r = 7. | 
| 123 | 1 |   // Solution: q = 1, r = 3. | 
| 124 | 1 |   checkSample(true, | 
| 125 | 1 |               makeFACFromConstraints(2, {{0, 1, 0}, {0, -1, 3}}, {{4, 1, -7}})); | 
| 126 | 1 |  | 
| 127 | 1 |   // 4q + r = 7 and r = 0. | 
| 128 | 1 |   // Solution: q = 1, r = 3. | 
| 129 | 1 |   checkSample(false, makeFACFromConstraints(2, {}, {{4, 1, -7}, {0, 1, 0}})); | 
| 130 | 1 |  | 
| 131 | 1 |   // The next two sets are large sets that should take a long time to sample | 
| 132 | 1 |   // with a naive branch and bound algorithm but can be sampled efficiently with | 
| 133 | 1 |   // the GBR algroithm. | 
| 134 | 1 |   // | 
| 135 | 1 |   // This is a triangle with vertices at (1/3, 0), (2/3, 0) and (10000, 10000). | 
| 136 | 1 |   checkSample( | 
| 137 | 1 |       true, | 
| 138 | 1 |       makeFACFromConstraints( | 
| 139 | 1 |           2, {{0, 1, 0}, {300000, -299999, -100000}, {-300000, 299998, 200000}}, | 
| 140 | 1 |           {})); | 
| 141 | 1 |  | 
| 142 | 1 |   // This is a tetrahedron with vertices at | 
| 143 | 1 |   // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 10000), and (10000, 10000, 10000). | 
| 144 | 1 |   // The first three points form a triangular base on the xz plane with the | 
| 145 | 1 |   // apex at the fourth point, which is the only integer point. | 
| 146 | 1 |   checkPermutationsSample( | 
| 147 | 1 |       true, 3, | 
| 148 | 1 |       { | 
| 149 | 1 |           {0, 1, 0, 0},  // y >= 0 | 
| 150 | 1 |           {0, -1, 1, 0}, // z >= y | 
| 151 | 1 |           {300000, -299998, -1, | 
| 152 | 1 |            -100000},                    // -300000x + 299998y + 100000 + z <= 0. | 
| 153 | 1 |           {-150000, 149999, 0, 100000}, // -150000x + 149999y + 100000 >= 0. | 
| 154 | 1 |       }, | 
| 155 | 1 |       {}); | 
| 156 | 1 |  | 
| 157 | 1 |   // Same thing with some spurious extra dimensions equated to constants. | 
| 158 | 1 |   checkSample(true, | 
| 159 | 1 |               makeFACFromConstraints( | 
| 160 | 1 |                   5, | 
| 161 | 1 |                   { | 
| 162 | 1 |                       {0, 1, 0, 1, -1, 0}, | 
| 163 | 1 |                       {0, -1, 1, -1, 1, 0}, | 
| 164 | 1 |                       {300000, -299998, -1, -9, 21, -112000}, | 
| 165 | 1 |                       {-150000, 149999, 0, -15, 47, 68000}, | 
| 166 | 1 |                   }, | 
| 167 | 1 |                   {{0, 0, 0, 1, -1, 0},       // p = q. | 
| 168 | 1 |                    {0, 0, 0, 1, 1, -2000}})); // p + q = 20000 => p = q = 10000. | 
| 169 | 1 |  | 
| 170 | 1 |   // This is a tetrahedron with vertices at | 
| 171 | 1 |   // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 100), (100, 100 - 1/3, 100). | 
| 172 | 1 |   checkPermutationsSample(false, 3, | 
| 173 | 1 |                           { | 
| 174 | 1 |                               {0, 1, 0, 0}, | 
| 175 | 1 |                               {0, -300, 299, 0}, | 
| 176 | 1 |                               {300 * 299, -89400, -299, -100 * 299}, | 
| 177 | 1 |                               {-897, 894, 0, 598}, | 
| 178 | 1 |                           }, | 
| 179 | 1 |                           {}); | 
| 180 | 1 |  | 
| 181 | 1 |   // Two tests involving equalities that are integer empty but not rational | 
| 182 | 1 |   // empty. | 
| 183 | 1 |  | 
| 184 | 1 |   // This is a line segment from (0, 1/3) to (100, 100 + 1/3). | 
| 185 | 1 |   checkSample(false, makeFACFromConstraints( | 
| 186 | 1 |                          2, | 
| 187 | 1 |                          { | 
| 188 | 1 |                              {1, 0, 0},   // x >= 0. | 
| 189 | 1 |                              {-1, 0, 100} // -x + 100 >= 0, i.e., x <= 100. | 
| 190 | 1 |                          }, | 
| 191 | 1 |                          { | 
| 192 | 1 |                              {3, -3, 1} // 3x - 3y + 1 = 0, i.e., y = x + 1/3. | 
| 193 | 1 |                          })); | 
| 194 | 1 |  | 
| 195 | 1 |   // A thin parallelogram. 0 <= x <= 100 and x + 1/3 <= y <= x + 2/3. | 
| 196 | 1 |   checkSample(false, makeFACFromConstraints(2, | 
| 197 | 1 |                                             { | 
| 198 | 1 |                                                 {1, 0, 0},    // x >= 0. | 
| 199 | 1 |                                                 {-1, 0, 100}, // x <= 100. | 
| 200 | 1 |                                                 {3, -3, 2},   // 3x - 3y >= -2. | 
| 201 | 1 |                                                 {-3, 3, -1},  // 3x - 3y <= -1. | 
| 202 | 1 |                                             }, | 
| 203 | 1 |                                             {})); | 
| 204 | 1 |  | 
| 205 | 1 |   checkSample(true, makeFACFromConstraints(2, | 
| 206 | 1 |                                            { | 
| 207 | 1 |                                                {2, 0, 0},   // 2x >= 1. | 
| 208 | 1 |                                                {-2, 0, 99}, // 2x <= 99. | 
| 209 | 1 |                                                {0, 2, 0},   // 2y >= 0. | 
| 210 | 1 |                                                {0, -2, 99}, // 2y <= 99. | 
| 211 | 1 |                                            }, | 
| 212 | 1 |                                            {})); | 
| 213 | 1 | } | 
| 214 |  |  | 
| 215 | 1 | TEST(FlatAffineConstraintsTest, IsIntegerEmptyTest) { | 
| 216 | 1 |   // 1 <= 5x and 5x <= 4 (no solution). | 
| 217 | 1 |   EXPECT_TRUE( | 
| 218 | 1 |       makeFACFromConstraints(1, {{5, -1}, {-5, 4}}, {}).isIntegerEmpty()); | 
| 219 | 1 |   // 1 <= 5x and 5x <= 9 (solution: x = 1). | 
| 220 | 1 |   EXPECT_FALSE( | 
| 221 | 1 |       makeFACFromConstraints(1, {{5, -1}, {-5, 9}}, {}).isIntegerEmpty()); | 
| 222 | 1 |  | 
| 223 | 1 |   // An unbounded set, which isIntegerEmpty should detect as unbounded and | 
| 224 | 1 |   // return without calling findIntegerSample. | 
| 225 | 1 |   EXPECT_FALSE(makeFACFromConstraints(3, | 
| 226 | 1 |                                       { | 
| 227 | 1 |                                           {2, 0, 0, -1}, | 
| 228 | 1 |                                           {-2, 0, 0, 1}, | 
| 229 | 1 |                                           {0, 2, 0, -1}, | 
| 230 | 1 |                                           {0, -2, 0, 1}, | 
| 231 | 1 |                                           {0, 0, 2, -1}, | 
| 232 | 1 |                                       }, | 
| 233 | 1 |                                       {}) | 
| 234 | 1 |                    .isIntegerEmpty()); | 
| 235 | 1 |  | 
| 236 | 1 |   // FlatAffineConstraints::isEmpty() does not detect the following sets to be | 
| 237 | 1 |   // empty. | 
| 238 | 1 |  | 
| 239 | 1 |   // 3x + 7y = 1 and 0 <= x, y <= 10. | 
| 240 | 1 |   // Since x and y are non-negative, 3x + 7y can never be 1. | 
| 241 | 1 |   EXPECT_TRUE( | 
| 242 | 1 |       makeFACFromConstraints( | 
| 243 | 1 |           2, {{1, 0, 0}, {-1, 0, 10}, {0, 1, 0}, {0, -1, 10}}, {{3, 7, -1}}) | 
| 244 | 1 |           .isIntegerEmpty()); | 
| 245 | 1 |  | 
| 246 | 1 |   // 2x = 3y and y = x - 1 and x + y = 6z + 2 and 0 <= x, y <= 100. | 
| 247 | 1 |   // Substituting y = x - 1 in 3y = 2x, we obtain x = 3 and hence y = 2. | 
| 248 | 1 |   // Since x + y = 5 cannot be equal to 6z + 2 for any z, the set is empty. | 
| 249 | 1 |   EXPECT_TRUE( | 
| 250 | 1 |       makeFACFromConstraints(3, | 
| 251 | 1 |                              { | 
| 252 | 1 |                                  {1, 0, 0, 0}, | 
| 253 | 1 |                                  {-1, 0, 0, 100}, | 
| 254 | 1 |                                  {0, 1, 0, 0}, | 
| 255 | 1 |                                  {0, -1, 0, 100}, | 
| 256 | 1 |                              }, | 
| 257 | 1 |                              {{2, -3, 0, 0}, {1, -1, 0, -1}, {1, 1, -6, -2}}) | 
| 258 | 1 |           .isIntegerEmpty()); | 
| 259 | 1 |  | 
| 260 | 1 |   // 2x = 3y and y = x - 1 + 6z and x + y = 6q + 2 and 0 <= x, y <= 100. | 
| 261 | 1 |   // 2x = 3y implies x is a multiple of 3 and y is even. | 
| 262 | 1 |   // Now y = x - 1 + 6z implies y = 2 mod 3. In fact, since y is even, we have | 
| 263 | 1 |   // y = 2 mod 6. Then since x = y + 1 + 6z, we have x = 3 mod 6, implying | 
| 264 | 1 |   // x + y = 5 mod 6, which contradicts x + y = 6q + 2, so the set is empty. | 
| 265 | 1 |   EXPECT_TRUE(makeFACFromConstraints( | 
| 266 | 1 |                   4, | 
| 267 | 1 |                   { | 
| 268 | 1 |                       {1, 0, 0, 0, 0}, | 
| 269 | 1 |                       {-1, 0, 0, 0, 100}, | 
| 270 | 1 |                       {0, 1, 0, 0, 0}, | 
| 271 | 1 |                       {0, -1, 0, 0, 100}, | 
| 272 | 1 |                   }, | 
| 273 | 1 |                   {{2, -3, 0, 0, 0}, {1, -1, 6, 0, -1}, {1, 1, 0, -6, -2}}) | 
| 274 | 1 |                   .isIntegerEmpty()); | 
| 275 | 1 | } | 
| 276 |  |  | 
| 277 |  | } // namespace mlir |