1 //===- Shape.cpp - MLIR Shape Operations ----------------------------------===// 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/Dialect/Shape/IR/Shape.h" 10 11 #include "mlir/Dialect/StandardOps/IR/Ops.h" 12 #include "mlir/Dialect/Tensor/IR/Tensor.h" 13 #include "mlir/Dialect/Traits.h" 14 #include "mlir/IR/Builders.h" 15 #include "mlir/IR/BuiltinTypes.h" 16 #include "mlir/IR/DialectImplementation.h" 17 #include "mlir/IR/PatternMatch.h" 18 #include "mlir/Transforms/InliningUtils.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/TypeSwitch.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace mlir; 24 using namespace mlir::shape; 25 26 namespace { 27 #include "ShapeCanonicalization.inc" 28 } 29 30 RankedTensorType shape::getExtentTensorType(MLIRContext *ctx) { 31 return RankedTensorType::get({ShapedType::kDynamicSize}, IndexType::get(ctx)); 32 } 33 34 static bool isErrorPropagationPossible(TypeRange operandTypes) { 35 return llvm::any_of(operandTypes, [](Type ty) { 36 return ty.isa<SizeType, ShapeType, ValueShapeType>(); 37 }); 38 } 39 40 static LogicalResult verifySizeOrIndexOp(Operation *op) { 41 assert(op != nullptr && op->getNumResults() == 1); 42 Type resultTy = op->getResultTypes().front(); 43 if (isErrorPropagationPossible(op->getOperandTypes())) { 44 if (!resultTy.isa<SizeType>()) 45 return op->emitOpError() 46 << "if at least one of the operands can hold error values then " 47 "the result must be of type `size` to propagate them"; 48 } 49 return success(); 50 } 51 52 static LogicalResult verifyShapeOrExtentTensorOp(Operation *op) { 53 assert(op != nullptr && op->getNumResults() == 1); 54 Type resultTy = op->getResultTypes().front(); 55 if (isErrorPropagationPossible(op->getOperandTypes())) { 56 if (!resultTy.isa<ShapeType>()) 57 return op->emitOpError() 58 << "if at least one of the operands can hold error values then " 59 "the result must be of type `shape` to propagate them"; 60 } 61 return success(); 62 } 63 64 //===----------------------------------------------------------------------===// 65 // InlinerInterface 66 //===----------------------------------------------------------------------===// 67 68 namespace { 69 /// This class defines the interface for inlining shape dialect ops. 70 struct ShapeInlinerInterface : public DialectInlinerInterface { 71 using DialectInlinerInterface::DialectInlinerInterface; 72 73 // Returns true if the given region 'src' can be inlined into the region 74 // 'dest' that is attached to an operation registered to the current dialect. 75 bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned, 76 BlockAndValueMapping &) const final { 77 return true; 78 } 79 80 // Returns true if the given operation 'op', that is registered to this 81 // dialect, can be inlined into the region 'dest' that is attached to an 82 // operation registered to the current dialect. 83 bool isLegalToInline(Operation *op, Region *dest, bool wouldBeCloned, 84 BlockAndValueMapping &) const final { 85 return true; 86 } 87 }; 88 } // namespace 89 90 void ShapeDialect::initialize() { 91 addOperations< 92 #define GET_OP_LIST 93 #include "mlir/Dialect/Shape/IR/ShapeOps.cpp.inc" 94 >(); 95 addTypes<ShapeType, SizeType, ValueShapeType, WitnessType>(); 96 addInterfaces<ShapeInlinerInterface>(); 97 // Allow unknown operations during prototyping and testing. As the dialect is 98 // still evolving it makes it simple to start with an unregistered ops and 99 // try different variants before actually defining the op. 100 allowUnknownOperations(); 101 } 102 103 Operation *ShapeDialect::materializeConstant(OpBuilder &builder, 104 Attribute value, Type type, 105 Location loc) { 106 if (type.isa<ShapeType>() || 107 type == getExtentTensorType(builder.getContext())) 108 return builder.create<ConstShapeOp>(loc, type, 109 value.cast<DenseIntElementsAttr>()); 110 if (type.isa<SizeType>()) 111 return builder.create<ConstSizeOp>(loc, type, value.cast<IntegerAttr>()); 112 if (type.isa<WitnessType>()) 113 return builder.create<ConstWitnessOp>(loc, type, value.cast<BoolAttr>()); 114 if (ConstantOp::isBuildableWith(value, type)) 115 return builder.create<ConstantOp>(loc, type, value); 116 return nullptr; 117 } 118 119 /// Parse a type registered to this dialect. 120 Type ShapeDialect::parseType(DialectAsmParser &parser) const { 121 StringRef keyword; 122 if (parser.parseKeyword(&keyword)) 123 return Type(); 124 125 if (keyword == "shape") 126 return ShapeType::get(getContext()); 127 if (keyword == "size") 128 return SizeType::get(getContext()); 129 if (keyword == "value_shape") 130 return ValueShapeType::get(getContext()); 131 if (keyword == "witness") 132 return WitnessType::get(getContext()); 133 134 parser.emitError(parser.getNameLoc(), "unknown shape type: ") << keyword; 135 return Type(); 136 } 137 138 /// Print a type registered to this dialect. 139 void ShapeDialect::printType(Type type, DialectAsmPrinter &os) const { 140 TypeSwitch<Type>(type) 141 .Case<ShapeType>([&](Type) { os << "shape"; }) 142 .Case<SizeType>([&](Type) { os << "size"; }) 143 .Case<ValueShapeType>([&](Type) { os << "value_shape"; }) 144 .Case<WitnessType>([&](Type) { os << "witness"; }) 145 .Default([](Type) { llvm_unreachable("unexpected 'shape' type kind"); }); 146 } 147 148 LogicalResult ShapeDialect::verifyOperationAttribute(Operation *op, 149 NamedAttribute attribute) { 150 // Verify shape.lib attribute. 151 if (attribute.first == "shape.lib") { 152 if (!op->hasTrait<OpTrait::SymbolTable>()) 153 return op->emitError( 154 "shape.lib attribute may only be on op implementing SymbolTable"); 155 156 if (auto symbolRef = attribute.second.dyn_cast<SymbolRefAttr>()) { 157 auto *symbol = SymbolTable::lookupSymbolIn(op, symbolRef); 158 if (!symbol) 159 return op->emitError("shape function library ") 160 << symbolRef << " not found"; 161 return isa<shape::FunctionLibraryOp>(symbol) 162 ? success() 163 : op->emitError() 164 << symbolRef << " required to be shape function library"; 165 } 166 167 if (auto arr = attribute.second.dyn_cast<ArrayAttr>()) { 168 // Verify all entries are function libraries and mappings in libraries 169 // refer to unique ops. 170 DenseSet<Identifier> key; 171 for (auto it : arr) { 172 if (!it.isa<SymbolRefAttr>()) 173 return op->emitError( 174 "only SymbolRefAttr allowed in shape.lib attribute array"); 175 176 auto shapeFnLib = dyn_cast<shape::FunctionLibraryOp>( 177 SymbolTable::lookupSymbolIn(op, it.cast<SymbolRefAttr>())); 178 if (!shapeFnLib) 179 return op->emitError() 180 << it << " does not refer to FunctionLibraryOp"; 181 for (auto mapping : shapeFnLib.mapping()) { 182 if (!key.insert(mapping.first).second) { 183 return op->emitError("only one op to shape mapping allowed, found " 184 "multiple for `") 185 << mapping.first << "`"; 186 } 187 } 188 } 189 return success(); 190 } 191 192 return op->emitError("only SymbolRefAttr or array of SymbolRefAttrs " 193 "allowed as shape.lib attribute"); 194 } 195 return success(); 196 } 197 198 //===----------------------------------------------------------------------===// 199 // AnyOp 200 //===----------------------------------------------------------------------===// 201 202 // TODO: Canonicalization should be implemented for shapes that can be 203 // determined through mixtures of the known dimensions of the inputs. 204 OpFoldResult AnyOp::fold(ArrayRef<Attribute> operands) { 205 // Only the last operand is checked because AnyOp is commutative. 206 if (operands.back()) 207 return operands.back(); 208 209 return nullptr; 210 } 211 212 //===----------------------------------------------------------------------===// 213 // AssumingOp 214 //===----------------------------------------------------------------------===// 215 216 static ParseResult parseAssumingOp(OpAsmParser &parser, 217 OperationState &result) { 218 result.regions.reserve(1); 219 Region *doRegion = result.addRegion(); 220 221 auto &builder = parser.getBuilder(); 222 OpAsmParser::OperandType cond; 223 if (parser.parseOperand(cond) || 224 parser.resolveOperand(cond, builder.getType<WitnessType>(), 225 result.operands)) 226 return failure(); 227 228 // Parse optional results type list. 229 if (parser.parseOptionalArrowTypeList(result.types)) 230 return failure(); 231 232 // Parse the region and add a terminator if elided. 233 if (parser.parseRegion(*doRegion, /*arguments=*/{}, /*argTypes=*/{})) 234 return failure(); 235 AssumingOp::ensureTerminator(*doRegion, parser.getBuilder(), result.location); 236 237 // Parse the optional attribute list. 238 if (parser.parseOptionalAttrDict(result.attributes)) 239 return failure(); 240 return success(); 241 } 242 243 static void print(OpAsmPrinter &p, AssumingOp op) { 244 bool yieldsResults = !op.results().empty(); 245 246 p << AssumingOp::getOperationName() << " " << op.witness(); 247 if (yieldsResults) { 248 p << " -> (" << op.getResultTypes() << ")"; 249 } 250 p.printRegion(op.doRegion(), 251 /*printEntryBlockArgs=*/false, 252 /*printBlockTerminators=*/yieldsResults); 253 p.printOptionalAttrDict(op.getAttrs()); 254 } 255 256 namespace { 257 // Removes AssumingOp with a passing witness and inlines the region. 258 struct AssumingWithTrue : public OpRewritePattern<AssumingOp> { 259 using OpRewritePattern<AssumingOp>::OpRewritePattern; 260 261 LogicalResult matchAndRewrite(AssumingOp op, 262 PatternRewriter &rewriter) const override { 263 auto witness = op.witness().getDefiningOp<ConstWitnessOp>(); 264 if (!witness || !witness.passingAttr()) 265 return failure(); 266 267 AssumingOp::inlineRegionIntoParent(op, rewriter); 268 return success(); 269 } 270 }; 271 } // namespace 272 273 void AssumingOp::getCanonicalizationPatterns(OwningRewritePatternList &patterns, 274 MLIRContext *context) { 275 // If taking a passing witness, inline region. 276 patterns.insert<AssumingWithTrue>(context); 277 } 278 279 // See RegionBranchOpInterface in Interfaces/ControlFlowInterfaces.td 280 void AssumingOp::getSuccessorRegions( 281 Optional<unsigned> index, ArrayRef<Attribute> operands, 282 SmallVectorImpl<RegionSuccessor> ®ions) { 283 // AssumingOp has unconditional control flow into the region and back to the 284 // parent, so return the correct RegionSuccessor purely based on the index 285 // being None or 0. 286 if (index.hasValue()) { 287 regions.push_back(RegionSuccessor(getResults())); 288 return; 289 } 290 291 regions.push_back(RegionSuccessor(&doRegion())); 292 } 293 294 void AssumingOp::inlineRegionIntoParent(AssumingOp &op, 295 PatternRewriter &rewriter) { 296 auto *blockBeforeAssuming = rewriter.getInsertionBlock(); 297 auto *assumingBlock = op.getBody(); 298 auto initPosition = rewriter.getInsertionPoint(); 299 auto *blockAfterAssuming = 300 rewriter.splitBlock(blockBeforeAssuming, initPosition); 301 302 // Remove the AssumingOp and AssumingYieldOp. 303 auto &yieldOp = assumingBlock->back(); 304 rewriter.inlineRegionBefore(op.doRegion(), blockAfterAssuming); 305 rewriter.replaceOp(op, yieldOp.getOperands()); 306 rewriter.eraseOp(&yieldOp); 307 308 // Merge blocks together as there was no branching behavior from the 309 // AssumingOp. 310 rewriter.mergeBlocks(assumingBlock, blockBeforeAssuming); 311 rewriter.mergeBlocks(blockAfterAssuming, blockBeforeAssuming); 312 } 313 314 //===----------------------------------------------------------------------===// 315 // AssumingAllOp 316 //===----------------------------------------------------------------------===// 317 318 void AssumingAllOp::getCanonicalizationPatterns( 319 OwningRewritePatternList &patterns, MLIRContext *context) { 320 patterns.insert<AssumingAllOneOp>(context); 321 } 322 323 OpFoldResult AssumingAllOp::fold(ArrayRef<Attribute> operands) { 324 // Iterate in reverse to first handle all constant operands. They are 325 // guaranteed to be the tail of the inputs because this is commutative. 326 for (int idx = operands.size() - 1; idx >= 0; idx--) { 327 Attribute a = operands[idx]; 328 // Cannot fold if any inputs are not constant; 329 if (!a) 330 return nullptr; 331 332 // We do not need to keep statically known values after handling them in 333 // this method. 334 getOperation()->eraseOperand(idx); 335 336 // Always false if any input is statically known false 337 if (!a.cast<BoolAttr>().getValue()) 338 return a; 339 } 340 // If this is reached, all inputs were statically known passing. 341 return BoolAttr::get(true, getContext()); 342 } 343 344 static LogicalResult verify(AssumingAllOp op) { 345 // Ensure that AssumingAllOp contains at least one operand 346 if (op.getNumOperands() == 0) 347 return op.emitOpError("no operands specified"); 348 349 return success(); 350 } 351 352 //===----------------------------------------------------------------------===// 353 // BroadcastOp 354 //===----------------------------------------------------------------------===// 355 356 OpFoldResult BroadcastOp::fold(ArrayRef<Attribute> operands) { 357 if (!operands[1]) 358 return nullptr; 359 360 auto rhsShape = llvm::to_vector<6>( 361 operands[1].cast<DenseIntElementsAttr>().getValues<int64_t>()); 362 if (rhsShape.empty()) 363 return lhs(); 364 365 if (!operands[0]) 366 return nullptr; 367 368 auto lhsShape = llvm::to_vector<6>( 369 operands[0].cast<DenseIntElementsAttr>().getValues<int64_t>()); 370 if (lhsShape.empty()) 371 return rhs(); 372 373 SmallVector<int64_t, 6> resultShape; 374 // If the shapes are not compatible, we can't fold it. 375 // TODO: Fold to an "error". 376 if (!OpTrait::util::getBroadcastedShape(lhsShape, rhsShape, resultShape)) 377 return nullptr; 378 Builder builder(getContext()); 379 return builder.getIndexTensorAttr(resultShape); 380 } 381 382 //===----------------------------------------------------------------------===// 383 // ConcatOp 384 //===----------------------------------------------------------------------===// 385 386 OpFoldResult ConcatOp::fold(ArrayRef<Attribute> operands) { 387 if (!operands[0] || !operands[1]) 388 return nullptr; 389 auto lhsShape = llvm::to_vector<6>( 390 operands[0].cast<DenseIntElementsAttr>().getValues<int64_t>()); 391 auto rhsShape = llvm::to_vector<6>( 392 operands[1].cast<DenseIntElementsAttr>().getValues<int64_t>()); 393 SmallVector<int64_t, 6> resultShape; 394 resultShape.append(lhsShape.begin(), lhsShape.end()); 395 resultShape.append(rhsShape.begin(), rhsShape.end()); 396 Builder builder(getContext()); 397 return builder.getIndexTensorAttr(resultShape); 398 } 399 400 //===----------------------------------------------------------------------===// 401 // ConstShapeOp 402 //===----------------------------------------------------------------------===// 403 404 static void print(OpAsmPrinter &p, ConstShapeOp &op) { 405 p << "shape.const_shape "; 406 p.printOptionalAttrDict(op.getAttrs(), /*elidedAttrs=*/{"shape"}); 407 p << "["; 408 interleaveComma(op.shape().getValues<int64_t>(), p, 409 [&](int64_t i) { p << i; }); 410 p << "] : "; 411 p.printType(op.getType()); 412 } 413 414 static ParseResult parseConstShapeOp(OpAsmParser &parser, 415 OperationState &result) { 416 if (parser.parseOptionalAttrDict(result.attributes)) 417 return failure(); 418 // We piggy-back on ArrayAttr parsing, though we don't internally store the 419 // shape as an ArrayAttr. 420 // TODO: Implement custom parser and maybe make syntax a bit more concise. 421 Attribute extentsRaw; 422 NamedAttrList dummy; 423 if (parser.parseAttribute(extentsRaw, "dummy", dummy)) 424 return failure(); 425 auto extentsArray = extentsRaw.dyn_cast<ArrayAttr>(); 426 if (!extentsArray) 427 return failure(); 428 SmallVector<int64_t, 6> ints; 429 for (Attribute extent : extentsArray) { 430 IntegerAttr attr = extent.dyn_cast<IntegerAttr>(); 431 if (!attr) 432 return failure(); 433 ints.push_back(attr.getInt()); 434 } 435 Builder &builder = parser.getBuilder(); 436 result.addAttribute("shape", builder.getIndexTensorAttr(ints)); 437 Type resultTy; 438 if (parser.parseColonType(resultTy)) 439 return failure(); 440 result.types.push_back(resultTy); 441 return success(); 442 } 443 444 OpFoldResult ConstShapeOp::fold(ArrayRef<Attribute>) { return shapeAttr(); } 445 446 void ConstShapeOp::getCanonicalizationPatterns( 447 OwningRewritePatternList &patterns, MLIRContext *context) { 448 patterns.insert<TensorCastConstShape>(context); 449 } 450 451 //===----------------------------------------------------------------------===// 452 // CstrBroadcastableOp 453 //===----------------------------------------------------------------------===// 454 455 namespace { 456 // Given an input shape Value, try to obtain the shape's values. 457 LogicalResult getShapeVec(Value input, SmallVectorImpl<int64_t> &shapeValues) { 458 if (auto inputOp = input.getDefiningOp<ShapeOfOp>()) { 459 auto type = inputOp.arg().getType().dyn_cast<ShapedType>(); 460 if (!type.hasRank()) 461 return failure(); 462 shapeValues = llvm::to_vector<6>(type.getShape()); 463 return success(); 464 } else if (auto inputOp = input.getDefiningOp<ConstShapeOp>()) { 465 shapeValues = llvm::to_vector<6>(inputOp.shape().getValues<int64_t>()); 466 return success(); 467 } else { 468 return failure(); 469 } 470 } 471 } // namespace 472 473 void CstrBroadcastableOp::getCanonicalizationPatterns( 474 OwningRewritePatternList &patterns, MLIRContext *context) { 475 // Canonicalization patterns have overlap with the considerations during 476 // folding in case additional shape information is inferred at some point that 477 // does not result in folding. 478 patterns.insert<CstrBroadcastableEqOps>(context); 479 } 480 481 OpFoldResult CstrBroadcastableOp::fold(ArrayRef<Attribute> operands) { 482 // Both operands are not needed if one is a scalar. 483 if (operands[0] && 484 operands[0].cast<DenseIntElementsAttr>().getNumElements() == 0) 485 return BoolAttr::get(true, getContext()); 486 if (operands[1] && 487 operands[1].cast<DenseIntElementsAttr>().getNumElements() == 0) 488 return BoolAttr::get(true, getContext()); 489 490 if (operands[0] && operands[1]) { 491 auto lhsShape = llvm::to_vector<6>( 492 operands[0].cast<DenseIntElementsAttr>().getValues<int64_t>()); 493 auto rhsShape = llvm::to_vector<6>( 494 operands[1].cast<DenseIntElementsAttr>().getValues<int64_t>()); 495 SmallVector<int64_t, 6> resultShape; 496 if (OpTrait::util::staticallyKnownBroadcastable(lhsShape, rhsShape)) 497 return BoolAttr::get(true, getContext()); 498 } 499 500 // Lastly, see if folding can be completed based on what constraints are known 501 // on the input shapes. 502 SmallVector<int64_t, 6> lhsShape, rhsShape; 503 if (failed(getShapeVec(lhs(), lhsShape))) 504 return nullptr; 505 if (failed(getShapeVec(rhs(), rhsShape))) 506 return nullptr; 507 508 if (OpTrait::util::staticallyKnownBroadcastable(lhsShape, rhsShape)) 509 return BoolAttr::get(true, getContext()); 510 511 // Because a failing witness result here represents an eventual assertion 512 // failure, we do not replace it with a constant witness. 513 return nullptr; 514 } 515 516 //===----------------------------------------------------------------------===// 517 // CstrEqOp 518 //===----------------------------------------------------------------------===// 519 520 void CstrEqOp::getCanonicalizationPatterns(OwningRewritePatternList &patterns, 521 MLIRContext *context) { 522 // If inputs are equal, return passing witness 523 patterns.insert<CstrEqEqOps>(context); 524 } 525 526 OpFoldResult CstrEqOp::fold(ArrayRef<Attribute> operands) { 527 if (llvm::all_of(operands, 528 [&](Attribute a) { return a && a == operands[0]; })) 529 return BoolAttr::get(true, getContext()); 530 531 // Because a failing witness result here represents an eventual assertion 532 // failure, we do not try to replace it with a constant witness. Similarly, we 533 // cannot if there are any non-const inputs. 534 return nullptr; 535 } 536 537 //===----------------------------------------------------------------------===// 538 // ConstSizeOp 539 //===----------------------------------------------------------------------===// 540 541 void ConstSizeOp::build(OpBuilder &builder, OperationState &result, 542 int64_t value) { 543 build(builder, result, builder.getIndexAttr(value)); 544 } 545 546 OpFoldResult ConstSizeOp::fold(ArrayRef<Attribute>) { return valueAttr(); } 547 548 void ConstSizeOp::getAsmResultNames( 549 llvm::function_ref<void(Value, StringRef)> setNameFn) { 550 SmallString<4> buffer; 551 llvm::raw_svector_ostream os(buffer); 552 os << "c" << value(); 553 setNameFn(getResult(), os.str()); 554 } 555 556 //===----------------------------------------------------------------------===// 557 // ConstWitnessOp 558 //===----------------------------------------------------------------------===// 559 560 OpFoldResult ConstWitnessOp::fold(ArrayRef<Attribute>) { return passingAttr(); } 561 562 //===----------------------------------------------------------------------===// 563 // CstrRequireOp 564 //===----------------------------------------------------------------------===// 565 566 OpFoldResult CstrRequireOp::fold(ArrayRef<Attribute> operands) { 567 return operands[0]; 568 } 569 570 //===----------------------------------------------------------------------===// 571 // ShapeEqOp 572 //===----------------------------------------------------------------------===// 573 574 OpFoldResult ShapeEqOp::fold(ArrayRef<Attribute> operands) { 575 if (lhs() == rhs()) 576 return BoolAttr::get(true, getContext()); 577 auto lhs = operands[0].dyn_cast_or_null<DenseIntElementsAttr>(); 578 if (lhs == nullptr) 579 return {}; 580 auto rhs = operands[1].dyn_cast_or_null<DenseIntElementsAttr>(); 581 if (rhs == nullptr) 582 return {}; 583 return BoolAttr::get(lhs == rhs, getContext()); 584 } 585 586 //===----------------------------------------------------------------------===// 587 // IndexToSizeOp 588 //===----------------------------------------------------------------------===// 589 590 OpFoldResult IndexToSizeOp::fold(ArrayRef<Attribute> operands) { 591 // Constant values of both types, `shape.size` and `index`, are represented as 592 // `IntegerAttr`s which makes constant folding simple. 593 if (Attribute arg = operands[0]) 594 return arg; 595 return {}; 596 } 597 598 void IndexToSizeOp::getCanonicalizationPatterns( 599 OwningRewritePatternList &patterns, MLIRContext *context) { 600 patterns.insert<SizeToIndexToSizeCanonicalization>(context); 601 } 602 603 //===----------------------------------------------------------------------===// 604 // FromExtentsOp 605 //===----------------------------------------------------------------------===// 606 607 OpFoldResult FromExtentsOp::fold(ArrayRef<Attribute> operands) { 608 if (llvm::any_of(operands, [](Attribute a) { return !a; })) 609 return nullptr; 610 SmallVector<int64_t, 6> extents; 611 for (auto attr : operands) 612 extents.push_back(attr.cast<IntegerAttr>().getInt()); 613 Builder builder(getContext()); 614 return builder.getIndexTensorAttr(extents); 615 } 616 617 //===----------------------------------------------------------------------===// 618 // FunctionLibraryOp 619 //===----------------------------------------------------------------------===// 620 621 void FunctionLibraryOp::build(OpBuilder &builder, OperationState &result, 622 StringRef name) { 623 ensureTerminator(*result.addRegion(), builder, result.location); 624 result.attributes.push_back(builder.getNamedAttr( 625 ::mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(name))); 626 } 627 628 FuncOp FunctionLibraryOp::getShapeFunction(Operation *op) { 629 auto attr = mapping() 630 .get(op->getName().getIdentifier()) 631 .dyn_cast_or_null<FlatSymbolRefAttr>(); 632 if (!attr) 633 return nullptr; 634 return lookupSymbol<FuncOp>(attr); 635 } 636 637 ParseResult parseFunctionLibraryOp(OpAsmParser &parser, 638 OperationState &result) { 639 // Parse the op name. 640 StringAttr nameAttr; 641 if (parser.parseSymbolName(nameAttr, ::mlir::SymbolTable::getSymbolAttrName(), 642 result.attributes)) 643 return failure(); 644 645 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 646 return failure(); 647 648 auto *bodyRegion = result.addRegion(); 649 if (parser.parseRegion(*bodyRegion)) 650 return failure(); 651 652 FunctionLibraryOp::ensureTerminator(*bodyRegion, parser.getBuilder(), 653 result.location); 654 if (parser.parseKeyword("mapping")) 655 return failure(); 656 657 DictionaryAttr mappingAttr; 658 if (parser.parseAttribute(mappingAttr, 659 parser.getBuilder().getType<NoneType>(), "mapping", 660 result.attributes)) 661 return failure(); 662 return success(); 663 } 664 665 void print(OpAsmPrinter &p, FunctionLibraryOp op) { 666 p << op.getOperationName() << ' '; 667 p.printSymbolName(op.getName()); 668 p.printOptionalAttrDictWithKeyword( 669 op.getAttrs(), {SymbolTable::getSymbolAttrName(), "mapping"}); 670 p.printRegion(op.getOperation()->getRegion(0), /*printEntryBlockArgs=*/false, 671 /*printBlockTerminators=*/false); 672 p << " mapping "; 673 p.printAttributeWithoutType(op.mappingAttr()); 674 } 675 676 //===----------------------------------------------------------------------===// 677 // GetExtentOp 678 //===----------------------------------------------------------------------===// 679 680 Optional<int64_t> GetExtentOp::getConstantDim() { 681 if (auto constSizeOp = dim().getDefiningOp<ConstSizeOp>()) 682 return constSizeOp.value().getLimitedValue(); 683 if (auto constantOp = dim().getDefiningOp<ConstantOp>()) 684 return constantOp.value().cast<IntegerAttr>().getInt(); 685 return llvm::None; 686 } 687 688 OpFoldResult GetExtentOp::fold(ArrayRef<Attribute> operands) { 689 auto elements = operands[0].dyn_cast_or_null<DenseIntElementsAttr>(); 690 if (!elements) 691 return nullptr; 692 Optional<int64_t> dim = getConstantDim(); 693 if (!dim.hasValue()) 694 return nullptr; 695 if (dim.getValue() >= elements.getNumElements()) 696 return nullptr; 697 return elements.getValue({(uint64_t)dim.getValue()}); 698 } 699 700 void GetExtentOp::build(OpBuilder &builder, OperationState &result, Value shape, 701 int64_t dim) { 702 auto loc = result.location; 703 auto dimAttr = builder.getIndexAttr(dim); 704 if (shape.getType().isa<ShapeType>()) { 705 Value dim = builder.create<ConstSizeOp>(loc, dimAttr); 706 build(builder, result, builder.getType<SizeType>(), shape, dim); 707 } else { 708 Value dim = 709 builder.create<ConstantOp>(loc, builder.getIndexType(), dimAttr); 710 build(builder, result, builder.getIndexType(), shape, dim); 711 } 712 } 713 714 //===----------------------------------------------------------------------===// 715 // RankOp 716 //===----------------------------------------------------------------------===// 717 718 OpFoldResult shape::RankOp::fold(ArrayRef<Attribute> operands) { 719 auto shape = operands[0].dyn_cast_or_null<DenseIntElementsAttr>(); 720 if (!shape) 721 return {}; 722 int64_t rank = shape.getNumElements(); 723 Builder builder(getContext()); 724 return builder.getIndexAttr(rank); 725 } 726 727 /// Evaluate the `rank` operation for shapes of ranked tensors at compile time. 728 /// Constant folding fails in cases where only the rank is constant, not the 729 /// shape itself. 730 /// This canonicalization matches `shape.rank(shape.shape_of(%ranked_tensor))`. 731 /// 732 /// Example: 733 /// 734 /// %shape = shape.shape_of %ranked_tensor : tensor<1x2x?xf32> 735 /// %rank = shape.rank %shape 736 /// 737 /// becomes 738 /// 739 /// %rank = shape.const_size 3 740 741 namespace { 742 struct RankShapeOfCanonicalizationPattern 743 : public OpRewritePattern<shape::RankOp> { 744 using OpRewritePattern<shape::RankOp>::OpRewritePattern; 745 746 LogicalResult matchAndRewrite(shape::RankOp op, 747 PatternRewriter &rewriter) const override { 748 auto shapeOfOp = op.shape().getDefiningOp<ShapeOfOp>(); 749 if (!shapeOfOp) 750 return failure(); 751 auto rankedTensorType = 752 shapeOfOp.arg().getType().dyn_cast<RankedTensorType>(); 753 if (!rankedTensorType) 754 return failure(); 755 int64_t rank = rankedTensorType.getRank(); 756 if (op.getType().isa<IndexType>()) { 757 rewriter.replaceOpWithNewOp<ConstantIndexOp>(op.getOperation(), rank); 758 } else if (op.getType().isa<shape::SizeType>()) { 759 rewriter.replaceOpWithNewOp<shape::ConstSizeOp>(op.getOperation(), rank); 760 } else { 761 return failure(); 762 } 763 return success(); 764 } 765 }; 766 } // namespace 767 768 void shape::RankOp::getCanonicalizationPatterns( 769 OwningRewritePatternList &patterns, MLIRContext *context) { 770 patterns.insert<RankShapeOfCanonicalizationPattern>(context); 771 } 772 773 //===----------------------------------------------------------------------===// 774 // NumElementsOp 775 //===----------------------------------------------------------------------===// 776 777 OpFoldResult NumElementsOp::fold(ArrayRef<Attribute> operands) { 778 779 // Fold only when argument constant. 780 Attribute shape = operands[0]; 781 if (!shape) 782 return {}; 783 784 APInt product(64, 1); 785 for (auto value : shape.cast<DenseIntElementsAttr>()) 786 product *= value; 787 Builder builder(getContext()); 788 return builder.getIndexAttr(product.getLimitedValue()); 789 } 790 791 void NumElementsOp::build(OpBuilder &builder, OperationState &result, 792 Value shape) { 793 if (shape.getType().isa<ShapedType>()) { 794 auto type = builder.getIndexType(); 795 return build(builder, result, type, shape); 796 } 797 auto type = SizeType::get(builder.getContext()); 798 return build(builder, result, type, shape); 799 } 800 801 //===----------------------------------------------------------------------===// 802 // MulOp 803 //===----------------------------------------------------------------------===// 804 805 OpFoldResult MulOp::fold(ArrayRef<Attribute> operands) { 806 auto lhs = operands[0].dyn_cast_or_null<IntegerAttr>(); 807 if (!lhs) 808 return nullptr; 809 auto rhs = operands[1].dyn_cast_or_null<IntegerAttr>(); 810 if (!rhs) 811 return nullptr; 812 APInt folded = lhs.getValue() * rhs.getValue(); 813 Type indexTy = IndexType::get(getContext()); 814 return IntegerAttr::get(indexTy, folded); 815 } 816 817 //===----------------------------------------------------------------------===// 818 // ShapeOfOp 819 //===----------------------------------------------------------------------===// 820 821 OpFoldResult ShapeOfOp::fold(ArrayRef<Attribute>) { 822 auto type = getOperand().getType().dyn_cast<ShapedType>(); 823 if (!type || !type.hasStaticShape()) 824 return nullptr; 825 Builder builder(getContext()); 826 return builder.getIndexTensorAttr(type.getShape()); 827 } 828 829 void ShapeOfOp::build(OpBuilder &builder, OperationState &result, Value arg) { 830 Type type = arg.getType().isa<ShapedType>() 831 ? (Type)getExtentTensorType(builder.getContext()) 832 : (Type)builder.getType<ShapeType>(); 833 return ShapeOfOp::build(builder, result, type, arg); 834 } 835 836 namespace { 837 struct ShapeOfWithTensor : public OpRewritePattern<shape::ShapeOfOp> { 838 using OpRewritePattern<shape::ShapeOfOp>::OpRewritePattern; 839 840 LogicalResult matchAndRewrite(shape::ShapeOfOp op, 841 PatternRewriter &rewriter) const override { 842 if (!op.arg().getType().isa<ShapedType>()) 843 return failure(); 844 if (op.getType().isa<ShapedType>()) 845 return failure(); 846 847 rewriter.replaceOpWithNewOp<shape::ShapeOfOp>(op.getOperation(), op.arg()); 848 return success(); 849 } 850 }; 851 } // namespace 852 853 void ShapeOfOp::getCanonicalizationPatterns(OwningRewritePatternList &patterns, 854 MLIRContext *context) { 855 patterns.insert<ShapeOfWithTensor>(context); 856 } 857 858 //===----------------------------------------------------------------------===// 859 // SizeToIndexOp 860 //===----------------------------------------------------------------------===// 861 862 OpFoldResult SizeToIndexOp::fold(ArrayRef<Attribute> operands) { 863 // Constant values of both types, `shape.size` and `index`, are represented as 864 // `IntegerAttr`s which makes constant folding simple. 865 if (Attribute arg = operands[0]) 866 return arg; 867 return impl::foldCastOp(*this); 868 } 869 870 void SizeToIndexOp::getCanonicalizationPatterns( 871 OwningRewritePatternList &patterns, MLIRContext *context) { 872 patterns.insert<IndexToSizeToIndexCanonicalization>(context); 873 } 874 875 //===----------------------------------------------------------------------===// 876 // YieldOp 877 //===----------------------------------------------------------------------===// 878 879 static LogicalResult verify(shape::YieldOp op) { 880 auto *parentOp = op->getParentOp(); 881 auto results = parentOp->getResults(); 882 auto operands = op.getOperands(); 883 884 if (parentOp->getNumResults() != op.getNumOperands()) 885 return op.emitOpError() << "number of operands does not match number of " 886 "results of its parent"; 887 for (auto e : llvm::zip(results, operands)) 888 if (std::get<0>(e).getType() != std::get<1>(e).getType()) 889 return op.emitOpError() 890 << "types mismatch between yield op and its parent"; 891 892 return success(); 893 } 894 895 //===----------------------------------------------------------------------===// 896 // SplitAtOp 897 //===----------------------------------------------------------------------===// 898 899 LogicalResult SplitAtOp::fold(ArrayRef<Attribute> operands, 900 SmallVectorImpl<OpFoldResult> &results) { 901 if (!operands[0] || !operands[1]) 902 return failure(); 903 auto shapeVec = llvm::to_vector<6>( 904 operands[0].cast<DenseIntElementsAttr>().getValues<int64_t>()); 905 auto shape = llvm::makeArrayRef(shapeVec); 906 auto splitPoint = operands[1].cast<IntegerAttr>().getInt(); 907 // Verify that the split point is in the correct range. 908 // TODO: Constant fold to an "error". 909 int64_t rank = shape.size(); 910 if (!(-rank <= splitPoint && splitPoint <= rank)) 911 return failure(); 912 if (splitPoint < 0) 913 splitPoint += shape.size(); 914 Builder builder(operands[0].getContext()); 915 results.push_back(builder.getIndexTensorAttr(shape.take_front(splitPoint))); 916 results.push_back(builder.getIndexTensorAttr(shape.drop_front(splitPoint))); 917 return success(); 918 } 919 920 //===----------------------------------------------------------------------===// 921 // ToExtentTensorOp 922 //===----------------------------------------------------------------------===// 923 924 OpFoldResult ToExtentTensorOp::fold(ArrayRef<Attribute> operands) { 925 if (!operands[0]) 926 return impl::foldCastOp(*this); 927 Builder builder(getContext()); 928 auto shape = llvm::to_vector<6>( 929 operands[0].cast<DenseIntElementsAttr>().getValues<int64_t>()); 930 auto type = RankedTensorType::get({static_cast<int64_t>(shape.size())}, 931 builder.getIndexType()); 932 return DenseIntElementsAttr::get(type, shape); 933 } 934 935 //===----------------------------------------------------------------------===// 936 // ReduceOp 937 //===----------------------------------------------------------------------===// 938 939 void ReduceOp::build(OpBuilder &builder, OperationState &result, Value shape, 940 ValueRange initVals) { 941 result.addOperands(shape); 942 result.addOperands(initVals); 943 944 Region *bodyRegion = result.addRegion(); 945 bodyRegion->push_back(new Block); 946 Block &bodyBlock = bodyRegion->front(); 947 bodyBlock.addArgument(builder.getIndexType()); 948 949 Type elementType; 950 if (auto tensorType = shape.getType().dyn_cast<TensorType>()) 951 elementType = tensorType.getElementType(); 952 else 953 elementType = SizeType::get(builder.getContext()); 954 bodyBlock.addArgument(elementType); 955 956 for (Type initValType : initVals.getTypes()) { 957 bodyBlock.addArgument(initValType); 958 result.addTypes(initValType); 959 } 960 } 961 962 static LogicalResult verify(ReduceOp op) { 963 // Verify block arg types. 964 Block &block = op.region().front(); 965 966 // The block takes index, extent, and aggregated values as arguments. 967 auto blockArgsCount = op.initVals().size() + 2; 968 if (block.getNumArguments() != blockArgsCount) 969 return op.emitOpError() << "ReduceOp body is expected to have " 970 << blockArgsCount << " arguments"; 971 972 // The first block argument is the index and must always be of type `index`. 973 if (!block.getArgument(0).getType().isa<IndexType>()) 974 return op.emitOpError( 975 "argument 0 of ReduceOp body is expected to be of IndexType"); 976 977 // The second block argument is the extent and must be of type `size` or 978 // `index`, depending on whether the reduce operation is applied to a shape or 979 // to an extent tensor. 980 Type extentTy = block.getArgument(1).getType(); 981 if (op.shape().getType().isa<ShapeType>()) { 982 if (!extentTy.isa<SizeType>()) 983 return op.emitOpError("argument 1 of ReduceOp body is expected to be of " 984 "SizeType if the ReduceOp operates on a ShapeType"); 985 } else { 986 if (!extentTy.isa<IndexType>()) 987 return op.emitOpError( 988 "argument 1 of ReduceOp body is expected to be of IndexType if the " 989 "ReduceOp operates on an extent tensor"); 990 } 991 992 for (auto type : llvm::enumerate(op.initVals())) 993 if (block.getArgument(type.index() + 2).getType() != type.value().getType()) 994 return op.emitOpError() 995 << "type mismatch between argument " << type.index() + 2 996 << " of ReduceOp body and initial value " << type.index(); 997 return success(); 998 } 999 1000 static ParseResult parseReduceOp(OpAsmParser &parser, OperationState &result) { 1001 // Parse operands. 1002 SmallVector<OpAsmParser::OperandType, 3> operands; 1003 Type shapeOrExtentTensorType; 1004 if (parser.parseOperandList(operands, /*requiredOperandCount=*/-1, 1005 OpAsmParser::Delimiter::Paren) || 1006 parser.parseColonType(shapeOrExtentTensorType) || 1007 parser.parseOptionalArrowTypeList(result.types)) 1008 return failure(); 1009 1010 // Resolve operands. 1011 auto initVals = llvm::makeArrayRef(operands).drop_front(); 1012 if (parser.resolveOperand(operands.front(), shapeOrExtentTensorType, 1013 result.operands) || 1014 parser.resolveOperands(initVals, result.types, parser.getNameLoc(), 1015 result.operands)) 1016 return failure(); 1017 1018 // Parse the body. 1019 Region *body = result.addRegion(); 1020 if (parser.parseRegion(*body, /*args=*/{}, /*argTypes=*/{})) 1021 return failure(); 1022 1023 // Parse attributes. 1024 if (parser.parseOptionalAttrDict(result.attributes)) 1025 return failure(); 1026 1027 return success(); 1028 } 1029 1030 static void print(OpAsmPrinter &p, ReduceOp op) { 1031 p << op.getOperationName() << '(' << op.shape() << ", " << op.initVals() 1032 << ") : " << op.shape().getType(); 1033 p.printOptionalArrowTypeList(op.getResultTypes()); 1034 p.printRegion(op.region()); 1035 p.printOptionalAttrDict(op.getAttrs()); 1036 } 1037 1038 #define GET_OP_CLASSES 1039 #include "mlir/Dialect/Shape/IR/ShapeOps.cpp.inc" 1040