1 //===- Sparsification.cpp - Implementation of sparsification --------------===// 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 // This file implements converting sparse tensor types to actual sparse code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodegenUtils.h" 14 15 #include "mlir/Dialect/Affine/IR/AffineOps.h" 16 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" 17 #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h" 18 #include "mlir/Dialect/Bufferization/IR/Bufferization.h" 19 #include "mlir/Dialect/Func/IR/FuncOps.h" 20 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 21 #include "mlir/Dialect/Linalg/IR/Linalg.h" 22 #include "mlir/Dialect/Linalg/Utils/Utils.h" 23 #include "mlir/Dialect/MemRef/IR/MemRef.h" 24 #include "mlir/Dialect/SCF/SCF.h" 25 #include "mlir/Dialect/SCF/Transforms.h" 26 #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h" 27 #include "mlir/Dialect/SparseTensor/Transforms/Passes.h" 28 #include "mlir/Dialect/SparseTensor/Utils/Merger.h" 29 #include "mlir/Dialect/Vector/IR/VectorOps.h" 30 #include "mlir/IR/Matchers.h" 31 #include "mlir/IR/TensorEncoding.h" 32 #include "llvm/ADT/SmallBitVector.h" 33 34 using namespace mlir; 35 using namespace mlir::sparse_tensor; 36 37 //===----------------------------------------------------------------------===// 38 // Declarations of data structures. 39 //===----------------------------------------------------------------------===// 40 41 namespace { 42 43 // Iteration graph sorting. 44 enum SortMask { kSparseOnly = 0x0, kIncludeDense = 0x1, kIncludeUndef = 0x2 }; 45 46 // Reduction kinds. 47 enum Reduction { kNoReduc, kSum, kProduct, kAnd, kOr, kXor }; 48 49 // Code generation. 50 struct CodeGen { 51 CodeGen(SparsificationOptions o, unsigned numTensors, unsigned numLoops, 52 OpOperand *op, unsigned nest) 53 : options(o), loops(numLoops), sizes(numLoops), buffers(numTensors), 54 pointers(numTensors, std::vector<Value>(numLoops)), 55 indices(numTensors, std::vector<Value>(numLoops)), 56 highs(numTensors, std::vector<Value>(numLoops)), 57 pidxs(numTensors, std::vector<Value>(numLoops)), 58 idxs(numTensors, std::vector<Value>(numLoops)), redVal(), sparseOut(op), 59 outerParNest(nest), lexIdx(), expValues(), expFilled(), expAdded(), 60 expCount(), curVecMask() {} 61 /// Sparsification options. 62 SparsificationOptions options; 63 /// Universal dense indices and upper bounds (by index). The loops array 64 /// is updated with the value of the universal dense index in the current 65 /// loop. The sizes array is set once with the inferred dimension sizes. 66 std::vector<Value> loops; 67 std::vector<Value> sizes; 68 /// Buffers for storing dense and sparse numerical values (by tensor). 69 /// This array is set once during bufferization of all tensors. 70 std::vector<Value> buffers; 71 /// Sparse storage schemes (1-D): pointers and indices (by tensor and index). 72 /// This array is set once during bufferization of all sparse tensors. 73 std::vector<std::vector<Value>> pointers; 74 std::vector<std::vector<Value>> indices; 75 /// Sparse iteration information (by tensor and index). These arrays 76 /// are updated to remain current within the current loop. 77 std::vector<std::vector<Value>> highs; 78 std::vector<std::vector<Value>> pidxs; 79 std::vector<std::vector<Value>> idxs; 80 /// Current reduction, updated during code generation. When indices of a 81 /// reduction are exhausted, all inner loops can use a scalarized reduction. 82 unsigned redExp = -1u; 83 Value redVal; 84 Reduction redKind = kNoReduc; 85 // Sparse tensor as output. Implemented either through direct injective 86 // insertion in lexicographic index order (where indices are updated 87 // in the temporary array `lexIdx`) or through access pattern expansion 88 // in the innermost loop nest (`expValues` through `expCount`). 89 OpOperand *sparseOut; 90 unsigned outerParNest; 91 Value lexIdx; 92 Value expValues; 93 Value expFilled; 94 Value expAdded; 95 Value expCount; 96 // Current vector length and mask. 97 unsigned curVecLength = 1; 98 Value curVecMask; 99 }; 100 101 } // namespace 102 103 //===----------------------------------------------------------------------===// 104 // Sparse compiler analysis methods. 105 //===----------------------------------------------------------------------===// 106 107 /// Helper method to apply dimension ordering permutation. 108 static unsigned perm(const SparseTensorEncodingAttr &enc, unsigned d) { 109 if (enc) { 110 auto order = enc.getDimOrdering(); 111 if (order) { 112 assert(order.isPermutation()); 113 return order.getDimPosition(d); 114 } 115 } 116 return d; 117 } 118 119 /// Helper method to translate dim level type to internal representation. 120 static Dim toDim(const SparseTensorEncodingAttr &enc, unsigned d) { 121 if (enc) { 122 SparseTensorEncodingAttr::DimLevelType tp = enc.getDimLevelType()[d]; 123 if (tp == SparseTensorEncodingAttr::DimLevelType::Compressed) 124 return Dim::kSparse; 125 if (tp == SparseTensorEncodingAttr::DimLevelType::Singleton) 126 return Dim::kSingle; 127 } 128 return Dim::kDense; 129 } 130 131 /// Helper method to inspect affine expressions. Rejects cases where the 132 /// same index is used more than once. Also rejects affine expressions 133 /// that are not a direct index for annotated tensors. 134 // TODO: accept more affine cases for sparse tensors 135 static bool findAffine(Merger &merger, unsigned tensor, AffineExpr a, Dim dim, 136 bool isDense) { 137 switch (a.getKind()) { 138 case AffineExprKind::DimId: { 139 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 140 if (!merger.isDim(tensor, idx, Dim::kUndef)) 141 return false; // used more than once 142 merger.setDim(tensor, idx, dim); 143 return true; 144 } 145 case AffineExprKind::Add: 146 case AffineExprKind::Mul: { 147 if (!isDense) 148 return false; 149 auto binOp = a.cast<AffineBinaryOpExpr>(); 150 return findAffine(merger, tensor, binOp.getLHS(), dim, isDense) && 151 findAffine(merger, tensor, binOp.getRHS(), dim, isDense); 152 } 153 case AffineExprKind::Constant: 154 return isDense; 155 default: 156 return false; 157 } 158 } 159 160 /// Helper method to inspect sparse encodings in the tensor types. 161 /// Fills the per-dimension sparsity information for all tensors. 162 /// Returns true if the sparse annotations and affine subscript 163 /// expressions of all tensors are admissable. Returns false if 164 /// no annotations are found or inadmissable constructs occur. 165 static bool findSparseAnnotations(Merger &merger, linalg::GenericOp op) { 166 bool annotated = false; 167 for (OpOperand *t : op.getInputAndOutputOperands()) { 168 auto map = op.getTiedIndexingMap(t); 169 auto enc = getSparseTensorEncoding(t->get().getType()); 170 if (enc) 171 annotated = true; 172 assert(map.getNumResults() == op.getRank(t)); 173 for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) { 174 unsigned tensor = t->getOperandNumber(); 175 AffineExpr a = map.getResult(perm(enc, d)); 176 if (!findAffine(merger, tensor, a, toDim(enc, d), !enc)) 177 return false; // inadmissable affine expression 178 } 179 } 180 return annotated; 181 } 182 183 /// A DFS helper to compute a topological sort. Note that recursion is 184 /// bounded by the number of implicit loops, which is always small. 185 /// Returns false when a cycle is detected. 186 static bool topSortDFS(unsigned i, std::vector<unsigned> &visit, 187 std::vector<unsigned> &topSort, 188 std::vector<std::vector<bool>> &adjM) { 189 if (visit[i] != 0) 190 return visit[i] != 1; // 1 denotes cycle! 191 visit[i] = 1; 192 for (unsigned j = 0, e = visit.size(); j < e; j++) 193 if (adjM[i][j]) 194 if (!topSortDFS(j, visit, topSort, adjM)) 195 return false; 196 visit[i] = 2; 197 topSort.push_back(i); 198 return true; 199 } 200 201 /// Helper method to add all constraints from the indices in one affine 202 /// expression before all indices in the other affine expression. For 203 /// example i0+i1 < i2+i3+1 yields i0<i2, i0<i3, i1<i2, and i1<i3. 204 static void addAffineOrderings(std::vector<std::vector<bool>> &adjM, 205 AffineExpr a, AffineExpr b, unsigned fidx) { 206 switch (a.getKind()) { 207 case AffineExprKind::DimId: { 208 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 209 if (b) 210 addAffineOrderings(adjM, b, AffineExpr(), idx); 211 else 212 adjM[fidx][idx] = true; 213 break; 214 } 215 case AffineExprKind::Add: 216 case AffineExprKind::Mul: { 217 auto binOp = a.cast<AffineBinaryOpExpr>(); 218 addAffineOrderings(adjM, binOp.getLHS(), b, fidx); 219 addAffineOrderings(adjM, binOp.getRHS(), b, fidx); 220 break; 221 } 222 default: 223 break; 224 } 225 } 226 227 /// Computes a topologically sorted iteration graph for the linalg operation. 228 /// Ensures all tensors are visited in natural index order. This is essential 229 /// for sparse storage formats since these only support access along fixed 230 /// dimensions. Even for dense storage formats, however, the natural index 231 /// order yields innermost unit-stride access with better spatial locality. 232 static bool computeIterationGraph(Merger &merger, linalg::GenericOp op, 233 std::vector<unsigned> &topSort, 234 unsigned mask) { 235 // Set up an n x n from/to adjacency matrix of the iteration graph 236 // for the implicit loop indices i_0 .. i_n-1. 237 unsigned n = op.getNumLoops(); 238 std::vector<std::vector<bool>> adjM(n, std::vector<bool>(n, false)); 239 240 // Iterate over the indexing maps of every tensor in the tensor expression. 241 for (OpOperand *t : op.getInputAndOutputOperands()) { 242 auto map = op.getTiedIndexingMap(t); 243 auto enc = getSparseTensorEncoding(t->get().getType()); 244 assert(map.getNumDims() == n); 245 // Skip dense tensor constraints when not requested. 246 if (!(mask & SortMask::kIncludeDense) && !enc) 247 continue; 248 // Each tensor expression and optional dimension ordering (row-major 249 // by default) puts an ordering constraint on the loop indices. For 250 // example, the tensor expresion A_ijk forces the ordering i < j < k 251 // on the loop indices if no explicit dimension ordering is given. 252 for (unsigned d = 1, rank = map.getNumResults(); d < rank; d++) { 253 AffineExpr f = map.getResult(perm(enc, d - 1)); 254 AffineExpr t = map.getResult(perm(enc, d)); 255 addAffineOrderings(adjM, f, t, 0); 256 } 257 // Push unrelated loops into sparse iteration space, so these 258 // will be skipped more often. 259 if (mask & SortMask::kIncludeUndef) { 260 unsigned tensor = t->getOperandNumber(); 261 for (unsigned i = 0; i < n; i++) 262 if (merger.isDim(tensor, i, Dim::kSparse)) 263 for (unsigned j = 0; j < n; j++) 264 if (merger.isDim(tensor, j, Dim::kUndef)) 265 adjM[i][j] = true; 266 } 267 } 268 269 // Topologically sort the iteration graph to determine loop order. 270 // Report failure for a cyclic iteration graph. 271 topSort.clear(); 272 topSort.reserve(n); 273 std::vector<unsigned> visit(n, 0); 274 for (unsigned i = 0; i < n; i++) 275 if (visit[i] == 0) 276 if (!topSortDFS(i, visit, topSort, adjM)) 277 return false; // cycle! 278 std::reverse(std::begin(topSort), std::end(topSort)); 279 return true; 280 } 281 282 /// Returns true if tensor has an in-place annotation. 283 static bool isInPlace(Value val) { 284 if (auto arg = val.dyn_cast<BlockArgument>()) 285 if (auto funcOp = dyn_cast<func::FuncOp>(arg.getOwner()->getParentOp())) 286 if (auto attr = funcOp.getArgAttrOfType<BoolAttr>( 287 arg.getArgNumber(), 288 bufferization::BufferizableOpInterface::kInplaceableAttrName)) 289 return attr.getValue(); 290 return false; 291 } 292 293 /// Returns true if tensor materializes uninitialized into the computation. 294 static bool isMaterializing(Value val) { 295 return val.getDefiningOp<linalg::InitTensorOp>() || 296 val.getDefiningOp<InitOp>(); 297 } 298 299 /// Returns true when the tensor expression is admissable for codegen. 300 /// Since all sparse input tensors are admissable, we just need to check 301 /// whether the out tensor in the tensor expression codegen is admissable. 302 /// Sets `sparseOut` to the tensor and `outerParNest` to the outer injective 303 /// nesting depth when a "truly dynamic" sparse tensor output occurs. 304 static bool isAdmissableTensorExp(Merger &merger, linalg::GenericOp op, 305 std::vector<unsigned> &topSort, unsigned exp, 306 OpOperand **sparseOut, 307 unsigned &outerParNest) { 308 OpOperand *lhs = op.getOutputOperand(0); 309 unsigned tensor = lhs->getOperandNumber(); 310 auto enc = getSparseTensorEncoding(lhs->get().getType()); 311 // An non-annotated output tensor is assumed dense, and becomes a random 312 // access n-dim memref. Admissable since insertions cannot occur. 313 if (!enc) 314 return true; 315 // An all-dense annotated "sparse" output tensor becomes a linearized random 316 // access 1-dim memref. Also admissable since insertions cannot occur. 317 bool allDense = true; 318 auto iteratorTypes = op.iterator_types().getValue(); 319 unsigned numLoops = iteratorTypes.size(); 320 for (unsigned i = 0; i < numLoops; i++) 321 if (merger.isDim(tensor, i, Dim::kSparse)) { 322 allDense = false; 323 break; 324 } 325 if (allDense) 326 return true; 327 // A tensor expression with a sparse output tensor that changes its values 328 // but not its nonzero structure, an operation called "simply dynamic" in 329 // [Bik96,Ch9], is also admissable without special codegen, provided 330 // the tensor's underlying sparse storage scheme can be modified in place. 331 if (merger.isSingleCondition(tensor, exp) && isInPlace(lhs->get())) 332 return true; 333 // Accept "truly dynamic" if the output tensor materializes uninitialized 334 // into the computation and insertions occur in lexicographic index order. 335 if (isMaterializing(lhs->get())) { 336 unsigned nest = 0; 337 for (unsigned i = 0; i < numLoops; i++) { 338 if (isReductionIterator(iteratorTypes[topSort[i]])) 339 break; // terminate at first reduction 340 nest++; 341 } 342 // Determine admissable dynamic insertion situations: 343 // (1) fully injective, since there are no reductions, 344 // (2) admissable 1-d expansion in innermost dimension. 345 if (nest >= op.getRank(lhs) - 1) { 346 *sparseOut = lhs; 347 outerParNest = nest; 348 return true; 349 } 350 } 351 return false; 352 } 353 354 //===----------------------------------------------------------------------===// 355 // Sparse compiler synthesis methods (reductions). 356 //===----------------------------------------------------------------------===// 357 358 /// Maps reduction kind to vector::CombiningKind. 359 static vector::CombiningKind getCombiningKind(Reduction kind) { 360 switch (kind) { 361 case kNoReduc: 362 break; 363 case kSum: 364 return vector::CombiningKind::ADD; 365 case kProduct: 366 return vector::CombiningKind::MUL; 367 case kAnd: 368 return vector::CombiningKind::AND; 369 case kOr: 370 return vector::CombiningKind::OR; 371 case kXor: 372 return vector::CombiningKind::XOR; 373 } 374 llvm_unreachable("unknown reduction kind"); 375 } 376 377 /// Maps operation to reduction. 378 static Reduction getReduction(Kind kind) { 379 switch (kind) { 380 case Kind::kAddF: 381 case Kind::kAddC: 382 case Kind::kAddI: 383 case Kind::kSubF: 384 case Kind::kSubC: 385 case Kind::kSubI: 386 return kSum; 387 case Kind::kMulF: 388 case Kind::kMulC: 389 case Kind::kMulI: 390 return kProduct; 391 case Kind::kAndI: 392 return kAnd; 393 case Kind::kOrI: 394 return kOr; 395 case Kind::kXorI: 396 return kXor; 397 default: 398 llvm_unreachable("unexpected reduction operator"); 399 } 400 } 401 402 /// Generates an initial value for a vector reduction, following the scheme 403 /// given in Chapter 5 of "The Software Vectorization Handbook", where the 404 /// initial scalar value is correctly embedded in the vector reduction value, 405 /// and a straightforward horizontal reduction will complete the operation. 406 static Value genVectorReducInit(CodeGen &codegen, OpBuilder &builder, 407 Location loc, VectorType vtp) { 408 Value r = codegen.redVal; 409 switch (codegen.redKind) { 410 case kNoReduc: 411 break; 412 case kSum: 413 case kXor: 414 // Initialize reduction vector to: | 0 | .. | 0 | r | 415 return builder.create<vector::InsertElementOp>( 416 loc, r, constantZero(builder, loc, vtp), 417 constantIndex(builder, loc, 0)); 418 case kProduct: 419 // Initialize reduction vector to: | 1 | .. | 1 | r | 420 return builder.create<vector::InsertElementOp>( 421 loc, r, constantOne(builder, loc, vtp), constantIndex(builder, loc, 0)); 422 case kAnd: 423 case kOr: 424 // Initialize reduction vector to: | r | .. | r | r | 425 return builder.create<vector::BroadcastOp>(loc, vtp, r); 426 } 427 llvm_unreachable("unknown reduction kind"); 428 } 429 430 /// Generates final value for a vector reduction. 431 static Value genVectorReducEnd(CodeGen &codegen, OpBuilder &builder, 432 Location loc, VectorType vtp) { 433 vector::CombiningKind kind = getCombiningKind(codegen.redKind); 434 return builder.create<vector::ReductionOp>(loc, kind, codegen.redVal); 435 } 436 437 /// Updates scalarized reduction value. 438 static void updateReduc(Merger &merger, CodeGen &codegen, Value reduc) { 439 assert(codegen.redKind != kNoReduc); 440 codegen.redVal = merger.exp(codegen.redExp).val = reduc; 441 } 442 443 //===----------------------------------------------------------------------===// 444 // Sparse compiler synthesis methods (statements and expressions). 445 //===----------------------------------------------------------------------===// 446 447 /// Generates buffer for the output tensor. Note that all sparse kernels 448 /// assume that when all elements are written to (viz. x(i) = y(i) * z(i)), 449 /// the output buffer is already initialized to all zeroes and only nonzeroes 450 /// values are computed and written out. For updates (viz. x(i) += y(i) * z(i)), 451 /// only nonzeroes values are used for the updates and no assumption on the 452 /// original contents of the output buffer is necessary.. 453 static Value genOutputBuffer(CodeGen &codegen, OpBuilder &builder, 454 linalg::GenericOp op, MemRefType denseTp, 455 ArrayRef<Value> args) { 456 Location loc = op.getLoc(); 457 Value tensor = op.getOutputOperand(0)->get(); 458 // The output tensor simply could materialize from the buffer that will 459 // be generated for the tensor present in the outs() clause. This has 460 // the major advantage that the sparse kernel only updates the nonzero 461 // positions for the output tensor. 462 if (isInPlace(tensor)) 463 return builder.create<bufferization::ToMemrefOp>(loc, denseTp, tensor); 464 // By default, a new buffer is allocated which is initialized to the 465 // tensor defined in the outs() clause. This is always correct but 466 // introduces a dense initialization component that may negatively 467 // impact the running complexity of the sparse kernel. If the tensor 468 // materializes into the computation, we need to preserve the zero 469 // initialization assumption of all sparse output buffers. 470 Value alloc = builder.create<memref::AllocOp>(loc, denseTp, args); 471 if (isMaterializing(tensor)) { 472 Value zero = constantZero(builder, loc, denseTp.getElementType()); 473 builder.create<linalg::FillOp>(loc, ValueRange{zero}, ValueRange{alloc}); 474 } else { 475 Value init = 476 builder.create<bufferization::ToMemrefOp>(loc, denseTp, tensor); 477 builder.create<memref::CopyOp>(loc, init, alloc); 478 } 479 return alloc; 480 } 481 482 /// Local bufferization of all dense and sparse data structures. 483 /// This code enables testing the first prototype sparse compiler. 484 // TODO: replace this with a proliferated bufferization strategy 485 static void genBuffers(Merger &merger, CodeGen &codegen, OpBuilder &builder, 486 linalg::GenericOp op) { 487 Location loc = op.getLoc(); 488 assert(op.getNumInputsAndOutputs() == op.getNumInputs() + 1); 489 // For every tensor, find lower and upper bound on dimensions, set the 490 // same bounds on loop indices, and obtain dense or sparse buffer(s). 491 SmallVector<Value, 4> args; 492 for (OpOperand *t : op.getInputAndOutputOperands()) { 493 unsigned tensor = t->getOperandNumber(); 494 auto shape = op.getShape(t); 495 auto map = op.getTiedIndexingMap(t); 496 auto enc = getSparseTensorEncoding(t->get().getType()); 497 // Scan all dimensions of current tensor. 498 args.clear(); 499 for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) { 500 AffineExpr a = map.getResult(perm(enc, d)); 501 if (a.getKind() != AffineExprKind::DimId) 502 continue; // compound 503 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 504 // Handle sparse storage schemes. 505 if (merger.isDim(tensor, idx, Dim::kSparse)) { 506 auto dynShape = {ShapedType::kDynamicSize}; 507 auto ptrTp = 508 MemRefType::get(dynShape, getPointerOverheadType(builder, enc)); 509 auto indTp = 510 MemRefType::get(dynShape, getIndexOverheadType(builder, enc)); 511 Value dim = constantIndex(builder, loc, d); 512 // Generate sparse primitives to obtains pointer and indices. 513 codegen.pointers[tensor][idx] = 514 builder.create<ToPointersOp>(loc, ptrTp, t->get(), dim); 515 codegen.indices[tensor][idx] = 516 builder.create<ToIndicesOp>(loc, indTp, t->get(), dim); 517 } 518 // Find upper bound in current dimension. 519 unsigned p = perm(enc, d); 520 Value up = linalg::createOrFoldDimOp(builder, loc, t->get(), p); 521 if (ShapedType::isDynamic(shape[p])) 522 args.push_back(up); 523 assert(codegen.highs[tensor][idx] == nullptr); 524 codegen.sizes[idx] = codegen.highs[tensor][idx] = up; 525 } 526 // Perform the required bufferization. Dense inputs materialize 527 // from the input tensors. Dense outputs need special handling. 528 // Sparse inputs use sparse primitives to obtain the values. 529 // We also accept in-place all-dense annotated "sparse" outputs. 530 Type elementType = getElementTypeOrSelf(t->get().getType()); 531 if (!enc) { 532 // Non-annotated dense tensors. 533 auto denseTp = MemRefType::get(shape, elementType); 534 if (tensor < op.getNumInputs()) 535 codegen.buffers[tensor] = 536 builder.create<bufferization::ToMemrefOp>(loc, denseTp, t->get()); 537 else 538 codegen.buffers[tensor] = 539 genOutputBuffer(codegen, builder, op, denseTp, args); 540 } else if (t == codegen.sparseOut) { 541 // True sparse output needs a lexIdx array. 542 Value rank = constantIndex(builder, loc, op.getRank(t)); 543 auto dynShape = {ShapedType::kDynamicSize}; 544 auto memTp = MemRefType::get(dynShape, builder.getIndexType()); 545 codegen.lexIdx = builder.create<memref::AllocaOp>(loc, memTp, rank); 546 } else { 547 // Annotated sparse tensors. 548 auto dynShape = {ShapedType::kDynamicSize}; 549 auto sparseTp = MemRefType::get(dynShape, elementType); 550 codegen.buffers[tensor] = 551 builder.create<ToValuesOp>(loc, sparseTp, t->get()); 552 } 553 } 554 } 555 556 /// Constructs vector type. 557 static VectorType vectorType(CodeGen &codegen, Type etp) { 558 unsigned numScalableDims = codegen.options.enableVLAVectorization; 559 return VectorType::get(codegen.curVecLength, etp, numScalableDims); 560 } 561 562 /// Constructs vector type from pointer. 563 static VectorType vectorType(CodeGen &codegen, Value ptr) { 564 return vectorType(codegen, ptr.getType().cast<MemRefType>().getElementType()); 565 } 566 567 /// Constructs vector iteration mask. 568 static Value genVectorMask(CodeGen &codegen, OpBuilder &builder, Value iv, 569 Value lo, Value hi, Value step) { 570 Location loc = iv.getLoc(); 571 VectorType mtp = vectorType(codegen, builder.getI1Type()); 572 // Special case if the vector length evenly divides the trip count (for 573 // example, "for i = 0, 128, 16"). A constant all-true mask is generated 574 // so that all subsequent masked memory operations are immediately folded 575 // into unconditional memory operations. 576 IntegerAttr loInt, hiInt, stepInt; 577 if (matchPattern(lo, m_Constant(&loInt)) && 578 matchPattern(hi, m_Constant(&hiInt)) && 579 matchPattern(step, m_Constant(&stepInt))) { 580 if (((hiInt.getInt() - loInt.getInt()) % stepInt.getInt()) == 0) 581 return builder.create<vector::BroadcastOp>( 582 loc, mtp, constantI1(builder, loc, true)); 583 } 584 // Otherwise, generate a vector mask that avoids overrunning the upperbound 585 // during vector execution. Here we rely on subsequent loop optimizations to 586 // avoid executing the mask in all iterations, for example, by splitting the 587 // loop into an unconditional vector loop and a scalar cleanup loop. 588 auto minMap = AffineMap::get( 589 /*dimCount=*/2, /*symbolCount=*/1, 590 {builder.getAffineSymbolExpr(0), 591 builder.getAffineDimExpr(0) - builder.getAffineDimExpr(1)}, 592 builder.getContext()); 593 Value end = 594 builder.createOrFold<AffineMinOp>(loc, minMap, ValueRange{hi, iv, step}); 595 return builder.create<vector::CreateMaskOp>(loc, mtp, end); 596 } 597 598 /// Generates a vectorized load lhs = a[ind[lo:hi]] or lhs = a[lo:hi]. 599 static Value genVectorLoad(CodeGen &codegen, OpBuilder &builder, Value ptr, 600 ArrayRef<Value> args) { 601 Location loc = ptr.getLoc(); 602 VectorType vtp = vectorType(codegen, ptr); 603 Value pass = constantZero(builder, loc, vtp); 604 if (args.back().getType().isa<VectorType>()) { 605 SmallVector<Value, 4> scalarArgs(args.begin(), args.end()); 606 Value indexVec = args.back(); 607 scalarArgs.back() = constantIndex(builder, loc, 0); 608 return builder.create<vector::GatherOp>(loc, vtp, ptr, scalarArgs, indexVec, 609 codegen.curVecMask, pass); 610 } 611 return builder.create<vector::MaskedLoadOp>(loc, vtp, ptr, args, 612 codegen.curVecMask, pass); 613 } 614 615 /// Generates a vectorized store a[ind[lo:hi]] = rhs or a[lo:hi] = rhs. 616 static void genVectorStore(CodeGen &codegen, OpBuilder &builder, Value rhs, 617 Value ptr, ArrayRef<Value> args) { 618 Location loc = ptr.getLoc(); 619 if (args.back().getType().isa<VectorType>()) { 620 SmallVector<Value, 4> scalarArgs(args.begin(), args.end()); 621 Value indexVec = args.back(); 622 scalarArgs.back() = constantIndex(builder, loc, 0); 623 builder.create<vector::ScatterOp>(loc, ptr, scalarArgs, indexVec, 624 codegen.curVecMask, rhs); 625 return; 626 } 627 builder.create<vector::MaskedStoreOp>(loc, ptr, args, codegen.curVecMask, 628 rhs); 629 } 630 631 /// Generates a vectorized invariant. Here we rely on subsequent loop 632 /// optimizations to hoist the invariant broadcast out of the vector loop. 633 static Value genVectorInvariantValue(CodeGen &codegen, OpBuilder &builder, 634 Value val) { 635 VectorType vtp = vectorType(codegen, val.getType()); 636 return builder.create<vector::BroadcastOp>(val.getLoc(), vtp, val); 637 } 638 639 /// Generates an affine expression. 640 // 641 // TODO: generalize for sparse tensor subscripts 642 // 643 static Value genAffine(CodeGen &codegen, OpBuilder &builder, AffineExpr a, 644 Location loc) { 645 switch (a.getKind()) { 646 case AffineExprKind::DimId: { 647 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 648 return codegen.loops[idx]; // universal dense index 649 } 650 case AffineExprKind::Add: { 651 auto binOp = a.cast<AffineBinaryOpExpr>(); 652 return builder.create<arith::AddIOp>( 653 loc, genAffine(codegen, builder, binOp.getLHS(), loc), 654 genAffine(codegen, builder, binOp.getRHS(), loc)); 655 } 656 case AffineExprKind::Mul: { 657 auto binOp = a.cast<AffineBinaryOpExpr>(); 658 return builder.create<arith::MulIOp>( 659 loc, genAffine(codegen, builder, binOp.getLHS(), loc), 660 genAffine(codegen, builder, binOp.getRHS(), loc)); 661 } 662 case AffineExprKind::Constant: { 663 int64_t c = a.cast<AffineConstantExpr>().getValue(); 664 return constantIndex(builder, loc, c); 665 } 666 default: 667 llvm_unreachable("unexpected affine subscript"); 668 } 669 } 670 671 /// Generates index for load/store on sparse tensor. 672 static Value genIndex(CodeGen &codegen, linalg::GenericOp op, OpOperand *t) { 673 auto map = op.getTiedIndexingMap(t); 674 auto enc = getSparseTensorEncoding(t->get().getType()); 675 AffineExpr a = map.getResult(perm(enc, map.getNumResults() - 1)); 676 assert(a.getKind() == AffineExprKind::DimId); 677 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 678 return codegen.loops[idx]; 679 } 680 681 /// Generates subscript for load/store on a dense or sparse tensor. 682 static Value genSubscript(CodeGen &codegen, OpBuilder &builder, 683 linalg::GenericOp op, OpOperand *t, 684 SmallVector<Value, 4> &args) { 685 unsigned tensor = t->getOperandNumber(); 686 auto map = op.getTiedIndexingMap(t); 687 auto enc = getSparseTensorEncoding(t->get().getType()); 688 unsigned rank = map.getNumResults(); 689 if (enc) { 690 // Note that currently, all sparse subscripts are simple. 691 // TODO: accept affine too? 692 AffineExpr a = map.getResult(perm(enc, rank - 1)); 693 assert(a.getKind() == AffineExprKind::DimId); 694 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 695 assert(codegen.pidxs[tensor][idx] != nullptr); 696 args.push_back(codegen.pidxs[tensor][idx]); // position index 697 } else { 698 for (unsigned d = 0; d < rank; d++) { 699 AffineExpr a = map.getResult(perm(enc, d)); 700 args.push_back(genAffine(codegen, builder, a, op.getLoc())); 701 } 702 } 703 return codegen.buffers[tensor]; 704 } 705 706 /// Generates insertion code to implement dynamic tensor load. 707 static Value genInsertionLoad(CodeGen &codegen, OpBuilder &builder, 708 linalg::GenericOp op, OpOperand *t) { 709 Location loc = op.getLoc(); 710 // Direct lexicographic index order, tensor loads as zero. 711 if (!codegen.expValues) { 712 Type tp = getElementTypeOrSelf(t->get().getType()); 713 return constantZero(builder, loc, tp); 714 } 715 // Load from expanded access pattern. 716 Value index = genIndex(codegen, op, t); 717 return builder.create<memref::LoadOp>(loc, codegen.expValues, index); 718 } 719 720 /// Generates insertion code to implement dynamic tensor store. 721 static void genInsertionStore(CodeGen &codegen, OpBuilder &builder, 722 linalg::GenericOp op, OpOperand *t, Value rhs) { 723 Location loc = op.getLoc(); 724 // Direct insertion in lexicographic index order. 725 if (!codegen.expValues) { 726 builder.create<LexInsertOp>(loc, t->get(), codegen.lexIdx, rhs); 727 return; 728 } 729 // Generates insertion code along expanded access pattern. 730 // if (!expFilled[i]) then 731 // expFilled[i] = true 732 // expAdded[inserts++] = i 733 // endif 734 // values[i] = rhs 735 Value index = genIndex(codegen, op, t); 736 Value fval = constantI1(builder, loc, false); 737 Value tval = constantI1(builder, loc, true); 738 // If statement. 739 Value filled = builder.create<memref::LoadOp>(loc, codegen.expFilled, index); 740 Value cond = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq, 741 filled, fval); 742 scf::IfOp ifOp = builder.create<scf::IfOp>(loc, builder.getIndexType(), cond, 743 /*else=*/true); 744 // True branch. 745 builder.setInsertionPointToStart(&ifOp.getThenRegion().front()); 746 builder.create<memref::StoreOp>(loc, tval, codegen.expFilled, index); 747 builder.create<memref::StoreOp>(loc, index, codegen.expAdded, 748 codegen.expCount); 749 Value one = constantIndex(builder, loc, 1); 750 Value add = builder.create<arith::AddIOp>(loc, codegen.expCount, one); 751 builder.create<scf::YieldOp>(loc, add); 752 // False branch. 753 builder.setInsertionPointToStart(&ifOp.getElseRegion().front()); 754 builder.create<scf::YieldOp>(loc, codegen.expCount); 755 builder.setInsertionPointAfter(ifOp); 756 // Value assignment. 757 codegen.expCount = ifOp.getResult(0); 758 builder.create<memref::StoreOp>(loc, rhs, codegen.expValues, index); 759 } 760 761 /// Generates a load on a dense or sparse tensor. 762 static Value genTensorLoad(Merger &merger, CodeGen &codegen, OpBuilder &builder, 763 linalg::GenericOp op, unsigned exp) { 764 // Test if the load was hoisted to a higher loop nest. 765 Value val = merger.exp(exp).val; 766 if (val) { 767 if (codegen.curVecLength > 1 && !val.getType().isa<VectorType>()) 768 return genVectorInvariantValue(codegen, builder, val); 769 return val; 770 } 771 // Load during insertion. 772 OpOperand *t = op.getInputAndOutputOperands()[merger.exp(exp).tensor]; 773 if (t == codegen.sparseOut) 774 return genInsertionLoad(codegen, builder, op, t); 775 // Actual load. 776 SmallVector<Value, 4> args; 777 Value ptr = genSubscript(codegen, builder, op, t, args); 778 if (codegen.curVecLength > 1) 779 return genVectorLoad(codegen, builder, ptr, args); 780 return builder.create<memref::LoadOp>(op.getLoc(), ptr, args); 781 } 782 783 /// Generates a store on a dense or sparse tensor. 784 static void genTensorStore(Merger &merger, CodeGen &codegen, OpBuilder &builder, 785 linalg::GenericOp op, unsigned exp, Value rhs) { 786 Location loc = op.getLoc(); 787 // Test if this is a scalarized reduction. 788 if (codegen.redVal) { 789 if (codegen.curVecLength > 1) 790 rhs = builder.create<arith::SelectOp>(loc, codegen.curVecMask, rhs, 791 codegen.redVal); 792 updateReduc(merger, codegen, rhs); 793 return; 794 } 795 // Store during insertion. 796 OpOperand *t = op.getOutputOperand(0); 797 if (t == codegen.sparseOut) { 798 if (!rhs) { 799 // Only unary and binary are allowed to return uninitialized rhs 800 // to indicate missing output. 801 assert(merger.exp(exp).kind == kUnary || merger.exp(exp).kind == kBinary); 802 } else { 803 genInsertionStore(codegen, builder, op, t, rhs); 804 } 805 return; 806 } 807 // Actual store. 808 SmallVector<Value, 4> args; 809 Value ptr = genSubscript(codegen, builder, op, t, args); 810 if (codegen.curVecLength > 1) 811 genVectorStore(codegen, builder, rhs, ptr, args); 812 else 813 builder.create<memref::StoreOp>(loc, rhs, ptr, args); 814 } 815 816 /// Generates a pointer/index load from the sparse storage scheme. Narrower 817 /// data types need to be zero extended before casting the value into the 818 /// index type used for looping and indexing. 819 static Value genLoad(CodeGen &codegen, OpBuilder &builder, Location loc, 820 Value ptr, Value s) { 821 // See https://llvm.org/docs/GetElementPtr.html for some background on 822 // the complications described below. 823 if (codegen.curVecLength > 1) { 824 // Since the index vector is used in a subsequent gather/scatter operations, 825 // which effectively defines an unsigned pointer + signed index, we must 826 // zero extend the vector to an index width. For 8-bit and 16-bit values, 827 // an 32-bit index width suffices. For 32-bit values, zero extending the 828 // elements into 64-bit loses some performance since the 32-bit indexed 829 // gather/scatter is more efficient than the 64-bit index variant (if the 830 // negative 32-bit index space is unused, the enableSIMDIndex32 flag can 831 // preserve this performance). For 64-bit values, there is no good way 832 // to state that the indices are unsigned, with creates the potential of 833 // incorrect address calculations in the unlikely case we need such 834 // extremely large offsets. 835 Type etp = ptr.getType().cast<MemRefType>().getElementType(); 836 Value vload = genVectorLoad(codegen, builder, ptr, {s}); 837 if (!etp.isa<IndexType>()) { 838 if (etp.getIntOrFloatBitWidth() < 32) 839 vload = builder.create<arith::ExtUIOp>( 840 loc, vectorType(codegen, builder.getI32Type()), vload); 841 else if (etp.getIntOrFloatBitWidth() < 64 && 842 !codegen.options.enableSIMDIndex32) 843 vload = builder.create<arith::ExtUIOp>( 844 loc, vectorType(codegen, builder.getI64Type()), vload); 845 } 846 return vload; 847 } 848 // For the scalar case, we simply zero extend narrower indices into 64-bit 849 // values before casting to index without a performance penalty. Here too, 850 // however, indices that already are 64-bit, in theory, cannot express the 851 // full range as explained above. 852 Value load = builder.create<memref::LoadOp>(loc, ptr, s); 853 if (!load.getType().isa<IndexType>()) { 854 if (load.getType().getIntOrFloatBitWidth() < 64) 855 load = builder.create<arith::ExtUIOp>(loc, builder.getI64Type(), load); 856 load = 857 builder.create<arith::IndexCastOp>(loc, builder.getIndexType(), load); 858 } 859 return load; 860 } 861 862 /// Generates an invariant value. 863 static Value genInvariantValue(Merger &merger, CodeGen &codegen, 864 OpBuilder &builder, unsigned exp) { 865 Value val = merger.exp(exp).val; 866 if (codegen.curVecLength > 1) 867 return genVectorInvariantValue(codegen, builder, val); 868 return val; 869 } 870 871 /// Generates an address computation "sz * p + i". 872 static Value genAddress(CodeGen &codegen, OpBuilder &builder, Location loc, 873 Value size, Value p, Value i) { 874 Value mul = builder.create<arith::MulIOp>(loc, size, p); 875 if (auto vtp = i.getType().dyn_cast<VectorType>()) { 876 Value inv = 877 builder.create<arith::IndexCastOp>(loc, vtp.getElementType(), mul); 878 mul = genVectorInvariantValue(codegen, builder, inv); 879 } 880 return builder.create<arith::AddIOp>(loc, mul, i); 881 } 882 883 /// Generates an index value. 884 static Value genIndexValue(Merger &merger, CodeGen &codegen, OpBuilder &builder, 885 unsigned exp, unsigned ldx) { 886 unsigned idx = merger.exp(exp).index; 887 Value ival = codegen.loops[idx]; 888 Type itype = ival.getType(); 889 // During vectorization, we either encounter: 890 // (1) indices already in vector form, as in ... = ind[lo:hi], good to go, or 891 // (2) single index, as in ... = i, must convert to [i, i+1, ...] for inner i. 892 unsigned vl = codegen.curVecLength; 893 if (vl > 1 && !itype.isa<VectorType>()) { 894 Location loc = ival.getLoc(); 895 VectorType vtp = vectorType(codegen, itype); 896 ival = builder.create<vector::BroadcastOp>(loc, vtp, ival); 897 if (idx == ldx) { 898 Value incr; 899 if (vtp.isScalable()) { 900 Type stepvty = vectorType(codegen, builder.getI64Type()); 901 Value stepv = builder.create<LLVM::StepVectorOp>(loc, stepvty); 902 incr = builder.create<arith::IndexCastOp>(loc, vtp, stepv); 903 } else { 904 SmallVector<APInt, 4> integers; 905 for (unsigned i = 0; i < vl; i++) 906 integers.push_back(APInt(/*width=*/64, i)); 907 auto values = DenseElementsAttr::get(vtp, integers); 908 incr = builder.create<arith::ConstantOp>(loc, vtp, values); 909 } 910 ival = builder.create<arith::AddIOp>(loc, ival, incr); 911 } 912 } 913 return ival; 914 } 915 916 /// Recursively generates tensor expression. 917 static Value genExp(Merger &merger, CodeGen &codegen, RewriterBase &rewriter, 918 linalg::GenericOp op, unsigned exp, unsigned ldx) { 919 Location loc = op.getLoc(); 920 if (exp == -1u) 921 return Value(); 922 if (merger.exp(exp).kind == Kind::kTensor) 923 return genTensorLoad(merger, codegen, rewriter, op, exp); 924 if (merger.exp(exp).kind == Kind::kInvariant) 925 return genInvariantValue(merger, codegen, rewriter, exp); 926 if (merger.exp(exp).kind == Kind::kIndex) 927 return genIndexValue(merger, codegen, rewriter, exp, ldx); 928 Value v0 = 929 genExp(merger, codegen, rewriter, op, merger.exp(exp).children.e0, ldx); 930 Value v1 = 931 genExp(merger, codegen, rewriter, op, merger.exp(exp).children.e1, ldx); 932 return merger.buildExp(rewriter, loc, exp, v0, v1); 933 } 934 935 /// Determines if affine expression is invariant. 936 static bool isInvariantAffine(const CodeGen &codegen, AffineExpr a, 937 unsigned ldx, bool &atLevel) { 938 switch (a.getKind()) { 939 case AffineExprKind::DimId: { 940 unsigned idx = a.cast<AffineDimExpr>().getPosition(); 941 if (idx == ldx) 942 atLevel = true; 943 return codegen.loops[idx] != nullptr; // no longer in play? 944 } 945 case AffineExprKind::Add: 946 case AffineExprKind::Mul: { 947 auto binOp = a.cast<AffineBinaryOpExpr>(); 948 return isInvariantAffine(codegen, binOp.getLHS(), ldx, atLevel) && 949 isInvariantAffine(codegen, binOp.getRHS(), ldx, atLevel); 950 } 951 default: 952 return true; 953 } 954 } 955 956 /// Hoists loop invariant tensor loads for which indices have been exhausted. 957 static void genInvariants(Merger &merger, CodeGen &codegen, OpBuilder &builder, 958 linalg::GenericOp op, unsigned exp, unsigned ldx, 959 bool atStart, Kind last = Kind::kTensor) { 960 if (exp == -1u) 961 return; 962 if (merger.exp(exp).kind == Kind::kTensor) { 963 // Inspect tensor indices. 964 bool atLevel = ldx == -1u; 965 OpOperand *t = op.getInputAndOutputOperands()[merger.exp(exp).tensor]; 966 auto map = op.getTiedIndexingMap(t); 967 auto enc = getSparseTensorEncoding(t->get().getType()); 968 for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) { 969 AffineExpr a = map.getResult(perm(enc, d)); 970 if (!isInvariantAffine(codegen, a, ldx, atLevel)) 971 return; // still in play 972 } 973 // All exhausted at this level (atLevel denotes exactly at this level). 974 if (!atLevel) 975 return; 976 OpOperand *lhs = op.getOutputOperand(0); 977 if (lhs == t) { 978 // Start or end a scalarized reduction 979 if (atStart) { 980 Value load = genTensorLoad(merger, codegen, builder, op, exp); 981 codegen.redKind = getReduction(last); 982 codegen.redExp = exp; 983 updateReduc(merger, codegen, load); 984 } else { 985 Value redVal = codegen.redVal; 986 updateReduc(merger, codegen, Value()); 987 codegen.redExp = -1u; 988 codegen.redKind = kNoReduc; 989 genTensorStore(merger, codegen, builder, op, exp, redVal); 990 } 991 } else { 992 // Start or end loop invariant hoisting of a tensor load. 993 merger.exp(exp).val = 994 atStart ? genTensorLoad(merger, codegen, builder, op, exp) : Value(); 995 } 996 } else if (merger.exp(exp).kind != Kind::kInvariant && 997 merger.exp(exp).kind != Kind::kIndex) { 998 // Traverse into the binary operations. Note that we only hoist 999 // tensor loads, since subsequent MLIR/LLVM passes know how to 1000 // deal with all other kinds of derived loop invariants. 1001 Kind last = merger.exp(exp).kind; 1002 unsigned e0 = merger.exp(exp).children.e0; 1003 unsigned e1 = merger.exp(exp).children.e1; 1004 genInvariants(merger, codegen, builder, op, e0, ldx, atStart, last); 1005 genInvariants(merger, codegen, builder, op, e1, ldx, atStart, last); 1006 } 1007 } 1008 1009 /// Generates an expanded access pattern in innermost dimension. 1010 static void genExpansion(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1011 linalg::GenericOp op, unsigned at, bool atStart) { 1012 OpOperand *lhs = codegen.sparseOut; 1013 if (!lhs || codegen.outerParNest != op.getRank(lhs) - 1 || 1014 at != codegen.outerParNest) 1015 return; // not needed at this level 1016 // Generate start or end of an expanded access pattern. 1017 Value tensor = lhs->get(); 1018 Location loc = op.getLoc(); 1019 if (atStart) { 1020 auto dynShape = {ShapedType::kDynamicSize}; 1021 Type etp = tensor.getType().cast<ShapedType>().getElementType(); 1022 Type t1 = MemRefType::get(dynShape, etp); 1023 Type t2 = MemRefType::get(dynShape, builder.getI1Type()); 1024 Type t3 = MemRefType::get(dynShape, builder.getIndexType()); 1025 Type t4 = builder.getIndexType(); 1026 auto res = 1027 builder.create<ExpandOp>(loc, TypeRange({t1, t2, t3, t4}), tensor); 1028 assert(res.getNumResults() == 4); 1029 assert(!codegen.expValues); 1030 codegen.expValues = res.getResult(0); 1031 codegen.expFilled = res.getResult(1); 1032 codegen.expAdded = res.getResult(2); 1033 codegen.expCount = res.getResult(3); 1034 } else { 1035 assert(codegen.expValues); 1036 builder.create<CompressOp>(loc, tensor, codegen.lexIdx, codegen.expValues, 1037 codegen.expFilled, codegen.expAdded, 1038 codegen.expCount); 1039 codegen.expValues = codegen.expFilled = codegen.expAdded = 1040 codegen.expCount = Value(); 1041 } 1042 } 1043 1044 /// Generates initialization code for the subsequent loop sequence at 1045 /// current index level. Returns true if the loop sequence needs to 1046 /// maintain the universal index. 1047 static bool genInit(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1048 linalg::GenericOp op, std::vector<unsigned> &topSort, 1049 unsigned at, BitVector &inits) { 1050 bool needsUniv = false; 1051 Location loc = op.getLoc(); 1052 unsigned idx = topSort[at]; 1053 1054 // Initialize sparse positions. 1055 for (unsigned b = 0, be = inits.size(); b < be; b++) { 1056 if (inits[b]) { 1057 unsigned tensor = merger.tensor(b); 1058 assert(idx == merger.index(b)); 1059 if (merger.isDim(b, Dim::kSparse)) { 1060 // Initialize sparse index. 1061 unsigned pat = at; 1062 for (; pat != 0; pat--) { 1063 if (codegen.pidxs[tensor][topSort[pat - 1]]) 1064 break; 1065 } 1066 Value ptr = codegen.pointers[tensor][idx]; 1067 Value one = constantIndex(builder, loc, 1); 1068 Value p0 = (pat == 0) ? constantIndex(builder, loc, 0) 1069 : codegen.pidxs[tensor][topSort[pat - 1]]; 1070 codegen.pidxs[tensor][idx] = genLoad(codegen, builder, loc, ptr, p0); 1071 Value p1 = builder.create<arith::AddIOp>(loc, p0, one); 1072 codegen.highs[tensor][idx] = genLoad(codegen, builder, loc, ptr, p1); 1073 } else { 1074 // Dense index still in play. 1075 needsUniv = true; 1076 } 1077 } 1078 } 1079 1080 // Initialize the universal dense index. 1081 codegen.loops[idx] = constantIndex(builder, loc, 0); 1082 return needsUniv; 1083 } 1084 1085 /// Returns vectorization strategy. Any implicit inner loop in the Linalg 1086 /// operation is a candidate. Whether it is actually converted to SIMD code 1087 /// depends on the requested strategy. 1088 static bool isVectorFor(CodeGen &codegen, bool isInner, bool isReduction, 1089 bool isSparse) { 1090 // Reject vectorization of sparse output, unless innermost is reduction. 1091 if (codegen.sparseOut && !isReduction) 1092 return false; 1093 // Inspect strategy. 1094 switch (codegen.options.vectorizationStrategy) { 1095 case SparseVectorizationStrategy::kNone: 1096 return false; 1097 case SparseVectorizationStrategy::kDenseInnerLoop: 1098 return isInner && !isSparse; 1099 case SparseVectorizationStrategy::kAnyStorageInnerLoop: 1100 return isInner; 1101 } 1102 llvm_unreachable("unexpected vectorization strategy"); 1103 } 1104 1105 /// Returns parallelization strategy. Any implicit loop in the Linalg operation 1106 /// that is marked "parallel" is a candidate. Whether it is actually converted 1107 /// to a parallel operation depends on the requested strategy. 1108 static bool isParallelFor(CodeGen &codegen, bool isOuter, bool isReduction, 1109 bool isSparse, bool isVector) { 1110 // Reject parallelization of sparse output. 1111 if (codegen.sparseOut) 1112 return false; 1113 // Inspect strategy. 1114 switch (codegen.options.parallelizationStrategy) { 1115 case SparseParallelizationStrategy::kNone: 1116 return false; 1117 case SparseParallelizationStrategy::kDenseOuterLoop: 1118 return isOuter && !isSparse && !isReduction && !isVector; 1119 case SparseParallelizationStrategy::kAnyStorageOuterLoop: 1120 return isOuter && !isReduction && !isVector; 1121 case SparseParallelizationStrategy::kDenseAnyLoop: 1122 return !isSparse && !isReduction && !isVector; 1123 case SparseParallelizationStrategy::kAnyStorageAnyLoop: 1124 return !isReduction && !isVector; 1125 } 1126 llvm_unreachable("unexpected parallelization strategy"); 1127 } 1128 1129 /// Checks unit stride for dense tensors. The iteration graph may have ignored 1130 /// dense access patterns in order to avoid cycles (sparse access patterns are 1131 /// always placed innermost), but that means dense access has become strided. 1132 /// This prevents effective vectorization. 1133 static bool denseUnitStrides(Merger &merger, linalg::GenericOp op, 1134 unsigned idx) { 1135 for (OpOperand *t : op.getInputAndOutputOperands()) { 1136 if (!getSparseTensorEncoding(t->get().getType())) { 1137 auto map = op.getTiedIndexingMap(t); 1138 for (unsigned d = 0, rank = map.getNumResults(); d < rank; d++) { 1139 AffineExpr a = map.getResult(d); 1140 // Report non-unit stride if innermost index appears at an outer 1141 // dimension (true non-unit stride) or if the innermost index appears 1142 // in a compound subscript in the innermost dimension. Even if the 1143 // latter is unit stride, it does not play well with scatter/gather. 1144 // TODO: accept unit stride affine innermost like a[i,j+k+1]? 1145 if (a.isFunctionOfDim(idx) && 1146 ((d != rank - 1) || (a.getKind() != AffineExprKind::DimId))) 1147 return false; 1148 } 1149 } 1150 } 1151 return true; 1152 } 1153 1154 /// Generates a for-loop on a single index. 1155 static Operation *genFor(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1156 linalg::GenericOp op, bool isOuter, bool isInner, 1157 unsigned idx, BitVector &indices) { 1158 unsigned fb = indices.find_first(); 1159 unsigned tensor = merger.tensor(fb); 1160 assert(idx == merger.index(fb)); 1161 auto iteratorTypes = op.iterator_types().getValue(); 1162 bool isReduction = isReductionIterator(iteratorTypes[idx]); 1163 bool isSparse = merger.isDim(fb, Dim::kSparse); 1164 bool isVector = isVectorFor(codegen, isInner, isReduction, isSparse) && 1165 denseUnitStrides(merger, op, idx); 1166 bool isParallel = 1167 isParallelFor(codegen, isOuter, isReduction, isSparse, isVector); 1168 1169 // Prepare vector length. 1170 if (isVector) 1171 codegen.curVecLength = codegen.options.vectorLength; 1172 1173 // Loop bounds and increment. 1174 Location loc = op.getLoc(); 1175 Value lo = isSparse ? codegen.pidxs[tensor][idx] : codegen.loops[idx]; 1176 Value hi = isSparse ? codegen.highs[tensor][idx] : codegen.sizes[idx]; 1177 Value step = constantIndex(builder, loc, codegen.curVecLength); 1178 if (isVector && codegen.options.enableVLAVectorization) { 1179 Value vscale = builder.create<vector::VectorScaleOp>( 1180 loc, IndexType::get(builder.getContext())); 1181 step = builder.create<arith::MulIOp>(loc, vscale, step); 1182 } 1183 1184 // Emit a parallel loop. 1185 if (isParallel) { 1186 assert(!isVector); 1187 scf::ParallelOp parOp = builder.create<scf::ParallelOp>(loc, lo, hi, step); 1188 if (isSparse) 1189 codegen.pidxs[tensor][idx] = parOp.getInductionVars()[0]; 1190 else 1191 codegen.loops[idx] = parOp.getInductionVars()[0]; 1192 builder.setInsertionPointToStart(parOp.getBody()); 1193 return parOp; 1194 } 1195 1196 // Emit a sequential or vector loop. 1197 SmallVector<Value, 4> operands; 1198 if (codegen.redVal) { 1199 // In a vector loop, bring reduction into SIMD form, if not already. 1200 if (isVector && !codegen.redVal.getType().isa<VectorType>()) { 1201 VectorType vtp = vectorType(codegen, codegen.redVal.getType()); 1202 Value vred = genVectorReducInit(codegen, builder, loc, vtp); 1203 updateReduc(merger, codegen, vred); 1204 } 1205 operands.push_back(codegen.redVal); 1206 } 1207 if (codegen.expValues) 1208 operands.push_back(codegen.expCount); 1209 scf::ForOp forOp = builder.create<scf::ForOp>(loc, lo, hi, step, operands); 1210 if (codegen.redVal) 1211 updateReduc(merger, codegen, forOp.getRegionIterArgs().front()); 1212 if (codegen.expValues) 1213 codegen.expCount = forOp.getRegionIterArgs().back(); 1214 // Assign induction variable to sparse or dense index. 1215 Value iv = forOp.getInductionVar(); 1216 if (isSparse) 1217 codegen.pidxs[tensor][idx] = iv; 1218 else 1219 codegen.loops[idx] = iv; 1220 builder.setInsertionPointToStart(forOp.getBody()); 1221 // Share vector iteration mask between all subsequent loads/stores. 1222 if (isVector) 1223 codegen.curVecMask = genVectorMask(codegen, builder, iv, lo, hi, step); 1224 return forOp; 1225 } 1226 1227 /// Emit a while-loop for co-iteration over multiple indices. 1228 static Operation *genWhile(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1229 linalg::GenericOp op, unsigned idx, bool needsUniv, 1230 BitVector &indices) { 1231 SmallVector<Type, 4> types; 1232 SmallVector<Value, 4> operands; 1233 // Construct the while-loop with a parameter for each index. 1234 Type indexType = builder.getIndexType(); 1235 for (unsigned b = 0, be = indices.size(); b < be; b++) { 1236 if (indices[b] && merger.isDim(b, Dim::kSparse)) { 1237 unsigned tensor = merger.tensor(b); 1238 assert(idx == merger.index(b)); 1239 types.push_back(indexType); 1240 operands.push_back(codegen.pidxs[tensor][idx]); 1241 } 1242 } 1243 if (codegen.redVal) { 1244 types.push_back(codegen.redVal.getType()); 1245 operands.push_back(codegen.redVal); 1246 } 1247 if (codegen.expValues) { 1248 types.push_back(indexType); 1249 operands.push_back(codegen.expCount); 1250 } 1251 if (needsUniv) { 1252 types.push_back(indexType); 1253 operands.push_back(codegen.loops[idx]); 1254 } 1255 assert(types.size() == operands.size()); 1256 Location loc = op.getLoc(); 1257 scf::WhileOp whileOp = builder.create<scf::WhileOp>(loc, types, operands); 1258 1259 SmallVector<Location> locs(types.size(), loc); 1260 Block *before = builder.createBlock(&whileOp.getBefore(), {}, types, locs); 1261 Block *after = builder.createBlock(&whileOp.getAfter(), {}, types, locs); 1262 1263 // Build the "before" region, which effectively consists 1264 // of a conjunction of "i < upper" tests on all induction. 1265 builder.setInsertionPointToStart(&whileOp.getBefore().front()); 1266 Value cond; 1267 unsigned o = 0; 1268 for (unsigned b = 0, be = indices.size(); b < be; b++) { 1269 if (indices[b] && merger.isDim(b, Dim::kSparse)) { 1270 unsigned tensor = merger.tensor(b); 1271 assert(idx == merger.index(b)); 1272 Value op1 = before->getArgument(o); 1273 Value op2 = codegen.highs[tensor][idx]; 1274 Value opc = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::ult, 1275 op1, op2); 1276 cond = cond ? builder.create<arith::AndIOp>(loc, cond, opc) : opc; 1277 codegen.pidxs[tensor][idx] = after->getArgument(o++); 1278 } 1279 } 1280 if (codegen.redVal) 1281 updateReduc(merger, codegen, after->getArgument(o++)); 1282 if (codegen.expValues) 1283 codegen.expCount = after->getArgument(o++); 1284 if (needsUniv) 1285 codegen.loops[idx] = after->getArgument(o++); 1286 assert(o == operands.size()); 1287 builder.create<scf::ConditionOp>(loc, cond, before->getArguments()); 1288 builder.setInsertionPointToStart(&whileOp.getAfter().front()); 1289 return whileOp; 1290 } 1291 1292 /// Generates a for-loop or a while-loop, depending on whether it implements 1293 /// singleton iteration or co-iteration over the given conjunction. 1294 static Operation *genLoop(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1295 linalg::GenericOp op, std::vector<unsigned> &topSort, 1296 unsigned at, bool needsUniv, BitVector &indices) { 1297 unsigned idx = topSort[at]; 1298 if (indices.count() == 1) { 1299 bool isOuter = at == 0; 1300 bool isInner = at == topSort.size() - 1; 1301 return genFor(merger, codegen, builder, op, isOuter, isInner, idx, indices); 1302 } 1303 return genWhile(merger, codegen, builder, op, idx, needsUniv, indices); 1304 } 1305 1306 /// Generates the local variables for this loop, consisting of the sparse 1307 /// indices, restored universal dense index, and dense positions. 1308 static void genLocals(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1309 linalg::GenericOp op, std::vector<unsigned> &topSort, 1310 unsigned at, bool needsUniv, BitVector &locals) { 1311 Location loc = op.getLoc(); 1312 unsigned idx = topSort[at]; 1313 1314 // Initialize sparse indices. 1315 Value min; 1316 for (unsigned b = 0, be = locals.size(); b < be; b++) { 1317 if (locals[b] && merger.isDim(b, Dim::kSparse)) { 1318 unsigned tensor = merger.tensor(b); 1319 assert(idx == merger.index(b)); 1320 Value ptr = codegen.indices[tensor][idx]; 1321 Value s = codegen.pidxs[tensor][idx]; 1322 Value load = genLoad(codegen, builder, loc, ptr, s); 1323 codegen.idxs[tensor][idx] = load; 1324 if (!needsUniv) { 1325 if (min) { 1326 Value cmp = builder.create<arith::CmpIOp>( 1327 loc, arith::CmpIPredicate::ult, load, min); 1328 min = builder.create<arith::SelectOp>(loc, cmp, load, min); 1329 } else { 1330 min = load; 1331 } 1332 } 1333 } 1334 } 1335 1336 // Merge dense universal index over minimum. 1337 if (min) { 1338 assert(!needsUniv); 1339 codegen.loops[idx] = min; 1340 } 1341 1342 // Initialize dense positions. Note that we generate dense indices of the 1343 // output tensor unconditionally, since they may not appear in the lattice, 1344 // but may be needed for linearized codegen. 1345 for (unsigned b = 0, be = locals.size(); b < be; b++) { 1346 if ((locals[b] || merger.isOutTensor(b, idx)) && 1347 merger.isDim(b, Dim::kDense)) { 1348 unsigned tensor = merger.tensor(b); 1349 assert(idx == merger.index(b)); 1350 unsigned pat = at; 1351 for (; pat != 0; pat--) 1352 if (codegen.pidxs[tensor][topSort[pat - 1]]) 1353 break; 1354 Value p = (pat == 0) ? constantIndex(builder, loc, 0) 1355 : codegen.pidxs[tensor][topSort[pat - 1]]; 1356 codegen.pidxs[tensor][idx] = genAddress( 1357 codegen, builder, loc, codegen.sizes[idx], p, codegen.loops[idx]); 1358 } 1359 } 1360 1361 // Move the insertion indices in lexicographic index order. During access 1362 // pattern expansion, we can skip setting the innermost dimension. 1363 if (codegen.sparseOut && !codegen.expValues) { 1364 Value pos = constantIndex(builder, loc, at); 1365 builder.create<memref::StoreOp>(loc, codegen.loops[idx], codegen.lexIdx, 1366 pos); 1367 } 1368 } 1369 1370 /// Generates the induction structure for a while-loop. 1371 static void genWhileInduction(Merger &merger, CodeGen &codegen, 1372 OpBuilder &builder, linalg::GenericOp op, 1373 unsigned idx, bool needsUniv, 1374 BitVector &induction, scf::WhileOp whileOp) { 1375 Location loc = op.getLoc(); 1376 // Finalize each else branch of all if statements. 1377 if (codegen.redVal || codegen.expValues) { 1378 while (auto ifOp = dyn_cast_or_null<scf::IfOp>( 1379 builder.getInsertionBlock()->getParentOp())) { 1380 unsigned y = 0; 1381 SmallVector<Value, 4> yields; 1382 if (codegen.redVal) { 1383 yields.push_back(codegen.redVal); 1384 updateReduc(merger, codegen, ifOp.getResult(y++)); 1385 } 1386 if (codegen.expValues) { 1387 yields.push_back(codegen.expCount); 1388 codegen.expCount = ifOp->getResult(y++); 1389 } 1390 assert(y == yields.size()); 1391 builder.create<scf::YieldOp>(loc, yields); 1392 builder.setInsertionPointAfter(ifOp); 1393 } 1394 } 1395 builder.setInsertionPointToEnd(&whileOp.getAfter().front()); 1396 // Finalize the induction. Note that the induction could be performed 1397 // in the individual if-branches to avoid re-evaluating the conditions. 1398 // However, that would result in a rather elaborate forest of yield 1399 // instructions during code generation. Moreover, performing the induction 1400 // after the if-statements more closely resembles code generated by TACO. 1401 unsigned o = 0; 1402 SmallVector<Value, 4> operands; 1403 Value one = constantIndex(builder, loc, 1); 1404 for (unsigned b = 0, be = induction.size(); b < be; b++) { 1405 if (induction[b] && merger.isDim(b, Dim::kSparse)) { 1406 unsigned tensor = merger.tensor(b); 1407 assert(idx == merger.index(b)); 1408 Value op1 = codegen.idxs[tensor][idx]; 1409 Value op2 = codegen.loops[idx]; 1410 Value op3 = codegen.pidxs[tensor][idx]; 1411 Value cmp = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq, 1412 op1, op2); 1413 Value add = builder.create<arith::AddIOp>(loc, op3, one); 1414 operands.push_back(builder.create<arith::SelectOp>(loc, cmp, add, op3)); 1415 codegen.pidxs[tensor][idx] = whileOp->getResult(o++); 1416 } 1417 } 1418 if (codegen.redVal) { 1419 operands.push_back(codegen.redVal); 1420 updateReduc(merger, codegen, whileOp->getResult(o++)); 1421 } 1422 if (codegen.expValues) { 1423 operands.push_back(codegen.expCount); 1424 codegen.expCount = whileOp->getResult(o++); 1425 } 1426 if (needsUniv) { 1427 operands.push_back( 1428 builder.create<arith::AddIOp>(loc, codegen.loops[idx], one)); 1429 codegen.loops[idx] = whileOp->getResult(o++); 1430 } 1431 assert(o == operands.size()); 1432 builder.create<scf::YieldOp>(loc, operands); 1433 builder.setInsertionPointAfter(whileOp); 1434 } 1435 1436 /// Generates the induction structure for a for-loop. 1437 static void genForInduction(Merger &merger, CodeGen &codegen, 1438 OpBuilder &builder, linalg::GenericOp op, 1439 Operation *loop) { 1440 Location loc = op.getLoc(); 1441 unsigned o = 0; 1442 SmallVector<Value, 4> operands; 1443 if (codegen.redVal) { 1444 operands.push_back(codegen.redVal); 1445 updateReduc(merger, codegen, loop->getResult(o++)); 1446 } 1447 if (codegen.expValues) { 1448 operands.push_back(codegen.expCount); 1449 codegen.expCount = loop->getResult(o++); 1450 } 1451 assert(o == operands.size()); 1452 if (o > 0) 1453 builder.create<scf::YieldOp>(loc, operands); 1454 builder.setInsertionPointAfter(loop); 1455 } 1456 1457 /// Generates a single if-statement within a while-loop. 1458 static scf::IfOp genIf(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1459 linalg::GenericOp op, unsigned idx, 1460 BitVector &conditions) { 1461 Location loc = op.getLoc(); 1462 SmallVector<Type, 4> types; 1463 Value cond; 1464 for (unsigned b = 0, be = conditions.size(); b < be; b++) { 1465 if (conditions[b]) { 1466 unsigned tensor = merger.tensor(b); 1467 assert(idx == merger.index(b)); 1468 Value clause; 1469 if (merger.isDim(b, Dim::kSparse)) { 1470 Value op1 = codegen.idxs[tensor][idx]; 1471 Value op2 = codegen.loops[idx]; 1472 clause = builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq, 1473 op1, op2); 1474 } else { 1475 clause = constantI1(builder, loc, true); 1476 } 1477 cond = cond ? builder.create<arith::AndIOp>(loc, cond, clause) : clause; 1478 } 1479 } 1480 if (codegen.redVal) 1481 types.push_back(codegen.redVal.getType()); 1482 if (codegen.expValues) 1483 types.push_back(builder.getIndexType()); 1484 scf::IfOp ifOp = builder.create<scf::IfOp>(loc, types, cond, /*else=*/true); 1485 builder.setInsertionPointToStart(&ifOp.getThenRegion().front()); 1486 return ifOp; 1487 } 1488 1489 /// Generates end of true branch of if-statement within a while-loop. 1490 static void endIf(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1491 linalg::GenericOp op, scf::IfOp ifOp, Operation *loop, 1492 Value redInput, Value cntInput) { 1493 SmallVector<Value, 4> operands; 1494 if (codegen.redVal) { 1495 operands.push_back(codegen.redVal); 1496 updateReduc(merger, codegen, redInput); 1497 } 1498 if (codegen.expValues) { 1499 operands.push_back(codegen.expCount); 1500 codegen.expCount = cntInput; 1501 } 1502 if (!operands.empty()) 1503 builder.create<scf::YieldOp>(op.getLoc(), operands); 1504 builder.setInsertionPointToStart(&ifOp.getElseRegion().front()); 1505 } 1506 1507 //===----------------------------------------------------------------------===// 1508 // Sparse compiler synthesis methods (loop sequence). 1509 //===----------------------------------------------------------------------===// 1510 1511 /// Starts a loop sequence at given level. Returns true if 1512 /// the universal loop index must be maintained at this level. 1513 static bool startLoopSeq(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1514 linalg::GenericOp op, std::vector<unsigned> &topSort, 1515 unsigned exp, unsigned at, unsigned idx, unsigned ldx, 1516 unsigned lts) { 1517 assert(codegen.curVecLength == 1); 1518 assert(!codegen.loops[idx]); 1519 // Emit invariants at this loop sequence level. 1520 genInvariants(merger, codegen, builder, op, exp, ldx, /*atStart=*/true); 1521 // Emit access pattern expansion for sparse tensor output. 1522 genExpansion(merger, codegen, builder, op, at, /*atStart=*/true); 1523 // Emit further intitialization at this loop sequence level. 1524 unsigned l0 = merger.set(lts)[0]; 1525 bool needsUniv = 1526 genInit(merger, codegen, builder, op, topSort, at, merger.lat(l0).bits); 1527 // Maintain the universal index only if it is actually 1528 // consumed by a subsequent lattice point. 1529 if (needsUniv) { 1530 unsigned lsize = merger.set(lts).size(); 1531 for (unsigned i = 1; i < lsize; i++) { 1532 unsigned li = merger.set(lts)[i]; 1533 if (!merger.hasAnyDimOf(merger.lat(li).simple, Dim::kSparse)) 1534 return true; 1535 } 1536 } 1537 return false; 1538 } 1539 1540 /// Starts a single loop in current sequence. 1541 static Operation *startLoop(Merger &merger, CodeGen &codegen, 1542 OpBuilder &builder, linalg::GenericOp op, 1543 std::vector<unsigned> &topSort, unsigned at, 1544 unsigned li, bool needsUniv) { 1545 assert(codegen.curVecLength == 1); 1546 // Emit the for/while-loop control. 1547 Operation *loop = genLoop(merger, codegen, builder, op, topSort, at, 1548 needsUniv, merger.lat(li).simple); 1549 // Emit the locals for this loop. 1550 genLocals(merger, codegen, builder, op, topSort, at, needsUniv, 1551 merger.lat(li).bits); 1552 return loop; 1553 } 1554 1555 /// Ends a single loop in current sequence. Returns new values for needsUniv. 1556 static bool endLoop(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1557 linalg::GenericOp op, Operation *loop, unsigned idx, 1558 unsigned li, bool needsUniv) { 1559 codegen.curVecLength = 1; 1560 // End a while-loop. 1561 if (auto whileOp = dyn_cast<scf::WhileOp>(loop)) { 1562 genWhileInduction(merger, codegen, builder, op, idx, needsUniv, 1563 merger.lat(li).bits, whileOp); 1564 return needsUniv; 1565 } 1566 // End a for-loop. 1567 genForInduction(merger, codegen, builder, op, loop); 1568 return false; 1569 } 1570 1571 /// Ends a loop sequence at given level. 1572 static void endLoopSeq(Merger &merger, CodeGen &codegen, OpBuilder &builder, 1573 linalg::GenericOp op, unsigned exp, unsigned at, 1574 unsigned idx, unsigned ldx) { 1575 assert(codegen.curVecLength == 1); 1576 codegen.loops[idx] = Value(); 1577 // Bring a pending reduction back from SIMD form when sequence ends. 1578 if (codegen.redVal) 1579 if (auto vtp = codegen.redVal.getType().dyn_cast<VectorType>()) 1580 updateReduc(merger, codegen, 1581 genVectorReducEnd(codegen, builder, op.getLoc(), vtp)); 1582 // Unmark bookkeeping of invariants and loop index. 1583 genInvariants(merger, codegen, builder, op, exp, ldx, /*atStart=*/false); 1584 // Finalize access pattern expansion for sparse tensor output. 1585 genExpansion(merger, codegen, builder, op, at, /*atStart=*/false); 1586 } 1587 1588 /// Recursively generates code while computing iteration lattices in order 1589 /// to manage the complexity of implementing co-iteration over unions 1590 /// and intersections of sparse iterations spaces. 1591 static void genStmt(Merger &merger, CodeGen &codegen, RewriterBase &rewriter, 1592 linalg::GenericOp op, std::vector<unsigned> &topSort, 1593 unsigned exp, unsigned at) { 1594 // At each leaf, assign remaining tensor (sub)expression to output tensor. 1595 if (at == topSort.size()) { 1596 unsigned ldx = topSort[at - 1]; 1597 Value rhs = genExp(merger, codegen, rewriter, op, exp, ldx); 1598 genTensorStore(merger, codegen, rewriter, op, exp, rhs); 1599 return; 1600 } 1601 1602 // Construct iteration lattices for current loop index, with L0 at top. 1603 unsigned idx = topSort[at]; 1604 unsigned ldx = at == 0 ? -1u : topSort[at - 1]; 1605 unsigned lts = merger.optimizeSet(merger.buildLattices(exp, idx)); 1606 1607 // Start a loop sequence. 1608 bool needsUniv = startLoopSeq(merger, codegen, rewriter, op, topSort, exp, at, 1609 idx, ldx, lts); 1610 1611 // Emit a loop for every lattice point L0 >= Li in this loop sequence. 1612 unsigned lsize = merger.set(lts).size(); 1613 for (unsigned i = 0; i < lsize; i++) { 1614 // Start a loop. 1615 unsigned li = merger.set(lts)[i]; 1616 Operation *loop = 1617 startLoop(merger, codegen, rewriter, op, topSort, at, li, needsUniv); 1618 1619 // Visit all lattices points with Li >= Lj to generate the 1620 // loop-body, possibly with if statements for coiteration. 1621 Value redInput = codegen.redVal; 1622 Value cntInput = codegen.expCount; 1623 bool isWhile = dyn_cast<scf::WhileOp>(loop) != nullptr; 1624 for (unsigned j = 0; j < lsize; j++) { 1625 unsigned lj = merger.set(lts)[j]; 1626 unsigned ej = merger.lat(lj).exp; 1627 if (li == lj || merger.latGT(li, lj)) { 1628 // Recurse into body of each branch. 1629 if (isWhile) { 1630 scf::IfOp ifOp = 1631 genIf(merger, codegen, rewriter, op, idx, merger.lat(lj).simple); 1632 genStmt(merger, codegen, rewriter, op, topSort, ej, at + 1); 1633 endIf(merger, codegen, rewriter, op, ifOp, loop, redInput, cntInput); 1634 } else { 1635 genStmt(merger, codegen, rewriter, op, topSort, ej, at + 1); 1636 } 1637 } 1638 } 1639 1640 // End a loop. 1641 needsUniv = 1642 endLoop(merger, codegen, rewriter, op, loop, idx, li, needsUniv); 1643 } 1644 1645 // End a loop sequence. 1646 endLoopSeq(merger, codegen, rewriter, op, exp, at, idx, ldx); 1647 } 1648 1649 /// Converts the result computed by the sparse kernel into the required form. 1650 static void genResult(Merger &merger, CodeGen &codegen, RewriterBase &rewriter, 1651 linalg::GenericOp op) { 1652 OpOperand *lhs = op.getOutputOperand(0); 1653 Type resType = lhs->get().getType(); 1654 if (getSparseTensorEncoding(resType)) { 1655 // The sparse tensor rematerializes from the original sparse tensor's 1656 // underlying sparse storage format. 1657 rewriter.replaceOpWithNewOp<LoadOp>(op, resType, lhs->get(), 1658 codegen.sparseOut == lhs); 1659 } else { 1660 // To rematerialize an non-annotated tensor, simply load it 1661 // from the bufferized value. 1662 Value val = codegen.buffers.back(); // value array 1663 rewriter.replaceOpWithNewOp<bufferization::ToTensorOp>(op, resType, val); 1664 } 1665 } 1666 1667 //===----------------------------------------------------------------------===// 1668 // Sparse compiler rewriting methods. 1669 //===----------------------------------------------------------------------===// 1670 1671 namespace { 1672 1673 /// Sparse rewriting rule for generic Lingalg operation. 1674 struct GenericOpSparsifier : public OpRewritePattern<linalg::GenericOp> { 1675 public: 1676 GenericOpSparsifier(MLIRContext *context, SparsificationOptions o) 1677 : OpRewritePattern<linalg::GenericOp>(context), options(o) {} 1678 1679 LogicalResult matchAndRewrite(linalg::GenericOp op, 1680 PatternRewriter &rewriter) const override { 1681 // Detects sparse annotations and translate the per-dimension sparsity 1682 // information for all tensors to loop indices in the kernel. 1683 assert(op.getNumOutputs() == 1); 1684 unsigned numTensors = op.getNumInputsAndOutputs(); 1685 unsigned numLoops = op.iterator_types().getValue().size(); 1686 Merger merger(numTensors, numLoops); 1687 if (!findSparseAnnotations(merger, op)) 1688 return failure(); 1689 1690 // Computes a topologically sorted iteration graph to ensure 1691 // tensors are visited in natural index order. Fails on cycles. 1692 // This assumes that higher-level passes have already put the 1693 // tensors in each tensor expression in a feasible order. 1694 std::vector<unsigned> topSort; 1695 if (!computeIterationGraph(merger, op, topSort, 1696 SortMask::kIncludeUndef | 1697 SortMask::kIncludeDense) && 1698 !computeIterationGraph(merger, op, topSort, SortMask::kIncludeUndef) && 1699 !computeIterationGraph(merger, op, topSort, SortMask::kIncludeDense) && 1700 !computeIterationGraph(merger, op, topSort, SortMask::kSparseOnly)) 1701 return failure(); 1702 1703 // Builds the tensor expression for the Linalg operation in SSA form. 1704 Optional<unsigned> optExp = merger.buildTensorExpFromLinalg(op); 1705 if (!optExp.hasValue()) 1706 return failure(); 1707 unsigned exp = optExp.getValue(); 1708 1709 // Rejects an inadmissable tensor expression. 1710 OpOperand *sparseOut = nullptr; 1711 unsigned outerParNest = 0; 1712 if (!isAdmissableTensorExp(merger, op, topSort, exp, &sparseOut, 1713 outerParNest)) 1714 return failure(); 1715 1716 // Recursively generates code. 1717 merger.setHasSparseOut(sparseOut != nullptr); 1718 CodeGen codegen(options, numTensors, numLoops, sparseOut, outerParNest); 1719 genBuffers(merger, codegen, rewriter, op); 1720 genStmt(merger, codegen, rewriter, op, topSort, exp, 0); 1721 genResult(merger, codegen, rewriter, op); 1722 return success(); 1723 } 1724 1725 private: 1726 /// Options to control sparse code generation. 1727 SparsificationOptions options; 1728 }; 1729 1730 } // namespace 1731 1732 /// Populates the given patterns list with rewriting rules required for 1733 /// the sparsification of linear algebra operations. 1734 void mlir::populateSparsificationPatterns( 1735 RewritePatternSet &patterns, const SparsificationOptions &options) { 1736 patterns.add<GenericOpSparsifier>(patterns.getContext(), options); 1737 } 1738