24#include "llvm/ADT/SetOperations.h"
25#include "llvm/ADT/SmallVectorExtras.h"
26#include "llvm/Support/raw_ostream.h"
32#include "mlir/Dialect/Shape/IR/ShapeOpsDialect.cpp.inc"
35#include "ShapeCanonicalization.inc"
39 return RankedTensorType::get({rank}, IndexType::get(ctx));
43 auto ranked = llvm::dyn_cast<RankedTensorType>(type);
44 return ranked && ranked.getRank() == 1 && ranked.getElementType().isIndex();
50 auto type = llvm::cast<ShapedType>(inputOp.getArg().getType());
53 llvm::append_range(shapeValues, type.getShape());
58 llvm::append_range(shapeValues, attr.getValues<
int64_t>());
65 return llvm::any_of(operandTypes,
66 llvm::IsaPred<SizeType, ShapeType, ValueShapeType>);
73 if (!llvm::isa<SizeType>(resultTy))
75 <<
"if at least one of the operands can hold error values then "
76 "the result must be of type `size` to propagate them";
85 if (!llvm::isa<ShapeType>(resultTy))
87 <<
"if at least one of the operands can hold error values then "
88 "the result must be of type `shape` to propagate them";
93template <
typename... Ty>
95 return typeRange.size() == 1 && llvm::isa<Ty...>(typeRange.front());
98template <
typename... Ty,
typename... ranges>
109struct ShapeInlinerInterface :
public DialectInlinerInterface {
110 using DialectInlinerInterface::DialectInlinerInterface;
115 IRMapping &)
const final {
123 IRMapping &)
const final {
129void ShapeDialect::initialize() {
132#include "mlir/Dialect/Shape/IR/ShapeOps.cpp.inc"
135#define GET_TYPEDEF_LIST
136#include "mlir/Dialect/Shape/IR/ShapeOpsTypes.cpp.inc"
138 addInterfaces<ShapeInlinerInterface>();
142 allowUnknownOperations();
143 declarePromisedInterfaces<bufferization::BufferizableOpInterface, AssumingOp,
150 if (
auto poison = dyn_cast<ub::PoisonAttr>(value))
151 return ub::PoisonOp::create(builder, loc, type, poison);
154 return ConstShapeOp::create(builder, loc, type,
155 llvm::cast<DenseIntElementsAttr>(value));
156 if (llvm::isa<SizeType>(type))
157 return ConstSizeOp::create(builder, loc, type,
158 llvm::cast<IntegerAttr>(value));
159 if (llvm::isa<WitnessType>(type))
160 return ConstWitnessOp::create(builder, loc, type,
161 llvm::cast<BoolAttr>(value));
163 return arith::ConstantOp::materialize(builder, value, type, loc);
166LogicalResult ShapeDialect::verifyOperationAttribute(
Operation *op,
169 if (attribute.
getName() ==
"shape.lib") {
172 "shape.lib attribute may only be on op implementing SymbolTable");
174 if (
auto symbolRef = llvm::dyn_cast<SymbolRefAttr>(attribute.
getValue())) {
177 return op->
emitError(
"shape function library ")
178 << symbolRef <<
" not found";
179 return isa<shape::FunctionLibraryOp>(symbol)
182 << symbolRef <<
" required to be shape function library";
185 if (
auto arr = llvm::dyn_cast<ArrayAttr>(attribute.
getValue())) {
189 for (
auto it : arr) {
190 if (!llvm::isa<SymbolRefAttr>(it))
192 "only SymbolRefAttr allowed in shape.lib attribute array");
194 auto shapeFnLib = dyn_cast_or_null<shape::FunctionLibraryOp>(
198 << it <<
" does not refer to FunctionLibraryOp";
199 for (
auto mapping : shapeFnLib.getMapping()) {
200 if (!key.insert(mapping.getName()).second) {
201 return op->
emitError(
"only one op to shape mapping allowed, found "
203 << mapping.getName() <<
"`";
210 return op->
emitError(
"only SymbolRefAttr or array of SymbolRefAttrs "
211 "allowed as shape.lib attribute");
224 if (adaptor.getInputs().back())
225 return adaptor.getInputs().back();
235 result.regions.reserve(1);
252 AssumingOp::ensureTerminator(*doRegion, parser.
getBuilder(),
result.location);
261 bool yieldsResults = !getResults().empty();
263 p <<
" " << getWitness();
265 p <<
" -> (" << getResultTypes() <<
")";
276 using OpRewritePattern<AssumingOp>::OpRewritePattern;
278 LogicalResult matchAndRewrite(AssumingOp op,
279 PatternRewriter &rewriter)
const override {
280 auto witness = op.getWitness().getDefiningOp<ConstWitnessOp>();
281 if (!witness || !witness.getPassingAttr())
284 AssumingOp::inlineRegionIntoParent(op, rewriter);
290 using OpRewritePattern<AssumingOp>::OpRewritePattern;
292 LogicalResult matchAndRewrite(AssumingOp op,
293 PatternRewriter &rewriter)
const override {
294 Block *body = op.getBody();
295 auto yieldOp = llvm::cast<AssumingYieldOp>(body->
getTerminator());
298 SmallVector<Value, 4> newYieldOperands;
299 for (
auto [opResult, yieldOperand] :
300 llvm::zip(op.getResults(), yieldOp.getOperands())) {
301 if (!opResult.getUses().empty()) {
302 newYieldOperands.push_back(yieldOperand);
307 if (newYieldOperands.size() == yieldOp->getNumOperands())
316 auto newOp = AssumingOp::create(
317 rewriter, op.getLoc(), newYieldOp->getOperandTypes(), op.getWitness());
318 newOp.getDoRegion().takeBody(op.getDoRegion());
321 SmallVector<Value, 4> replacementValues;
322 auto src = newOp.getResults().begin();
323 for (
auto it : op.getResults()) {
324 if (it.getUses().empty())
325 replacementValues.push_back(
nullptr);
327 replacementValues.push_back(*src++);
329 rewriter.
replaceOp(op, replacementValues);
337 patterns.
add<AssumingOpRemoveUnusedResults, AssumingWithTrue>(context);
341void AssumingOp::getSuccessorRegions(
358void AssumingOp::inlineRegionIntoParent(AssumingOp &op,
361 auto *assumingBlock = op.getBody();
363 auto *blockAfterAssuming =
364 rewriter.
splitBlock(blockBeforeAssuming, initPosition);
367 auto &yieldOp = assumingBlock->
back();
369 rewriter.
replaceOp(op, yieldOp.getOperands());
374 rewriter.
mergeBlocks(assumingBlock, blockBeforeAssuming);
375 rewriter.
mergeBlocks(blockAfterAssuming, blockBeforeAssuming);
378void AssumingOp::build(
383 result.addOperands(witness);
389 AssumingYieldOp::create(builder,
result.location, yieldValues);
392 for (
Value v : yieldValues)
393 assumingTypes.push_back(v.getType());
394 result.addTypes(assumingTypes);
401LogicalResult mlir::shape::AddOp::inferReturnTypes(
402 MLIRContext *context, std::optional<Location> location,
404 if (llvm::isa<SizeType>(adaptor.getLhs().getType()) ||
405 llvm::isa<SizeType>(adaptor.getRhs().getType()))
406 inferredReturnTypes.assign({SizeType::get(context)});
408 inferredReturnTypes.assign({IndexType::get(context)});
417OpFoldResult mlir::shape::AddOp::fold(FoldAdaptor adaptor) {
423 adaptor.getOperands(),
424 [](APInt a,
const APInt &
b) { return std::move(a) + b; });
444 using OpRewritePattern<AssumingAllOp>::OpRewritePattern;
446 LogicalResult matchAndRewrite(AssumingAllOp op,
447 PatternRewriter &rewriter)
const override {
448 SmallVector<Value> operands;
450 for (Value operand : op.getInputs()) {
451 if (
auto assumeAll = operand.getDefiningOp<AssumingAllOp>())
452 operands.append(assumeAll.operand_begin(), assumeAll->operand_end());
454 operands.push_back(operand);
458 if (operands.size() == op.getNumOperands())
488struct AssumingAllOfCstrBroadcastable :
public OpRewritePattern<AssumingAllOp> {
489 using OpRewritePattern<AssumingAllOp>::OpRewritePattern;
491 LogicalResult matchAndRewrite(AssumingAllOp op,
492 PatternRewriter &rewriter)
const override {
495 for (Value operand : op.getInputs()) {
498 auto broadcastable = operand.getDefiningOp<CstrBroadcastableOp>();
502 operands.insert(broadcastable);
506 if (operands.size() <= 1)
510 SmallVector<std::pair<CstrBroadcastableOp, DenseSet<Value>>> shapes;
511 for (
auto cstr : operands) {
513 shapes.emplace_back(cstr, std::move(shapesSet));
517 llvm::sort(shapes, [](
auto a,
auto b) {
518 return a.first.getNumOperands() >
b.first.getNumOperands();
525 SmallVector<CstrBroadcastableOp> markedForErase;
527 for (
unsigned i = 0; i < shapes.size(); ++i) {
528 auto isSubset = [&](
auto pair) {
529 return llvm::set_is_subset(pair.second, shapes[i].second);
533 auto *it = std::remove_if(shapes.begin() + i + 1, shapes.end(), isSubset);
534 for (
auto *it0 = it; it0 < shapes.end(); ++it0)
535 markedForErase.push_back(it0->first);
536 shapes.erase(it, shapes.end());
540 if (markedForErase.empty())
544 SmallVector<Value> uniqueConstraints;
545 for (
auto &shape : shapes)
546 uniqueConstraints.push_back(shape.first.getResult());
552 for (
auto &op : markedForErase)
560struct AssumingAllToCstrEqCanonicalization
562 using OpRewritePattern<AssumingAllOp>::OpRewritePattern;
564 LogicalResult matchAndRewrite(AssumingAllOp op,
565 PatternRewriter &rewriter)
const override {
566 SmallVector<Value, 8> shapes;
567 for (Value w : op.getInputs()) {
568 auto cstrEqOp = w.getDefiningOp<CstrEqOp>();
571 bool disjointShapes = llvm::none_of(cstrEqOp.getShapes(), [&](Value s) {
572 return llvm::is_contained(shapes, s);
574 if (!shapes.empty() && !cstrEqOp.getShapes().empty() && disjointShapes)
576 shapes.append(cstrEqOp.getShapes().begin(), cstrEqOp.getShapes().end());
583template <
typename OpTy>
585 using OpRewritePattern<OpTy>::OpRewritePattern;
587 LogicalResult matchAndRewrite(OpTy op,
588 PatternRewriter &rewriter)
const override {
593 if (unique.size() < op.getNumOperands()) {
595 op, op->getResultTypes(), unique.takeVector(), op.getProperties(),
596 op->getDiscardableAttrDictionary().getValue());
608 .
add<MergeAssumingAllOps, AssumingAllOneOp,
609 AssumingAllOfCstrBroadcastable, AssumingAllToCstrEqCanonicalization,
610 RemoveDuplicateOperandsPattern<AssumingAllOp>>(context);
616 for (
int idx = adaptor.getInputs().size() - 1; idx >= 0; idx--) {
624 getOperation()->eraseOperand(idx);
627 if (!llvm::cast<BoolAttr>(a).getValue())
634LogicalResult AssumingAllOp::verify() {
637 return emitOpError(
"no operands specified");
647 if (getShapes().size() == 1) {
651 return getShapes().front();
655 dyn_cast_or_null<DenseIntElementsAttr>(adaptor.getShapes().front());
661 for (
auto next : adaptor.getShapes().drop_front()) {
662 auto nextAttr = dyn_cast_or_null<DenseIntElementsAttr>(next);
665 auto nextShape = llvm::to_vector<6>(nextAttr.getValues<
int64_t>());
674 std::copy(tmpShape.begin(), tmpShape.end(),
675 std::back_inserter(resultShape));
682LogicalResult BroadcastOp::verify() {
687template <
typename OpTy>
689 using OpRewritePattern<OpTy>::OpRewritePattern;
691 LogicalResult matchAndRewrite(OpTy op,
692 PatternRewriter &rewriter)
const override {
693 auto isPotentiallyNonEmptyShape = [](Value shape) {
694 if (
auto extentTensorTy =
695 llvm::dyn_cast<RankedTensorType>(shape.getType())) {
696 if (extentTensorTy.getDimSize(0) == 0)
699 if (
auto constShape = shape.getDefiningOp<ConstShapeOp>()) {
700 if (constShape.getShape().empty())
705 auto newOperands = llvm::filter_to_vector<8>(op->getOperands(),
706 isPotentiallyNonEmptyShape);
710 if (newOperands.empty()) {
717 if (newOperands.size() < op.getNumOperands()) {
719 op, op->getResultTypes(), newOperands, op.getProperties(),
720 op->getDiscardableAttrDictionary().getValue());
728struct BroadcastForwardSingleOperandPattern
730 using OpRewritePattern<BroadcastOp>::OpRewritePattern;
732 LogicalResult matchAndRewrite(BroadcastOp op,
733 PatternRewriter &rewriter)
const override {
734 if (op.getNumOperands() != 1)
740 auto loc = op.getLoc();
741 if (llvm::isa<ShapeType>(op.getType())) {
744 assert(!llvm::isa<ShapeType>(op.getType()) &&
746 "expect extent tensor cast");
748 tensor::CastOp::create(rewriter, loc, op.getType(),
replacement);
757struct BroadcastFoldConstantOperandsPattern
759 using OpRewritePattern<BroadcastOp>::OpRewritePattern;
761 LogicalResult matchAndRewrite(BroadcastOp op,
762 PatternRewriter &rewriter)
const override {
763 SmallVector<int64_t, 8> foldedConstantShape;
764 SmallVector<Value, 8> newShapeOperands;
765 for (Value shape : op.getShapes()) {
766 if (
auto constShape = shape.getDefiningOp<ConstShapeOp>()) {
767 SmallVector<int64_t, 8> newFoldedConstantShape;
770 llvm::to_vector<8>(constShape.getShape().getValues<int64_t>()),
771 newFoldedConstantShape)) {
772 foldedConstantShape = newFoldedConstantShape;
776 newShapeOperands.push_back(shape);
780 if (op.getNumOperands() - newShapeOperands.size() < 2)
783 auto foldedConstantOperandsTy = RankedTensorType::get(
784 {
static_cast<int64_t
>(foldedConstantShape.size())},
786 newShapeOperands.push_back(
787 ConstShapeOp::create(rewriter, op.getLoc(), foldedConstantOperandsTy,
790 op,
TypeRange{op.getType()}, newShapeOperands, op.getProperties(),
791 op->getDiscardableAttrDictionary().getValue());
796template <
typename OpTy>
797struct CanonicalizeCastExtentTensorOperandsPattern
799 using OpRewritePattern<OpTy>::OpRewritePattern;
801 LogicalResult matchAndRewrite(OpTy op,
802 PatternRewriter &rewriter)
const override {
804 bool anyChange =
false;
805 auto canonicalizeOperand = [&](Value operand) -> Value {
806 if (
auto castOp = operand.getDefiningOp<tensor::CastOp>()) {
808 bool isInformationLoosingCast =
809 llvm::cast<RankedTensorType>(castOp.getType()).isDynamicDim(0);
810 if (isInformationLoosingCast) {
812 return castOp.getSource();
818 llvm::map_to_vector<8>(op.getOperands(), canonicalizeOperand);
824 op, op->getResultTypes(), newOperands, op.getProperties(),
825 op->getDiscardableAttrDictionary().getValue());
830struct BroadcastConcretizeResultTypePattern
832 using OpRewritePattern<BroadcastOp>::OpRewritePattern;
834 LogicalResult matchAndRewrite(BroadcastOp op,
835 PatternRewriter &rewriter)
const override {
837 auto resultTy = llvm::dyn_cast<RankedTensorType>(op.getType());
838 if (!resultTy || !resultTy.isDynamicDim(0))
843 for (Value shape : op.getShapes()) {
844 if (
auto extentTensorTy =
845 llvm::dyn_cast<RankedTensorType>(shape.getType())) {
848 if (extentTensorTy.isDynamicDim(0))
850 maxRank = std::max(maxRank, extentTensorTy.getDimSize(0));
854 auto newOp = BroadcastOp::create(rewriter, op.getLoc(),
856 op.getShapes(),
nullptr);
865 patterns.
add<BroadcastConcretizeResultTypePattern,
866 BroadcastFoldConstantOperandsPattern,
867 BroadcastForwardSingleOperandPattern,
868 CanonicalizeCastExtentTensorOperandsPattern<BroadcastOp>,
869 RemoveDuplicateOperandsPattern<BroadcastOp>,
870 RemoveEmptyShapeOperandsPattern<BroadcastOp>>(context);
878 if (!adaptor.getLhs() || !adaptor.getRhs())
880 auto lhsShape = llvm::to_vector<6>(
881 llvm::cast<DenseIntElementsAttr>(adaptor.getLhs()).getValues<
int64_t>());
882 auto rhsShape = llvm::to_vector<6>(
883 llvm::cast<DenseIntElementsAttr>(adaptor.getRhs()).getValues<
int64_t>());
885 resultShape.append(lhsShape.begin(), lhsShape.end());
886 resultShape.append(rhsShape.begin(), rhsShape.end());
899 interleaveComma(
getShape().getValues<int64_t>(), p);
914 auto extentsArray = llvm::dyn_cast<ArrayAttr>(extentsRaw);
919 IntegerAttr attr = llvm::dyn_cast<IntegerAttr>(extent);
922 ints.push_back(attr.getInt());
929 result.types.push_back(resultTy);
933OpFoldResult ConstShapeOp::fold(FoldAdaptor) {
return getShapeAttr(); }
937 patterns.
add<TensorCastConstShape>(context);
940LogicalResult mlir::shape::ConstShapeOp::inferReturnTypes(
941 MLIRContext *context, std::optional<Location> location,
944 const Properties prop = adaptor.getProperties();
945 inferredReturnTypes.assign({RankedTensorType::get(
946 {
static_cast<int64_t>(prop.shape.size())},
b.getIndexType())});
950bool mlir::shape::ConstShapeOp::isCompatibleReturnTypes(
TypeRange l,
952 if (l.size() != 1 || r.size() != 1)
955 Type lhs = l.front();
956 Type rhs = r.front();
958 if (llvm::isa<ShapeType>(lhs) || llvm::isa<ShapeType>(rhs))
968void CstrBroadcastableOp::getCanonicalizationPatterns(
973 patterns.
add<CanonicalizeCastExtentTensorOperandsPattern<CstrBroadcastableOp>,
974 CstrBroadcastableEqOps,
975 RemoveDuplicateOperandsPattern<CstrBroadcastableOp>,
976 RemoveEmptyShapeOperandsPattern<CstrBroadcastableOp>>(context);
982 bool nonScalarSeen =
false;
984 if (!a || llvm::cast<DenseIntElementsAttr>(a).
getNumElements() != 0) {
987 nonScalarSeen =
true;
993OpFoldResult CstrBroadcastableOp::fold(FoldAdaptor adaptor) {
1000 for (
const auto &operand : adaptor.getShapes()) {
1003 extents.push_back(llvm::to_vector<6>(
1004 llvm::cast<DenseIntElementsAttr>(operand).getValues<int64_t>()));
1014 for (
auto shapeValue : getShapes()) {
1015 extents.emplace_back();
1028LogicalResult CstrBroadcastableOp::verify() {
1031 return emitOpError(
"required at least 2 input shapes");
1042 patterns.
add<CstrEqEqOps>(context);
1046 if (llvm::all_of(adaptor.getShapes(), [&](
Attribute a) {
1047 return a && a == adaptor.getShapes().front();
1066OpFoldResult ConstSizeOp::fold(FoldAdaptor) {
return getValueAttr(); }
1068void ConstSizeOp::getAsmResultNames(
1071 llvm::raw_svector_ostream os(buffer);
1072 os <<
"c" << getValue();
1073 setNameFn(getResult(), os.str());
1080OpFoldResult ConstWitnessOp::fold(FoldAdaptor) {
return getPassingAttr(); }
1087 return adaptor.getPred();
1094std::optional<int64_t> DimOp::getConstantIndex() {
1095 if (
auto constSizeOp =
getIndex().getDefiningOp<ConstSizeOp>())
1096 return constSizeOp.getValue().getLimitedValue();
1097 if (
auto constantOp =
getIndex().getDefiningOp<arith::ConstantOp>())
1098 return llvm::cast<IntegerAttr>(constantOp.getValue()).getInt();
1099 return std::nullopt;
1103 Type valType = getValue().getType();
1104 auto valShapedType = llvm::dyn_cast<ShapedType>(valType);
1105 if (!valShapedType || !valShapedType.hasRank())
1107 std::optional<int64_t>
index = getConstantIndex();
1108 if (!
index.has_value())
1110 if (
index.value() < 0 ||
index.value() >= valShapedType.getRank())
1112 auto extent = valShapedType.getDimSize(*
index);
1113 if (ShapedType::isDynamic(extent))
1115 return IntegerAttr::get(IndexType::get(
getContext()), extent);
1118LogicalResult mlir::shape::DimOp::inferReturnTypes(
1119 MLIRContext *context, std::optional<Location> location,
1121 inferredReturnTypes.assign({adaptor.getIndex().getType()});
1134 auto lhs = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getLhs());
1137 auto rhs = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getRhs());
1138 if (!rhs || rhs.getValue().isZero())
1143 APInt quotient, remainder;
1144 APInt::sdivrem(lhs.getValue(), rhs.getValue(), quotient, remainder);
1145 if (quotient.isNegative() && !remainder.isZero()) {
1150 return IntegerAttr::get(indexTy, quotient);
1153LogicalResult mlir::shape::DivOp::inferReturnTypes(
1154 MLIRContext *context, std::optional<Location> location,
1156 if (llvm::isa<SizeType>(adaptor.getLhs().getType()) ||
1157 llvm::isa<SizeType>(adaptor.getRhs().getType()))
1158 inferredReturnTypes.assign({SizeType::get(context)});
1160 inferredReturnTypes.assign({IndexType::get(context)});
1176 bool allSame =
true;
1177 if (!adaptor.getShapes().empty() && !adaptor.getShapes().front())
1179 for (
Attribute operand : adaptor.getShapes().drop_front()) {
1182 allSame = allSame && operand == adaptor.getShapes().front();
1201 patterns.
add<SizeToIndexToSizeCanonicalization>(context);
1210 for (
Attribute attr : adaptor.getExtents()) {
1211 auto intAttr = llvm::dyn_cast_if_present<IntegerAttr>(attr);
1214 extents.push_back(intAttr.getInt());
1226 result.getOrAddProperties<Properties>().sym_name =
1230FuncOp FunctionLibraryOp::getShapeFunction(
Operation *op) {
1231 auto attr = llvm::dyn_cast_or_null<FlatSymbolRefAttr>(
1235 return lookupSymbol<FuncOp>(attr);
1238ParseResult FunctionLibraryOp::parse(
OpAsmParser &parser,
1241 StringAttr nameAttr;
1249 auto *bodyRegion =
result.addRegion();
1256 DictionaryAttr mappingAttr;
1268 (*this)->getDiscardableAttrDictionary().getValue());
1280FuncOp FuncOp::create(
Location location, StringRef name, FunctionType type,
1284 FuncOp::build(builder, state, name, type, attrs);
1287FuncOp FuncOp::create(
Location location, StringRef name, FunctionType type,
1292FuncOp FuncOp::create(
Location location, StringRef name, FunctionType type,
1295 FuncOp
func = create(location, name, type, attrs);
1296 func.setAllArgAttrs(argAttrs);
1306 TypeAttr::get(type));
1310 if (argAttrs.empty())
1312 assert(type.getNumInputs() == argAttrs.size());
1314 builder, state, argAttrs, {},
1315 getArgAttrsAttrName(state.
name), getResAttrsAttrName(state.
name));
1319 auto buildFuncType =
1322 std::string &) {
return builder.
getFunctionType(argTypes, results); };
1326 getFunctionTypeAttrName(
result.name), buildFuncType,
1327 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
1332 p, *
this,
false, getFunctionTypeAttrName(),
1333 getArgAttrsAttrName(), getResAttrsAttrName());
1340std::optional<int64_t> GetExtentOp::getConstantDim() {
1341 if (
auto constSizeOp = getDim().getDefiningOp<ConstSizeOp>())
1342 return constSizeOp.getValue().getLimitedValue();
1343 if (
auto constantOp = getDim().getDefiningOp<arith::ConstantOp>())
1344 return llvm::cast<IntegerAttr>(constantOp.getValue()).getInt();
1345 return std::nullopt;
1350 llvm::dyn_cast_if_present<DenseIntElementsAttr>(adaptor.getShape());
1353 std::optional<int64_t> dim = getConstantDim();
1354 if (!dim.has_value())
1356 if (dim.value() >= elements.getNumElements())
1358 if (dim.value() < 0)
1360 return elements.getValues<
Attribute>()[(uint64_t)dim.value()];
1365 auto loc =
result.location;
1367 if (llvm::isa<ShapeType>(
shape.getType())) {
1368 Value dim = ConstSizeOp::create(builder, loc, dimAttr);
1377LogicalResult mlir::shape::GetExtentOp::inferReturnTypes(
1378 MLIRContext *context, std::optional<Location> location,
1380 inferredReturnTypes.assign({IndexType::get(context)});
1384bool mlir::shape::GetExtentOp::isCompatibleReturnTypes(
TypeRange l,
1398 patterns.
add<RemoveDuplicateOperandsPattern<IsBroadcastableOp>>(context);
1401OpFoldResult IsBroadcastableOp::fold(FoldAdaptor adaptor) {
1403 if (adaptor.getShapes().size() < 2) {
1414LogicalResult mlir::shape::MeetOp::inferReturnTypes(
1415 MLIRContext *context, std::optional<Location> location,
1417 if (adaptor.getOperands().empty())
1420 auto isShapeType = [](
Type arg) {
1421 if (llvm::isa<ShapeType>(arg))
1428 for (
auto t : drop_begin(types)) {
1430 if (!llvm::isa<ShapeType, SizeType>(l))
1434 if (llvm::isa<SizeType>(l)) {
1435 if (llvm::isa<SizeType, IndexType>(r))
1439 }
else if (llvm::isa<IndexType>(l)) {
1440 if (llvm::isa<IndexType>(r))
1444 }
else if (llvm::isa<ShapeType>(l)) {
1451 auto rank1 = llvm::cast<RankedTensorType>(l).getShape()[0];
1452 auto rank2 = llvm::cast<RankedTensorType>(r).getShape()[0];
1453 if (ShapedType::isDynamic(rank1))
1455 else if (ShapedType::isDynamic(rank2))
1457 else if (rank1 != rank2)
1463 inferredReturnTypes.assign({
acc});
1468 if (l.size() != 1 || r.size() != 1)
1473 Type lhs = l.front();
1474 Type rhs = r.front();
1476 if (!llvm::isa<ShapeType, SizeType>(lhs))
1477 std::swap(lhs, rhs);
1479 if (llvm::isa<SizeType>(lhs))
1480 return llvm::isa<SizeType, IndexType>(rhs);
1481 if (llvm::isa<ShapeType>(lhs))
1482 return llvm::isa<ShapeType, TensorType>(rhs);
1495 llvm::dyn_cast_if_present<DenseIntElementsAttr>(adaptor.getShape());
1518struct RankShapeOfCanonicalizationPattern
1520 using OpRewritePattern<shape::RankOp>::OpRewritePattern;
1522 LogicalResult matchAndRewrite(shape::RankOp op,
1523 PatternRewriter &rewriter)
const override {
1524 auto shapeOfOp = op.getShape().getDefiningOp<ShapeOfOp>();
1527 auto rankedTensorType =
1528 llvm::dyn_cast<RankedTensorType>(shapeOfOp.getArg().getType());
1529 if (!rankedTensorType)
1531 int64_t rank = rankedTensorType.getRank();
1532 if (llvm::isa<IndexType>(op.getType())) {
1535 }
else if (llvm::isa<shape::SizeType>(op.getType())) {
1547 patterns.
add<RankShapeOfCanonicalizationPattern>(context);
1550LogicalResult mlir::shape::RankOp::inferReturnTypes(
1551 MLIRContext *context, std::optional<Location> location,
1553 if (llvm::isa<ShapeType>(adaptor.getShape().getType()))
1554 inferredReturnTypes.assign({SizeType::get(context)});
1556 inferredReturnTypes.assign({IndexType::get(context)});
1579 for (
auto value : llvm::cast<DenseIntElementsAttr>(
shape))
1585LogicalResult mlir::shape::NumElementsOp::inferReturnTypes(
1586 MLIRContext *context, std::optional<Location> location,
1587 NumElementsOp::Adaptor adaptor,
1589 if (llvm::isa<ShapeType>(adaptor.getShape().getType()))
1590 inferredReturnTypes.assign({SizeType::get(context)});
1592 inferredReturnTypes.assign({IndexType::get(context)});
1596bool mlir::shape::NumElementsOp::isCompatibleReturnTypes(
TypeRange l,
1602LogicalResult shape::NumElementsOp::verify() {
1612 if (getLhs() == getRhs())
1617LogicalResult mlir::shape::MaxOp::inferReturnTypes(
1618 MLIRContext *context, std::optional<Location> location,
1620 if (adaptor.getLhs().getType() == adaptor.getRhs().getType())
1621 inferredReturnTypes.assign({adaptor.getLhs().getType()});
1623 inferredReturnTypes.assign({SizeType::get(context)});
1628 if (l.size() != 1 || r.size() != 1)
1630 if (llvm::isa<ShapeType>(l.front()) && llvm::isa<ShapeType>(r.front()))
1632 if (llvm::isa<SizeType>(l.front()) && llvm::isa<SizeType>(r.front()))
1643 if (getLhs() == getRhs())
1648LogicalResult mlir::shape::MinOp::inferReturnTypes(
1649 MLIRContext *context, std::optional<Location> location,
1651 if (adaptor.getLhs().getType() == adaptor.getRhs().getType())
1652 inferredReturnTypes.assign({adaptor.getLhs().getType()});
1654 inferredReturnTypes.assign({SizeType::get(context)});
1659 if (l.size() != 1 || r.size() != 1)
1661 if (llvm::isa<ShapeType>(l.front()) && llvm::isa<ShapeType>(r.front()))
1663 if (llvm::isa<SizeType>(l.front()) && llvm::isa<SizeType>(r.front()))
1673 auto lhs = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getLhs());
1676 auto rhs = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getRhs());
1679 APInt folded = lhs.getValue() * rhs.getValue();
1681 return IntegerAttr::get(indexTy, folded);
1684LogicalResult mlir::shape::MulOp::inferReturnTypes(
1685 MLIRContext *context, std::optional<Location> location,
1687 if (llvm::isa<SizeType>(adaptor.getLhs().getType()) ||
1688 llvm::isa<SizeType>(adaptor.getRhs().getType()))
1689 inferredReturnTypes.assign({SizeType::get(context)});
1691 inferredReturnTypes.assign({IndexType::get(context)});
1708struct ShapeOfOpToConstShapeOp :
public OpRewritePattern<shape::ShapeOfOp> {
1709 using OpRewritePattern<shape::ShapeOfOp>::OpRewritePattern;
1711 LogicalResult matchAndRewrite(shape::ShapeOfOp op,
1712 PatternRewriter &rewriter)
const override {
1713 auto type = llvm::dyn_cast<ShapedType>(op.getArg().getType());
1714 if (!type || !type.hasStaticShape())
1717 Type resultType = op.getResult().getType();
1718 Location loc = op.getLoc();
1720 isa<ShapeType>(resultType)
1722 : RankedTensorType::get({type.getRank()}, rewriter.
getIndexType());
1724 ConstShapeOp::create(rewriter, loc, constResType,
1727 if (constShape.
getType() != resultType)
1729 tensor::CastOp::create(rewriter, loc, resultType, constShape);
1748 using OpRewritePattern<shape::ShapeOfOp>::OpRewritePattern;
1750 LogicalResult matchAndRewrite(shape::ShapeOfOp op,
1751 PatternRewriter &rewriter)
const override {
1752 auto tensorReshapeOp = op.getArg().getDefiningOp<tensor::ReshapeOp>();
1753 if (!tensorReshapeOp)
1755 if (!isa<TensorType>(op.getType()))
1766 Value shape = tensorReshapeOp.getShape();
1768 auto opTensorTy = cast<RankedTensorType>(op.getType());
1769 auto shapeTensorTy = cast<RankedTensorType>(shape.
getType());
1771 if (opTensorTy != shapeTensorTy) {
1772 if (opTensorTy.getElementType() == shapeTensorTy.getElementType())
1774 tensor::CastOp::create(rewriter, op.getLoc(), opTensorTy, shape);
1776 shape = arith::IndexCastOp::create(rewriter, op.getLoc(), opTensorTy,
1795 using OpRewritePattern<tensor::CastOp>::OpRewritePattern;
1797 LogicalResult matchAndRewrite(tensor::CastOp op,
1798 PatternRewriter &rewriter)
const override {
1799 auto ty = llvm::dyn_cast<RankedTensorType>(op.getType());
1800 if (!ty || ty.getRank() != 1)
1803 auto shapeOfOp = op.getSource().getDefiningOp<ShapeOfOp>();
1808 auto argTy = llvm::dyn_cast<RankedTensorType>(shapeOfOp.getArg().getType());
1809 if (!argTy || (!ty.isDynamicDim(0) && ty.getDimSize(0) != argTy.getRank()))
1820 patterns.
add<ShapeOfCastExtentTensor, ShapeOfFromReshape,
1821 ExtractFromShapeOfExtentTensor, ShapeOfOpToConstShapeOp>(
1825LogicalResult mlir::shape::ShapeOfOp::inferReturnTypes(
1826 MLIRContext *context, std::optional<Location> location,
1828 if (llvm::isa<ValueShapeType>(adaptor.getArg().getType()))
1829 inferredReturnTypes.assign({ShapeType::get(context)});
1831 auto shapedTy = llvm::cast<ShapedType>(adaptor.getArg().getType());
1833 shapedTy.hasRank() ? shapedTy.getRank() : ShapedType::kDynamic;
1834 Type indexTy = IndexType::get(context);
1835 Type extentTensorTy = RankedTensorType::get({rank}, indexTy);
1836 inferredReturnTypes.assign({extentTensorTy});
1842 if (l.size() != 1 || r.size() != 1)
1847 Type lhs = l.front();
1848 Type rhs = r.front();
1850 if (!llvm::isa<ShapeType, ShapedType>(lhs) ||
1851 !llvm::isa<ShapeType, ShapedType>(rhs))
1854 if (llvm::isa<ShapeType>(lhs) || llvm::isa<ShapeType>(rhs))
1863LogicalResult shape::ShapeOfOp::verify() {
1881 patterns.
add<IndexToSizeToIndexCanonicalization>(context);
1885 if (inputs.size() != 1 || outputs.size() != 1)
1887 return llvm::isa<IndexType, SizeType>(inputs[0]) &&
1888 llvm::isa<IndexType>(outputs[0]);
1895LogicalResult shape::YieldOp::verify() {
1896 auto *parentOp = (*this)->getParentOp();
1897 auto results = parentOp->getResults();
1898 auto operands = getOperands();
1901 return emitOpError() <<
"number of operands does not match number of "
1902 "results of its parent";
1903 for (
auto e : llvm::zip(results, operands))
1905 return emitOpError() <<
"types mismatch between yield op and its parent";
1914LogicalResult SplitAtOp::fold(FoldAdaptor adaptor,
1916 if (!adaptor.getOperand() || !adaptor.getIndex())
1919 llvm::to_vector<6>(llvm::cast<DenseIntElementsAttr>(adaptor.getOperand())
1922 auto splitPoint = llvm::cast<IntegerAttr>(adaptor.getIndex()).getInt();
1926 if (-rank > splitPoint || splitPoint > rank)
1929 splitPoint +=
shape.size();
1930 Builder builder(adaptor.getOperand().getContext());
1940OpFoldResult ToExtentTensorOp::fold(FoldAdaptor adaptor) {
1941 if (!adaptor.getInput())
1945 llvm::to_vector<6>(llvm::cast<DenseIntElementsAttr>(adaptor.getInput())
1947 auto type = RankedTensorType::get({
static_cast<int64_t>(
shape.size())},
1953 if (inputs.size() != 1 || outputs.size() != 1)
1955 if (
auto inputTensor = llvm::dyn_cast<RankedTensorType>(inputs[0])) {
1956 if (!llvm::isa<IndexType>(inputTensor.getElementType()) ||
1957 inputTensor.getRank() != 1)
1959 }
else if (!llvm::isa<ShapeType>(inputs[0])) {
1963 TensorType outputTensor = llvm::dyn_cast<TensorType>(outputs[0]);
1964 return outputTensor && llvm::isa<IndexType>(outputTensor.
getElementType());
1975 result.addOperands(initVals);
1982 if (
auto tensorType = llvm::dyn_cast<TensorType>(
shape.getType()))
1983 elementType = tensorType.getElementType();
1985 elementType = SizeType::get(builder.
getContext());
1988 for (
Value initVal : initVals) {
1989 bodyBlock->
addArgument(initVal.getType(), initVal.getLoc());
1990 result.addTypes(initVal.getType());
1994LogicalResult ReduceOp::verify() {
1999 auto blockArgsCount = getInitVals().size() + 2;
2001 return emitOpError() <<
"ReduceOp body is expected to have "
2002 << blockArgsCount <<
" arguments";
2007 "argument 0 of ReduceOp body is expected to be of IndexType");
2014 if (!llvm::isa<SizeType>(extentTy))
2015 return emitOpError(
"argument 1 of ReduceOp body is expected to be of "
2016 "SizeType if the ReduceOp operates on a ShapeType");
2018 if (!llvm::isa<IndexType>(extentTy))
2020 "argument 1 of ReduceOp body is expected to be of IndexType if the "
2021 "ReduceOp operates on an extent tensor");
2024 for (
const auto &type : llvm::enumerate(getInitVals()))
2026 return emitOpError() <<
"type mismatch between argument "
2028 <<
" of ReduceOp body and initial value "
2036 Type shapeOrExtentTensorType;
2045 if (parser.
resolveOperand(operands.front(), shapeOrExtentTensorType,
2064 p <<
'(' <<
getShape() <<
", " << getInitVals()
2072#define GET_OP_CLASSES
2073#include "mlir/Dialect/Shape/IR/ShapeOps.cpp.inc"
2075#define GET_TYPEDEF_CLASSES
2076#include "mlir/Dialect/Shape/IR/ShapeOpsTypes.cpp.inc"
getNumOperands() - 1))) return failure()
Given a list of lists of parsed operands, populates uniqueOperands with unique operands.
static bool isErrorPropagationPossible(TypeRange operandTypes)
static bool hasAtMostSingleNonScalar(ArrayRef< Attribute > attributes)
static LogicalResult verifyShapeOrExtentTensorOp(Operation *op)
static bool eachHasOnlyOneOfTypes(TypeRange typeRange)
static LogicalResult verifySizeOrIndexOp(Operation *op)
static int64_t product(ArrayRef< int64_t > vals)
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
ParseResult parseSymbolName(StringAttr &result)
Parse an -identifier and store it (without the '@' symbol) in a string attribute.
@ Paren
Parens surrounding zero or more operands.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result)=0
Parse a named dictionary into 'result' if the attributes keyword is present.
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalArrowTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional arrow followed by a type list.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
virtual void printAttributeWithoutType(Attribute attr)
Print the given attribute without its type.
virtual void printType(Type type)
virtual void printSymbolName(StringRef symbolRef)
Print the given string as a symbol reference, i.e.
void printOptionalArrowTypeList(TypeRange &&types)
Print an optional arrow followed by a type list.
Attributes are known-constant values of operations.
MLIRContext * getContext() const
Return the context this attribute belongs to.
Block represents an ordered list of Operations.
BlockArgument getArgument(unsigned i)
unsigned getNumArguments()
Operation * getTerminator()
Get the terminator operation of this block.
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
static BoolAttr get(MLIRContext *context, bool value)
This class is a general helper class for creating context-global objects like types,...
IntegerAttr getIndexAttr(int64_t value)
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
Ty getType(Args &&...args)
Get or construct an instance of the type Ty with provided arguments.
StringAttr getStringAttr(const Twine &bytes)
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
MLIRContext * getContext() const
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
void append(StringRef name, Attribute attr)
Add an attribute with the specified name.
NamedAttribute represents a combination of a name and an Attribute value.
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual ParseResult parseRegion(Region ®ion, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region.
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
virtual ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void printOptionalAttrDictWithKeyword(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary prefixed with 'attribute...
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
This class represents a single result from folding an operation.
A trait used to provide symbol table functionalities to a region operation.
StringAttr getIdentifier() const
Return the name of this operation as a StringAttr.
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
iterator_range< dialect_attr_iterator > dialect_attr_range
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OperationName getName()
The name of an operation is the key identifier for it.
operand_type_range getOperandTypes()
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, PropertyRef properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
result_type_range getResultTypes()
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
unsigned getNumResults()
Return the number of results held by this operation.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class represents a point being branched from in the methods of the RegionBranchOpInterface.
bool isParent() const
Returns true if branching from the parent op.
This class represents a successor of a region.
bool isOperation() const
Return true if the successor is an operation.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Block * splitBlock(Block *block, Block::iterator before)
Split the operations starting at "before" (inclusive) out of the given block into a new block,...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void mergeBlocks(Block *source, Block *dest, ValueRange argValues={})
Inline the operations of block 'source' into the end of block 'dest'.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
Tensor types represent multi-dimensional arrays, and have two variants: RankedTensorType and Unranked...
Type getElementType() const
Returns the element type of this tensor type.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
ValueTypeRange< ValueRange > type_range
Type front()
Return first type in the range.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
A named class for passing around the variadic flag.
bool staticallyKnownBroadcastable(ArrayRef< SmallVector< int64_t, 6 > > shapes)
Returns true if a broadcast between n shapes is guaranteed to be successful and not result in an erro...
bool getBroadcastedShape(ArrayRef< int64_t > shape1, ArrayRef< int64_t > shape2, SmallVectorImpl< int64_t > &resultShape)
Returns true and sets resultShape to the broadcasted shape from the two given shapes if they are broa...
void addArgAndResultAttrs(Builder &builder, OperationState &result, ArrayRef< DictionaryAttr > argAttrs, ArrayRef< DictionaryAttr > resultAttrs, StringAttr argAttrsName, StringAttr resAttrsName)
Adds argument and result attributes, provided as argAttrs and resultAttrs arguments,...
void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic, StringRef typeAttrName, StringAttr argAttrsName, StringAttr resAttrsName)
Printer implementation for function-like operations.
ParseResult parseFunctionOp(OpAsmParser &parser, OperationState &result, bool allowVariadic, StringAttr typeAttrName, FuncTypeBuilder funcTypeBuilder, StringAttr argAttrsName, StringAttr resAttrsName)
Parser implementation for function-like operations.
DynamicAPInt getIndex(const ConeV &cone)
Get the index of a cone, i.e., the volume of the parallelepiped spanned by its generators,...
bool isExtentTensorType(Type)
LogicalResult getShapeVec(Value input, SmallVectorImpl< int64_t > &shapeValues)
RankedTensorType getExtentTensorType(MLIRContext *ctx, int64_t rank=ShapedType::kDynamic)
Alias type for extent tensors.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
LogicalResult verifyCompatibleShapes(TypeRange types1, TypeRange types2)
Returns success if the given two arrays have the same number of elements and each pair wise entries h...
Attribute constFoldBinaryOp(ArrayRef< Attribute > operands, Type resultType, CalculationT &&calculate)
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
LogicalResult emitOptionalError(std::optional< Location > loc, Args &&...args)
Overloads of the above emission functions that take an optionally null location.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::SetVector< T, Vector, Set, N > SetVector
detail::constant_int_predicate_matcher m_Zero()
Matches a constant scalar / vector splat / tensor splat integer zero.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
llvm::function_ref< Fn > function_ref
This is the representation of an operand reference.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
This represents an operation in an abstracted form, suitable for use with the builder APIs.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
Region * addRegion()
Create a region that should be attached to the operation.