1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
|
//===- Sparsification.cpp - Implementation of sparsification --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements converting sparse tensor types to actual sparse code.
//
//===----------------------------------------------------------------------===//
#include "CodegenEnv.h"
#include "CodegenUtils.h"
#include "LoopEmitter.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Utils/Utils.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/SCF/Transforms/Transforms.h"
#include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
#include "mlir/Dialect/SparseTensor/Transforms/Passes.h"
#include "mlir/Dialect/SparseTensor/Utils/Merger.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/AffineExprVisitor.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/TensorEncoding.h"
#include "llvm/ADT/SmallBitVector.h"
#include <optional>
using namespace mlir;
using namespace mlir::sparse_tensor;
//===----------------------------------------------------------------------===//
// Declarations
//===----------------------------------------------------------------------===//
namespace {
/// Iteration graph sorting.
enum SortMask {
kSparseOnly = 0x0,
kIncludeDense = 0x1,
kIncludeUndef = 0x2,
kIncludeAll = 0x3
};
/// A helper class that visits an affine expression and tries to find an
/// AffineDimExpr to which the corresponding iterator from a GenericOp matches
/// the desired iterator type.
class AffineDimFinder : public AffineExprVisitor<AffineDimFinder> {
public:
explicit AffineDimFinder(linalg::GenericOp op)
: iterTypes(op.getIteratorTypesArray()) {}
void visitDimExpr(AffineDimExpr expr) {
if (pickedDim == nullptr || pickIterType == iterTypes[expr.getPosition()]) {
pickedDim = expr;
}
}
/// Set the desired iterator type that we want to pick.
void setPickedIterType(utils::IteratorType iterType) {
pickIterType = iterType;
}
/// Get the desired AffineDimExpr.
AffineDimExpr getDimExpr() const { return pickedDim.cast<AffineDimExpr>(); }
private:
/// The picked AffineDimExpr after visit.
AffineExpr pickedDim;
/// The iterator type that we want.
utils::IteratorType pickIterType;
/// The mapping between dim=>iterator type.
SmallVector<utils::IteratorType> iterTypes;
};
} // namespace
//===----------------------------------------------------------------------===//
// Sparse compiler analysis methods.
//===----------------------------------------------------------------------===//
/// Determines if affine expression is invariant.
static bool isInvariantAffine(AffineExpr a, ArrayRef<unsigned> loopStack,
unsigned ldx, bool &atLevel) {
switch (a.getKind()) {
case AffineExprKind::DimId: {
unsigned idx = a.cast<AffineDimExpr>().getPosition();
if (idx == ldx) {
atLevel = true;
// Must be invariant if we are at the level.
return true;
}
bool isInvariant = false;
for (unsigned loop : loopStack) {
isInvariant = (loop == idx);
if (isInvariant)
break;
}
return isInvariant;
}
case AffineExprKind::Add:
case AffineExprKind::Mul: {
auto binOp = a.cast<AffineBinaryOpExpr>();
return isInvariantAffine(binOp.getLHS(), loopStack, ldx, atLevel) &&
isInvariantAffine(binOp.getRHS(), loopStack, ldx, atLevel);
}
default: {
assert(a.isa<AffineConstantExpr>());
return true;
}
}
}
/// Determines if affine expression is invariant.
static bool isInvariantAffine(CodegenEnv &env, AffineExpr a, unsigned ldx,
bool &atLevel) {
return isInvariantAffine(a, env.getLoopCurStack(), ldx, atLevel);
}
/// Helper method to construct a permuted dimension ordering
/// that adheres to the given topological sort.
static AffineMap permute(CodegenEnv &env, AffineMap m) {
assert(m.getNumDims() + env.merger().getNumFilterLoops() ==
env.topSortSize() &&
"size mismatch");
// Construct the inverse of `m`; to avoid the asymptotic complexity
// of calling `m.getPermutedPosition` repeatedly.
SmallVector<unsigned> perm;
unsigned numResults = m.getNumResults();
BitVector worklist(numResults, true);
unsigned loopDepth = 1;
// Construct the permutation.
while (worklist.any() && loopDepth <= env.topSortSize()) {
unsigned preSize = perm.size();
for (auto dim : worklist.set_bits()) {
bool atLevel = false;
if (m.getResult(dim).isa<AffineConstantExpr>() ||
(isInvariantAffine(m.getResult(dim),
env.getTopSortSlice(0, loopDepth),
env.topSortAt(loopDepth - 1), atLevel) &&
atLevel)) {
// If the matching affine is constant expression or just become
// invariant. We can visit the dimension now without breaking the
// topSort constraint.
perm.push_back(dim);
}
}
// Removes resolved dimension.
for (unsigned i = preSize, e = perm.size(); i < e; i++)
worklist.reset(perm[i]);
// Tries to entering the next loop level.
loopDepth += 1;
}
assert(perm.size() == numResults);
return AffineMap::getPermutationMap(perm, env.op().getContext());
}
/// Helper method to inspect affine expressions. Rejects cases where the
/// same index is used more than once. Also rejects compound affine
/// expressions in sparse dimensions.
/// filterIdx stores the current filter loop idx should be used for the next
/// compound affine sparse level, and it will be incremented by one when
/// used.
static bool findAffine(Merger &merger, unsigned tensor, unsigned dim,
AffineExpr a, DimLevelType dlt, unsigned &filterLdx,
bool setLvlFormat = true) {
switch (a.getKind()) {
case AffineExprKind::DimId: {
unsigned idx = a.cast<AffineDimExpr>().getPosition();
if (!isUndefDLT(merger.getDimLevelType(tensor, idx)))
return false; // used more than once
if (setLvlFormat)
merger.setDimAndDimLevelType(tensor, idx, dim, dlt);
return true;
}
case AffineExprKind::Add:
case AffineExprKind::Mul:
case AffineExprKind::Constant: {
if (!isDenseDLT(dlt) && setLvlFormat) {
assert(isUndefDLT(merger.getDimLevelType(tensor, filterLdx)));
// Use a filter loop for sparse affine expression.
merger.setDimAndDimLevelType(tensor, filterLdx++, dim, dlt);
}
if (auto binOp = a.dyn_cast<AffineBinaryOpExpr>()) {
// We do not set dim level format for affine expresssion like d0 + d1 on
// either loop index at d0 or d1.
// We continue the recursion merely to check whether current affine is
// admissible or not.
return findAffine(merger, tensor, dim, binOp.getLHS(), dlt, filterLdx,
false) &&
findAffine(merger, tensor, dim, binOp.getRHS(), dlt, filterLdx,
false);
}
// Falls through when it is a constant Affine
return true;
}
default:
return false;
}
}
/// Get the total number of compound affine expressions in affineMap that are
/// attached to the given tensor. For the following inputs:
///
/// affineMap = (d0, d1, d2) => (d0 + d1, d2)
/// tensor = ["compressed", "compressed"]
///
/// Returns 1 (because the first level is compressed and its corresponding
/// affineMap is d0 + d1)
static unsigned getNumCompoundAffineOnSparseDims(AffineMap affineMap,
Value tensor) {
unsigned num = 0;
auto enc = getSparseTensorEncoding(tensor.getType());
if (enc) {
ArrayRef<AffineExpr> exps = affineMap.getResults();
for (unsigned rank = 0; rank < exps.size(); rank++) {
auto aidx = toOrigDim(enc, rank);
auto affine = exps[aidx];
if (!affine.isa<AffineDimExpr>())
if (!isDenseDLT(getDimLevelType(enc, rank)))
num++;
}
}
return num;
}
/// Get the total number of compound affine expressions attached on a sparse
/// level in the given GenericOp.
static unsigned getNumCompoundAffineOnSparseDims(linalg::GenericOp op) {
unsigned num = 0;
for (OpOperand &t : op->getOpOperands())
num += getNumCompoundAffineOnSparseDims(op.getMatchingIndexingMap(&t),
t.get());
return num;
}
/// Helper method to inspect sparse encodings in the tensor types.
/// Fills the per-dimension sparsity information for all tensors.
/// Returns true if the sparse annotations and affine subscript
/// expressions of all tensors are admissible. Returns false if
/// no annotations are found or inadmissible constructs occur.
static bool findSparseAnnotations(CodegenEnv &env) {
bool annotated = false;
unsigned filterLdx = env.merger().getFilterLoopStartingIdx();
for (OpOperand &t : env.op()->getOpOperands()) {
auto map = env.op().getMatchingIndexingMap(&t);
auto enc = getSparseTensorEncoding(t.get().getType());
if (enc)
annotated = true;
assert(map.getNumResults() == env.op().getRank(&t));
for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) {
unsigned tensor = t.getOperandNumber();
AffineExpr a = map.getResult(toOrigDim(enc, d));
if (!findAffine(env.merger(), tensor, d, a, getDimLevelType(enc, d),
filterLdx))
return false; // inadmissible affine expression
}
}
assert(filterLdx == env.merger().getNumLoops());
return annotated;
}
/// A helper to compute a topological sort. O(n^2) time complexity
/// as we use adj matrix for the graph.
/// The sorted result will put the first Reduction iterator to the
/// latest possible index.
static bool topSortOptimal(CodegenEnv &env, unsigned n,
ArrayRef<utils::IteratorType> iteratorTypes,
std::vector<unsigned> &inDegree,
std::vector<std::vector<bool>> &adjM) {
std::vector<unsigned> redIt; // reduce iterator with 0 degree
std::vector<unsigned> parIt; // parallel iterator with 0 degree
std::vector<unsigned> filterIt; // filter loop with 0 degree
for (unsigned i = 0; i < n; i++) {
if (inDegree[i] == 0) {
if (env.merger().isFilterLoop(i))
filterIt.push_back(i);
else if (linalg::isReductionIterator(iteratorTypes[i]))
redIt.push_back(i);
else
parIt.push_back(i);
}
}
while (!redIt.empty() || !parIt.empty() || !filterIt.empty()) {
// We always choose in order of filter loop -> parallel loop -> reduction
// loop because
// 1. Putting reduction loop early might make the loop sequence
// inadmissible.
// 2. Filter loops should be put as early as possible for better
// performance, since only one (if any) iteration will carry the
// computation. E.g., for (1 to N)
// for (1 to M)
// for (1 to K)
// if (xxx)
// O(X) computation => O(NMK+NMX) time complexity
//
// By putting the filter loop one level up, we got
//
// for (1 to N)
// for (1 to K)
// if (xxx)
// for (1 to M)
// O(X) computation => O(NK+NMX) time complexity
auto &it = !filterIt.empty() ? filterIt : (!parIt.empty() ? parIt : redIt);
auto src = it.back();
env.topSortPushBack(src);
it.pop_back();
// Update in-degree, and push 0-degree node into worklist.
for (unsigned dst = 0; dst < n; dst++) {
if (adjM[src][dst] && --inDegree[dst] == 0) {
if (env.merger().isFilterLoop(dst))
filterIt.push_back(dst);
else if (linalg::isReductionIterator(iteratorTypes[dst]))
redIt.push_back(dst);
else
parIt.push_back(dst);
}
}
}
return env.topSortSize() == n;
}
/// Helper method to add all constraints from the indices in one affine
/// expression before all indices in the other affine expression. For
/// example i0+i1 < i2+i3+1 yields i0<i2, i0<i3, i1<i2, and i1<i3.
/// The affine expression `a` is empty iff `fidx` have a value, leading to
/// b = (i0 + i1) < fidx => i0 < fidx, i1 < fidx.
/// The affine expression `b` is empty iff `tidx` have a value, leading to
/// tidx < a = (i0 + i1) => tidx < i0, tidx < i1.
static void addAffineOrderings(std::vector<std::vector<bool>> &adjM,
std::vector<unsigned> &inDegree, AffineExpr a,
AffineExpr b, std::optional<unsigned> fidx,
std::optional<unsigned> tidx) {
if (!a && !b) {
// Recursion leaf.
assert(fidx && tidx);
unsigned f = *fidx, t = *tidx;
if (!adjM[f][t]) {
adjM[f][t] = true;
inDegree[t]++;
}
return;
}
// Picks an affine expression and expand (recurse into) it.
auto toExpand = a ? a : b;
switch (toExpand.getKind()) {
case AffineExprKind::DimId: {
auto idx = toExpand.cast<AffineDimExpr>().getPosition();
if (toExpand == a)
addAffineOrderings(adjM, inDegree, AffineExpr(), b, idx, tidx);
else // toExpand == b
addAffineOrderings(adjM, inDegree, a, AffineExpr(), fidx, idx);
break;
}
case AffineExprKind::Add:
case AffineExprKind::Mul: {
auto binOp = toExpand.cast<AffineBinaryOpExpr>();
if (toExpand == a) {
addAffineOrderings(adjM, inDegree, binOp.getLHS(), b, fidx, tidx);
addAffineOrderings(adjM, inDegree, binOp.getRHS(), b, fidx, tidx);
} else {
addAffineOrderings(adjM, inDegree, a, binOp.getLHS(), fidx, tidx);
addAffineOrderings(adjM, inDegree, a, binOp.getRHS(), fidx, tidx);
}
break;
}
default:
break;
}
}
static void tryLoosenAffineDenseConstraints(linalg::GenericOp op,
std::optional<unsigned> &fldx,
AffineExpr &fa,
std::optional<unsigned> &tldx,
AffineExpr &ta) {
// We use a heuristic here to only pick one dim expression from each
// compound affine expression to establish the order between two dense
// dimensions.
if (!tldx) {
AffineDimFinder finder(op);
// NOTE: The ordering can only be loosen when the destination level is
// dense (when !tldx), for [dense, sparse] -> (d0 + d1, d2), we still
// require both d0 < d2 and d1 < d2 to ensure correct ordering (i.e.,
// no ordering like d0->d2->d1).
// TODO: this is obviously a sub optimal solution.
if (!fldx && !fa.isa<AffineConstantExpr>()) {
// Heuristic: we prefer parallel loop for lhs to reduce the chance
// we add reduce < parallel ordering.
finder.setPickedIterType(utils::IteratorType::parallel);
finder.walkPostOrder(fa);
fa = finder.getDimExpr();
fldx = finder.getDimExpr().getPosition();
}
if (!ta.isa<AffineConstantExpr>()) {
// Heuristic: we prefer reduction loop for rhs to reduce the chance
// addint reduce < parallel ordering.
finder.setPickedIterType(utils::IteratorType::reduction);
finder.walkPostOrder(ta);
ta = finder.getDimExpr();
tldx = finder.getDimExpr().getPosition();
}
}
}
/// Computes a topologically sorted iteration graph for the linalg
/// operation. Ensures all tensors are visited in natural index order. This
/// is essential for sparse storage formats since these only support access
/// along fixed dimensions. Even for dense storage formats, however, the
/// natural index order yields innermost unit-stride access with better
/// spatial locality.
static bool computeIterationGraph(CodegenEnv &env, unsigned mask,
OpOperand *skip = nullptr) {
// Set up an n x n from/to adjacency matrix of the iteration graph
// for the implicit loop indices i_0 .. i_n-1.
unsigned n = env.merger().getNumLoops();
std::vector<std::vector<bool>> adjM(n, std::vector<bool>(n, false));
std::vector<unsigned> inDegree(n, 0); // in-degree of each node.
auto iteratorTypes = env.op().getIteratorTypesArray();
// Iterate over the indexing maps of every tensor in the tensor expression.
for (OpOperand &t : env.op()->getOpOperands()) {
// Get map and encoding.
auto map = env.op().getMatchingIndexingMap(&t);
auto enc = getSparseTensorEncoding(t.get().getType());
assert(map.getNumDims() + getNumCompoundAffineOnSparseDims(env.op()) == n);
// Skip dense tensor constraints when not requested.
if (!(mask & SortMask::kIncludeDense) && !enc)
continue;
// Each tensor expression and optional dimension ordering (row-major
// by default) puts an ordering constraint on the loop indices. For
// example, the tensor expresion A_ijk forces the ordering i < j < k
// on the loop indices if no explicit dimension ordering is given.
for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) {
AffineExpr ta = map.getResult(toOrigDim(enc, d));
std::optional<unsigned> tldx =
env.merger().getLoopIdx(t.getOperandNumber(), d);
// Filter loops should be constructed after all the dependent loops,
// i.e., d0 + d1 < filter_loop(d0 + d1)
if (tldx && env.merger().isFilterLoop(*tldx)) {
assert(!ta.isa<AffineDimExpr>() &&
!isDenseDLT(getDimLevelType(enc, d)));
addAffineOrderings(adjM, inDegree, ta, AffineExpr(), std::nullopt,
tldx);
// Now that the ordering of affine expression is captured by filter
// loop idx, we only need to ensure the affine ordering against filter
// loop. Thus, we reset the affine express to nil here to mark it as
// resolved.
ta = AffineExpr();
}
// Skip tensor during cycle resolution, though order between filter loop
// and dependent loops need to be guaranteed unconditionally.
if (&t == skip)
continue;
if (d > 0) {
AffineExpr fa = map.getResult(toOrigDim(enc, d - 1));
std::optional<unsigned> fldx =
env.merger().getLoopIdx(t.getOperandNumber(), d - 1);
// Applying order constraints on every pair of dimExpr between two
// compound affine expressions can sometime too strict:
// E.g, for [dense, dense] -> (d0 + d1, d2 + d3).
// It is totally fine to have loop sequence d0->d2->d1->d3 instead of
// requiring d0 < d2, d1 < d2, d0 < d3, d1 < d3.
if (!(mask & SortMask::kIncludeDense))
tryLoosenAffineDenseConstraints(env.op(), fldx, fa, tldx, ta);
// (d0 + d1) < (d2 + d3), or
// filter_loop_d-1 < (d2 + d3), or
// (d0 + d1) < filter_loop_d, or
// filter_loop_d-1 < filter_loop_d depending on whether fa/ta is reset
// above.
addAffineOrderings(adjM, inDegree, fa, ta, fldx, tldx);
}
}
// Push unrelated loops into sparse iteration space, so these
// will be skipped more often.
if (mask & SortMask::kIncludeUndef) {
unsigned tensor = t.getOperandNumber();
for (unsigned i = 0; i < n; i++)
if (isCompressedDLT(env.dlt(tensor, i)) ||
isSingletonDLT(env.dlt(tensor, i))) {
for (unsigned j = 0; j < n; j++)
if (isUndefDLT(env.dlt(tensor, j))) {
adjM[i][j] = true;
inDegree[j]++;
}
} else {
assert(isDenseDLT(env.dlt(tensor, i)) ||
isUndefDLT(env.dlt(tensor, i)));
}
}
}
// Topologically sort the iteration graph to determine loop order.
// Report failure for a cyclic iteration graph.
env.topSortClear(n);
return topSortOptimal(env, n, iteratorTypes, inDegree, adjM);
}
/// Returns true if tensor materializes uninitialized into the computation.
static bool isMaterializing(Value val) {
return val.getDefiningOp<tensor::EmptyOp>() ||
val.getDefiningOp<bufferization::AllocTensorOp>();
}
/// Returns true when the tensor expression is admissible for codegen.
/// Since all sparse input tensors are admissible, we just need to check
/// whether the out tensor in the tensor expression codegen is admissible.
/// Sets `sparseOut` to the tensor and `outerParNest` to the outer injective
/// nesting depth when a "truly dynamic" sparse tensor output occurs.
static bool isAdmissibleTensorExp(CodegenEnv &env, unsigned exp,
OpOperand **sparseOut,
unsigned *outerParNest) {
// We reject any expression that makes a reduction from `-outTensor`, as those
// expressions create a dependency between the current iteration (i) and the
// previous iteration (i-1). It would require iterating over the whole
// coordinate space, which prevent exploiting sparsity for faster code.
for (utils::IteratorType it : env.op().getIteratorTypesArray()) {
if (it == utils::IteratorType::reduction) {
if (env.merger().hasNegateOnOut(exp))
return false;
break;
}
}
OpOperand *lhs = env.op().getDpsInitOperand(0);
unsigned tensor = lhs->getOperandNumber();
auto enc = getSparseTensorEncoding(lhs->get().getType());
// An non-annotated output tensor is assumed dense, and becomes a random
// access n-dim memref. Admissible since insertions cannot occur.
if (!enc)
return true;
// An all-dense annotated "sparse" output tensor becomes a linearized random
// access 1-dim memref. Also admissible since insertions cannot occur.
bool allDense = true;
unsigned numLoops =
env.merger().getNumLoops(); // numNativeLoops + numFilterLoops
for (unsigned i = 0; i < env.merger().getNumLoops(); i++)
if (isCompressedDLT(env.dlt(tensor, i)) ||
isSingletonDLT(env.dlt(tensor, i))) {
allDense = false;
break;
} else {
assert(isDenseDLT(env.dlt(tensor, i)) || isUndefDLT(env.dlt(tensor, i)));
}
if (allDense)
return true;
// TODO: support compound affine expression on sparse output.
if (getNumCompoundAffineOnSparseDims(env.op().getMatchingIndexingMap(lhs),
lhs->get()) != 0)
return false;
// A tensor expression with a sparse output tensor that changes its values
// but not its nonzero structure, an operation called "simply dynamic" in
// [Bik96,Ch9], is also admissible without special env.
if (env.merger().isSingleCondition(tensor, exp))
return true;
// Accept "truly dynamic" if the output tensor materializes uninitialized
// into the computation and insertions occur in lexicographic index order.
if (isMaterializing(lhs->get())) {
auto iteratorTypes = env.op().getIteratorTypesArray();
unsigned nest = 0;
for (unsigned i = 0; i < numLoops; i++) {
if (!env.merger().isFilterLoop(env.topSortAt(i))) {
// We only count non-filter loops as filter loops should be considered
// as a special type of parallel loops.
if (linalg::isReductionIterator(iteratorTypes[env.topSortAt(i)]))
break; // terminate at first reduction
nest++;
}
}
// Determine admissible dynamic insertion situations:
// (1) fully injective, since there are no reductions,
// (2) admissible 1-d expansion in innermost dimension.
if (nest >= env.op().getRank(lhs) - 1) {
*sparseOut = lhs;
*outerParNest = nest;
return true;
}
}
return false;
}
//===----------------------------------------------------------------------===//
// Sparse compiler synthesis methods (statements and expressions).
//===----------------------------------------------------------------------===//
/// Local bufferization of all dense and sparse data structures.
static void genBuffers(CodegenEnv &env, OpBuilder &builder) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
assert(op.getNumOperands() == op.getNumDpsInputs() + 1);
env.emitter().initializeLoopEmit(
builder, loc,
/// Generates buffer for the output tensor.
/// Note that all sparse kernels assume that when all elements are written
/// to (viz. x(i) = y(i) * z(i)), the output buffer is already initialized
/// to all zeroes and only nonzeroes values are computed and written out.
/// For updates (viz. x(i) += y(i) * z(i)), only nonzeroes values are used
/// for the updates and no assumption on the original contents of the
/// output buffer is necessary.
[&op](OpBuilder &builder, Location loc, Value memref,
Value tensor) -> Value {
// Must not be a sparse tensor.
assert(!getSparseTensorEncoding(tensor.getType()));
// Two output tensor references should point to the same object.
OpOperand *lhs = op.getDpsInitOperand(0);
assert(lhs->get() == tensor);
// An output tensor can simply materialize from the buffer of the tensor
// that appears in the outs() clause. For updates, this has the
// advantage that only the nonzero value are involved in the
// computation, keeping the operation O(nnz). In all other cases, we are
// forced to zero out the buffer to enforce the assumption above, which
// may negatively impact running complexity (viz. O(n^2 + nnz) vs.
// O(nnz) for matrices).
// TODO: use better analysis to avoid zeroing out the buffer?
bool isInit = op.isInitTensor(lhs);
Value init = memref;
if (!isInit) {
Value zero = constantZero(builder, loc,
getElementTypeOrSelf(tensor.getType()));
builder.create<linalg::FillOp>(loc, ValueRange{zero},
ValueRange{init});
}
return init;
});
}
/// Generates index for load/store on sparse tensor.
static Value genIndex(CodegenEnv &env, OpOperand *t) {
auto map = env.op().getMatchingIndexingMap(t);
auto enc = getSparseTensorEncoding(t->get().getType());
AffineExpr a = map.getResult(toOrigDim(enc, map.getNumResults() - 1));
assert(a.getKind() == AffineExprKind::DimId);
unsigned idx = a.cast<AffineDimExpr>().getPosition();
return env.getLoopIdxValue(idx);
}
/// Generates subscript for load/store on a dense or sparse tensor.
static Value genSubscript(CodegenEnv &env, OpBuilder &builder, OpOperand *t,
SmallVectorImpl<Value> &args) {
linalg::GenericOp op = env.op();
unsigned tensor = t->getOperandNumber();
auto map = op.getMatchingIndexingMap(t);
auto enc = getSparseTensorEncoding(t->get().getType());
unsigned rank = map.getNumResults();
if (enc) {
Value pidx = env.emitter().getPidxs()[tensor].back();
assert(pidx);
args.push_back(pidx); // position index
} else {
for (unsigned d = 0; d < rank; d++) {
AffineExpr a = map.getResult(d);
args.push_back(env.emitter().genAffine(builder, a, op.getLoc()));
}
}
return env.emitter().getValBuffer()[tensor];
}
/// Generates insertion code to implement dynamic tensor load.
static Value genInsertionLoad(CodegenEnv &env, OpBuilder &builder,
OpOperand *t) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
// Direct lexicographic index order, tensor loads as zero.
if (!env.isExpand()) {
Type tp = getElementTypeOrSelf(t->get().getType());
return constantZero(builder, loc, tp);
}
// Load from expanded access pattern.
Value index = genIndex(env, t);
return builder.create<memref::LoadOp>(loc, env.getExpandValues(), index);
}
/// Generates insertion code to implement dynamic tensor load for reduction.
static Value genInsertionLoadReduce(CodegenEnv &env, OpBuilder &builder,
OpOperand *t) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
Value identity = env.getCustomRedId();
// Direct lexicographic index order, tensor loads as identity.
if (!env.isExpand())
return identity;
// Load from expanded access pattern if filled, identity otherwise.
Value values = env.getExpandValues();
Value filled = env.getExpandFilled();
Value index = genIndex(env, t);
Value isFilled = builder.create<memref::LoadOp>(loc, filled, index);
Value valAtIndex = builder.create<memref::LoadOp>(loc, values, index);
return builder.create<arith::SelectOp>(loc, isFilled, valAtIndex, identity);
}
/// Generates insertion code to implement dynamic tensor store.
static void genInsertionStore(CodegenEnv &env, OpBuilder &builder, OpOperand *t,
Value rhs) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
// Direct insertion in lexicographic index order.
if (!env.isExpand()) {
unsigned rank = op.getRank(t);
SmallVector<Value> indices;
for (unsigned i = 0; i < rank; i++) {
assert(env.emitter().getLoopIV(i));
indices.push_back(env.emitter().getLoopIV(i));
}
Value chain = env.getInsertionChain();
env.updateInsertionChain(
builder.create<InsertOp>(loc, rhs, chain, indices));
return;
}
// Generates insertion code along expanded access pattern.
// if (!expFilled[i]) then
// expFilled[i] = true
// expAdded[inserts++] = i
// endif
// values[i] = rhs
Value values = env.getExpandValues();
Value filled = env.getExpandFilled();
Value added = env.getExpandAdded();
Value count = env.getExpandCount();
Value index = genIndex(env, t);
Value fval = constantI1(builder, loc, false);
Value tval = constantI1(builder, loc, true);
// If statement.
Value isFilled = builder.create<memref::LoadOp>(loc, filled, index);
Value cond = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq,
isFilled, fval);
scf::IfOp ifOp = builder.create<scf::IfOp>(loc, builder.getIndexType(), cond,
/*else=*/true);
// True branch.
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
builder.create<memref::StoreOp>(loc, tval, filled, index);
builder.create<memref::StoreOp>(loc, index, added, count);
Value one = constantIndex(builder, loc, 1);
Value add = builder.create<arith::AddIOp>(loc, count, one);
builder.create<scf::YieldOp>(loc, add);
// False branch.
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
builder.create<scf::YieldOp>(loc, count);
builder.setInsertionPointAfter(ifOp);
// Value assignment.
env.updateExpandCount(ifOp.getResult(0));
builder.create<memref::StoreOp>(loc, rhs, values, index);
}
/// Generates a load on a dense or sparse tensor.
static Value genTensorLoad(CodegenEnv &env, OpBuilder &builder, unsigned exp) {
// Test if the load was hoisted to a higher loop nest.
Value val = env.exp(exp).val;
if (val)
return val;
// Load during insertion.
linalg::GenericOp op = env.op();
OpOperand *t = &op->getOpOperand(env.exp(exp).tensor);
if (env.isSparseOutput(t)) {
if (env.isCustomReduc())
return genInsertionLoadReduce(env, builder, t);
return genInsertionLoad(env, builder, t);
}
// Actual load.
SmallVector<Value> args;
Value ptr = genSubscript(env, builder, t, args);
return builder.create<memref::LoadOp>(op.getLoc(), ptr, args);
}
/// Generates a store on a dense or sparse tensor.
static void genTensorStore(CodegenEnv &env, OpBuilder &builder, unsigned exp,
Value rhs) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
// Test if this is a scalarized reduction.
if (env.isReduc()) {
env.updateReduc(rhs);
return;
}
// Store during insertion.
OpOperand *t = op.getDpsInitOperand(0);
if (env.isSparseOutput(t)) {
if (!rhs) {
// Only unary and binary are allowed to return uninitialized rhs
// to indicate missing output.
assert(env.exp(exp).kind == kUnary || env.exp(exp).kind == kBinary);
} else if (env.exp(exp).kind == kSelect) {
// Select operation insertion.
Value chain = env.getInsertionChain();
scf::IfOp ifOp =
builder.create<scf::IfOp>(loc, chain.getType(), rhs, /*else=*/true);
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
// Existing value was preserved to be used here.
assert(env.exp(exp).val);
Value v0 = env.exp(exp).val;
genInsertionStore(env, builder, t, v0);
env.exp(exp).val = Value();
// Yield modified insertion chain along true branch.
Value mchain = env.getInsertionChain();
builder.create<scf::YieldOp>(op.getLoc(), mchain);
// Yield original insertion chain along false branch.
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
builder.create<scf::YieldOp>(loc, chain);
// Done with if statement.
env.updateInsertionChain(ifOp->getResult(0));
builder.setInsertionPointAfter(ifOp);
} else {
genInsertionStore(env, builder, t, rhs);
}
return;
}
// Actual store.
SmallVector<Value> args;
Value ptr = genSubscript(env, builder, t, args);
builder.create<memref::StoreOp>(loc, rhs, ptr, args);
}
/// Generates an invariant value.
inline static Value genInvariantValue(CodegenEnv &env, unsigned exp) {
return env.exp(exp).val;
}
/// Generates an index value.
inline static Value genIndexValue(CodegenEnv &env, unsigned idx) {
return env.getLoopIdxValue(idx);
}
/// Semi-ring branches are simply inlined by the sparse compiler. Prior
/// analysis has verified that all computations are "local" to the inlined
/// branch or otherwise invariantly defined outside the loop nest, with the
/// exception of index computations, which need to be relinked to actual
/// inlined cloned code.
static Value relinkBranch(CodegenEnv &env, RewriterBase &rewriter, Block *block,
Value e, unsigned ldx) {
if (Operation *def = e.getDefiningOp()) {
if (auto indexOp = dyn_cast<linalg::IndexOp>(def))
return genIndexValue(env, indexOp.getDim());
if (def->getBlock() == block) {
for (unsigned i = 0, n = def->getNumOperands(); i < n; i++)
def->setOperand(
i, relinkBranch(env, rewriter, block, def->getOperand(i), ldx));
}
}
return e;
}
/// Recursively generates tensor expression.
static Value genExp(CodegenEnv &env, RewriterBase &rewriter, unsigned exp,
unsigned ldx) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
if (exp == -1u)
return Value();
if (env.exp(exp).kind == Kind::kTensor)
return genTensorLoad(env, rewriter, exp);
if (env.exp(exp).kind == Kind::kInvariant)
return genInvariantValue(env, exp);
if (env.exp(exp).kind == Kind::kIndex)
return genIndexValue(env, env.exp(exp).index);
if (env.exp(exp).kind == Kind::kReduce)
env.startCustomReduc(exp); // enter custom
Value v0 = genExp(env, rewriter, env.exp(exp).children.e0, ldx);
Value v1 = genExp(env, rewriter, env.exp(exp).children.e1, ldx);
Value ee = env.merger().buildExp(rewriter, loc, exp, v0, v1);
if (ee && (env.exp(exp).kind == Kind::kUnary ||
env.exp(exp).kind == Kind::kBinary ||
env.exp(exp).kind == Kind::kBinaryBranch ||
env.exp(exp).kind == Kind::kReduce ||
env.exp(exp).kind == Kind::kSelect))
ee = relinkBranch(env, rewriter, ee.getParentBlock(), ee, ldx);
if (env.exp(exp).kind == Kind::kReduce)
env.endCustomReduc(); // exit custom
if (env.exp(exp).kind == kSelect) {
assert(!env.exp(exp).val);
env.exp(exp).val = v0; // Preserve value for later use.
}
return ee;
}
/// Hoists loop invariant tensor loads for which indices have been exhausted.
static void genInvariants(CodegenEnv &env, OpBuilder &builder, unsigned exp,
unsigned ldx, bool atStart) {
if (exp == -1u)
return;
if (env.exp(exp).kind == Kind::kTensor) {
// Inspect tensor indices.
bool atLevel = ldx == -1u;
linalg::GenericOp op = env.op();
OpOperand &t = op->getOpOperand(env.exp(exp).tensor);
auto map = op.getMatchingIndexingMap(&t);
auto enc = getSparseTensorEncoding(t.get().getType());
for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) {
AffineExpr a = map.getResult(toOrigDim(enc, d));
std::optional<unsigned> sldx =
env.merger().getLoopIdx(t.getOperandNumber(), d);
if (sldx && env.merger().isFilterLoop(*sldx)) {
if (!env.getLoopIdxValue(*sldx))
// The filter loops has not been constructed.
return;
if (*sldx == ldx)
atLevel = true;
} else if (!isInvariantAffine(env, a, ldx, atLevel))
return; // still in play
}
// All exhausted at this level (atLevel denotes exactly at this level).
if (!atLevel)
return;
OpOperand *lhs = op.getDpsInitOperand(0);
if (lhs == &t) {
// Start or end a scalarized reduction
if (atStart) {
Value load = env.isCustomReduc() ? env.getCustomRedId()
: genTensorLoad(env, builder, exp);
env.startReduc(exp, load);
} else {
genTensorStore(env, builder, exp, env.endReduc());
}
} else {
// Start or end loop invariant hoisting of a tensor load.
env.exp(exp).val = atStart ? genTensorLoad(env, builder, exp) : Value();
}
} else if (env.exp(exp).kind != Kind::kInvariant &&
env.exp(exp).kind != Kind::kIndex) {
// Traverse into the binary operations. Note that we only hoist
// tensor loads, since subsequent MLIR/LLVM passes know how to
// deal with all other kinds of derived loop invariants.
if (env.exp(exp).kind == Kind::kReduce)
env.startCustomReduc(exp); // enter custom
unsigned e0 = env.exp(exp).children.e0;
unsigned e1 = env.exp(exp).children.e1;
genInvariants(env, builder, e0, ldx, atStart);
genInvariants(env, builder, e1, ldx, atStart);
if (env.exp(exp).kind == Kind::kReduce)
env.endCustomReduc(); // exit custom
}
}
/// Generates an expanded access pattern in innermost dimension.
static void genExpand(CodegenEnv &env, OpBuilder &builder, unsigned at,
bool atStart) {
linalg::GenericOp op = env.op();
OpOperand *lhs = op.getDpsInitOperand(0);
if (!env.atExpandLevel(lhs, op.getRank(lhs), at))
return; // not needed at this level
assert(!env.isReduc());
// Generate start or end of an expanded access pattern. Note that because
// an expension does not rely on the ongoing contents of the sparse storage
// scheme, we can use the original tensor as incoming SSA value (which
// simplifies codegen a bit). If expansion on the actual contents is ever
// needed, we will need to use the SSA value in the insertion chain instead.
Value tensor = lhs->get();
Location loc = op.getLoc();
if (atStart) {
auto dynShape = {ShapedType::kDynamic};
Type etp = tensor.getType().cast<ShapedType>().getElementType();
Type t1 = MemRefType::get(dynShape, etp);
Type t2 = MemRefType::get(dynShape, builder.getI1Type());
Type t3 = MemRefType::get(dynShape, builder.getIndexType());
Type t4 = builder.getIndexType();
auto r = builder.create<ExpandOp>(loc, TypeRange({t1, t2, t3, t4}), tensor);
assert(r.getNumResults() == 4);
env.startExpand(r.getResult(0), r.getResult(1), r.getResult(2),
r.getResult(3));
} else {
SmallVector<Value> indices;
for (unsigned i = 0; i < at; i++)
indices.push_back(env.emitter().getLoopIV(i));
Value values = env.getExpandValues();
Value filled = env.getExpandFilled();
Value added = env.getExpandAdded();
Value count = env.getExpandCount();
Value chain = env.getInsertionChain();
Value compress = builder.create<CompressOp>(loc, values, filled, added,
count, chain, indices);
env.updateInsertionChain(compress);
env.endExpand();
}
}
/// Returns parallelization strategy. Any implicit loop in the Linalg
/// operation that is marked "parallel" is a candidate. Whether it is actually
/// converted to a parallel operation depends on the requested strategy.
static bool isParallelFor(CodegenEnv &env, bool isOuter, bool isSparse) {
// Reject parallelization of sparse output.
if (env.hasSparseOutput())
return false;
// Parallel loops on tensor expansion can cause data races.
if (env.isExpand())
return false;
// Inspect strategy.
switch (env.options().parallelizationStrategy) {
case SparseParallelizationStrategy::kNone:
return false;
case SparseParallelizationStrategy::kDenseOuterLoop:
return isOuter && !isSparse;
case SparseParallelizationStrategy::kAnyStorageOuterLoop:
return isOuter;
case SparseParallelizationStrategy::kDenseAnyLoop:
return !isSparse;
case SparseParallelizationStrategy::kAnyStorageAnyLoop:
return true;
}
llvm_unreachable("unexpected parallelization strategy");
}
/// Generates a for-loop on a single index.
static Operation *genFor(CodegenEnv &env, OpBuilder &builder, bool isOuter,
bool isInner, unsigned idx, ArrayRef<size_t> tids,
ArrayRef<size_t> dims) {
linalg::GenericOp op = env.op();
Location loc = op.getLoc();
auto iteratorTypes = op.getIteratorTypesArray();
bool isSparse = llvm::any_of(tids, [idx, &env](size_t tid) {
return isCompressedDLT(env.dlt(tid, idx)) ||
isSingletonDLT(env.dlt(tid, idx));
});
bool isParallel = isParallelFor(env, isOuter, isSparse);
Operation *loop = *env.genLoopBoundary([&](MutableArrayRef<Value> reduc) {
if (env.merger().isFilterLoop(idx)) {
size_t tid = tids.front(), dim = dims.front();
// tids/dims must only have one value because filter loops only
// corresponding to the one and only sparse tensor level.
assert(isSparse && tids.size() == 1 && dims.size() == 1);
OpOperand *t = &op->getOpOperand(tid);
auto enc = getSparseTensorEncoding(t->get().getType());
// Retrieves the affine expression for the filter loop.
AffineExpr a =
op.getMatchingIndexingMap(t).getResult(toOrigDim(enc, dim));
return env.emitter().enterFilterLoopOverTensorAtDim(builder, loc, tid,
dim, a, reduc);
}
return env.emitter().enterLoopOverTensorAtDim(builder, loc, tids, dims,
reduc, isParallel);
});
assert(loop);
return loop;
}
/// Emit a while-loop for co-iteration over multiple indices.
static Operation *genWhile(CodegenEnv &env, OpBuilder &builder, unsigned idx,
bool needsUniv, ArrayRef<size_t> tids,
ArrayRef<size_t> dims) {
Operation *loop = *env.genLoopBoundary([&](MutableArrayRef<Value> reduc) {
// Construct the while-loop with a parameter for each
// index.
return env.emitter().enterCoIterationOverTensorsAtDims(
builder, env.op().getLoc(), tids, dims, needsUniv, reduc);
});
assert(loop);
return loop;
}
/// Generates a for-loop or a while-loop, depending on whether it implements
/// singleton iteration or co-iteration over the given conjunction.
static Operation *genLoop(CodegenEnv &env, OpBuilder &builder, unsigned at,
bool needsUniv, ArrayRef<size_t> tids,
ArrayRef<size_t> dims, bool isFor) {
assert(tids.size() == dims.size());
unsigned idx = env.topSortAt(at);
if (isFor) {
bool isOuter = at == 0;
bool isInner = at == env.topSortSize() - 1;
return genFor(env, builder, isOuter, isInner, idx, tids, dims);
}
return genWhile(env, builder, idx, needsUniv, tids, dims);
}
/// Generates the induction structure for a while-loop.
static void finalizeWhileOp(CodegenEnv &env, OpBuilder &builder, unsigned idx,
bool needsUniv, BitVector &induction,
scf::WhileOp whileOp) {
Location loc = env.op().getLoc();
// Finalize each else branch of all if statements.
if (env.isReduc() || env.isExpand() || env.getInsertionChain()) {
while (auto ifOp = dyn_cast_or_null<scf::IfOp>(
builder.getInsertionBlock()->getParentOp())) {
unsigned y = 0;
SmallVector<Value> yields;
if (env.isReduc()) {
yields.push_back(env.getReduc());
env.updateReduc(ifOp.getResult(y++));
}
if (env.isExpand()) {
yields.push_back(env.getExpandCount());
env.updateExpandCount(ifOp->getResult(y++));
}
if (env.getInsertionChain()) {
yields.push_back(env.getInsertionChain());
env.updateInsertionChain(ifOp->getResult(y++));
}
assert(y == yields.size());
builder.create<scf::YieldOp>(loc, yields);
builder.setInsertionPointAfter(ifOp);
}
}
builder.setInsertionPointToEnd(&whileOp.getAfter().front());
}
/// Generates a single if-statement within a while-loop.
static scf::IfOp genIf(CodegenEnv &env, OpBuilder &builder, unsigned idx,
BitVector &conditions) {
Location loc = env.op().getLoc();
SmallVector<Type> types;
Value cond;
for (unsigned b = 0, be = conditions.size(); b < be; b++) {
if (!conditions[b])
continue;
unsigned tensor = env.merger().tensor(b);
assert(idx == env.merger().index(b));
Value clause;
if (isCompressedDLT(env.dlt(b)) || isSingletonDLT(env.dlt(b))) {
auto dim = *env.merger().getDimNum(tensor, idx);
Value op1 = env.emitter().getCoord()[tensor][dim];
Value op2 = env.getLoopIdxValue(idx);
clause = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq, op1,
op2);
} else {
assert(isDenseDLT(env.merger().getDimLevelType(b)) ||
isUndefDLT(env.merger().getDimLevelType(b)));
clause = constantI1(builder, loc, true);
}
cond = cond ? builder.create<arith::AndIOp>(loc, cond, clause) : clause;
}
if (env.isReduc())
types.push_back(env.getReduc().getType());
if (env.isExpand())
types.push_back(builder.getIndexType());
if (env.getInsertionChain())
types.push_back(env.getInsertionChain().getType());
scf::IfOp ifOp = builder.create<scf::IfOp>(loc, types, cond, /*else=*/true);
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
return ifOp;
}
/// Generates end of true branch of if-statement within a while-loop.
static void endIf(CodegenEnv &env, OpBuilder &builder, scf::IfOp ifOp,
Operation *loop, Value redInput, Value cntInput,
Value insInput) {
SmallVector<Value> operands;
if (env.isReduc()) {
operands.push_back(env.getReduc());
env.updateReduc(redInput);
}
if (env.isExpand()) {
operands.push_back(env.getExpandCount());
env.updateExpandCount(cntInput);
}
if (env.getInsertionChain()) {
operands.push_back(env.getInsertionChain());
env.updateInsertionChain(insInput);
}
if (!operands.empty())
builder.create<scf::YieldOp>(env.op().getLoc(), operands);
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
}
//===----------------------------------------------------------------------===//
// Sparse compiler synthesis methods (loop sequence).
//===----------------------------------------------------------------------===//
/// Starts a loop sequence at given level. Returns true if
/// the universal loop index must be maintained at this level.
static bool startLoopSeq(CodegenEnv &env, OpBuilder &builder, unsigned exp,
unsigned at, unsigned idx, unsigned ldx,
unsigned lts) {
assert(!env.getLoopIdxValue(idx));
// Emit invariants at this loop sequence level.
genInvariants(env, builder, exp, ldx, /*atStart=*/true);
// Emit access pattern expansion for sparse tensor output.
genExpand(env, builder, at, /*atStart=*/true);
// Emit further intitialization at this loop sequence level.
unsigned l0 = env.set(lts)[0];
bool needsUniv = false;
SmallVector<size_t> tids;
SmallVector<size_t> dims;
env.merger().foreachTidDimPairInBits(
env.lat(l0).bits, [&](unsigned b, unsigned tid,
std::optional<unsigned> dim, DimLevelType dlt) {
assert(env.merger().index(b) == idx);
if (isDenseDLT(dlt) || isUndefDLT(dlt)) {
needsUniv = true;
} else {
// sparse/singleton dim levels.
tids.push_back(tid);
dims.push_back(*dim);
}
});
env.emitter().enterNewLoopSeq(builder, env.op().getLoc(), tids, dims);
// Maintain the universal index only if it is actually
// consumed by a subsequent lattice point.
if (needsUniv) {
unsigned lsize = env.set(lts).size();
for (unsigned i = 1; i < lsize; i++) {
unsigned li = env.set(lts)[i];
if (!env.merger().hasAnySparse(env.lat(li).simple))
return true;
}
}
return false;
}
static void genConstantDenseAddressFromLevel(CodegenEnv &env,
OpBuilder &builder, unsigned tid,
unsigned lvl) {
// TODO: Handle affine expression on output tensor.
linalg::GenericOp op = env.op();
assert(tid < op.getNumDpsInputs());
OpOperand *input = op.getDpsInputOperands()[tid];
ArrayRef<AffineExpr> affines = op.getMatchingIndexingMap(input).getResults();
auto enc = getSparseTensorEncoding(input->get().getType());
if (enc) {
for (unsigned i = lvl, e = affines.size(); i < e; i++) {
AffineExpr affine = affines[toOrigDim(enc, i)];
if (isDenseDLT(getDimLevelType(enc, i)) &&
affine.isa<AffineConstantExpr>())
env.emitter().genDenseAffineAddressAtCurLevel(
builder, op.getLoc(), input->getOperandNumber(), i, affine);
else
return; // break on first non-dense non-constant level
}
}
}
static void genInitConstantDenseAddress(CodegenEnv &env,
RewriterBase &rewriter) {
// We can generate address for constant affine expression before any loops
// starting from the first level as they do not depend on any thing.
// E.g., [Dense, Dense, Sparse] -> (1, 2, d0), the addresses for the first two
// levels can be determined before loops.
for (unsigned tid = 0, e = env.op().getNumDpsInputs(); tid < e; tid++)
genConstantDenseAddressFromLevel(env, rewriter, tid, 0);
}
/// Return true if the lattices bit can be iterated by a for loop.
static bool translateBitsToTidDimPairs(
CodegenEnv &env, unsigned li, unsigned idx, SmallVectorImpl<size_t> &tids,
SmallVectorImpl<size_t> &dims, SmallVectorImpl<size_t> &affineTids,
SmallVectorImpl<size_t> &affineDims, SmallVectorImpl<AffineExpr> &exps) {
const BitVector &all = env.lat(li).bits;
const BitVector &simple = env.lat(li).simple;
unsigned numloopCond = 0;
// Converts bits to array + dim pair
env.merger().foreachTidDimPairInBits(all, [&,
idx](unsigned b, unsigned tid,
std::optional<unsigned> dim,
DimLevelType dlt) {
if (simple.test(b)) {
if (isUndefDLT(dlt)) {
// An undefined dlt in the lattices, we probably mean to iterate based
// on the dim of output tensor.
// E.g., this could be a synthetic tensor (for invariants and sparse
// output tensor).
// out[i][j] = invariant; or a broadcast
// out[i][j] = in[i] (j is undef for input)
tid = env.merger().getOutTensorID();
dim = env.merger().getDimNum(tid, idx);
// Skips invalid dim (e.g., when this is a zero ranked tensor).
if (!dim)
return;
}
tids.push_back(tid);
dims.push_back(*dim);
numloopCond++;
} else if (isDenseDLT(dlt)) {
tids.push_back(tid);
dims.push_back(*dim);
} else {
assert(isUndefDLT(dlt));
linalg::GenericOp op = env.op();
if (tid >= op.getNumDpsInputs())
// We only handle affine expression on input tensors (for now).
return;
OpOperand *operand = &op->getOpOperand(tid);
auto enc = getSparseTensorEncoding(operand->get().getType());
// Non-annotated dense tensors requires no special handling.
if (!enc)
return;
ArrayRef<AffineExpr> affines =
op.getMatchingIndexingMap(operand).getResults();
assert(affines.size() == enc.getDimLevelType().size());
for (unsigned i = 0, e = affines.size(); i < e; i++) {
AffineExpr exp = affines[toOrigDim(enc, i)];
// Skip simple affine expression and non dense dimensions (which has
// it own filter loop).
if (exp.isa<AffineDimExpr>() || !isDenseDLT(getDimLevelType(enc, i)))
continue;
// Constant affine expression are handled in genLoop
if (!exp.isa<AffineConstantExpr>()) {
bool atLevel = false;
if (isInvariantAffine(env, exp, idx, atLevel) && atLevel) {
// If the compound affine is invariant and we are right at the
// level. We need to generate the address according to the affine
// expression. This is also the best place we can do it to avoid
// putting it inside inner loops.
// NOTE: It assumes that the levels of the input tensor are
// initialized in order (and it is also currently guaranteed by
// computeIterationGraph), another more admissible approach might be
// accepting out-of-order access between consecutive dense levels.
affineTids.push_back(tid);
affineDims.push_back(i);
exps.push_back(exp);
}
}
}
}
});
if (isDenseDLT(env.dlt(env.merger().getOutTensorID(), idx))) {
// Note that we generate dense indices of the output tensor
// unconditionally, since they may not appear in the lattice, but may be
// needed for linearized env.
auto dim = *env.merger().getDimNum(env.merger().getOutTensorID(), idx);
tids.push_back(env.merger().getOutTensorID());
dims.push_back(dim);
}
assert(numloopCond > 0);
// If we just need to one loop conditions, the loop can be generated by a for
// loop.
return numloopCond == 1;
}
/// Starts a single loop in current sequence.
static Operation *startLoop(CodegenEnv &env, OpBuilder &builder, unsigned at,
unsigned li, bool needsUniv) {
// The set of tensors + dims to generate loops on
SmallVector<size_t> tids, dims;
// The set of dense tensors with non-trivial affine expression that just
// becomes invariant and the address shall now be generated at the current
// level.
SmallVector<size_t> affineTids, affineDims;
SmallVector<AffineExpr> affines;
bool isFor = translateBitsToTidDimPairs(
env, li, env.topSortAt(at), tids, dims, affineTids, affineDims, affines);
// Emit the for/while-loop control.
Operation *loop = genLoop(env, builder, at, needsUniv, tids, dims, isFor);
for (auto [tid, dim, exp] : llvm::zip(affineTids, affineDims, affines)) {
env.emitter().genDenseAffineAddressAtCurLevel(builder, env.op().getLoc(),
tid, dim, exp);
}
// Until now, we have entered every <tid, dim> pair in {cond, extra,
// affine}Tids/Dims. The addresses of the upcoming levels which are dependent
// on constant affines expression may now be determined.
auto allTids = llvm::concat<size_t>(tids, affineTids);
auto allDims = llvm::concat<size_t>(dims, affineDims);
for (auto [tid, dim] : llvm::zip(allTids, allDims)) {
if (tid != env.merger().getOutTensorID())
genConstantDenseAddressFromLevel(env, builder, tid, dim + 1);
}
return loop;
}
/// Ends a single loop in current sequence. Returns new values for needsUniv.
static bool endLoop(CodegenEnv &env, RewriterBase &rewriter, Operation *loop,
unsigned idx, unsigned li, bool needsUniv) {
// End a while-loop.
if (auto whileOp = dyn_cast<scf::WhileOp>(loop)) {
finalizeWhileOp(env, rewriter, idx, needsUniv, env.lat(li).bits, whileOp);
} else {
needsUniv = false;
}
env.genLoopBoundary([&](MutableArrayRef<Value> reduc) {
env.emitter().exitCurrentLoop(rewriter, env.op().getLoc(), reduc);
return std::nullopt;
});
return needsUniv;
}
/// Ends a loop sequence at given level.
static void endLoopSeq(CodegenEnv &env, OpBuilder &builder, unsigned exp,
unsigned at, unsigned idx, unsigned ldx) {
assert(env.getLoopIdxValue(idx) == nullptr);
env.emitter().exitCurrentLoopSeq();
// Unmark bookkeeping of invariants and loop index.
genInvariants(env, builder, exp, ldx, /*atStart=*/false);
// Finalize access pattern expansion for sparse tensor output.
genExpand(env, builder, at, /*atStart=*/false);
}
/// Recursively generates code while computing iteration lattices in order
/// to manage the complexity of implementing co-iteration over unions
/// and intersections of sparse iterations spaces.
static void genStmt(CodegenEnv &env, RewriterBase &rewriter, unsigned exp,
unsigned at) {
// At each leaf, assign remaining tensor (sub)expression to output tensor.
if (at == env.topSortSize()) {
unsigned ldx = env.topSortAt(at - 1);
Value rhs = genExp(env, rewriter, exp, ldx);
genTensorStore(env, rewriter, exp, rhs);
return;
}
// Construct iteration lattices for current loop index, with L0 at top.
unsigned idx = env.topSortAt(at);
unsigned ldx = at == 0 ? -1u : env.topSortAt(at - 1);
unsigned lts = env.merger().optimizeSet(env.merger().buildLattices(exp, idx));
// TODO: sort
// TODO: dedup
// Start a loop sequence.
bool needsUniv = startLoopSeq(env, rewriter, exp, at, idx, ldx, lts);
// Emit a loop for every lattice point L0 >= Li in this loop sequence.
unsigned lsize = env.set(lts).size();
for (unsigned i = 0; i < lsize; i++) {
// Start a loop.
unsigned li = env.set(lts)[i];
Operation *loop = startLoop(env, rewriter, at, li, needsUniv);
// Visit all lattices points with Li >= Lj to generate the
// loop-body, possibly with if statements for coiteration.
Value redInput = env.getReduc();
Value cntInput = env.getExpandCount();
Value insInput = env.getInsertionChain();
bool isWhile = dyn_cast<scf::WhileOp>(loop) != nullptr;
for (unsigned j = 0; j < lsize; j++) {
unsigned lj = env.set(lts)[j];
unsigned ej = env.lat(lj).exp;
if (li == lj || env.merger().latGT(li, lj)) {
// Recurse into body of each branch.
if (isWhile) {
scf::IfOp ifOp = genIf(env, rewriter, idx, env.lat(lj).simple);
genStmt(env, rewriter, ej, at + 1);
endIf(env, rewriter, ifOp, loop, redInput, cntInput, insInput);
} else {
genStmt(env, rewriter, ej, at + 1);
}
}
}
// End a loop.
needsUniv = endLoop(env, rewriter, loop, idx, li, needsUniv);
}
// End a loop sequence.
endLoopSeq(env, rewriter, exp, at, idx, ldx);
}
/// Converts the result computed by the sparse kernel into the required form.
static void genResult(CodegenEnv &env, RewriterBase &rewriter) {
linalg::GenericOp op = env.op();
OpOperand *lhs = op.getDpsInitOperand(0);
Value tensor = lhs->get();
Type resType = tensor.getType();
if (getSparseTensorEncoding(resType)) {
// The sparse tensor rematerializes from the original sparse tensor's
// underlying sparse storage format. For an insertion chain, the
// tensor materializes from the chain with 'hasInserts' enabled.
bool hasInserts = false;
if (Value chain = env.getInsertionChain()) {
hasInserts = true;
tensor = chain;
}
rewriter.replaceOpWithNewOp<LoadOp>(op, resType, tensor, hasInserts);
} else {
// To rematerialize an non-annotated tensor, simply load it
// from the bufferized value.
Value val = env.emitter().getValBuffer().back(); // value array
rewriter.replaceOpWithNewOp<bufferization::ToTensorOp>(op, resType, val);
}
}
//===----------------------------------------------------------------------===//
// Sparse compiler rewriting methods.
//===----------------------------------------------------------------------===//
namespace {
/// Sparse rewriting rule for generic Lingalg operation.
struct GenericOpSparsifier : public OpRewritePattern<linalg::GenericOp> {
public:
GenericOpSparsifier(MLIRContext *context, SparsificationOptions o)
: OpRewritePattern<linalg::GenericOp>(context), options(o) {}
LogicalResult matchAndRewrite(linalg::GenericOp op,
PatternRewriter &rewriter) const override {
// Only accept single output operations.
if (op.getNumDpsInits() != 1)
return failure();
// Sets up a code generation environment.
unsigned numTensors = op->getNumOperands();
unsigned numLoops = op.getNumLoops();
unsigned numFilterLoops = getNumCompoundAffineOnSparseDims(op);
CodegenEnv env(op, options, numTensors, numLoops, numFilterLoops);
// Detects sparse annotations and translates the per-dimension sparsity
// information for all tensors to loop indices in the kernel.
if (!findSparseAnnotations(env))
return failure();
// Builds the tensor expression for the Linalg operation in SSA form.
std::optional<unsigned> optExp = env.merger().buildTensorExpFromLinalg(op);
if (!optExp)
return failure();
unsigned exp = *optExp;
// Computes a topologically sorted iteration graph to ensure tensors
// are visited in natural index order. Gradually relaxes the considered
// constraints until an acyclic iteration graph results, such that sparse
// code generation can proceed. As a last resort, an attempt is made
// to resolve cycles by inserting a conversion.
bool isAdmissible = false;
bool hasCycle = true;
OpOperand *sparseOut = nullptr;
unsigned outerParNest = -1u;
// An const list of all masks that we used for interation graph
// computation. Must be ordered from more strict to less strict.
const auto allMask = {SortMask::kIncludeAll, SortMask::kIncludeUndef,
SortMask::kIncludeDense, SortMask::kSparseOnly};
for (auto mask : allMask)
if (computeIterationGraph(env, mask)) {
hasCycle = false;
if (isAdmissibleTensorExp(env, exp, &sparseOut, &outerParNest)) {
isAdmissible = true;
break;
}
// else try a set of less strict constraints.
}
if (hasCycle)
return resolveCycle(env, rewriter); // one last shot
if (!isAdmissible)
return failure(); // inadmissible expression, reject
// Recursively generates code if admissible.
env.startEmit(sparseOut, outerParNest);
genBuffers(env, rewriter);
genInitConstantDenseAddress(env, rewriter);
genStmt(env, rewriter, exp, 0);
genResult(env, rewriter);
return success();
}
private:
// Last resort cycle resolution.
LogicalResult resolveCycle(CodegenEnv &env, PatternRewriter &rewriter) const {
// Compute topological sort while leaving out every
// sparse input tensor in succession until an acylic
// iteration graph results.
for (OpOperand *t : env.op().getDpsInputOperands()) {
unsigned tensor = t->getOperandNumber();
Value tval = t->get();
auto srcEnc = getSparseTensorEncoding(tval.getType());
if (!srcEnc || !computeIterationGraph(env, SortMask::kSparseOnly, t))
continue;
// Found an input tensor that resolves the cycle by inserting a
// conversion into a sparse tensor that adheres to the iteration
// graph order. Also releases the temporary sparse tensor.
//
// TODO: investigate fusing the conversion with computation,
// especially if it is a direct yield!
//
auto srcTp = getRankedTensorType(tval);
auto dstEnc = SparseTensorEncodingAttr::get(
getContext(), srcEnc.getDimLevelType(),
permute(env, env.op().getMatchingIndexingMap(t)), // new order
srcEnc.getHigherOrdering(), srcEnc.getPointerBitWidth(),
srcEnc.getIndexBitWidth());
auto dstTp = RankedTensorType::get(srcTp.getShape(),
srcTp.getElementType(), dstEnc);
auto convert = rewriter.create<ConvertOp>(tval.getLoc(), dstTp, tval);
env.op()->setOperand(tensor, convert);
rewriter.setInsertionPointAfter(env.op());
rewriter.create<bufferization::DeallocTensorOp>(tval.getLoc(), convert);
return success();
}
// Cannot be resolved with a single conversion.
// TODO: convert more than one?
return failure();
}
/// Options to control sparse code generation.
SparsificationOptions options;
};
} // namespace
/// Populates the given patterns list with rewriting rules required for
/// the sparsification of linear algebra operations.
void mlir::populateSparsificationPatterns(
RewritePatternSet &patterns, const SparsificationOptions &options) {
patterns.add<GenericOpSparsifier>(patterns.getContext(), options);
}
|