1 //===- AffineMap.cpp - MLIR Affine Map Classes ----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "mlir/IR/AffineMap.h" 10 #include "AffineMapDetail.h" 11 #include "mlir/IR/BuiltinAttributes.h" 12 #include "mlir/IR/BuiltinTypes.h" 13 #include "mlir/Support/LogicalResult.h" 14 #include "mlir/Support/MathExtras.h" 15 #include "llvm/ADT/SmallBitVector.h" 16 #include "llvm/ADT/SmallSet.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 using namespace mlir; 21 22 namespace { 23 24 // AffineExprConstantFolder evaluates an affine expression using constant 25 // operands passed in 'operandConsts'. Returns an IntegerAttr attribute 26 // representing the constant value of the affine expression evaluated on 27 // constant 'operandConsts', or nullptr if it can't be folded. 28 class AffineExprConstantFolder { 29 public: 30 AffineExprConstantFolder(unsigned numDims, ArrayRef<Attribute> operandConsts) 31 : numDims(numDims), operandConsts(operandConsts) {} 32 33 /// Attempt to constant fold the specified affine expr, or return null on 34 /// failure. 35 IntegerAttr constantFold(AffineExpr expr) { 36 if (auto result = constantFoldImpl(expr)) 37 return IntegerAttr::get(IndexType::get(expr.getContext()), *result); 38 return nullptr; 39 } 40 41 private: 42 Optional<int64_t> constantFoldImpl(AffineExpr expr) { 43 switch (expr.getKind()) { 44 case AffineExprKind::Add: 45 return constantFoldBinExpr( 46 expr, [](int64_t lhs, int64_t rhs) { return lhs + rhs; }); 47 case AffineExprKind::Mul: 48 return constantFoldBinExpr( 49 expr, [](int64_t lhs, int64_t rhs) { return lhs * rhs; }); 50 case AffineExprKind::Mod: 51 return constantFoldBinExpr( 52 expr, [](int64_t lhs, int64_t rhs) { return mod(lhs, rhs); }); 53 case AffineExprKind::FloorDiv: 54 return constantFoldBinExpr( 55 expr, [](int64_t lhs, int64_t rhs) { return floorDiv(lhs, rhs); }); 56 case AffineExprKind::CeilDiv: 57 return constantFoldBinExpr( 58 expr, [](int64_t lhs, int64_t rhs) { return ceilDiv(lhs, rhs); }); 59 case AffineExprKind::Constant: 60 return expr.cast<AffineConstantExpr>().getValue(); 61 case AffineExprKind::DimId: 62 if (auto attr = operandConsts[expr.cast<AffineDimExpr>().getPosition()] 63 .dyn_cast_or_null<IntegerAttr>()) 64 return attr.getInt(); 65 return llvm::None; 66 case AffineExprKind::SymbolId: 67 if (auto attr = operandConsts[numDims + 68 expr.cast<AffineSymbolExpr>().getPosition()] 69 .dyn_cast_or_null<IntegerAttr>()) 70 return attr.getInt(); 71 return llvm::None; 72 } 73 llvm_unreachable("Unknown AffineExpr"); 74 } 75 76 // TODO: Change these to operate on APInts too. 77 Optional<int64_t> constantFoldBinExpr(AffineExpr expr, 78 int64_t (*op)(int64_t, int64_t)) { 79 auto binOpExpr = expr.cast<AffineBinaryOpExpr>(); 80 if (auto lhs = constantFoldImpl(binOpExpr.getLHS())) 81 if (auto rhs = constantFoldImpl(binOpExpr.getRHS())) 82 return op(*lhs, *rhs); 83 return llvm::None; 84 } 85 86 // The number of dimension operands in AffineMap containing this expression. 87 unsigned numDims; 88 // The constant valued operands used to evaluate this AffineExpr. 89 ArrayRef<Attribute> operandConsts; 90 }; 91 92 } // end anonymous namespace 93 94 /// Returns a single constant result affine map. 95 AffineMap AffineMap::getConstantMap(int64_t val, MLIRContext *context) { 96 return get(/*dimCount=*/0, /*symbolCount=*/0, 97 {getAffineConstantExpr(val, context)}); 98 } 99 100 /// Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most 101 /// minor dimensions. 102 AffineMap AffineMap::getMinorIdentityMap(unsigned dims, unsigned results, 103 MLIRContext *context) { 104 assert(dims >= results && "Dimension mismatch"); 105 auto id = AffineMap::getMultiDimIdentityMap(dims, context); 106 return AffineMap::get(dims, 0, id.getResults().take_back(results), context); 107 } 108 109 bool AffineMap::isMinorIdentity() const { 110 return getNumDims() >= getNumResults() && 111 *this == 112 getMinorIdentityMap(getNumDims(), getNumResults(), getContext()); 113 } 114 115 /// Returns true if this affine map is a minor identity up to broadcasted 116 /// dimensions which are indicated by value 0 in the result. 117 bool AffineMap::isMinorIdentityWithBroadcasting( 118 SmallVectorImpl<unsigned> *broadcastedDims) const { 119 if (broadcastedDims) 120 broadcastedDims->clear(); 121 if (getNumDims() < getNumResults()) 122 return false; 123 unsigned suffixStart = getNumDims() - getNumResults(); 124 for (auto idxAndExpr : llvm::enumerate(getResults())) { 125 unsigned resIdx = idxAndExpr.index(); 126 AffineExpr expr = idxAndExpr.value(); 127 if (auto constExpr = expr.dyn_cast<AffineConstantExpr>()) { 128 // Each result may be either a constant 0 (broadcasted dimension). 129 if (constExpr.getValue() != 0) 130 return false; 131 if (broadcastedDims) 132 broadcastedDims->push_back(resIdx); 133 } else if (auto dimExpr = expr.dyn_cast<AffineDimExpr>()) { 134 // Or it may be the input dimension corresponding to this result position. 135 if (dimExpr.getPosition() != suffixStart + resIdx) 136 return false; 137 } else { 138 return false; 139 } 140 } 141 return true; 142 } 143 144 /// Return true if this affine map can be converted to a minor identity with 145 /// broadcast by doing a permute. Return a permutation (there may be 146 /// several) to apply to get to a minor identity with broadcasts. 147 /// Ex: 148 /// * (d0, d1, d2) -> (0, d1) maps to minor identity (d1, 0 = d2) with 149 /// perm = [1, 0] and broadcast d2 150 /// * (d0, d1, d2) -> (d0, 0) cannot be mapped to a minor identity by 151 /// permutation + broadcast 152 /// * (d0, d1, d2, d3) -> (0, d1, d3) maps to minor identity (d1, 0 = d2, d3) 153 /// with perm = [1, 0, 2] and broadcast d2 154 /// * (d0, d1) -> (d1, 0, 0, d0) maps to minor identity (d0, d1) with extra 155 /// leading broadcat dimensions. The map returned would be (0, 0, d0, d1) with 156 /// perm = [3, 0, 1, 2] 157 bool AffineMap::isPermutationOfMinorIdentityWithBroadcasting( 158 SmallVectorImpl<unsigned> &permutedDims) const { 159 unsigned projectionStart = 160 getNumResults() < getNumInputs() ? getNumInputs() - getNumResults() : 0; 161 permutedDims.clear(); 162 SmallVector<unsigned> broadcastDims; 163 permutedDims.resize(getNumResults(), 0); 164 // If there are more results than input dimensions we want the new map to 165 // start with broadcast dimensions in order to be a minor identity with 166 // broadcasting. 167 unsigned leadingBroadcast = 168 getNumResults() > getNumInputs() ? getNumResults() - getNumInputs() : 0; 169 llvm::SmallBitVector dimFound(std::max(getNumInputs(), getNumResults()), 170 false); 171 for (auto idxAndExpr : llvm::enumerate(getResults())) { 172 unsigned resIdx = idxAndExpr.index(); 173 AffineExpr expr = idxAndExpr.value(); 174 // Each result may be either a constant 0 (broadcast dimension) or a 175 // dimension. 176 if (auto constExpr = expr.dyn_cast<AffineConstantExpr>()) { 177 if (constExpr.getValue() != 0) 178 return false; 179 broadcastDims.push_back(resIdx); 180 } else if (auto dimExpr = expr.dyn_cast<AffineDimExpr>()) { 181 if (dimExpr.getPosition() < projectionStart) 182 return false; 183 unsigned newPosition = 184 dimExpr.getPosition() - projectionStart + leadingBroadcast; 185 permutedDims[resIdx] = newPosition; 186 dimFound[newPosition] = true; 187 } else { 188 return false; 189 } 190 } 191 // Find a permuation for the broadcast dimension. Since they are broadcasted 192 // any valid permutation is acceptable. We just permute the dim into a slot 193 // without an existing dimension. 194 unsigned pos = 0; 195 for (auto dim : broadcastDims) { 196 while (pos < dimFound.size() && dimFound[pos]) { 197 pos++; 198 } 199 permutedDims[dim] = pos++; 200 } 201 return true; 202 } 203 204 /// Returns an AffineMap representing a permutation. 205 AffineMap AffineMap::getPermutationMap(ArrayRef<unsigned> permutation, 206 MLIRContext *context) { 207 assert(!permutation.empty() && 208 "Cannot create permutation map from empty permutation vector"); 209 SmallVector<AffineExpr, 4> affExprs; 210 for (auto index : permutation) 211 affExprs.push_back(getAffineDimExpr(index, context)); 212 auto m = std::max_element(permutation.begin(), permutation.end()); 213 auto permutationMap = AffineMap::get(*m + 1, 0, affExprs, context); 214 assert(permutationMap.isPermutation() && "Invalid permutation vector"); 215 return permutationMap; 216 } 217 218 template <typename AffineExprContainer> 219 static void getMaxDimAndSymbol(ArrayRef<AffineExprContainer> exprsList, 220 int64_t &maxDim, int64_t &maxSym) { 221 for (const auto &exprs : exprsList) { 222 for (auto expr : exprs) { 223 expr.walk([&maxDim, &maxSym](AffineExpr e) { 224 if (auto d = e.dyn_cast<AffineDimExpr>()) 225 maxDim = std::max(maxDim, static_cast<int64_t>(d.getPosition())); 226 if (auto s = e.dyn_cast<AffineSymbolExpr>()) 227 maxSym = std::max(maxSym, static_cast<int64_t>(s.getPosition())); 228 }); 229 } 230 } 231 } 232 233 template <typename AffineExprContainer> 234 static SmallVector<AffineMap, 4> 235 inferFromExprList(ArrayRef<AffineExprContainer> exprsList) { 236 assert(!exprsList.empty()); 237 assert(!exprsList[0].empty()); 238 auto context = exprsList[0][0].getContext(); 239 int64_t maxDim = -1, maxSym = -1; 240 getMaxDimAndSymbol(exprsList, maxDim, maxSym); 241 SmallVector<AffineMap, 4> maps; 242 maps.reserve(exprsList.size()); 243 for (const auto &exprs : exprsList) 244 maps.push_back(AffineMap::get(/*dimCount=*/maxDim + 1, 245 /*symbolCount=*/maxSym + 1, exprs, context)); 246 return maps; 247 } 248 249 SmallVector<AffineMap, 4> 250 AffineMap::inferFromExprList(ArrayRef<ArrayRef<AffineExpr>> exprsList) { 251 return ::inferFromExprList(exprsList); 252 } 253 254 SmallVector<AffineMap, 4> 255 AffineMap::inferFromExprList(ArrayRef<SmallVector<AffineExpr, 4>> exprsList) { 256 return ::inferFromExprList(exprsList); 257 } 258 259 AffineMap AffineMap::getMultiDimIdentityMap(unsigned numDims, 260 MLIRContext *context) { 261 SmallVector<AffineExpr, 4> dimExprs; 262 dimExprs.reserve(numDims); 263 for (unsigned i = 0; i < numDims; ++i) 264 dimExprs.push_back(mlir::getAffineDimExpr(i, context)); 265 return get(/*dimCount=*/numDims, /*symbolCount=*/0, dimExprs, context); 266 } 267 268 MLIRContext *AffineMap::getContext() const { return map->context; } 269 270 bool AffineMap::isIdentity() const { 271 if (getNumDims() != getNumResults()) 272 return false; 273 ArrayRef<AffineExpr> results = getResults(); 274 for (unsigned i = 0, numDims = getNumDims(); i < numDims; ++i) { 275 auto expr = results[i].dyn_cast<AffineDimExpr>(); 276 if (!expr || expr.getPosition() != i) 277 return false; 278 } 279 return true; 280 } 281 282 bool AffineMap::isEmpty() const { 283 return getNumDims() == 0 && getNumSymbols() == 0 && getNumResults() == 0; 284 } 285 286 bool AffineMap::isSingleConstant() const { 287 return getNumResults() == 1 && getResult(0).isa<AffineConstantExpr>(); 288 } 289 290 bool AffineMap::isConstant() const { 291 return llvm::all_of(getResults(), [](AffineExpr expr) { 292 return expr.isa<AffineConstantExpr>(); 293 }); 294 } 295 296 int64_t AffineMap::getSingleConstantResult() const { 297 assert(isSingleConstant() && "map must have a single constant result"); 298 return getResult(0).cast<AffineConstantExpr>().getValue(); 299 } 300 301 SmallVector<int64_t> AffineMap::getConstantResults() const { 302 assert(isConstant() && "map must have only constant results"); 303 SmallVector<int64_t> result; 304 for (auto expr : getResults()) 305 result.emplace_back(expr.cast<AffineConstantExpr>().getValue()); 306 return result; 307 } 308 309 unsigned AffineMap::getNumDims() const { 310 assert(map && "uninitialized map storage"); 311 return map->numDims; 312 } 313 unsigned AffineMap::getNumSymbols() const { 314 assert(map && "uninitialized map storage"); 315 return map->numSymbols; 316 } 317 unsigned AffineMap::getNumResults() const { 318 assert(map && "uninitialized map storage"); 319 return map->results.size(); 320 } 321 unsigned AffineMap::getNumInputs() const { 322 assert(map && "uninitialized map storage"); 323 return map->numDims + map->numSymbols; 324 } 325 326 ArrayRef<AffineExpr> AffineMap::getResults() const { 327 assert(map && "uninitialized map storage"); 328 return map->results; 329 } 330 AffineExpr AffineMap::getResult(unsigned idx) const { 331 assert(map && "uninitialized map storage"); 332 return map->results[idx]; 333 } 334 335 unsigned AffineMap::getDimPosition(unsigned idx) const { 336 return getResult(idx).cast<AffineDimExpr>().getPosition(); 337 } 338 339 /// Folds the results of the application of an affine map on the provided 340 /// operands to a constant if possible. Returns false if the folding happens, 341 /// true otherwise. 342 LogicalResult 343 AffineMap::constantFold(ArrayRef<Attribute> operandConstants, 344 SmallVectorImpl<Attribute> &results) const { 345 // Attempt partial folding. 346 SmallVector<int64_t, 2> integers; 347 partialConstantFold(operandConstants, &integers); 348 349 // If all expressions folded to a constant, populate results with attributes 350 // containing those constants. 351 if (integers.empty()) 352 return failure(); 353 354 auto range = llvm::map_range(integers, [this](int64_t i) { 355 return IntegerAttr::get(IndexType::get(getContext()), i); 356 }); 357 results.append(range.begin(), range.end()); 358 return success(); 359 } 360 361 AffineMap 362 AffineMap::partialConstantFold(ArrayRef<Attribute> operandConstants, 363 SmallVectorImpl<int64_t> *results) const { 364 assert(getNumInputs() == operandConstants.size()); 365 366 // Fold each of the result expressions. 367 AffineExprConstantFolder exprFolder(getNumDims(), operandConstants); 368 SmallVector<AffineExpr, 4> exprs; 369 exprs.reserve(getNumResults()); 370 371 for (auto expr : getResults()) { 372 auto folded = exprFolder.constantFold(expr); 373 // If did not fold to a constant, keep the original expression, and clear 374 // the integer results vector. 375 if (folded) { 376 exprs.push_back( 377 getAffineConstantExpr(folded.getInt(), folded.getContext())); 378 if (results) 379 results->push_back(folded.getInt()); 380 } else { 381 exprs.push_back(expr); 382 if (results) { 383 results->clear(); 384 results = nullptr; 385 } 386 } 387 } 388 389 return get(getNumDims(), getNumSymbols(), exprs, getContext()); 390 } 391 392 /// Walk all of the AffineExpr's in this mapping. Each node in an expression 393 /// tree is visited in postorder. 394 void AffineMap::walkExprs(std::function<void(AffineExpr)> callback) const { 395 for (auto expr : getResults()) 396 expr.walk(callback); 397 } 398 399 /// This method substitutes any uses of dimensions and symbols (e.g. 400 /// dim#0 with dimReplacements[0]) in subexpressions and returns the modified 401 /// expression mapping. Because this can be used to eliminate dims and 402 /// symbols, the client needs to specify the number of dims and symbols in 403 /// the result. The returned map always has the same number of results. 404 AffineMap AffineMap::replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements, 405 ArrayRef<AffineExpr> symReplacements, 406 unsigned numResultDims, 407 unsigned numResultSyms) const { 408 SmallVector<AffineExpr, 8> results; 409 results.reserve(getNumResults()); 410 for (auto expr : getResults()) 411 results.push_back( 412 expr.replaceDimsAndSymbols(dimReplacements, symReplacements)); 413 return get(numResultDims, numResultSyms, results, getContext()); 414 } 415 416 /// Sparse replace method. Apply AffineExpr::replace(`expr`, `replacement`) to 417 /// each of the results and return a new AffineMap with the new results and 418 /// with the specified number of dims and symbols. 419 AffineMap AffineMap::replace(AffineExpr expr, AffineExpr replacement, 420 unsigned numResultDims, 421 unsigned numResultSyms) const { 422 SmallVector<AffineExpr, 4> newResults; 423 newResults.reserve(getNumResults()); 424 for (AffineExpr e : getResults()) 425 newResults.push_back(e.replace(expr, replacement)); 426 return AffineMap::get(numResultDims, numResultSyms, newResults, getContext()); 427 } 428 429 /// Sparse replace method. Apply AffineExpr::replace(`map`) to each of the 430 /// results and return a new AffineMap with the new results and with the 431 /// specified number of dims and symbols. 432 AffineMap AffineMap::replace(const DenseMap<AffineExpr, AffineExpr> &map, 433 unsigned numResultDims, 434 unsigned numResultSyms) const { 435 SmallVector<AffineExpr, 4> newResults; 436 newResults.reserve(getNumResults()); 437 for (AffineExpr e : getResults()) 438 newResults.push_back(e.replace(map)); 439 return AffineMap::get(numResultDims, numResultSyms, newResults, getContext()); 440 } 441 442 AffineMap 443 AffineMap::replace(const DenseMap<AffineExpr, AffineExpr> &map) const { 444 SmallVector<AffineExpr, 4> newResults; 445 newResults.reserve(getNumResults()); 446 for (AffineExpr e : getResults()) 447 newResults.push_back(e.replace(map)); 448 return AffineMap::inferFromExprList(newResults).front(); 449 } 450 451 AffineMap AffineMap::compose(AffineMap map) const { 452 assert(getNumDims() == map.getNumResults() && "Number of results mismatch"); 453 // Prepare `map` by concatenating the symbols and rewriting its exprs. 454 unsigned numDims = map.getNumDims(); 455 unsigned numSymbolsThisMap = getNumSymbols(); 456 unsigned numSymbols = numSymbolsThisMap + map.getNumSymbols(); 457 SmallVector<AffineExpr, 8> newDims(numDims); 458 for (unsigned idx = 0; idx < numDims; ++idx) { 459 newDims[idx] = getAffineDimExpr(idx, getContext()); 460 } 461 SmallVector<AffineExpr, 8> newSymbols(numSymbols - numSymbolsThisMap); 462 for (unsigned idx = numSymbolsThisMap; idx < numSymbols; ++idx) { 463 newSymbols[idx - numSymbolsThisMap] = 464 getAffineSymbolExpr(idx, getContext()); 465 } 466 auto newMap = 467 map.replaceDimsAndSymbols(newDims, newSymbols, numDims, numSymbols); 468 SmallVector<AffineExpr, 8> exprs; 469 exprs.reserve(getResults().size()); 470 for (auto expr : getResults()) 471 exprs.push_back(expr.compose(newMap)); 472 return AffineMap::get(numDims, numSymbols, exprs, map.getContext()); 473 } 474 475 SmallVector<int64_t, 4> AffineMap::compose(ArrayRef<int64_t> values) const { 476 assert(getNumSymbols() == 0 && "Expected symbol-less map"); 477 SmallVector<AffineExpr, 4> exprs; 478 exprs.reserve(values.size()); 479 MLIRContext *ctx = getContext(); 480 for (auto v : values) 481 exprs.push_back(getAffineConstantExpr(v, ctx)); 482 auto resMap = compose(AffineMap::get(0, 0, exprs, ctx)); 483 SmallVector<int64_t, 4> res; 484 res.reserve(resMap.getNumResults()); 485 for (auto e : resMap.getResults()) 486 res.push_back(e.cast<AffineConstantExpr>().getValue()); 487 return res; 488 } 489 490 bool AffineMap::isProjectedPermutation() const { 491 if (getNumSymbols() > 0) 492 return false; 493 SmallVector<bool, 8> seen(getNumInputs(), false); 494 for (auto expr : getResults()) { 495 if (auto dim = expr.dyn_cast<AffineDimExpr>()) { 496 if (seen[dim.getPosition()]) 497 return false; 498 seen[dim.getPosition()] = true; 499 continue; 500 } 501 return false; 502 } 503 return true; 504 } 505 506 bool AffineMap::isPermutation() const { 507 if (getNumDims() != getNumResults()) 508 return false; 509 return isProjectedPermutation(); 510 } 511 512 AffineMap AffineMap::getSubMap(ArrayRef<unsigned> resultPos) const { 513 SmallVector<AffineExpr, 4> exprs; 514 exprs.reserve(resultPos.size()); 515 for (auto idx : resultPos) 516 exprs.push_back(getResult(idx)); 517 return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext()); 518 } 519 520 AffineMap AffineMap::getSliceMap(unsigned start, unsigned length) const { 521 return AffineMap::get(getNumDims(), getNumSymbols(), 522 getResults().slice(start, length), getContext()); 523 } 524 525 AffineMap AffineMap::getMajorSubMap(unsigned numResults) const { 526 if (numResults == 0) 527 return AffineMap(); 528 if (numResults > getNumResults()) 529 return *this; 530 return getSubMap(llvm::to_vector<4>(llvm::seq<unsigned>(0, numResults))); 531 } 532 533 AffineMap AffineMap::getMinorSubMap(unsigned numResults) const { 534 if (numResults == 0) 535 return AffineMap(); 536 if (numResults > getNumResults()) 537 return *this; 538 return getSubMap(llvm::to_vector<4>( 539 llvm::seq<unsigned>(getNumResults() - numResults, getNumResults()))); 540 } 541 542 AffineMap mlir::compressDims(AffineMap map, 543 const llvm::SmallDenseSet<unsigned> &unusedDims) { 544 unsigned numDims = 0; 545 SmallVector<AffineExpr> dimReplacements; 546 dimReplacements.reserve(map.getNumDims()); 547 MLIRContext *context = map.getContext(); 548 for (unsigned dim = 0, e = map.getNumDims(); dim < e; ++dim) { 549 if (unusedDims.contains(dim)) 550 dimReplacements.push_back(getAffineConstantExpr(0, context)); 551 else 552 dimReplacements.push_back(getAffineDimExpr(numDims++, context)); 553 } 554 SmallVector<AffineExpr> resultExprs; 555 resultExprs.reserve(map.getNumResults()); 556 for (auto e : map.getResults()) 557 resultExprs.push_back(e.replaceDims(dimReplacements)); 558 return AffineMap::get(numDims, map.getNumSymbols(), resultExprs, context); 559 } 560 561 AffineMap mlir::compressUnusedDims(AffineMap map) { 562 llvm::SmallDenseSet<unsigned> usedDims; 563 map.walkExprs([&](AffineExpr expr) { 564 if (auto dimExpr = expr.dyn_cast<AffineDimExpr>()) 565 usedDims.insert(dimExpr.getPosition()); 566 }); 567 llvm::SmallDenseSet<unsigned> unusedDims; 568 for (unsigned d = 0, e = map.getNumDims(); d != e; ++d) 569 if (!usedDims.contains(d)) 570 unusedDims.insert(d); 571 return compressDims(map, unusedDims); 572 } 573 574 static SmallVector<AffineMap> 575 compressUnusedImpl(ArrayRef<AffineMap> maps, 576 llvm::function_ref<AffineMap(AffineMap)> compressionFun) { 577 if (maps.empty()) 578 return SmallVector<AffineMap>(); 579 SmallVector<AffineExpr> allExprs; 580 allExprs.reserve(maps.size() * maps.front().getNumResults()); 581 unsigned numDims = maps.front().getNumDims(), 582 numSymbols = maps.front().getNumSymbols(); 583 for (auto m : maps) { 584 assert(numDims == m.getNumDims() && numSymbols == m.getNumSymbols() && 585 "expected maps with same num dims and symbols"); 586 llvm::append_range(allExprs, m.getResults()); 587 } 588 AffineMap unifiedMap = compressionFun( 589 AffineMap::get(numDims, numSymbols, allExprs, maps.front().getContext())); 590 unsigned unifiedNumDims = unifiedMap.getNumDims(), 591 unifiedNumSymbols = unifiedMap.getNumSymbols(); 592 ArrayRef<AffineExpr> unifiedResults = unifiedMap.getResults(); 593 SmallVector<AffineMap> res; 594 res.reserve(maps.size()); 595 for (auto m : maps) { 596 res.push_back(AffineMap::get(unifiedNumDims, unifiedNumSymbols, 597 unifiedResults.take_front(m.getNumResults()), 598 m.getContext())); 599 unifiedResults = unifiedResults.drop_front(m.getNumResults()); 600 } 601 return res; 602 } 603 604 SmallVector<AffineMap> mlir::compressUnusedDims(ArrayRef<AffineMap> maps) { 605 return compressUnusedImpl(maps, 606 [](AffineMap m) { return compressUnusedDims(m); }); 607 } 608 609 AffineMap 610 mlir::compressSymbols(AffineMap map, 611 const llvm::SmallDenseSet<unsigned> &unusedSymbols) { 612 unsigned numSymbols = 0; 613 SmallVector<AffineExpr> symReplacements; 614 symReplacements.reserve(map.getNumSymbols()); 615 MLIRContext *context = map.getContext(); 616 for (unsigned sym = 0, e = map.getNumSymbols(); sym < e; ++sym) { 617 if (unusedSymbols.contains(sym)) 618 symReplacements.push_back(getAffineConstantExpr(0, context)); 619 else 620 symReplacements.push_back(getAffineSymbolExpr(numSymbols++, context)); 621 } 622 SmallVector<AffineExpr> resultExprs; 623 resultExprs.reserve(map.getNumResults()); 624 for (auto e : map.getResults()) 625 resultExprs.push_back(e.replaceSymbols(symReplacements)); 626 return AffineMap::get(map.getNumDims(), numSymbols, resultExprs, context); 627 } 628 629 AffineMap mlir::compressUnusedSymbols(AffineMap map) { 630 llvm::SmallDenseSet<unsigned> usedSymbols; 631 map.walkExprs([&](AffineExpr expr) { 632 if (auto symExpr = expr.dyn_cast<AffineSymbolExpr>()) 633 usedSymbols.insert(symExpr.getPosition()); 634 }); 635 llvm::SmallDenseSet<unsigned> unusedSymbols; 636 for (unsigned d = 0, e = map.getNumSymbols(); d != e; ++d) 637 if (!usedSymbols.contains(d)) 638 unusedSymbols.insert(d); 639 return compressSymbols(map, unusedSymbols); 640 } 641 642 SmallVector<AffineMap> mlir::compressUnusedSymbols(ArrayRef<AffineMap> maps) { 643 return compressUnusedImpl( 644 maps, [](AffineMap m) { return compressUnusedSymbols(m); }); 645 } 646 647 AffineMap mlir::simplifyAffineMap(AffineMap map) { 648 SmallVector<AffineExpr, 8> exprs; 649 for (auto e : map.getResults()) { 650 exprs.push_back( 651 simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols())); 652 } 653 return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs, 654 map.getContext()); 655 } 656 657 AffineMap mlir::removeDuplicateExprs(AffineMap map) { 658 auto results = map.getResults(); 659 SmallVector<AffineExpr, 4> uniqueExprs(results.begin(), results.end()); 660 uniqueExprs.erase(std::unique(uniqueExprs.begin(), uniqueExprs.end()), 661 uniqueExprs.end()); 662 return AffineMap::get(map.getNumDims(), map.getNumSymbols(), uniqueExprs, 663 map.getContext()); 664 } 665 666 AffineMap mlir::inversePermutation(AffineMap map) { 667 if (map.isEmpty()) 668 return map; 669 assert(map.getNumSymbols() == 0 && "expected map without symbols"); 670 SmallVector<AffineExpr, 4> exprs(map.getNumDims()); 671 for (auto en : llvm::enumerate(map.getResults())) { 672 auto expr = en.value(); 673 // Skip non-permutations. 674 if (auto d = expr.dyn_cast<AffineDimExpr>()) { 675 if (exprs[d.getPosition()]) 676 continue; 677 exprs[d.getPosition()] = getAffineDimExpr(en.index(), d.getContext()); 678 } 679 } 680 SmallVector<AffineExpr, 4> seenExprs; 681 seenExprs.reserve(map.getNumDims()); 682 for (auto expr : exprs) 683 if (expr) 684 seenExprs.push_back(expr); 685 if (seenExprs.size() != map.getNumInputs()) 686 return AffineMap(); 687 return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext()); 688 } 689 690 AffineMap mlir::inverseAndBroadcastProjectedPermuation(AffineMap map) { 691 assert(map.isProjectedPermutation()); 692 MLIRContext *context = map.getContext(); 693 AffineExpr zero = mlir::getAffineConstantExpr(0, context); 694 // Start with all the results as 0. 695 SmallVector<AffineExpr, 4> exprs(map.getNumInputs(), zero); 696 for (unsigned i : llvm::seq(unsigned(0), map.getNumResults())) { 697 // Reverse each dimension existing in the oringal map result. 698 exprs[map.getDimPosition(i)] = getAffineDimExpr(i, context); 699 } 700 return AffineMap::get(map.getNumResults(), /*symbolCount=*/0, exprs, context); 701 } 702 703 AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) { 704 unsigned numResults = 0, numDims = 0, numSymbols = 0; 705 for (auto m : maps) 706 numResults += m.getNumResults(); 707 SmallVector<AffineExpr, 8> results; 708 results.reserve(numResults); 709 for (auto m : maps) { 710 for (auto res : m.getResults()) 711 results.push_back(res.shiftSymbols(m.getNumSymbols(), numSymbols)); 712 713 numSymbols += m.getNumSymbols(); 714 numDims = std::max(m.getNumDims(), numDims); 715 } 716 return AffineMap::get(numDims, numSymbols, results, 717 maps.front().getContext()); 718 } 719 720 AffineMap 721 mlir::getProjectedMap(AffineMap map, 722 const llvm::SmallDenseSet<unsigned> &unusedDims) { 723 return compressUnusedSymbols(compressDims(map, unusedDims)); 724 } 725 726 //===----------------------------------------------------------------------===// 727 // MutableAffineMap. 728 //===----------------------------------------------------------------------===// 729 730 MutableAffineMap::MutableAffineMap(AffineMap map) 731 : numDims(map.getNumDims()), numSymbols(map.getNumSymbols()), 732 context(map.getContext()) { 733 for (auto result : map.getResults()) 734 results.push_back(result); 735 } 736 737 void MutableAffineMap::reset(AffineMap map) { 738 results.clear(); 739 numDims = map.getNumDims(); 740 numSymbols = map.getNumSymbols(); 741 context = map.getContext(); 742 for (auto result : map.getResults()) 743 results.push_back(result); 744 } 745 746 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const { 747 if (results[idx].isMultipleOf(factor)) 748 return true; 749 750 // TODO: use simplifyAffineExpr and FlatAffineConstraints to 751 // complete this (for a more powerful analysis). 752 return false; 753 } 754 755 // Simplifies the result affine expressions of this map. The expressions have to 756 // be pure for the simplification implemented. 757 void MutableAffineMap::simplify() { 758 // Simplify each of the results if possible. 759 // TODO: functional-style map 760 for (unsigned i = 0, e = getNumResults(); i < e; i++) { 761 results[i] = simplifyAffineExpr(getResult(i), numDims, numSymbols); 762 } 763 } 764 765 AffineMap MutableAffineMap::getAffineMap() const { 766 return AffineMap::get(numDims, numSymbols, results, context); 767 } 768