| 1 |
#include "mlir/Dialect/SparseTensor/Utils/Merger.h" |
| 2 |
#include "llvm/Support/Compiler.h" |
| 3 |
#include "gmock/gmock.h" |
| 4 |
#include "gtest/gtest.h" |
| 5 |
#include <memory> |
| 6 |
|
| 7 |
using namespace mlir; |
| 8 |
using namespace mlir::sparse_tensor; |
| 9 |
|
| 10 |
// Silence 'warning C4002: 'too many arguments for function-liked macro |
| 11 |
// invocation' |
| 12 |
// as MSVC handles ##__VA_ARGS__ differently as gcc/clang |
| 13 |
|
| 14 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 15 |
#pragma warning(push) |
| 16 |
#pragma warning(disable : 4002) |
| 17 |
#endif |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
/// |
| 22 |
/// Defines macros to iterate binary and the combination of binary operations. |
| 23 |
/// |
| 24 |
|
| 25 |
#define FOREVERY_BINOP(DO) \ |
| 26 |
DO(mulf, Kind::kMulF) \ |
| 27 |
DO(mulc, Kind::kMulC) \ |
| 28 |
DO(muli, Kind::kMulI) \ |
| 29 |
DO(addf, Kind::kAddF) \ |
| 30 |
DO(addc, Kind::kAddC) \ |
| 31 |
DO(addi, Kind::kAddI) \ |
| 32 |
DO(subf, Kind::kSubF) \ |
| 33 |
DO(subc, Kind::kSubC) \ |
| 34 |
DO(subi, Kind::kSubI) \ |
| 35 |
DO(andi, Kind::kAndI) \ |
| 36 |
DO(xori, Kind::kXorI) \ |
| 37 |
DO(ori, Kind::kOrI) |
| 38 |
|
| 39 |
// TODO: Disjunctive binary operations that need special handling are not |
| 40 |
// included, e.g., Division are not tested (for now) as it need a constant |
| 41 |
// non-zero dividend. |
| 42 |
// ##__VA_ARGS__ handles cases when __VA_ARGS__ is empty. |
| 43 |
#define FOREVERY_COMMON_DISJ_BINOP(TEST, ...) \ |
| 44 |
TEST(addf, ##__VA_ARGS__) \ |
| 45 |
TEST(addc, ##__VA_ARGS__) \ |
| 46 |
TEST(addi, ##__VA_ARGS__) \ |
| 47 |
TEST(xori, ##__VA_ARGS__) \ |
| 48 |
TEST(ori, ##__VA_ARGS__) |
| 49 |
|
| 50 |
// TODO: Conjunctive binary operations that need special handling are not |
| 51 |
// included, e.g., substraction yields a different pattern as it is mapped to |
| 52 |
// negate operation. |
| 53 |
#define FOREVERY_COMMON_CONJ_BINOP(TEST, ...) \ |
| 54 |
TEST(mulf, ##__VA_ARGS__) \ |
| 55 |
TEST(mulc, ##__VA_ARGS__) \ |
| 56 |
TEST(muli, ##__VA_ARGS__) \ |
| 57 |
TEST(andi, ##__VA_ARGS__) |
| 58 |
|
| 59 |
#define FOREVERY_PAIR_OF_COMMON_CONJ_DISJ_BINOP(TEST) \ |
| 60 |
FOREVERY_COMMON_CONJ_BINOP(TEST, addf) \ |
| 61 |
FOREVERY_COMMON_CONJ_BINOP(TEST, addc) \ |
| 62 |
FOREVERY_COMMON_CONJ_BINOP(TEST, addi) \ |
| 63 |
FOREVERY_COMMON_CONJ_BINOP(TEST, xori) \ |
| 64 |
FOREVERY_COMMON_CONJ_BINOP(TEST, ori) |
| 65 |
|
| 66 |
#define FOREVERY_PAIR_OF_COMMON_CONJ_CONJ_BINOP(TEST) \ |
| 67 |
FOREVERY_COMMON_CONJ_BINOP(TEST, mulf) \ |
| 68 |
FOREVERY_COMMON_CONJ_BINOP(TEST, mulc) \ |
| 69 |
FOREVERY_COMMON_CONJ_BINOP(TEST, muli) \ |
| 70 |
FOREVERY_COMMON_CONJ_BINOP(TEST, andi) |
| 71 |
|
| 72 |
#define FOREVERY_PAIR_OF_COMMON_DISJ_DISJ_BINOP(TEST) \ |
| 73 |
FOREVERY_COMMON_DISJ_BINOP(TEST, addf) \ |
| 74 |
FOREVERY_COMMON_DISJ_BINOP(TEST, addc) \ |
| 75 |
FOREVERY_COMMON_DISJ_BINOP(TEST, addi) \ |
| 76 |
FOREVERY_COMMON_DISJ_BINOP(TEST, ori) \ |
| 77 |
FOREVERY_COMMON_DISJ_BINOP(TEST, xori) |
| 78 |
|
| 79 |
/// |
| 80 |
/// Helper classes/functions for testing Merger. |
| 81 |
/// |
| 82 |
|
| 83 |
/// Simple recursive data structure used to match expressions in Mergers. |
| 84 |
struct Pattern { |
| 85 |
Kind kind; |
| 86 |
|
| 87 |
/// Expressions representing tensors simply have a tensor number. |
| 88 |
unsigned tensorNum; |
| 89 |
|
| 90 |
/// Tensor operations point to their children. |
| 91 |
std::shared_ptr<Pattern> e0; |
| 92 |
std::shared_ptr<Pattern> e1; |
| 93 |
|
| 94 |
/// Constructors. |
| 95 |
/// Rather than using these, please use the readable helper constructor |
| 96 |
/// functions below to make tests more readable. |
| 97 |
Pattern(unsigned tensorNum) : kind(Kind::kTensor), tensorNum(tensorNum) {} |
| 98 |
Pattern(Kind kind, const std::shared_ptr<Pattern> &e0, |
| 99 |
const std::shared_ptr<Pattern> &e1) |
| 100 |
: kind(kind), e0(e0), e1(e1) { |
| 101 |
assert(kind >= Kind::kMulF); |
| 102 |
assert(e0 && e1); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
/// |
| 107 |
/// Readable Pattern builder functions. |
| 108 |
/// These should be preferred over the actual constructors. |
| 109 |
/// |
| 110 |
|
| 111 |
static std::shared_ptr<Pattern> tensorPattern(unsigned tensorNum) { |
| 112 |
return std::make_shared<Pattern>(tensorNum); |
| 113 |
} |
| 114 |
|
| 115 |
#define IMPL_BINOP_PATTERN(OP, KIND) \ |
| 116 |
LLVM_ATTRIBUTE_UNUSED static std::shared_ptr<Pattern> OP##Pattern( \ |
| 117 |
const std::shared_ptr<Pattern> &e0, \ |
| 118 |
const std::shared_ptr<Pattern> &e1) { \ |
| 119 |
return std::make_shared<Pattern>(KIND, e0, e1); \ |
| 120 |
} |
| 121 |
|
| 122 |
FOREVERY_BINOP(IMPL_BINOP_PATTERN) |
| 123 |
|
| 124 |
#undef IMPL_BINOP_PATTERN |
| 125 |
|
| 126 |
class MergerTestBase : public ::testing::Test { |
| 127 |
protected: |
| 128 |
MergerTestBase(unsigned numTensors, unsigned numLoops) |
| 129 |
: numTensors(numTensors), numLoops(numLoops), |
| 130 |
merger(numTensors, numLoops, /*numFilterLoops=*/0) {} |
| 131 |
|
| 132 |
/// |
| 133 |
/// Expression construction helpers. |
| 134 |
/// |
| 135 |
|
| 136 |
unsigned tensor(unsigned tensor) { |
| 137 |
return merger.addExp(Kind::kTensor, tensor); |
| 138 |
} |
| 139 |
|
| 140 |
#define IMPL_BINOP_EXPR(OP, KIND) \ |
| 141 |
LLVM_ATTRIBUTE_UNUSED unsigned OP##Expr(unsigned e0, unsigned e1) { \ |
| 142 |
return merger.addExp(KIND, e0, e1); \ |
| 143 |
} |
| 144 |
|
| 145 |
FOREVERY_BINOP(IMPL_BINOP_EXPR) |
| 146 |
|
| 147 |
#undef IMPL_BINOP_EXPR |
| 148 |
|
| 149 |
/// |
| 150 |
/// Comparison helpers. |
| 151 |
/// |
| 152 |
|
| 153 |
/// For readability of tests. |
| 154 |
unsigned lat(unsigned lat) { return lat; } |
| 155 |
|
| 156 |
/// Returns true if a lattice point with an expression matching the given |
| 157 |
/// pattern and bits matching the given bits is present in lattice points |
| 158 |
/// [p, p+n) of lattice set s. This is useful for testing partial ordering |
| 159 |
/// constraints between lattice points. We generally know how contiguous |
| 160 |
/// groups of lattice points should be ordered with respect to other groups, |
| 161 |
/// but there is no required ordering within groups. |
| 162 |
/// If simple is true, then compare the lat.simple field instead to test the |
| 163 |
/// result after optimization |
| 164 |
bool latPointWithinRange(unsigned s, unsigned p, unsigned n, |
| 165 |
const std::shared_ptr<Pattern> &pattern, |
| 166 |
const BitVector &bits, bool simple) { |
| 167 |
for (unsigned i = p; i < p + n; ++i) { |
| 168 |
if (compareExpression(merger.lat(merger.set(s)[i]).exp, pattern) && |
| 169 |
compareBits(s, i, bits, simple)) |
| 170 |
return true; |
| 171 |
} |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
/// Wrapper over latPointWithinRange for readability of tests. |
| 176 |
void expectLatPointWithinRange(unsigned s, unsigned p, unsigned n, |
| 177 |
const std::shared_ptr<Pattern> &pattern, |
| 178 |
const BitVector &bits, bool simple = false) { |
| 179 |
EXPECT_TRUE(latPointWithinRange(s, p, n, pattern, bits, simple)); |
| 180 |
} |
| 181 |
|
| 182 |
/// Wrapper over expectLatPointWithinRange for a single lat point. |
| 183 |
void expectLatPoint(unsigned s, unsigned p, |
| 184 |
const std::shared_ptr<Pattern> &pattern, |
| 185 |
const BitVector &bits, bool simple = false) { |
| 186 |
EXPECT_TRUE(latPointWithinRange(s, p, 1, pattern, bits, simple)); |
| 187 |
} |
| 188 |
|
| 189 |
/// Converts a vector of (loop, tensor) pairs to a bitvector with the |
| 190 |
/// corresponding bits set. |
| 191 |
BitVector |
| 192 |
loopsToBits(const std::vector<std::pair<unsigned, unsigned>> &loops) { |
| 193 |
BitVector testBits = BitVector(numTensors + 1, false); |
| 194 |
for (auto l : loops) { |
| 195 |
auto loop = std::get<0>(l); |
| 196 |
auto tensor = std::get<1>(l); |
| 197 |
testBits.set(numTensors * loop + tensor); |
| 198 |
} |
| 199 |
return testBits; |
| 200 |
} |
| 201 |
|
| 202 |
/// Returns true if the bits of lattice point p in set s match the given bits. |
| 203 |
/// If simple is true, then compare the lat.simple field instead to test the |
| 204 |
/// result after optimization |
| 205 |
bool compareBits(unsigned s, unsigned p, const BitVector &bits, bool simple) { |
| 206 |
if (simple) |
| 207 |
return merger.lat(merger.set(s)[p]).simple == bits; |
| 208 |
return merger.lat(merger.set(s)[p]).bits == bits; |
| 209 |
} |
| 210 |
|
| 211 |
/// Check that there are n lattice points in set s. |
| 212 |
void expectNumLatPoints(unsigned s, unsigned n) { |
| 213 |
EXPECT_THAT(merger.set(s).size(), n); |
| 214 |
} |
| 215 |
|
| 216 |
/// Compares expressions for equality. Equality is defined recursively as: |
| 217 |
/// - Operations are equal if they have the same kind and children. |
| 218 |
/// - Leaf tensors are equal if they refer to the same tensor. |
| 219 |
bool compareExpression(unsigned e, const std::shared_ptr<Pattern> &pattern) { |
| 220 |
auto tensorExp = merger.exp(e); |
| 221 |
if (tensorExp.kind != pattern->kind) |
| 222 |
return false; |
| 223 |
switch (tensorExp.kind) { |
| 224 |
// Leaf. |
| 225 |
case kTensor: |
| 226 |
return tensorExp.tensor == pattern->tensorNum; |
| 227 |
case kInvariant: |
| 228 |
case kIndex: |
| 229 |
llvm_unreachable("invariant not handled yet"); |
| 230 |
// Unary operations. |
| 231 |
case kAbsF: |
| 232 |
case kAbsC: |
| 233 |
case kAbsI: |
| 234 |
case kCeilF: |
| 235 |
case kFloorF: |
| 236 |
case kSqrtF: |
| 237 |
case kSqrtC: |
| 238 |
case kExpm1F: |
| 239 |
case kExpm1C: |
| 240 |
case kLog1pF: |
| 241 |
case kLog1pC: |
| 242 |
case kSinF: |
| 243 |
case kSinC: |
| 244 |
case kTanhF: |
| 245 |
case kTanhC: |
| 246 |
case kNegF: |
| 247 |
case kNegC: |
| 248 |
case kNegI: |
| 249 |
case kTruncF: |
| 250 |
case kExtF: |
| 251 |
case kCastFS: |
| 252 |
case kCastFU: |
| 253 |
case kCastSF: |
| 254 |
case kCastUF: |
| 255 |
case kCastS: |
| 256 |
case kCastU: |
| 257 |
case kCastIdx: |
| 258 |
case kTruncI: |
| 259 |
case kCIm: |
| 260 |
case kCRe: |
| 261 |
case kBitCast: |
| 262 |
case kSelect: |
| 263 |
case kBinaryBranch: |
| 264 |
case kUnary: |
| 265 |
return compareExpression(tensorExp.children.e0, pattern->e0); |
| 266 |
// Binary operations. |
| 267 |
case kMulF: |
| 268 |
case kMulC: |
| 269 |
case kMulI: |
| 270 |
case kDivF: |
| 271 |
case kDivC: |
| 272 |
case kDivS: |
| 273 |
case kDivU: |
| 274 |
case kAddF: |
| 275 |
case kAddC: |
| 276 |
case kAddI: |
| 277 |
case kSubF: |
| 278 |
case kSubC: |
| 279 |
case kSubI: |
| 280 |
case kAndI: |
| 281 |
case kOrI: |
| 282 |
case kXorI: |
| 283 |
case kShrS: |
| 284 |
case kShrU: |
| 285 |
case kShlI: |
| 286 |
case kBinary: |
| 287 |
case kReduce: |
| 288 |
return compareExpression(tensorExp.children.e0, pattern->e0) && |
| 289 |
compareExpression(tensorExp.children.e1, pattern->e1); |
| 290 |
} |
| 291 |
llvm_unreachable("unexpected kind"); |
| 292 |
} |
| 293 |
|
| 294 |
unsigned numTensors; |
| 295 |
unsigned numLoops; |
| 296 |
Merger merger; |
| 297 |
}; |
| 298 |
|
| 299 |
/// |
| 300 |
/// Tests with all sparse inputs. |
| 301 |
/// |
| 302 |
|
| 303 |
class MergerTest3T1L : public MergerTestBase { |
| 304 |
protected: |
| 305 |
// Our three tensors (two inputs, one output). |
| 306 |
const unsigned t0 = 0, t1 = 1, t2 = 2; |
| 307 |
|
| 308 |
// Our single loop. |
| 309 |
const unsigned l0 = 0; |
| 310 |
|
| 311 |
MergerTest3T1L() : MergerTestBase(3, 1) { |
| 312 |
EXPECT_TRUE(merger.getOutTensorID() == t2); |
| 313 |
|
| 314 |
// Tensor 0: sparse input vector. |
| 315 |
merger.addExp(Kind::kTensor, t0, -1u); |
| 316 |
merger.setDimAndDimLevelType(t0, l0, 0, DimLevelType::Compressed); |
| 317 |
|
| 318 |
// Tensor 1: sparse input vector. |
| 319 |
merger.addExp(Kind::kTensor, t1, -1u); |
| 320 |
merger.setDimAndDimLevelType(t1, l0, 0, DimLevelType::Compressed); |
| 321 |
|
| 322 |
// Tensor 2: dense output vector. |
| 323 |
merger.addExp(Kind::kTensor, t2, -1u); |
| 324 |
merger.setDimAndDimLevelType(t2, l0, 0, DimLevelType::Dense); |
| 325 |
} |
| 326 |
}; |
| 327 |
|
| 328 |
class MergerTest4T1L : public MergerTestBase { |
| 329 |
protected: |
| 330 |
// Our four tensors (three inputs, one output). |
| 331 |
const unsigned t0 = 0, t1 = 1, t2 = 2, t3 = 3; |
| 332 |
|
| 333 |
// Our single loop. |
| 334 |
const unsigned l0 = 0; |
| 335 |
|
| 336 |
MergerTest4T1L() : MergerTestBase(4, 1) { |
| 337 |
EXPECT_TRUE(merger.getOutTensorID() == t3); |
| 338 |
|
| 339 |
// Tensor 0: sparse input vector. |
| 340 |
merger.addExp(Kind::kTensor, t0, -1u); |
| 341 |
merger.setDimAndDimLevelType(t0, l0, 0, DimLevelType::Compressed); |
| 342 |
|
| 343 |
// Tensor 1: sparse input vector. |
| 344 |
merger.addExp(Kind::kTensor, t1, -1u); |
| 345 |
merger.setDimAndDimLevelType(t1, l0, 0, DimLevelType::Compressed); |
| 346 |
|
| 347 |
// Tensor 2: sparse input vector |
| 348 |
merger.addExp(Kind::kTensor, t2, -1u); |
| 349 |
merger.setDimAndDimLevelType(t2, l0, 0, DimLevelType::Compressed); |
| 350 |
|
| 351 |
// Tensor 3: dense output vector |
| 352 |
merger.addExp(Kind::kTensor, t3, -1u); |
| 353 |
merger.setDimAndDimLevelType(t3, l0, 0, DimLevelType::Dense); |
| 354 |
} |
| 355 |
}; |
| 356 |
|
| 357 |
/// |
| 358 |
/// Tests with both sparse and dense input. |
| 359 |
/// |
| 360 |
|
| 361 |
class MergerTest3T1LD : public MergerTestBase { |
| 362 |
protected: |
| 363 |
// Our three tensors (two inputs, one output). |
| 364 |
const unsigned t0 = 0, t1 = 1, t2 = 2; |
| 365 |
|
| 366 |
// Our single loop. |
| 367 |
const unsigned l0 = 0; |
| 368 |
|
| 369 |
MergerTest3T1LD() : MergerTestBase(3, 1) { |
| 370 |
EXPECT_TRUE(merger.getOutTensorID() == t2); |
| 371 |
|
| 372 |
// Tensor 0: sparse input vector. |
| 373 |
merger.addExp(Kind::kTensor, t0, -1u); |
| 374 |
merger.setDimAndDimLevelType(t0, l0, 0, DimLevelType::Compressed); |
| 375 |
|
| 376 |
// Tensor 1: dense input vector. |
| 377 |
merger.addExp(Kind::kTensor, t1, -1u); |
| 378 |
merger.setDimAndDimLevelType(t1, l0, 0, DimLevelType::Dense); |
| 379 |
|
| 380 |
// Tensor 2: dense output vector. |
| 381 |
merger.addExp(Kind::kTensor, t2, -1u); |
| 382 |
merger.setDimAndDimLevelType(t2, l0, 0, DimLevelType::Dense); |
| 383 |
} |
| 384 |
}; |
| 385 |
|
| 386 |
/// |
| 387 |
/// Tests with both undef and dense input. |
| 388 |
/// |
| 389 |
|
| 390 |
class MergerTest4T1LU : public MergerTestBase { |
| 391 |
protected: |
| 392 |
// Our three tensors (three inputs, one output). |
| 393 |
const unsigned t0 = 0, t1 = 1, t2 = 2, t3 = 3; |
| 394 |
|
| 395 |
// Our single loop. |
| 396 |
const unsigned l0 = 0; |
| 397 |
|
| 398 |
MergerTest4T1LU() : MergerTestBase(4, 1) { |
| 399 |
EXPECT_TRUE(merger.getOutTensorID() == t3); |
| 400 |
|
| 401 |
// Tensor 0: undef input vector. |
| 402 |
merger.addExp(Kind::kTensor, t0, -1u); |
| 403 |
merger.setDimAndDimLevelType(t0, l0, 0, DimLevelType::Undef); |
| 404 |
|
| 405 |
// Tensor 1: dense input vector. |
| 406 |
merger.addExp(Kind::kTensor, t1, -1u); |
| 407 |
merger.setDimAndDimLevelType(t1, l0, 0, DimLevelType::Dense); |
| 408 |
|
| 409 |
// Tensor 2: undef input vector. |
| 410 |
merger.addExp(Kind::kTensor, t2, -1u); |
| 411 |
merger.setDimAndDimLevelType(t2, l0, 0, DimLevelType::Undef); |
| 412 |
|
| 413 |
// Tensor 3: dense output vector. |
| 414 |
merger.addExp(Kind::kTensor, t3, -1u); |
| 415 |
merger.setDimAndDimLevelType(t3, l0, 0, DimLevelType::Dense); |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
/// |
| 420 |
/// Tests with operation on sparse output. |
| 421 |
/// |
| 422 |
|
| 423 |
class MergerTest3T1LSo : public MergerTestBase { |
| 424 |
protected: |
| 425 |
// Our three tensors (two inputs, one output, one synthetic). |
| 426 |
const unsigned t0 = 0, t1 = 1, t2 = 2, t3 = 3; |
| 427 |
|
| 428 |
// Our single loop. |
| 429 |
const unsigned l0 = 0; |
| 430 |
|
| 431 |
MergerTest3T1LSo() : MergerTestBase(3, 1) { |
| 432 |
EXPECT_TRUE(merger.getOutTensorID() == t2); |
| 433 |
EXPECT_TRUE(merger.getSynTensorID() == t3); |
| 434 |
|
| 435 |
merger.setHasSparseOut(true); |
| 436 |
|
| 437 |
// Tensor 0: undef input vector. |
| 438 |
merger.addExp(Kind::kTensor, t0, -1u); |
| 439 |
merger.setDimAndDimLevelType(t0, l0, 0, DimLevelType::Undef); |
| 440 |
|
| 441 |
// Tensor 1: undef input vector. |
| 442 |
merger.addExp(Kind::kTensor, t1, -1u); |
| 443 |
merger.setDimAndDimLevelType(t1, l0, 0, DimLevelType::Undef); |
| 444 |
|
| 445 |
// Tensor 2: sparse output vector. |
| 446 |
merger.addExp(Kind::kTensor, t2, -1u); |
| 447 |
merger.setDimAndDimLevelType(t2, l0, 0, DimLevelType::Compressed); |
| 448 |
} |
| 449 |
}; |
| 450 |
|
| 451 |
} // namespace |
| 452 |
|
| 453 |
/// Vector multiplication (conjunction) of 3 vectors, i.e.; |
| 454 |
/// a(i) = b(i) * c(i) * d(i) |
| 455 |
/// which should form the single lattice point |
| 456 |
/// { |
| 457 |
/// lat( i_00_U i_01_D i_02_U / (tensor_0 * tensor_1 * tensor2) ) |
| 458 |
/// } |
| 459 |
/// after optimization, the dense dimesion should be kept, despite it appears |
| 460 |
/// in the middle |
| 461 |
/// { |
| 462 |
/// lat( i_01_D / (tensor_0 * tensor_1 * tensor2) ) |
| 463 |
/// } |
| 464 |
#define IMPL_MERGER_TEST_CONJ_CONJ_UNDEF(CONJ1, CONJ2) \ |
| 465 |
TEST_F(MergerTest4T1LU, vector_##CONJ1##_##CONJ2) { \ |
| 466 |
auto em = CONJ1##Expr(t0, t1); \ |
| 467 |
auto e = CONJ2##Expr(em, t2); \ |
| 468 |
auto p0 = tensorPattern(t0); \ |
| 469 |
auto p1 = tensorPattern(t1); \ |
| 470 |
auto p2 = tensorPattern(t2); \ |
| 471 |
auto s = merger.buildLattices(e, l0); \ |
| 472 |
expectNumLatPoints(s, 1); \ |
| 473 |
expectLatPoint(s, lat(0), CONJ2##Pattern(CONJ1##Pattern(p0, p1), p2), \ |
| 474 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}})); \ |
| 475 |
s = merger.optimizeSet(s); \ |
| 476 |
expectNumLatPoints(s, 1); \ |
| 477 |
expectLatPoint(s, lat(0), CONJ2##Pattern(CONJ1##Pattern(p0, p1), p2), \ |
| 478 |
loopsToBits({{l0, t1}}), true); \ |
| 479 |
} |
| 480 |
|
| 481 |
FOREVERY_PAIR_OF_COMMON_CONJ_CONJ_BINOP(IMPL_MERGER_TEST_CONJ_CONJ_UNDEF) |
| 482 |
|
| 483 |
#undef IMPL_MERGER_TEST_CONJ_CONJ_UNDEF |
| 484 |
|
| 485 |
/// Vector multiplication (conjunction) of 2 vectors, i.e.; |
| 486 |
/// o(i) = b(i) * c(i) * o(i) |
| 487 |
/// which should form the single lattice point (note how a synthetic tensor |
| 488 |
/// i_03_U is created for the sparse output) |
| 489 |
/// { |
| 490 |
/// lat( i_00_U i_01_U i_03_U / (tensor_0 * tensor_1 * output_tensor_2) ) |
| 491 |
/// } |
| 492 |
/// after optimization, the synthetic tensor should be preserved. |
| 493 |
/// { |
| 494 |
/// lat( i_03_U / (tensor_0 * tensor_1 * output_tensor2) ) |
| 495 |
/// } |
| 496 |
#define IMPL_MERGER_TEST_CONJ_CONJ_SPARSE_OUT(CONJ1, CONJ2) \ |
| 497 |
TEST_F(MergerTest3T1LSo, vector_##CONJ1##_##CONJ2) { \ |
| 498 |
auto em = CONJ1##Expr(t0, t1); \ |
| 499 |
auto e = CONJ2##Expr(em, t2); \ |
| 500 |
auto p0 = tensorPattern(t0); \ |
| 501 |
auto p1 = tensorPattern(t1); \ |
| 502 |
auto p2 = tensorPattern(t2); \ |
| 503 |
auto s = merger.buildLattices(e, l0); \ |
| 504 |
expectNumLatPoints(s, 1); \ |
| 505 |
expectLatPoint(s, lat(0), CONJ2##Pattern(CONJ1##Pattern(p0, p1), p2), \ |
| 506 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t3}})); \ |
| 507 |
s = merger.optimizeSet(s); \ |
| 508 |
expectNumLatPoints(s, 1); \ |
| 509 |
expectLatPoint(s, lat(0), CONJ2##Pattern(CONJ1##Pattern(p0, p1), p2), \ |
| 510 |
loopsToBits({{l0, t3}}), true); \ |
| 511 |
} |
| 512 |
|
| 513 |
FOREVERY_PAIR_OF_COMMON_CONJ_CONJ_BINOP(IMPL_MERGER_TEST_CONJ_CONJ_SPARSE_OUT) |
| 514 |
|
| 515 |
#undef IMPL_MERGER_TEST_CONJ_CONJ_SPARSE_OUT |
| 516 |
|
| 517 |
/// Vector addition (disjunction) of 2 vectors. i.e.; |
| 518 |
/// a(i) = b(i) + c(i) |
| 519 |
/// which should form the 3 lattice points |
| 520 |
/// { |
| 521 |
/// lat( i_00 i_01 / (tensor_0 + tensor_1) ) |
| 522 |
/// lat( i_00 / tensor_0 ) |
| 523 |
/// lat( i_01 / tensor_1 ) |
| 524 |
/// } |
| 525 |
/// and after optimization, the lattice points do not change (as there is no |
| 526 |
/// duplicated point and all input vectors are sparse vector). |
| 527 |
/// { |
| 528 |
/// lat( i_00 i_01 / (tensor_0 + tensor_1) ) |
| 529 |
/// lat( i_00 / tensor_0 ) |
| 530 |
/// lat( i_01 / tensor_1 ) |
| 531 |
/// } |
| 532 |
#define IMPL_MERGER_TEST_DISJ(OP) \ |
| 533 |
TEST_F(MergerTest3T1L, vector_##OP) { \ |
| 534 |
auto e = OP##Expr(tensor(t0), tensor(t1)); \ |
| 535 |
auto p0 = tensorPattern(t0); \ |
| 536 |
auto p1 = tensorPattern(t1); \ |
| 537 |
auto s = merger.buildLattices(e, l0); \ |
| 538 |
\ |
| 539 |
expectNumLatPoints(s, 3); \ |
| 540 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 541 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 542 |
expectLatPointWithinRange(s, lat(1), 2, p0, loopsToBits({{l0, t0}})); \ |
| 543 |
expectLatPointWithinRange(s, lat(1), 2, p1, loopsToBits({{l0, t1}})); \ |
| 544 |
\ |
| 545 |
s = merger.optimizeSet(s); \ |
| 546 |
expectNumLatPoints(s, 3); \ |
| 547 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 548 |
loopsToBits({{l0, t0}, {l0, t1}}), true); \ |
| 549 |
expectLatPointWithinRange(s, lat(1), 2, p0, loopsToBits({{l0, t0}}), \ |
| 550 |
true); \ |
| 551 |
expectLatPointWithinRange(s, lat(1), 2, p1, loopsToBits({{l0, t1}}), \ |
| 552 |
true); \ |
| 553 |
} |
| 554 |
|
| 555 |
FOREVERY_COMMON_DISJ_BINOP(IMPL_MERGER_TEST_DISJ) |
| 556 |
|
| 557 |
#undef IMPL_MERGER_TEST_DISJ |
| 558 |
|
| 559 |
/// Vector multiplication (conjunction) of 2 vectors, i.e.; |
| 560 |
/// a(i) = b(i) * c(i) |
| 561 |
/// which should form the single lattice point |
| 562 |
/// { |
| 563 |
/// lat( i_00 i_01 / (tensor_0 * tensor_1) ) |
| 564 |
/// } |
| 565 |
#define IMPL_MERGER_TEST_CONJ(OP) \ |
| 566 |
TEST_F(MergerTest3T1L, vector_##OP) { \ |
| 567 |
auto e = OP##Expr(t0, t1); \ |
| 568 |
auto p0 = tensorPattern(t0); \ |
| 569 |
auto p1 = tensorPattern(t1); \ |
| 570 |
auto s = merger.buildLattices(e, l0); \ |
| 571 |
\ |
| 572 |
expectNumLatPoints(s, 1); \ |
| 573 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 574 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 575 |
\ |
| 576 |
s = merger.optimizeSet(s); \ |
| 577 |
expectNumLatPoints(s, 1); \ |
| 578 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 579 |
loopsToBits({{l0, t0}, {l0, t1}}), true); \ |
| 580 |
} |
| 581 |
|
| 582 |
FOREVERY_COMMON_CONJ_BINOP(IMPL_MERGER_TEST_CONJ) |
| 583 |
|
| 584 |
#undef IMPL_MERGER_TEST_CONJ |
| 585 |
|
| 586 |
/// Vector multiplication (conjunction) then addition (disjunction), i.e.; |
| 587 |
/// a(i) = b(i) * c(i) + d(i); |
| 588 |
/// which should form |
| 589 |
/// { |
| 590 |
/// lat( i_00 i_01 i_02 / (tensor_0 * tensor_1) + tensor_2 ) |
| 591 |
/// lat( i_00 i_01 / tensor_0 * tensor_1 |
| 592 |
/// lat( i_02 / tensor_2 ) |
| 593 |
/// } |
| 594 |
#define IMPL_MERGER_TEST_CONJ_DISJ(CONJ, DISJ) \ |
| 595 |
TEST_F(MergerTest4T1L, vector_##CONJ##_##DISJ) { \ |
| 596 |
auto em = CONJ##Expr(t0, t1); \ |
| 597 |
auto e = DISJ##Expr(em, t2); \ |
| 598 |
auto p0 = tensorPattern(t0); \ |
| 599 |
auto p1 = tensorPattern(t1); \ |
| 600 |
auto p2 = tensorPattern(t2); \ |
| 601 |
auto s = merger.buildLattices(e, l0); \ |
| 602 |
\ |
| 603 |
expectNumLatPoints(s, 3); \ |
| 604 |
expectLatPoint(s, lat(0), DISJ##Pattern(CONJ##Pattern(p0, p1), p2), \ |
| 605 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}})); \ |
| 606 |
expectLatPointWithinRange(s, lat(1), 2, CONJ##Pattern(p0, p1), \ |
| 607 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 608 |
expectLatPointWithinRange(s, lat(1), 2, p2, loopsToBits({{l0, t2}})); \ |
| 609 |
\ |
| 610 |
s = merger.optimizeSet(s); \ |
| 611 |
expectNumLatPoints(s, 3); \ |
| 612 |
expectLatPoint(s, lat(0), DISJ##Pattern(CONJ##Pattern(p0, p1), p2), \ |
| 613 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}})); \ |
| 614 |
expectLatPointWithinRange(s, lat(1), 2, CONJ##Pattern(p0, p1), \ |
| 615 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 616 |
expectLatPointWithinRange(s, lat(1), 2, p2, loopsToBits({{l0, t2}})); \ |
| 617 |
} |
| 618 |
|
| 619 |
FOREVERY_PAIR_OF_COMMON_CONJ_DISJ_BINOP(IMPL_MERGER_TEST_CONJ_DISJ) |
| 620 |
|
| 621 |
#undef IMPL_MERGER_TEST_CONJ_DISJ |
| 622 |
|
| 623 |
/// Vector addition (disjunction) then addition (disjunction), i.e.; |
| 624 |
/// a(i) = b(i) + c(i) + d(i) |
| 625 |
/// which should form |
| 626 |
/// { |
| 627 |
/// lat( i_00 i_01 i_02 / (tensor_0 + tensor_1) + tensor_2 ) |
| 628 |
/// lat( i_02 i_01 / tensor_2 + tensor_1 ) |
| 629 |
/// lat( i_02 i_00 / tensor_2 + tensor_0 ) |
| 630 |
/// lat( i_01 i_00 / tensor_1 + tensor_0 ) |
| 631 |
/// lat( i_02 / tensor_2 ) |
| 632 |
/// lat( i_01 / tensor_1 ) |
| 633 |
/// lat( i_00 / tensor_0 ) |
| 634 |
/// } |
| 635 |
#define IMPL_MERGER_TEST_DISJ_DISJ(DISJ1, DISJ2) \ |
| 636 |
TEST_F(MergerTest4T1L, Vector_##DISJ1##_##DISJ2) { \ |
| 637 |
auto em = DISJ1##Expr(t0, t1); \ |
| 638 |
auto e = DISJ2##Expr(em, t2); \ |
| 639 |
auto p0 = tensorPattern(t0); \ |
| 640 |
auto p1 = tensorPattern(t1); \ |
| 641 |
auto p2 = tensorPattern(t2); \ |
| 642 |
auto s = merger.buildLattices(e, l0); \ |
| 643 |
\ |
| 644 |
expectNumLatPoints(s, 7); \ |
| 645 |
expectLatPoint(s, lat(0), DISJ2##Pattern(DISJ1##Pattern(p0, p1), p2), \ |
| 646 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}})); \ |
| 647 |
expectLatPointWithinRange(s, lat(1), 6, DISJ2##Pattern(p1, p2), \ |
| 648 |
loopsToBits({{l0, t1}, {l0, t2}})); \ |
| 649 |
expectLatPointWithinRange(s, lat(1), 6, DISJ2##Pattern(p0, p2), \ |
| 650 |
loopsToBits({{l0, t0}, {l0, t2}})); \ |
| 651 |
expectLatPointWithinRange(s, lat(1), 6, DISJ1##Pattern(p0, p1), \ |
| 652 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 653 |
expectLatPointWithinRange(s, lat(1), 6, p2, loopsToBits({{l0, t2}})); \ |
| 654 |
expectLatPointWithinRange(s, lat(1), 6, p1, loopsToBits({{l0, t1}})); \ |
| 655 |
expectLatPointWithinRange(s, lat(1), 6, p0, loopsToBits({{l0, t0}})); \ |
| 656 |
\ |
| 657 |
s = merger.optimizeSet(s); \ |
| 658 |
expectNumLatPoints(s, 7); \ |
| 659 |
expectLatPoint(s, lat(0), DISJ2##Pattern(DISJ1##Pattern(p0, p1), p2), \ |
| 660 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}})); \ |
| 661 |
expectLatPointWithinRange(s, lat(1), 6, DISJ2##Pattern(p1, p2), \ |
| 662 |
loopsToBits({{l0, t1}, {l0, t2}})); \ |
| 663 |
expectLatPointWithinRange(s, lat(1), 6, DISJ2##Pattern(p0, p2), \ |
| 664 |
loopsToBits({{l0, t0}, {l0, t2}})); \ |
| 665 |
expectLatPointWithinRange(s, lat(1), 6, DISJ1##Pattern(p0, p1), \ |
| 666 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 667 |
expectLatPointWithinRange(s, lat(1), 6, p2, loopsToBits({{l0, t2}})); \ |
| 668 |
expectLatPointWithinRange(s, lat(1), 6, p1, loopsToBits({{l0, t1}})); \ |
| 669 |
expectLatPointWithinRange(s, lat(1), 6, p0, loopsToBits({{l0, t0}})); \ |
| 670 |
} |
| 671 |
|
| 672 |
FOREVERY_PAIR_OF_COMMON_DISJ_DISJ_BINOP(IMPL_MERGER_TEST_DISJ_DISJ) |
| 673 |
|
| 674 |
#undef IMPL_MERGER_TEST_DISJ_DISJ |
| 675 |
|
| 676 |
/// Vector multiplication (conjunction) then multiplication (conjunction), i.e.; |
| 677 |
/// a(i) = b(i) * c(i) * d(i); |
| 678 |
/// which should form |
| 679 |
/// { |
| 680 |
/// lat( i_00 i_01 i_02 / tensor_0 * tensor_1 * tensor_2 ) |
| 681 |
/// } |
| 682 |
#define IMPL_MERGER_TEST_CONJ_CONJ(CONJ1, CONJ2) \ |
| 683 |
TEST_F(MergerTest4T1L, vector_##CONJ1##_##CONJ2) { \ |
| 684 |
auto em = CONJ1##Expr(t0, t1); \ |
| 685 |
auto e = CONJ2##Expr(em, t2); \ |
| 686 |
auto p0 = tensorPattern(t0); \ |
| 687 |
auto p1 = tensorPattern(t1); \ |
| 688 |
auto p2 = tensorPattern(t2); \ |
| 689 |
auto s = merger.buildLattices(e, l0); \ |
| 690 |
expectNumLatPoints(s, 1); \ |
| 691 |
expectLatPoint(s, lat(0), CONJ2##Pattern(CONJ1##Pattern(p0, p1), p2), \ |
| 692 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}})); \ |
| 693 |
s = merger.optimizeSet(s); \ |
| 694 |
expectNumLatPoints(s, 1); \ |
| 695 |
expectLatPoint(s, lat(0), CONJ2##Pattern(CONJ1##Pattern(p0, p1), p2), \ |
| 696 |
loopsToBits({{l0, t0}, {l0, t1}, {l0, t2}}), true); \ |
| 697 |
} |
| 698 |
|
| 699 |
FOREVERY_PAIR_OF_COMMON_CONJ_CONJ_BINOP(IMPL_MERGER_TEST_CONJ_CONJ) |
| 700 |
|
| 701 |
#undef IMPL_MERGER_TEST_CONJ_CONJ |
| 702 |
|
| 703 |
/// Vector addition (disjunction) of 2 vectors, i.e.; |
| 704 |
/// a(i) = b(i) + c(i) |
| 705 |
/// which should form the 3 lattice points |
| 706 |
/// { |
| 707 |
/// lat( i_00 i_01 / (sparse_tensor_0 + dense_tensor_1) ) |
| 708 |
/// lat( i_00 / sparse_tensor_0 ) |
| 709 |
/// lat( i_01 / dense_tensor_1 ) |
| 710 |
/// } |
| 711 |
/// which should be optimized to |
| 712 |
/// { |
| 713 |
/// lat( i_00 i_01 / (sparse_tensor_0 + dense_tensor_1) ) (not singleton) |
| 714 |
/// lat( i_01 / dense_tensor_0 ) (no sparse dimension) |
| 715 |
/// } |
| 716 |
/// |
| 717 |
/// lat( i_00 / sparse_tensor_0 ) should be opted out as it only has dense diff |
| 718 |
/// with lat( i_00 i_01 / (sparse_tensor_0 + dense_tensor_1) ). |
| 719 |
#define IMPL_MERGER_TEST_OPTIMIZED_DISJ(OP) \ |
| 720 |
TEST_F(MergerTest3T1LD, vector_opted_##OP) { \ |
| 721 |
auto e = OP##Expr(tensor(t0), tensor(t1)); \ |
| 722 |
auto p0 = tensorPattern(t0); \ |
| 723 |
auto p1 = tensorPattern(t1); \ |
| 724 |
auto s = merger.buildLattices(e, l0); \ |
| 725 |
\ |
| 726 |
expectNumLatPoints(s, 3); \ |
| 727 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 728 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 729 |
expectLatPointWithinRange(s, lat(1), 2, p0, loopsToBits({{l0, t0}})); \ |
| 730 |
expectLatPointWithinRange(s, lat(1), 2, p1, loopsToBits({{l0, t1}})); \ |
| 731 |
\ |
| 732 |
s = merger.optimizeSet(s); \ |
| 733 |
expectNumLatPoints(s, 2); \ |
| 734 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 735 |
loopsToBits({{l0, t0}, {l0, t1}}), true); \ |
| 736 |
expectLatPoint(s, lat(1), p1, loopsToBits({{l0, t1}}), true); \ |
| 737 |
} |
| 738 |
|
| 739 |
FOREVERY_COMMON_DISJ_BINOP(IMPL_MERGER_TEST_OPTIMIZED_DISJ) |
| 740 |
|
| 741 |
#undef IMPL_MERGER_TEST_OPTIMIZED_CONJ |
| 742 |
|
| 743 |
/// Vector multiplication (conjunction) of 2 vectors, i.e.: |
| 744 |
/// a(i) = b(i) * c(i) |
| 745 |
/// which should form the single lattice point |
| 746 |
/// { |
| 747 |
/// lat( i_00 i_01 / (sparse_tensor_0 * dense_tensor_1) ) |
| 748 |
/// } |
| 749 |
/// it should be optimized to |
| 750 |
/// { |
| 751 |
/// lat( i_00 / (sparse_tensor_0 * dense_tensor_1) ) |
| 752 |
/// } |
| 753 |
/// since i_01 is a dense dimension. |
| 754 |
#define IMPL_MERGER_TEST_OPTIMIZED_CONJ(OP) \ |
| 755 |
TEST_F(MergerTest3T1LD, vector_opted_##OP) { \ |
| 756 |
auto e = OP##Expr(t0, t1); \ |
| 757 |
auto p0 = tensorPattern(t0); \ |
| 758 |
auto p1 = tensorPattern(t1); \ |
| 759 |
auto s = merger.buildLattices(e, l0); \ |
| 760 |
\ |
| 761 |
expectNumLatPoints(s, 1); \ |
| 762 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), \ |
| 763 |
loopsToBits({{l0, t0}, {l0, t1}})); \ |
| 764 |
\ |
| 765 |
s = merger.optimizeSet(s); \ |
| 766 |
expectNumLatPoints(s, 1); \ |
| 767 |
expectLatPoint(s, lat(0), OP##Pattern(p0, p1), loopsToBits({{l0, t0}}), \ |
| 768 |
true); \ |
| 769 |
} |
| 770 |
|
| 771 |
FOREVERY_COMMON_CONJ_BINOP(IMPL_MERGER_TEST_OPTIMIZED_CONJ) |
| 772 |
|
| 773 |
#undef IMPL_MERGER_TEST_OPTIMIZED_CONJ |
| 774 |
|
| 775 |
// TODO: mult-dim tests |
| 776 |
|
| 777 |
// restore warning status |
| 778 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 779 |
#pragma warning(pop) |
| 780 |
#endif |