24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallBitVector.h"
35 return arith::ConstantOp::materialize(builder, value, type, loc);
48 auto cast = operand.get().getDefiningOp<CastOp>();
49 if (cast && operand.get() != inner &&
50 !llvm::isa<UnrankedMemRefType>(cast.getOperand().getType())) {
51 operand.set(cast.getOperand());
55 return success(folded);
61 if (
auto memref = llvm::dyn_cast<MemRefType>(type))
63 if (
auto memref = llvm::dyn_cast<UnrankedMemRefType>(type))
70 auto memrefType = llvm::cast<MemRefType>(value.
getType());
71 if (memrefType.isDynamicDim(dim))
72 return builder.
createOrFold<memref::DimOp>(loc, value, dim);
79 auto memrefType = llvm::cast<MemRefType>(value.
getType());
81 for (int64_t i = 0; i < memrefType.getRank(); ++i)
98 assert(constValues.size() == values.size() &&
99 "incorrect number of const values");
102 if (ShapedType::isStatic(cstVal)) {
118 void AllocOp::getAsmResultNames(
120 setNameFn(getResult(),
"alloc");
123 void AllocaOp::getAsmResultNames(
125 setNameFn(getResult(),
"alloca");
128 template <
typename AllocLikeOp>
130 static_assert(llvm::is_one_of<AllocLikeOp, AllocOp, AllocaOp>::value,
131 "applies to only alloc or alloca");
132 auto memRefType = llvm::dyn_cast<MemRefType>(op.getResult().getType());
134 return op.emitOpError(
"result must be a memref");
136 if (op.getDynamicSizes().size() != memRefType.getNumDynamicDims())
137 return op.emitOpError(
"dimension operand count does not equal memref "
138 "dynamic dimension count");
140 unsigned numSymbols = 0;
141 if (!memRefType.getLayout().isIdentity())
142 numSymbols = memRefType.getLayout().getAffineMap().getNumSymbols();
143 if (op.getSymbolOperands().size() != numSymbols)
144 return op.emitOpError(
"symbol operand count does not equal memref symbol "
146 << numSymbols <<
", got " << op.getSymbolOperands().size();
157 "requires an ancestor op with AutomaticAllocationScope trait");
164 template <
typename AllocLikeOp>
168 LogicalResult matchAndRewrite(AllocLikeOp alloc,
172 if (llvm::none_of(alloc.getDynamicSizes(), [](
Value operand) {
174 if (!matchPattern(operand, m_ConstantInt(&constSizeArg)))
176 return constSizeArg.isNonNegative();
180 auto memrefType = alloc.getType();
185 newShapeConstants.reserve(memrefType.getRank());
188 unsigned dynamicDimPos = 0;
189 for (
unsigned dim = 0, e = memrefType.getRank(); dim < e; ++dim) {
190 int64_t dimSize = memrefType.getDimSize(dim);
192 if (ShapedType::isStatic(dimSize)) {
193 newShapeConstants.push_back(dimSize);
196 auto dynamicSize = alloc.getDynamicSizes()[dynamicDimPos];
199 constSizeArg.isNonNegative()) {
201 newShapeConstants.push_back(constSizeArg.getZExtValue());
204 newShapeConstants.push_back(ShapedType::kDynamic);
205 dynamicSizes.push_back(dynamicSize);
211 MemRefType newMemRefType =
213 assert(dynamicSizes.size() == newMemRefType.getNumDynamicDims());
216 auto newAlloc = AllocLikeOp::create(rewriter, alloc.getLoc(), newMemRefType,
217 dynamicSizes, alloc.getSymbolOperands(),
218 alloc.getAlignmentAttr());
226 template <
typename T>
230 LogicalResult matchAndRewrite(T alloc,
232 if (llvm::any_of(alloc->getUsers(), [&](
Operation *op) {
233 if (auto storeOp = dyn_cast<StoreOp>(op))
234 return storeOp.getValue() == alloc;
235 return !isa<DeallocOp>(op);
239 for (
Operation *user : llvm::make_early_inc_range(alloc->getUsers()))
250 results.
add<SimplifyAllocConst<AllocOp>, SimplifyDeadAlloc<AllocOp>>(context);
255 results.
add<SimplifyAllocConst<AllocaOp>, SimplifyDeadAlloc<AllocaOp>>(
264 auto sourceType = llvm::cast<MemRefType>(getOperand(0).
getType());
265 MemRefType resultType =
getType();
268 if (!sourceType.getLayout().isIdentity())
269 return emitError(
"unsupported layout for source memref type ")
273 if (!resultType.getLayout().isIdentity())
274 return emitError(
"unsupported layout for result memref type ")
278 if (sourceType.getMemorySpace() != resultType.getMemorySpace())
279 return emitError(
"different memory spaces specified for source memref "
281 << sourceType <<
" and result memref type " << resultType;
284 if (sourceType.getElementType() != resultType.getElementType())
285 return emitError(
"different element types specified for source memref "
287 << sourceType <<
" and result memref type " << resultType;
290 if (resultType.getNumDynamicDims() && !getDynamicResultSize())
291 return emitError(
"missing dimension operand for result type ")
293 if (!resultType.getNumDynamicDims() && getDynamicResultSize())
294 return emitError(
"unnecessary dimension operand for result type ")
302 results.
add<SimplifyDeadAlloc<ReallocOp>>(context);
310 bool printBlockTerminators =
false;
313 if (!getResults().empty()) {
314 p <<
" -> (" << getResultTypes() <<
")";
315 printBlockTerminators =
true;
320 printBlockTerminators);
336 AllocaScopeOp::ensureTerminator(*bodyRegion, parser.
getBuilder(),
346 void AllocaScopeOp::getSuccessorRegions(
359 MemoryEffectOpInterface
interface = dyn_cast<MemoryEffectOpInterface>(op);
365 if (isa<SideEffects::AutomaticAllocationScopeResource>(
366 effect->getResource()))
382 MemoryEffectOpInterface
interface = dyn_cast<MemoryEffectOpInterface>(op);
388 if (isa<SideEffects::AutomaticAllocationScopeResource>(
389 effect->getResource()))
413 bool hasPotentialAlloca =
426 if (hasPotentialAlloca) {
459 if (!lastParentWithoutScope ||
472 lastParentWithoutScope = lastParentWithoutScope->
getParentOp();
473 if (!lastParentWithoutScope ||
480 Region *containingRegion =
nullptr;
481 for (
auto &r : lastParentWithoutScope->
getRegions()) {
482 if (r.isAncestor(op->getParentRegion())) {
483 assert(containingRegion ==
nullptr &&
484 "only one region can contain the op");
485 containingRegion = &r;
488 assert(containingRegion &&
"op must be contained in a region");
498 return containingRegion->isAncestor(v.getParentRegion());
501 toHoist.push_back(alloc);
508 for (
auto *op : toHoist) {
509 auto *cloned = rewriter.
clone(*op);
510 rewriter.
replaceOp(op, cloned->getResults());
526 if (!llvm::isPowerOf2_32(getAlignment()))
527 return emitOpError(
"alignment must be power of 2");
531 void AssumeAlignmentOp::getAsmResultNames(
533 setNameFn(getResult(),
"assume_align");
536 OpFoldResult AssumeAlignmentOp::fold(FoldAdaptor adaptor) {
537 auto source = getMemref().getDefiningOp<AssumeAlignmentOp>();
540 if (source.getAlignment() != getAlignment())
550 setNameFn(getResult(),
"cast");
591 MemRefType sourceType =
592 llvm::dyn_cast<MemRefType>(castOp.getSource().getType());
593 MemRefType resultType = llvm::dyn_cast<MemRefType>(castOp.getType());
596 if (!sourceType || !resultType)
600 if (sourceType.getElementType() != resultType.getElementType())
604 if (sourceType.getRank() != resultType.getRank())
608 int64_t sourceOffset, resultOffset;
610 if (
failed(sourceType.getStridesAndOffset(sourceStrides, sourceOffset)) ||
611 failed(resultType.getStridesAndOffset(resultStrides, resultOffset)))
615 for (
auto it : llvm::zip(sourceType.getShape(), resultType.getShape())) {
616 auto ss = std::get<0>(it), st = std::get<1>(it);
618 if (ShapedType::isDynamic(ss) && ShapedType::isStatic(st))
623 if (sourceOffset != resultOffset)
624 if (ShapedType::isDynamic(sourceOffset) &&
625 ShapedType::isStatic(resultOffset))
629 for (
auto it : llvm::zip(sourceStrides, resultStrides)) {
630 auto ss = std::get<0>(it), st = std::get<1>(it);
632 if (ShapedType::isDynamic(ss) && ShapedType::isStatic(st))
640 if (inputs.size() != 1 || outputs.size() != 1)
642 Type a = inputs.front(), b = outputs.front();
643 auto aT = llvm::dyn_cast<MemRefType>(a);
644 auto bT = llvm::dyn_cast<MemRefType>(b);
646 auto uaT = llvm::dyn_cast<UnrankedMemRefType>(a);
647 auto ubT = llvm::dyn_cast<UnrankedMemRefType>(b);
650 if (aT.getElementType() != bT.getElementType())
652 if (aT.getLayout() != bT.getLayout()) {
653 int64_t aOffset, bOffset;
655 if (
failed(aT.getStridesAndOffset(aStrides, aOffset)) ||
656 failed(bT.getStridesAndOffset(bStrides, bOffset)) ||
657 aStrides.size() != bStrides.size())
664 auto checkCompatible = [](int64_t a, int64_t b) {
665 return (ShapedType::isDynamic(a) || ShapedType::isDynamic(b) || a == b);
667 if (!checkCompatible(aOffset, bOffset))
669 for (
const auto &aStride :
enumerate(aStrides))
670 if (!checkCompatible(aStride.value(), bStrides[aStride.index()]))
673 if (aT.getMemorySpace() != bT.getMemorySpace())
677 if (aT.getRank() != bT.getRank())
680 for (
unsigned i = 0, e = aT.getRank(); i != e; ++i) {
681 int64_t aDim = aT.getDimSize(i), bDim = bT.getDimSize(i);
682 if (ShapedType::isStatic(aDim) && ShapedType::isStatic(bDim) &&
696 auto aEltType = (aT) ? aT.getElementType() : uaT.getElementType();
697 auto bEltType = (bT) ? bT.getElementType() : ubT.getElementType();
698 if (aEltType != bEltType)
701 auto aMemSpace = (aT) ? aT.getMemorySpace() : uaT.getMemorySpace();
702 auto bMemSpace = (bT) ? bT.getMemorySpace() : ubT.getMemorySpace();
703 return aMemSpace == bMemSpace;
723 LogicalResult matchAndRewrite(CopyOp copyOp,
725 if (copyOp.getSource() != copyOp.getTarget())
740 LogicalResult matchAndRewrite(CopyOp copyOp,
742 if (isEmptyMemRef(copyOp.getSource().getType()) ||
743 isEmptyMemRef(copyOp.getTarget().getType())) {
755 results.
add<FoldEmptyCopy, FoldSelfCopy>(context);
762 for (
OpOperand &operand : op->getOpOperands()) {
765 operand.set(castOp.getOperand());
772 LogicalResult CopyOp::fold(FoldAdaptor adaptor,
783 LogicalResult DeallocOp::fold(FoldAdaptor adaptor,
794 setNameFn(getResult(),
"dim");
801 build(builder, result, source, indexValue);
804 std::optional<int64_t> DimOp::getConstantIndex() {
813 auto rankedSourceType = dyn_cast<MemRefType>(getSource().
getType());
814 if (!rankedSourceType)
825 setResultRange(getResult(),
834 std::map<int64_t, unsigned> numOccurences;
835 for (
auto val : vals)
836 numOccurences[val]++;
837 return numOccurences;
847 static FailureOr<llvm::SmallBitVector>
850 llvm::SmallBitVector unusedDims(originalType.getRank());
851 if (originalType.getRank() == reducedType.getRank())
855 if (
auto attr = llvm::dyn_cast_if_present<Attribute>(dim.value()))
856 if (llvm::cast<IntegerAttr>(attr).getInt() == 1)
857 unusedDims.set(dim.index());
861 if (
static_cast<int64_t
>(unusedDims.count()) + reducedType.getRank() ==
862 originalType.getRank())
866 int64_t originalOffset, candidateOffset;
868 originalType.getStridesAndOffset(originalStrides, originalOffset)) ||
870 reducedType.getStridesAndOffset(candidateStrides, candidateOffset)))
882 std::map<int64_t, unsigned> currUnaccountedStrides =
884 std::map<int64_t, unsigned> candidateStridesNumOccurences =
886 for (
size_t dim = 0, e = unusedDims.size(); dim != e; ++dim) {
887 if (!unusedDims.test(dim))
889 int64_t originalStride = originalStrides[dim];
890 if (currUnaccountedStrides[originalStride] >
891 candidateStridesNumOccurences[originalStride]) {
893 currUnaccountedStrides[originalStride]--;
896 if (currUnaccountedStrides[originalStride] ==
897 candidateStridesNumOccurences[originalStride]) {
899 unusedDims.reset(dim);
902 if (currUnaccountedStrides[originalStride] <
903 candidateStridesNumOccurences[originalStride]) {
910 if ((int64_t)unusedDims.count() + reducedType.getRank() !=
911 originalType.getRank())
917 MemRefType sourceType = getSourceType();
918 MemRefType resultType =
getType();
919 FailureOr<llvm::SmallBitVector> unusedDims =
921 assert(succeeded(unusedDims) &&
"unable to find unused dims of subview");
927 auto index = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getIndex());
932 auto memrefType = llvm::dyn_cast<MemRefType>(getSource().
getType());
938 int64_t indexVal = index.getInt();
939 if (indexVal < 0 || indexVal >= memrefType.getRank())
943 if (!memrefType.isDynamicDim(index.getInt())) {
945 return builder.getIndexAttr(memrefType.getShape()[index.getInt()]);
949 unsigned unsignedIndex = index.getValue().getZExtValue();
952 Operation *definingOp = getSource().getDefiningOp();
954 if (
auto alloc = dyn_cast_or_null<AllocOp>(definingOp))
955 return *(alloc.getDynamicSizes().begin() +
956 memrefType.getDynamicDimIndex(unsignedIndex));
958 if (
auto alloca = dyn_cast_or_null<AllocaOp>(definingOp))
959 return *(alloca.getDynamicSizes().begin() +
960 memrefType.getDynamicDimIndex(unsignedIndex));
962 if (
auto view = dyn_cast_or_null<ViewOp>(definingOp))
963 return *(view.getDynamicSizes().begin() +
964 memrefType.getDynamicDimIndex(unsignedIndex));
966 if (
auto subview = dyn_cast_or_null<SubViewOp>(definingOp)) {
967 llvm::SmallBitVector unusedDims = subview.getDroppedDims();
968 unsigned resultIndex = 0;
969 unsigned sourceRank = subview.getSourceType().getRank();
970 unsigned sourceIndex = 0;
971 for (
auto i : llvm::seq<unsigned>(0, sourceRank)) {
972 if (unusedDims.test(i))
974 if (resultIndex == unsignedIndex) {
980 assert(subview.isDynamicSize(sourceIndex) &&
981 "expected dynamic subview size");
982 return subview.getDynamicSize(sourceIndex);
985 if (
auto sizeInterface =
986 dyn_cast_or_null<OffsetSizeAndStrideOpInterface>(definingOp)) {
987 assert(sizeInterface.isDynamicSize(unsignedIndex) &&
988 "Expected dynamic subview size");
989 return sizeInterface.getDynamicSize(unsignedIndex);
1005 LogicalResult matchAndRewrite(DimOp dim,
1007 auto reshape = dim.getSource().getDefiningOp<ReshapeOp>();
1011 dim,
"Dim op is not defined by a reshape op.");
1022 if (dim.getIndex().getParentBlock() == reshape->getBlock()) {
1023 if (
auto *definingOp = dim.getIndex().getDefiningOp()) {
1024 if (reshape->isBeforeInBlock(definingOp)) {
1027 "dim.getIndex is not defined before reshape in the same block.");
1032 else if (dim->getBlock() != reshape->getBlock() &&
1033 !dim.getIndex().getParentRegion()->isProperAncestor(
1034 reshape->getParentRegion())) {
1039 dim,
"dim.getIndex does not dominate reshape.");
1047 LoadOp::create(rewriter, loc, reshape.getShape(), dim.getIndex());
1048 if (load.
getType() != dim.getType())
1049 load = arith::IndexCastOp::create(rewriter, loc, dim.getType(), load);
1059 results.
add<DimOfMemRefReshape>(context);
1070 Value elementsPerStride) {
1082 p <<
" " << getSrcMemRef() <<
'[' << getSrcIndices() <<
"], "
1083 << getDstMemRef() <<
'[' << getDstIndices() <<
"], " <<
getNumElements()
1084 <<
", " << getTagMemRef() <<
'[' << getTagIndices() <<
']';
1086 p <<
", " <<
getStride() <<
", " << getNumElementsPerStride();
1089 p <<
" : " << getSrcMemRef().getType() <<
", " << getDstMemRef().getType()
1090 <<
", " << getTagMemRef().getType();
1131 bool isStrided = strideInfo.size() == 2;
1132 if (!strideInfo.empty() && !isStrided) {
1134 "expected two stride related operands");
1139 if (types.size() != 3)
1162 unsigned numOperands = getNumOperands();
1166 if (numOperands < 4)
1167 return emitOpError(
"expected at least 4 operands");
1172 if (!llvm::isa<MemRefType>(getSrcMemRef().
getType()))
1173 return emitOpError(
"expected source to be of memref type");
1174 if (numOperands < getSrcMemRefRank() + 4)
1175 return emitOpError() <<
"expected at least " << getSrcMemRefRank() + 4
1177 if (!getSrcIndices().empty() &&
1178 !llvm::all_of(getSrcIndices().getTypes(),
1180 return emitOpError(
"expected source indices to be of index type");
1183 if (!llvm::isa<MemRefType>(getDstMemRef().
getType()))
1184 return emitOpError(
"expected destination to be of memref type");
1185 unsigned numExpectedOperands = getSrcMemRefRank() + getDstMemRefRank() + 4;
1186 if (numOperands < numExpectedOperands)
1187 return emitOpError() <<
"expected at least " << numExpectedOperands
1189 if (!getDstIndices().empty() &&
1190 !llvm::all_of(getDstIndices().getTypes(),
1192 return emitOpError(
"expected destination indices to be of index type");
1196 return emitOpError(
"expected num elements to be of index type");
1199 if (!llvm::isa<MemRefType>(getTagMemRef().
getType()))
1200 return emitOpError(
"expected tag to be of memref type");
1201 numExpectedOperands += getTagMemRefRank();
1202 if (numOperands < numExpectedOperands)
1203 return emitOpError() <<
"expected at least " << numExpectedOperands
1205 if (!getTagIndices().empty() &&
1206 !llvm::all_of(getTagIndices().getTypes(),
1208 return emitOpError(
"expected tag indices to be of index type");
1212 if (numOperands != numExpectedOperands &&
1213 numOperands != numExpectedOperands + 2)
1214 return emitOpError(
"incorrect number of operands");
1219 !getNumElementsPerStride().
getType().isIndex())
1221 "expected stride and num elements per stride to be of type index");
1227 LogicalResult DmaStartOp::fold(FoldAdaptor adaptor,
1237 LogicalResult DmaWaitOp::fold(FoldAdaptor adaptor,
1245 unsigned numTagIndices = getTagIndices().size();
1246 unsigned tagMemRefRank = getTagMemRefRank();
1247 if (numTagIndices != tagMemRefRank)
1248 return emitOpError() <<
"expected tagIndices to have the same number of "
1249 "elements as the tagMemRef rank, expected "
1250 << tagMemRefRank <<
", but got " << numTagIndices;
1258 void ExtractAlignedPointerAsIndexOp::getAsmResultNames(
1260 setNameFn(getResult(),
"intptr");
1269 LogicalResult ExtractStridedMetadataOp::inferReturnTypes(
1270 MLIRContext *context, std::optional<Location> location,
1271 ExtractStridedMetadataOp::Adaptor adaptor,
1273 auto sourceType = llvm::dyn_cast<MemRefType>(adaptor.getSource().getType());
1277 unsigned sourceRank = sourceType.getRank();
1281 MemRefLayoutAttrInterface{}, sourceType.getMemorySpace());
1283 inferredReturnTypes.push_back(memrefType);
1285 inferredReturnTypes.push_back(indexType);
1287 for (
unsigned i = 0; i < sourceRank * 2; ++i)
1288 inferredReturnTypes.push_back(indexType);
1292 void ExtractStridedMetadataOp::getAsmResultNames(
1294 setNameFn(getBaseBuffer(),
"base_buffer");
1295 setNameFn(getOffset(),
"offset");
1298 if (!getSizes().empty()) {
1299 setNameFn(getSizes().front(),
"sizes");
1300 setNameFn(getStrides().front(),
"strides");
1307 template <
typename Container>
1311 assert(values.size() == maybeConstants.size() &&
1312 " expected values and maybeConstants of the same size");
1313 bool atLeastOneReplacement =
false;
1314 for (
auto [maybeConstant, result] : llvm::zip(maybeConstants, values)) {
1319 assert(isa<Attribute>(maybeConstant) &&
1320 "The constified value should be either unchanged (i.e., == result) "
1324 llvm::cast<IntegerAttr>(cast<Attribute>(maybeConstant)).getInt());
1325 for (
Operation *op : llvm::make_early_inc_range(result.getUsers())) {
1329 atLeastOneReplacement =
true;
1332 return atLeastOneReplacement;
1336 ExtractStridedMetadataOp::fold(FoldAdaptor adaptor,
1342 getConstifiedMixedOffset());
1344 getConstifiedMixedSizes());
1346 builder, getLoc(), getStrides(), getConstifiedMixedStrides());
1348 return success(atLeastOneReplacement);
1358 ExtractStridedMetadataOp::getConstifiedMixedStrides() {
1362 LogicalResult status =
1363 getSource().getType().getStridesAndOffset(staticValues, unused);
1365 assert(succeeded(status) &&
"could not get strides from type");
1370 OpFoldResult ExtractStridedMetadataOp::getConstifiedMixedOffset() {
1375 LogicalResult status =
1376 getSource().getType().getStridesAndOffset(unused, offset);
1378 assert(succeeded(status) &&
"could not get offset from type");
1379 staticValues.push_back(offset);
1394 if (
auto memrefType = llvm::dyn_cast<MemRefType>(memref.
getType())) {
1395 Type elementType = memrefType.getElementType();
1405 auto &body = getRegion();
1406 if (body.getNumArguments() != 1)
1407 return emitOpError(
"expected single number of entry block arguments");
1409 if (getResult().
getType() != body.getArgument(0).getType())
1410 return emitOpError(
"expected block argument of the same type result type");
1417 "body of 'memref.generic_atomic_rmw' should contain "
1418 "only operations with no side effects");
1448 p <<
' ' << getMemref() <<
"[" <<
getIndices()
1449 <<
"] : " << getMemref().
getType() <<
' ';
1459 Type parentType = (*this)->getParentOp()->getResultTypes().front();
1460 Type resultType = getResult().getType();
1461 if (parentType != resultType)
1462 return emitOpError() <<
"types mismatch between yield op: " << resultType
1463 <<
" and its parent: " << parentType;
1475 if (!op.isExternal()) {
1477 if (op.isUninitialized())
1478 p <<
"uninitialized";
1491 auto memrefType = llvm::dyn_cast<MemRefType>(type);
1492 if (!memrefType || !memrefType.hasStaticShape())
1494 <<
"type should be static shaped memref, but got " << type;
1508 if (!llvm::isa<ElementsAttr>(initialValue))
1510 <<
"initial value should be a unit or elements attribute";
1515 auto memrefType = llvm::dyn_cast<MemRefType>(
getType());
1516 if (!memrefType || !memrefType.hasStaticShape())
1517 return emitOpError(
"type should be static shaped memref, but got ")
1522 if (getInitialValue().has_value()) {
1523 Attribute initValue = getInitialValue().value();
1524 if (!llvm::isa<UnitAttr>(initValue) && !llvm::isa<ElementsAttr>(initValue))
1525 return emitOpError(
"initial value should be a unit or elements "
1526 "attribute, but got ")
1531 if (
auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
1533 auto initElementType =
1534 cast<TensorType>(elementsAttr.getType()).getElementType();
1535 auto memrefElementType = memrefType.getElementType();
1537 if (initElementType != memrefElementType)
1538 return emitOpError(
"initial value element expected to be of type ")
1539 << memrefElementType <<
", but was of type " << initElementType;
1544 auto initShape = elementsAttr.getShapedType().getShape();
1545 auto memrefShape = memrefType.getShape();
1546 if (initShape != memrefShape)
1547 return emitOpError(
"initial value shape expected to be ")
1548 << memrefShape <<
" but was " << initShape;
1556 ElementsAttr GlobalOp::getConstantInitValue() {
1557 auto initVal = getInitialValue();
1558 if (getConstant() && initVal.has_value())
1559 return llvm::cast<ElementsAttr>(initVal.value());
1574 return emitOpError(
"'")
1575 << getName() <<
"' does not reference a valid global memref";
1577 Type resultType = getResult().getType();
1578 if (global.getType() != resultType)
1579 return emitOpError(
"result type ")
1580 << resultType <<
" does not match type " << global.getType()
1581 <<
" of the global memref @" << getName();
1591 return emitOpError(
"incorrect number of indices for load, expected ")
1608 void MemorySpaceCastOp::getAsmResultNames(
1610 setNameFn(getResult(),
"memspacecast");
1614 if (inputs.size() != 1 || outputs.size() != 1)
1616 Type a = inputs.front(), b = outputs.front();
1617 auto aT = llvm::dyn_cast<MemRefType>(a);
1618 auto bT = llvm::dyn_cast<MemRefType>(b);
1620 auto uaT = llvm::dyn_cast<UnrankedMemRefType>(a);
1621 auto ubT = llvm::dyn_cast<UnrankedMemRefType>(b);
1624 if (aT.getElementType() != bT.getElementType())
1626 if (aT.getLayout() != bT.getLayout())
1628 if (aT.getShape() != bT.getShape())
1633 return uaT.getElementType() == ubT.getElementType();
1638 OpFoldResult MemorySpaceCastOp::fold(FoldAdaptor adaptor) {
1641 if (
auto parentCast = getSource().getDefiningOp<MemorySpaceCastOp>()) {
1642 getSourceMutable().assign(parentCast.getSource());
1653 p <<
" " << getMemref() <<
'[';
1655 p <<
']' <<
", " << (getIsWrite() ?
"write" :
"read");
1656 p <<
", locality<" << getLocalityHint();
1657 p <<
">, " << (getIsDataCache() ?
"data" :
"instr");
1659 (*this)->getAttrs(),
1660 {
"localityHint",
"isWrite",
"isDataCache"});
1667 IntegerAttr localityHint;
1669 StringRef readOrWrite, cacheType;
1686 if (readOrWrite !=
"read" && readOrWrite !=
"write")
1688 "rw specifier has to be 'read' or 'write'");
1689 result.
addAttribute(PrefetchOp::getIsWriteAttrStrName(),
1692 if (cacheType !=
"data" && cacheType !=
"instr")
1694 "cache type has to be 'data' or 'instr'");
1696 result.
addAttribute(PrefetchOp::getIsDataCacheAttrStrName(),
1704 return emitOpError(
"too few indices");
1709 LogicalResult PrefetchOp::fold(FoldAdaptor adaptor,
1721 auto type = getOperand().getType();
1722 auto shapedType = llvm::dyn_cast<ShapedType>(type);
1723 if (shapedType && shapedType.hasRank())
1725 return IntegerAttr();
1732 void ReinterpretCastOp::getAsmResultNames(
1734 setNameFn(getResult(),
"reinterpret_cast");
1741 MemRefType resultType,
Value source,
1751 build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
1762 auto sourceType = cast<BaseMemRefType>(source.
getType());
1769 b.
getContext(), staticOffsets.front(), staticStrides);
1770 auto resultType =
MemRefType::get(staticSizes, sourceType.getElementType(),
1771 stridedLayout, sourceType.getMemorySpace());
1772 build(b, result, resultType, source, offset, sizes, strides, attrs);
1776 MemRefType resultType,
Value source,
1781 llvm::to_vector<4>(llvm::map_range(sizes, [&](int64_t v) ->
OpFoldResult {
1785 llvm::map_range(strides, [&](int64_t v) ->
OpFoldResult {
1789 strideValues, attrs);
1793 MemRefType resultType,
Value source,
Value offset,
1800 build(b, result, resultType, source, offset, sizeValues, strideValues, attrs);
1807 auto srcType = llvm::cast<BaseMemRefType>(getSource().
getType());
1808 auto resultType = llvm::cast<MemRefType>(
getType());
1809 if (srcType.getMemorySpace() != resultType.getMemorySpace())
1810 return emitError(
"different memory spaces specified for source type ")
1811 << srcType <<
" and result memref type " << resultType;
1812 if (srcType.getElementType() != resultType.getElementType())
1813 return emitError(
"different element types specified for source type ")
1814 << srcType <<
" and result memref type " << resultType;
1817 for (
auto [idx, resultSize, expectedSize] :
1819 if (ShapedType::isStatic(resultSize) && resultSize != expectedSize)
1820 return emitError(
"expected result type with size = ")
1821 << (ShapedType::isDynamic(expectedSize)
1822 ? std::string(
"dynamic")
1823 : std::to_string(expectedSize))
1824 <<
" instead of " << resultSize <<
" in dim = " << idx;
1830 int64_t resultOffset;
1832 if (
failed(resultType.getStridesAndOffset(resultStrides, resultOffset)))
1833 return emitError(
"expected result type to have strided layout but found ")
1837 int64_t expectedOffset = getStaticOffsets().front();
1838 if (ShapedType::isStatic(resultOffset) && resultOffset != expectedOffset)
1839 return emitError(
"expected result type with offset = ")
1840 << (ShapedType::isDynamic(expectedOffset)
1841 ? std::string(
"dynamic")
1842 : std::to_string(expectedOffset))
1843 <<
" instead of " << resultOffset;
1846 for (
auto [idx, resultStride, expectedStride] :
1848 if (ShapedType::isStatic(resultStride) && resultStride != expectedStride)
1849 return emitError(
"expected result type with stride = ")
1850 << (ShapedType::isDynamic(expectedStride)
1851 ? std::string(
"dynamic")
1852 : std::to_string(expectedStride))
1853 <<
" instead of " << resultStride <<
" in dim = " << idx;
1860 Value src = getSource();
1861 auto getPrevSrc = [&]() ->
Value {
1864 return prev.getSource();
1868 return prev.getSource();
1874 return prev.getSource();
1879 if (
auto prevSrc = getPrevSrc()) {
1880 getSourceMutable().assign(prevSrc);
1903 LogicalResult status =
getType().getStridesAndOffset(staticValues, unused);
1905 assert(succeeded(status) &&
"could not get strides from type");
1910 OpFoldResult ReinterpretCastOp::getConstifiedMixedOffset() {
1912 assert(values.size() == 1 &&
1913 "reinterpret_cast must have one and only one offset");
1916 LogicalResult status =
getType().getStridesAndOffset(unused, offset);
1918 assert(succeeded(status) &&
"could not get offset from type");
1919 staticValues.push_back(offset);
1967 struct ReinterpretCastOpExtractStridedMetadataFolder
1972 LogicalResult matchAndRewrite(ReinterpretCastOp op,
1974 auto extractStridedMetadata =
1975 op.getSource().getDefiningOp<ExtractStridedMetadataOp>();
1976 if (!extractStridedMetadata)
1981 auto isReinterpretCastNoop = [&]() ->
bool {
1983 if (!llvm::equal(extractStridedMetadata.getConstifiedMixedStrides(),
1984 op.getConstifiedMixedStrides()))
1988 if (!llvm::equal(extractStridedMetadata.getConstifiedMixedSizes(),
1989 op.getConstifiedMixedSizes()))
1993 assert(op.getMixedOffsets().size() == 1 &&
1994 "reinterpret_cast with more than one offset should have been "
1995 "rejected by the verifier");
1996 return extractStridedMetadata.getConstifiedMixedOffset() ==
1997 op.getConstifiedMixedOffset();
2000 if (!isReinterpretCastNoop()) {
2017 op.getSourceMutable().assign(extractStridedMetadata.getSource());
2027 Type srcTy = extractStridedMetadata.getSource().getType();
2028 if (srcTy == op.getResult().getType())
2029 rewriter.
replaceOp(op, extractStridedMetadata.getSource());
2032 extractStridedMetadata.getSource());
2041 results.
add<ReinterpretCastOpExtractStridedMetadataFolder>(context);
2048 void CollapseShapeOp::getAsmResultNames(
2050 setNameFn(getResult(),
"collapse_shape");
2053 void ExpandShapeOp::getAsmResultNames(
2055 setNameFn(getResult(),
"expand_shape");
2060 reifiedResultShapes = {
2061 getMixedValues(getStaticOutputShape(), getOutputShape(), builder)};
2070 static LogicalResult
2074 bool allowMultipleDynamicDimsPerGroup) {
2076 if (collapsedShape.size() != reassociation.size())
2077 return op->
emitOpError(
"invalid number of reassociation groups: found ")
2078 << reassociation.size() <<
", expected " << collapsedShape.size();
2082 int64_t nextDim = 0;
2085 int64_t collapsedDim = it.index();
2087 bool foundDynamic =
false;
2088 for (int64_t expandedDim : group) {
2089 if (expandedDim != nextDim++)
2090 return op->
emitOpError(
"reassociation indices must be contiguous");
2092 if (expandedDim >=
static_cast<int64_t
>(expandedShape.size()))
2094 << expandedDim <<
" is out of bounds";
2097 if (ShapedType::isDynamic(expandedShape[expandedDim])) {
2098 if (foundDynamic && !allowMultipleDynamicDimsPerGroup)
2100 "at most one dimension in a reassociation group may be dynamic");
2101 foundDynamic =
true;
2106 if (ShapedType::isDynamic(collapsedShape[collapsedDim]) != foundDynamic)
2109 <<
") must be dynamic if and only if reassociation group is "
2114 if (!foundDynamic) {
2115 int64_t groupSize = 1;
2116 for (int64_t expandedDim : group)
2117 groupSize *= expandedShape[expandedDim];
2118 if (groupSize != collapsedShape[collapsedDim])
2120 << collapsedShape[collapsedDim]
2121 <<
") must equal reassociation group size (" << groupSize <<
")";
2125 if (collapsedShape.empty()) {
2127 for (int64_t d : expandedShape)
2130 "rank 0 memrefs can only be extended/collapsed with/from ones");
2131 }
else if (nextDim !=
static_cast<int64_t
>(expandedShape.size())) {
2135 << expandedShape.size()
2136 <<
") inconsistent with number of reassociation indices (" << nextDim
2149 getReassociationIndices());
2158 getReassociationIndices());
2163 static FailureOr<StridedLayoutAttr>
2168 if (
failed(srcType.getStridesAndOffset(srcStrides, srcOffset)))
2170 assert(srcStrides.size() == reassociation.size() &&
"invalid reassociation");
2185 reverseResultStrides.reserve(resultShape.size());
2186 unsigned shapeIndex = resultShape.size() - 1;
2187 for (
auto it : llvm::reverse(llvm::zip(reassociation, srcStrides))) {
2189 int64_t currentStrideToExpand = std::get<1>(it);
2190 for (
unsigned idx = 0, e = reassoc.size(); idx < e; ++idx) {
2191 reverseResultStrides.push_back(currentStrideToExpand);
2192 currentStrideToExpand =
2198 auto resultStrides = llvm::to_vector<8>(llvm::reverse(reverseResultStrides));
2199 resultStrides.resize(resultShape.size(), 1);
2203 FailureOr<MemRefType> ExpandShapeOp::computeExpandedType(
2206 if (srcType.getLayout().isIdentity()) {
2209 MemRefLayoutAttrInterface layout;
2211 srcType.getMemorySpace());
2215 FailureOr<StridedLayoutAttr> computedLayout =
2217 if (
failed(computedLayout))
2219 return MemRefType::get(resultShape, srcType.getElementType(), *computedLayout,
2220 srcType.getMemorySpace());
2223 FailureOr<SmallVector<OpFoldResult>>
2225 MemRefType expandedType,
2228 std::optional<SmallVector<OpFoldResult>> outputShape =
2233 return *outputShape;
2240 auto [staticOutputShape, dynamicOutputShape] =
2242 build(builder, result, llvm::cast<MemRefType>(resultType), src,
2244 dynamicOutputShape, staticOutputShape);
2252 MemRefType memrefResultTy = llvm::cast<MemRefType>(resultType);
2253 FailureOr<SmallVector<OpFoldResult>> outputShape = inferOutputShape(
2254 builder, result.
location, memrefResultTy, reassociation, inputShape);
2257 assert(succeeded(outputShape) &&
"unable to infer output shape");
2258 build(builder, result, memrefResultTy, src, reassociation, *outputShape);
2265 auto srcType = llvm::cast<MemRefType>(src.
getType());
2266 FailureOr<MemRefType> resultType =
2267 ExpandShapeOp::computeExpandedType(srcType, resultShape, reassociation);
2270 assert(succeeded(resultType) &&
"could not compute layout");
2271 build(builder, result, *resultType, src, reassociation);
2279 auto srcType = llvm::cast<MemRefType>(src.
getType());
2280 FailureOr<MemRefType> resultType =
2281 ExpandShapeOp::computeExpandedType(srcType, resultShape, reassociation);
2284 assert(succeeded(resultType) &&
"could not compute layout");
2285 build(builder, result, *resultType, src, reassociation, outputShape);
2289 MemRefType srcType = getSrcType();
2290 MemRefType resultType = getResultType();
2292 if (srcType.getRank() > resultType.getRank()) {
2293 auto r0 = srcType.getRank();
2294 auto r1 = resultType.getRank();
2295 return emitOpError(
"has source rank ")
2296 << r0 <<
" and result rank " << r1 <<
". This is not an expansion ("
2297 << r0 <<
" > " << r1 <<
").";
2302 resultType.getShape(),
2303 getReassociationIndices(),
2308 FailureOr<MemRefType> expectedResultType = ExpandShapeOp::computeExpandedType(
2309 srcType, resultType.getShape(), getReassociationIndices());
2310 if (
failed(expectedResultType))
2311 return emitOpError(
"invalid source layout map");
2314 if (*expectedResultType != resultType)
2315 return emitOpError(
"expected expanded type to be ")
2316 << *expectedResultType <<
" but found " << resultType;
2318 if ((int64_t)getStaticOutputShape().size() != resultType.getRank())
2319 return emitOpError(
"expected number of static shape bounds to be equal to "
2320 "the output rank (")
2321 << resultType.getRank() <<
") but found "
2322 << getStaticOutputShape().size() <<
" inputs instead";
2324 if ((int64_t)getOutputShape().size() !=
2325 llvm::count(getStaticOutputShape(), ShapedType::kDynamic))
2326 return emitOpError(
"mismatch in dynamic dims in output_shape and "
2327 "static_output_shape: static_output_shape has ")
2328 << llvm::count(getStaticOutputShape(), ShapedType::kDynamic)
2329 <<
" dynamic dims while output_shape has " << getOutputShape().size()
2336 if (ShapedType::isStatic(shape) && shape != staticOutputShapes[pos]) {
2337 return emitOpError(
"invalid output shape provided at pos ") << pos;
2358 static FailureOr<StridedLayoutAttr>
2361 bool strict =
false) {
2364 auto srcShape = srcType.getShape();
2365 if (
failed(srcType.getStridesAndOffset(srcStrides, srcOffset)))
2374 resultStrides.reserve(reassociation.size());
2377 while (srcShape[ref.back()] == 1 && ref.size() > 1)
2378 ref = ref.drop_back();
2379 if (ShapedType::isStatic(srcShape[ref.back()]) || ref.size() == 1) {
2380 resultStrides.push_back(srcStrides[ref.back()]);
2386 resultStrides.push_back(ShapedType::kDynamic);
2391 unsigned resultStrideIndex = resultStrides.size() - 1;
2395 for (int64_t idx : llvm::reverse(trailingReassocs)) {
2407 if (strict && (stride.saturated || srcStride.saturated))
2412 if (srcShape[idx - 1] == 1)
2415 if (!stride.saturated && !srcStride.saturated && stride != srcStride)
2422 bool CollapseShapeOp::isGuaranteedCollapsible(
2425 if (srcType.getLayout().isIdentity())
2432 MemRefType CollapseShapeOp::computeCollapsedType(
2435 resultShape.reserve(reassociation.size());
2438 for (int64_t srcDim : group)
2441 resultShape.push_back(groupSize.asInteger());
2444 if (srcType.getLayout().isIdentity()) {
2447 MemRefLayoutAttrInterface layout;
2449 srcType.getMemorySpace());
2455 FailureOr<StridedLayoutAttr> computedLayout =
2457 assert(succeeded(computedLayout) &&
2458 "invalid source layout map or collapsing non-contiguous dims");
2459 return MemRefType::get(resultShape, srcType.getElementType(), *computedLayout,
2460 srcType.getMemorySpace());
2466 auto srcType = llvm::cast<MemRefType>(src.
getType());
2467 MemRefType resultType =
2468 CollapseShapeOp::computeCollapsedType(srcType, reassociation);
2471 build(b, result, resultType, src, attrs);
2475 MemRefType srcType = getSrcType();
2476 MemRefType resultType = getResultType();
2478 if (srcType.getRank() < resultType.getRank()) {
2479 auto r0 = srcType.getRank();
2480 auto r1 = resultType.getRank();
2481 return emitOpError(
"has source rank ")
2482 << r0 <<
" and result rank " << r1 <<
". This is not a collapse ("
2483 << r0 <<
" < " << r1 <<
").";
2488 srcType.getShape(), getReassociationIndices(),
2493 MemRefType expectedResultType;
2494 if (srcType.getLayout().isIdentity()) {
2497 MemRefLayoutAttrInterface layout;
2498 expectedResultType =
2499 MemRefType::get(resultType.getShape(), srcType.getElementType(), layout,
2500 srcType.getMemorySpace());
2505 FailureOr<StridedLayoutAttr> computedLayout =
2507 if (
failed(computedLayout))
2509 "invalid source layout map or collapsing non-contiguous dims");
2510 expectedResultType =
2512 *computedLayout, srcType.getMemorySpace());
2515 if (expectedResultType != resultType)
2516 return emitOpError(
"expected collapsed type to be ")
2517 << expectedResultType <<
" but found " << resultType;
2529 auto cast = op.getOperand().getDefiningOp<CastOp>();
2536 Type newResultType = CollapseShapeOp::computeCollapsedType(
2537 llvm::cast<MemRefType>(cast.getOperand().getType()),
2538 op.getReassociationIndices());
2540 if (newResultType == op.getResultType()) {
2542 op, [&]() { op.getSrcMutable().assign(cast.getSource()); });
2545 CollapseShapeOp::create(rewriter, op->getLoc(), cast.getSource(),
2546 op.getReassociationIndices());
2558 memref::DimOp, MemRefType>,
2562 OpFoldResult ExpandShapeOp::fold(FoldAdaptor adaptor) {
2563 return foldReshapeOp<ExpandShapeOp, CollapseShapeOp>(*
this,
2564 adaptor.getOperands());
2567 OpFoldResult CollapseShapeOp::fold(FoldAdaptor adaptor) {
2568 return foldReshapeOp<CollapseShapeOp, ExpandShapeOp>(*
this,
2569 adaptor.getOperands());
2576 void ReshapeOp::getAsmResultNames(
2578 setNameFn(getResult(),
"reshape");
2582 Type operandType = getSource().getType();
2583 Type resultType = getResult().getType();
2585 Type operandElementType =
2586 llvm::cast<ShapedType>(operandType).getElementType();
2587 Type resultElementType = llvm::cast<ShapedType>(resultType).getElementType();
2588 if (operandElementType != resultElementType)
2589 return emitOpError(
"element types of source and destination memref "
2590 "types should be the same");
2592 if (
auto operandMemRefType = llvm::dyn_cast<MemRefType>(operandType))
2593 if (!operandMemRefType.getLayout().isIdentity())
2594 return emitOpError(
"source memref type should have identity affine map");
2598 auto resultMemRefType = llvm::dyn_cast<MemRefType>(resultType);
2599 if (resultMemRefType) {
2600 if (!resultMemRefType.getLayout().isIdentity())
2601 return emitOpError(
"result memref type should have identity affine map");
2602 if (shapeSize == ShapedType::kDynamic)
2603 return emitOpError(
"cannot use shape operand with dynamic length to "
2604 "reshape to statically-ranked memref type");
2605 if (shapeSize != resultMemRefType.getRank())
2607 "length of shape operand differs from the result's memref rank");
2618 return emitOpError(
"store index operand count not equal to memref rank");
2623 LogicalResult StoreOp::fold(FoldAdaptor adaptor,
2633 void SubViewOp::getAsmResultNames(
2635 setNameFn(getResult(),
"subview");
2641 MemRefType SubViewOp::inferResultType(MemRefType sourceMemRefType,
2645 unsigned rank = sourceMemRefType.getRank();
2647 assert(staticOffsets.size() == rank &&
"staticOffsets length mismatch");
2648 assert(staticSizes.size() == rank &&
"staticSizes length mismatch");
2649 assert(staticStrides.size() == rank &&
"staticStrides length mismatch");
2652 auto [sourceStrides, sourceOffset] = sourceMemRefType.getStridesAndOffset();
2656 int64_t targetOffset = sourceOffset;
2657 for (
auto it : llvm::zip(staticOffsets, sourceStrides)) {
2658 auto staticOffset = std::get<0>(it), sourceStride = std::get<1>(it);
2668 targetStrides.reserve(staticOffsets.size());
2669 for (
auto it : llvm::zip(sourceStrides, staticStrides)) {
2670 auto sourceStride = std::get<0>(it), staticStride = std::get<1>(it);
2677 return MemRefType::get(staticSizes, sourceMemRefType.getElementType(),
2679 targetOffset, targetStrides),
2680 sourceMemRefType.getMemorySpace());
2683 MemRefType SubViewOp::inferResultType(MemRefType sourceMemRefType,
2698 return SubViewOp::inferResultType(sourceMemRefType, staticOffsets,
2699 staticSizes, staticStrides);
2702 MemRefType SubViewOp::inferRankReducedResultType(
2706 MemRefType inferredType =
2707 inferResultType(sourceRankedTensorType, offsets, sizes, strides);
2708 assert(inferredType.getRank() >=
static_cast<int64_t
>(resultShape.size()) &&
2710 if (inferredType.getRank() ==
static_cast<int64_t
>(resultShape.size()))
2711 return inferredType;
2714 std::optional<llvm::SmallDenseSet<unsigned>> dimsToProject =
2716 assert(dimsToProject.has_value() &&
"invalid rank reduction");
2719 auto inferredLayout = llvm::cast<StridedLayoutAttr>(inferredType.getLayout());
2721 rankReducedStrides.reserve(resultShape.size());
2722 for (
auto [idx, value] :
llvm::enumerate(inferredLayout.getStrides())) {
2723 if (!dimsToProject->contains(idx))
2724 rankReducedStrides.push_back(value);
2728 inferredLayout.getOffset(),
2729 rankReducedStrides),
2730 inferredType.getMemorySpace());
2733 MemRefType SubViewOp::inferRankReducedResultType(
2742 return SubViewOp::inferRankReducedResultType(
2743 resultShape, sourceRankedTensorType, staticOffsets, staticSizes,
2750 MemRefType resultType,
Value source,
2760 auto sourceMemRefType = llvm::cast<MemRefType>(source.
getType());
2763 resultType = SubViewOp::inferResultType(sourceMemRefType, staticOffsets,
2764 staticSizes, staticStrides);
2767 build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
2780 build(b, result, MemRefType(), source, offsets, sizes, strides, attrs);
2789 llvm::map_range(offsets, [&](int64_t v) ->
OpFoldResult {
2793 llvm::to_vector<4>(llvm::map_range(sizes, [&](int64_t v) ->
OpFoldResult {
2797 llvm::map_range(strides, [&](int64_t v) ->
OpFoldResult {
2800 build(b, result, source, offsetValues, sizeValues, strideValues, attrs);
2806 MemRefType resultType,
Value source,
2811 llvm::map_range(offsets, [&](int64_t v) ->
OpFoldResult {
2815 llvm::to_vector<4>(llvm::map_range(sizes, [&](int64_t v) ->
OpFoldResult {
2819 llvm::map_range(strides, [&](int64_t v) ->
OpFoldResult {
2822 build(b, result, resultType, source, offsetValues, sizeValues, strideValues,
2838 build(b, result, resultType, source, offsetValues, sizeValues, strideValues);
2845 build(b, result, MemRefType(), source, offsets, sizes, strides, attrs);
2849 Value SubViewOp::getViewSource() {
return getSource(); }
2854 int64_t t1Offset, t2Offset;
2856 auto res1 = t1.getStridesAndOffset(t1Strides, t1Offset);
2857 auto res2 = t2.getStridesAndOffset(t2Strides, t2Offset);
2858 return succeeded(res1) && succeeded(res2) && t1Offset == t2Offset;
2865 const llvm::SmallBitVector &droppedDims) {
2866 assert(
size_t(t1.getRank()) == droppedDims.size() &&
2867 "incorrect number of bits");
2868 assert(
size_t(t1.getRank() - t2.getRank()) == droppedDims.count() &&
2869 "incorrect number of dropped dims");
2870 int64_t t1Offset, t2Offset;
2872 auto res1 = t1.getStridesAndOffset(t1Strides, t1Offset);
2873 auto res2 = t2.getStridesAndOffset(t2Strides, t2Offset);
2876 for (int64_t i = 0,
j = 0, e = t1.getRank(); i < e; ++i) {
2879 if (t1Strides[i] != t2Strides[
j])
2887 SubViewOp op,
Type expectedType) {
2888 auto memrefType = llvm::cast<ShapedType>(expectedType);
2893 return op->emitError(
"expected result rank to be smaller or equal to ")
2894 <<
"the source rank, but got " << op.getType();
2896 return op->emitError(
"expected result type to be ")
2898 <<
" or a rank-reduced version. (mismatch of result sizes), but got "
2901 return op->emitError(
"expected result element type to be ")
2902 << memrefType.getElementType() <<
", but got " << op.getType();
2904 return op->emitError(
2905 "expected result and source memory spaces to match, but got ")
2908 return op->emitError(
"expected result type to be ")
2910 <<
" or a rank-reduced version. (mismatch of result layout), but "
2914 llvm_unreachable(
"unexpected subview verification result");
2919 MemRefType baseType = getSourceType();
2920 MemRefType subViewType =
getType();
2926 if (baseType.getMemorySpace() != subViewType.getMemorySpace())
2927 return emitError(
"different memory spaces specified for base memref "
2929 << baseType <<
" and subview memref type " << subViewType;
2932 if (!baseType.isStrided())
2933 return emitError(
"base type ") << baseType <<
" is not strided";
2937 MemRefType expectedType = SubViewOp::inferResultType(
2938 baseType, staticOffsets, staticSizes, staticStrides);
2943 expectedType, subViewType);
2948 if (expectedType.getMemorySpace() != subViewType.getMemorySpace())
2950 *
this, expectedType);
2955 *
this, expectedType);
2965 *
this, expectedType);
2970 *
this, expectedType);
2976 staticStrides,
true);
2978 return getOperation()->emitError(boundsResult.
errorMessage);
2984 return os <<
"range " << range.
offset <<
":" << range.
size <<
":"
2993 std::array<unsigned, 3> ranks = op.getArrayAttrMaxRanks();
2994 assert(ranks[0] == ranks[1] &&
"expected offset and sizes of equal ranks");
2995 assert(ranks[1] == ranks[2] &&
"expected sizes and strides of equal ranks");
2997 unsigned rank = ranks[0];
2999 for (
unsigned idx = 0; idx < rank; ++idx) {
3001 op.isDynamicOffset(idx)
3002 ? op.getDynamicOffset(idx)
3005 op.isDynamicSize(idx)
3006 ? op.getDynamicSize(idx)
3009 op.isDynamicStride(idx)
3010 ? op.getDynamicStride(idx)
3012 res.emplace_back(
Range{offset, size, stride});
3025 MemRefType currentResultType, MemRefType currentSourceType,
3028 MemRefType nonRankReducedType = SubViewOp::inferResultType(
3029 sourceType, mixedOffsets, mixedSizes, mixedStrides);
3031 currentSourceType, currentResultType, mixedSizes);
3035 auto layout = llvm::cast<StridedLayoutAttr>(nonRankReducedType.getLayout());
3037 unsigned numDimsAfterReduction =
3038 nonRankReducedType.getRank() - unusedDims->count();
3039 shape.reserve(numDimsAfterReduction);
3040 strides.reserve(numDimsAfterReduction);
3041 for (
const auto &[idx, size, stride] :
3042 llvm::zip(llvm::seq<unsigned>(0, nonRankReducedType.getRank()),
3043 nonRankReducedType.getShape(), layout.getStrides())) {
3044 if (unusedDims->test(idx))
3046 shape.push_back(size);
3047 strides.push_back(stride);
3052 layout.getOffset(), strides),
3053 nonRankReducedType.getMemorySpace());
3058 auto memrefType = llvm::cast<MemRefType>(memref.
getType());
3059 unsigned rank = memrefType.getRank();
3063 MemRefType targetType = SubViewOp::inferRankReducedResultType(
3064 targetShape, memrefType, offsets, sizes, strides);
3065 return b.
createOrFold<memref::SubViewOp>(loc, targetType, memref, offsets,
3072 auto sourceMemrefType = llvm::dyn_cast<MemRefType>(value.
getType());
3073 assert(sourceMemrefType &&
"not a ranked memref type");
3074 auto sourceShape = sourceMemrefType.getShape();
3075 if (sourceShape.equals(desiredShape))
3077 auto maybeRankReductionMask =
3079 if (!maybeRankReductionMask)
3089 if (subViewOp.getSourceType().getRank() != subViewOp.getType().getRank())
3092 auto mixedOffsets = subViewOp.getMixedOffsets();
3093 auto mixedSizes = subViewOp.getMixedSizes();
3094 auto mixedStrides = subViewOp.getMixedStrides();
3099 return !intValue || intValue.value() != 0;
3106 return !intValue || intValue.value() != 1;
3114 if (!intValue || *intValue != sourceShape[size.index()])
3138 class SubViewOpMemRefCastFolder final :
public OpRewritePattern<SubViewOp> {
3142 LogicalResult matchAndRewrite(SubViewOp subViewOp,
3146 if (llvm::any_of(subViewOp.getOperands(), [](
Value operand) {
3147 return matchPattern(operand, matchConstantIndex());
3151 auto castOp = subViewOp.getSource().getDefiningOp<CastOp>();
3163 subViewOp.getType(), subViewOp.getSourceType(),
3164 llvm::cast<MemRefType>(castOp.getSource().getType()),
3165 subViewOp.getMixedOffsets(), subViewOp.getMixedSizes(),
3166 subViewOp.getMixedStrides());
3170 Value newSubView = SubViewOp::create(
3171 rewriter, subViewOp.getLoc(), resultType, castOp.getSource(),
3172 subViewOp.getOffsets(), subViewOp.getSizes(), subViewOp.getStrides(),
3173 subViewOp.getStaticOffsets(), subViewOp.getStaticSizes(),
3174 subViewOp.getStaticStrides());
3187 LogicalResult matchAndRewrite(SubViewOp subViewOp,
3191 if (subViewOp.getSourceType() == subViewOp.getType()) {
3192 rewriter.
replaceOp(subViewOp, subViewOp.getSource());
3196 subViewOp.getSource());
3208 MemRefType resTy = SubViewOp::inferResultType(
3209 op.getSourceType(), mixedOffsets, mixedSizes, mixedStrides);
3212 MemRefType nonReducedType = resTy;
3215 llvm::SmallBitVector droppedDims = op.getDroppedDims();
3216 if (droppedDims.none())
3217 return nonReducedType;
3220 auto [nonReducedStrides, offset] = nonReducedType.getStridesAndOffset();
3225 for (int64_t i = 0; i < static_cast<int64_t>(mixedSizes.size()); ++i) {
3226 if (droppedDims.test(i))
3228 targetStrides.push_back(nonReducedStrides[i]);
3229 targetShape.push_back(nonReducedType.getDimSize(i));
3234 offset, targetStrides),
3235 nonReducedType.getMemorySpace());
3251 SubViewOpMemRefCastFolder, TrivialSubViewOpFolder>(context);
3255 MemRefType sourceMemrefType = getSource().getType();
3256 MemRefType resultMemrefType = getResult().getType();
3258 dyn_cast_if_present<StridedLayoutAttr>(resultMemrefType.getLayout());
3260 if (resultMemrefType == sourceMemrefType &&
3261 resultMemrefType.hasStaticShape() &&
3262 (!resultLayout || resultLayout.hasStaticLayout())) {
3263 return getViewSource();
3269 if (
auto srcSubview = getViewSource().getDefiningOp<SubViewOp>()) {
3270 auto srcSizes = srcSubview.getMixedSizes();
3272 auto offsets = getMixedOffsets();
3274 auto strides = getMixedStrides();
3275 bool allStridesOne = llvm::all_of(strides,
isOneInteger);
3276 bool allSizesSame = llvm::equal(sizes, srcSizes);
3277 if (allOffsetsZero && allStridesOne && allSizesSame &&
3278 resultMemrefType == sourceMemrefType)
3279 return getViewSource();
3289 void TransposeOp::getAsmResultNames(
3291 setNameFn(getResult(),
"transpose");
3297 auto originalSizes = memRefType.getShape();
3298 auto [originalStrides, offset] = memRefType.getStridesAndOffset();
3299 assert(originalStrides.size() ==
static_cast<unsigned>(memRefType.getRank()));
3302 auto sizes = applyPermutationMap<int64_t>(permutationMap, originalSizes);
3303 auto strides = applyPermutationMap<int64_t>(permutationMap, originalStrides);
3312 AffineMapAttr permutation,
3314 auto permutationMap = permutation.getValue();
3315 assert(permutationMap);
3317 auto memRefType = llvm::cast<MemRefType>(in.
getType());
3321 result.
addAttribute(TransposeOp::getPermutationAttrStrName(), permutation);
3322 build(b, result, resultType, in, attrs);
3327 p <<
" " << getIn() <<
" " << getPermutation();
3329 p <<
" : " << getIn().getType() <<
" to " <<
getType();
3335 MemRefType srcType, dstType;
3344 result.
addAttribute(TransposeOp::getPermutationAttrStrName(),
3351 return emitOpError(
"expected a permutation map");
3352 if (getPermutation().getNumDims() != getIn().
getType().getRank())
3353 return emitOpError(
"expected a permutation map of same rank as the input");
3355 auto srcType = llvm::cast<MemRefType>(getIn().
getType());
3356 auto resultType = llvm::cast<MemRefType>(
getType());
3358 .canonicalizeStridedLayout();
3360 if (resultType.canonicalizeStridedLayout() != canonicalResultType)
3361 return emitOpError(
"result type ")
3363 <<
" is not equivalent to the canonical transposed input type "
3364 << canonicalResultType;
3371 if (getPermutation().isIdentity() &&
getType() == getIn().
getType())
3375 if (
auto otherTransposeOp = getIn().getDefiningOp<memref::TransposeOp>()) {
3377 getPermutation().
compose(otherTransposeOp.getPermutation());
3378 getInMutable().assign(otherTransposeOp.getIn());
3379 setPermutation(composedPermutation);
3389 void ViewOp::getAsmResultNames(
function_ref<
void(
Value, StringRef)> setNameFn) {
3390 setNameFn(getResult(),
"view");
3394 auto baseType = llvm::cast<MemRefType>(getOperand(0).
getType());
3398 if (!baseType.getLayout().isIdentity())
3399 return emitError(
"unsupported map for base memref type ") << baseType;
3402 if (!viewType.getLayout().isIdentity())
3403 return emitError(
"unsupported map for result memref type ") << viewType;
3406 if (baseType.getMemorySpace() != viewType.getMemorySpace())
3407 return emitError(
"different memory spaces specified for base memref "
3409 << baseType <<
" and view memref type " << viewType;
3412 unsigned numDynamicDims = viewType.getNumDynamicDims();
3413 if (getSizes().size() != numDynamicDims)
3414 return emitError(
"incorrect number of size operands for type ") << viewType;
3419 Value ViewOp::getViewSource() {
return getSource(); }
3422 MemRefType sourceMemrefType = getSource().getType();
3423 MemRefType resultMemrefType = getResult().getType();
3425 if (resultMemrefType == sourceMemrefType && resultMemrefType.hasStaticShape())
3426 return getViewSource();
3436 LogicalResult matchAndRewrite(ViewOp viewOp,
3439 if (llvm::none_of(viewOp.getOperands(), [](
Value operand) {
3440 return matchPattern(operand, matchConstantIndex());
3445 auto memrefType = viewOp.getType();
3450 if (
failed(memrefType.getStridesAndOffset(oldStrides, oldOffset)))
3452 assert(oldOffset == 0 &&
"Expected 0 offset");
3460 newShapeConstants.reserve(memrefType.getRank());
3462 unsigned dynamicDimPos = 0;
3463 unsigned rank = memrefType.getRank();
3464 for (
unsigned dim = 0, e = rank; dim < e; ++dim) {
3465 int64_t dimSize = memrefType.getDimSize(dim);
3467 if (ShapedType::isStatic(dimSize)) {
3468 newShapeConstants.push_back(dimSize);
3471 auto *defOp = viewOp.getSizes()[dynamicDimPos].getDefiningOp();
3472 if (
auto constantIndexOp =
3473 dyn_cast_or_null<arith::ConstantIndexOp>(defOp)) {
3475 newShapeConstants.push_back(constantIndexOp.value());
3478 newShapeConstants.push_back(dimSize);
3479 newOperands.push_back(viewOp.getSizes()[dynamicDimPos]);
3485 MemRefType newMemRefType =
3488 if (newMemRefType == memrefType)
3492 auto newViewOp = ViewOp::create(rewriter, viewOp.getLoc(), newMemRefType,
3493 viewOp.getOperand(0), viewOp.getByteShift(),
3504 LogicalResult matchAndRewrite(ViewOp viewOp,
3506 Value memrefOperand = viewOp.getOperand(0);
3507 CastOp memrefCastOp = memrefOperand.
getDefiningOp<CastOp>();
3510 Value allocOperand = memrefCastOp.getOperand();
3515 viewOp.getByteShift(),
3525 results.
add<ViewOpShapeFolder, ViewOpMemrefCastFolder>(context);
3535 "expects the number of subscripts to be equal to memref rank");
3536 switch (getKind()) {
3537 case arith::AtomicRMWKind::addf:
3538 case arith::AtomicRMWKind::maximumf:
3539 case arith::AtomicRMWKind::minimumf:
3540 case arith::AtomicRMWKind::mulf:
3541 if (!llvm::isa<FloatType>(getValue().
getType()))
3542 return emitOpError() <<
"with kind '"
3543 << arith::stringifyAtomicRMWKind(getKind())
3544 <<
"' expects a floating-point type";
3546 case arith::AtomicRMWKind::addi:
3547 case arith::AtomicRMWKind::maxs:
3548 case arith::AtomicRMWKind::maxu:
3549 case arith::AtomicRMWKind::mins:
3550 case arith::AtomicRMWKind::minu:
3551 case arith::AtomicRMWKind::muli:
3552 case arith::AtomicRMWKind::ori:
3553 case arith::AtomicRMWKind::xori:
3554 case arith::AtomicRMWKind::andi:
3555 if (!llvm::isa<IntegerType>(getValue().
getType()))
3556 return emitOpError() <<
"with kind '"
3557 << arith::stringifyAtomicRMWKind(getKind())
3558 <<
"' expects an integer type";
3577 #define GET_OP_CLASSES
3578 #include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc"
static Value getStride(Location loc, MemRefType mType, Value base, RewriterBase &rewriter)
Maps the 2-dim memref shape to the 64-bit stride.
static bool hasSideEffects(Operation *op)
static Operation * materializeConstant(Dialect *dialect, OpBuilder &builder, Attribute value, Type type, Location loc)
A utility function used to materialize a constant for a given attribute and type.
static bool isPermutation(const std::vector< PermutationTy > &permutation)
static MLIRContext * getContext(OpFoldResult val)
static Type getElementType(Type type)
Determine the element type of type.
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
static void constifyIndexValues(SmallVectorImpl< OpFoldResult > &values, ArrayRef< int64_t > constValues)
Helper function that sets values[i] to constValues[i] if the latter is a static value,...
static void printGlobalMemrefOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp op, TypeAttr type, Attribute initialValue)
static LogicalResult verifyCollapsedShape(Operation *op, ArrayRef< int64_t > collapsedShape, ArrayRef< int64_t > expandedShape, ArrayRef< ReassociationIndices > reassociation, bool allowMultipleDynamicDimsPerGroup)
Helper function for verifying the shape of ExpandShapeOp and ResultShapeOp result and operand.
static bool isOpItselfPotentialAutomaticAllocation(Operation *op)
Given an operation, return whether this op itself could allocate an AutomaticAllocationScopeResource.
static MemRefType inferTransposeResultType(MemRefType memRefType, AffineMap permutationMap)
Build a strided memref type by applying permutationMap to memRefType.
static bool isGuaranteedAutomaticAllocation(Operation *op)
Given an operation, return whether this op is guaranteed to allocate an AutomaticAllocationScopeResou...
static bool haveCompatibleOffsets(MemRefType t1, MemRefType t2)
Return true if t1 and t2 have equal offsets (both dynamic or of same static value).
static LogicalResult FoldCopyOfCast(CopyOp op)
If the source/target of a CopyOp is a CastOp that does not modify the shape and element type,...
static bool replaceConstantUsesOf(OpBuilder &rewriter, Location loc, Container values, ArrayRef< OpFoldResult > maybeConstants)
Helper function to perform the replacement of all constant uses of values by a materialized constant ...
static LogicalResult produceSubViewErrorMsg(SliceVerificationResult result, SubViewOp op, Type expectedType)
static MemRefType getCanonicalSubViewResultType(MemRefType currentResultType, MemRefType currentSourceType, MemRefType sourceType, ArrayRef< OpFoldResult > mixedOffsets, ArrayRef< OpFoldResult > mixedSizes, ArrayRef< OpFoldResult > mixedStrides)
Compute the canonical result type of a SubViewOp.
static ParseResult parseGlobalMemrefOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &initialValue)
static FailureOr< llvm::SmallBitVector > computeMemRefRankReductionMask(MemRefType originalType, MemRefType reducedType, ArrayRef< OpFoldResult > sizes)
Given the originalType and a candidateReducedType whose shape is assumed to be a subset of originalTy...
static bool isTrivialSubViewOp(SubViewOp subViewOp)
Helper method to check if a subview operation is trivially a no-op.
static bool lastNonTerminatorInRegion(Operation *op)
Return whether this op is the last non terminating op in a region.
static bool haveCompatibleStrides(MemRefType t1, MemRefType t2, const llvm::SmallBitVector &droppedDims)
Return true if t1 and t2 have equal strides (both dynamic or of same static value).
static std::map< int64_t, unsigned > getNumOccurences(ArrayRef< int64_t > vals)
Return a map with key being elements in vals and data being number of occurences of it.
static FailureOr< StridedLayoutAttr > computeExpandedLayoutMap(MemRefType srcType, ArrayRef< int64_t > resultShape, ArrayRef< ReassociationIndices > reassociation)
Compute the layout map after expanding a given source MemRef type with the specified reassociation in...
static FailureOr< StridedLayoutAttr > computeCollapsedLayoutMap(MemRefType srcType, ArrayRef< ReassociationIndices > reassociation, bool strict=false)
Compute the layout map after collapsing a given source MemRef type with the specified reassociation i...
static LogicalResult verifyAllocLikeOp(AllocLikeOp op)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
static llvm::SmallBitVector getDroppedDims(ArrayRef< int64_t > reducedShape, ArrayRef< OpFoldResult > mixedSizes)
Compute the dropped dimensions of a rank-reducing tensor.extract_slice op or rank-extending tensor....
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
AffineMap compose(AffineMap map) const
Returns the AffineMap resulting from composing this with map.
@ Square
Square brackets surrounding zero or more operands.
virtual ParseResult parseColonTypeList(SmallVectorImpl< Type > &result)=0
Parse a colon followed by a type list, which must have at least one type.
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 parseOptionalEqual()=0
Parse a = token if present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseAffineMap(AffineMap &map)=0
Parse an affine map instance into 'map'.
ParseResult addTypeToList(Type type, SmallVectorImpl< Type > &result)
Add the specified type to the end of the specified type list and return success.
virtual ParseResult parseLess()=0
Parse a '<' token.
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 parseGreater()=0
Parse a '>' token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseOptionalArrowTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional arrow followed by a type list.
ParseResult parseKeywordType(const char *keyword, Type &result)
Parse a keyword followed by a type.
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.
Attributes are known-constant values of operations.
This class provides a shared interface for ranked and unranked memref types.
ArrayRef< int64_t > getShape() const
Returns the shape of this memref type.
bool hasRank() const
Returns if this type is ranked, i.e. it has a known number of dimensions.
Block represents an ordered list of Operations.
Operation * getTerminator()
Get the terminator operation of this block.
bool mightHaveTerminator()
Return "true" if this block might have a terminator.
This class is a general helper class for creating context-global objects like types,...
IntegerAttr getIndexAttr(int64_t value)
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
IntegerAttr getI64IntegerAttr(int64_t value)
IntegerType getIntegerType(unsigned width)
BoolAttr getBoolAttr(bool value)
MLIRContext * getContext() const
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.
This is a builder type that keeps local references to arguments.
Builder & setLayout(MemRefLayoutAttrInterface newLayout)
Builder & setShape(ArrayRef< int64_t > newShape)
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.
ParseResult parseTrailingOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None)
Parse zero or more trailing SSA comma-separated trailing operand references with a specified surround...
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...
void printOperands(const ContainerType &container)
Print a comma separated list of operands.
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 * 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.
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
void createOrFold(SmallVectorImpl< Value > &results, Location location, Args &&...args)
Create an operation of specific op type at the current insertion point, and immediately try to fold i...
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
This class represents a single result from folding an operation.
This class represents an operand of an operation.
A trait of region holding operations that define a new scope for automatic allocations,...
This trait indicates that the memory effects of an operation includes the effects of operations neste...
Pattern to rewrite dynamic offsets/sizes/strides of view/slice-like ops as constant arguments.
type_range getType() const
Operation is the basic unit of execution within MLIR.
void replaceUsesOfWith(Value from, Value to)
Replace any uses of 'from' with 'to' within this operation.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Block * getBlock()
Returns the operation block that contains this operation.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
MutableArrayRef< OpOperand > getOpOperands()
operand_range getOperands()
Returns an iterator on the underlying Value's.
Region * getParentRegion()
Returns the region to which the instruction belongs.
result_range getResults()
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
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.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
bool hasOneBlock()
Return true if this region has exactly one block.
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
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,...
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.
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={})
Inline the operations of block 'source' into block 'dest' before the given position.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class represents a collection of SymbolTables.
virtual Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
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...
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
This class provides an abstraction over the different types of ranges over Values.
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.
Location getLoc() const
Return the location of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
static WalkResult advance()
static WalkResult interrupt()
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Base class for DenseArrayAttr that is instantiated and specialized for each supported element type be...
Speculatability
This enum is returned from the getSpeculatability method in the ConditionallySpeculatable op interfac...
constexpr auto Speculatable
constexpr auto NotSpeculatable
BaseMemRefType getMemRefType(TensorType tensorType, const BufferizationOptions &options, MemRefLayoutAttrInterface layout={}, Attribute memorySpace=nullptr)
Return a MemRefType to which the TensorType can be bufferized.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
ConstantIntRanges inferShapedDimOpInterface(ShapedDimOpInterface op, const IntegerValueRange &maybeDim)
Returns the integer range for the result of a ShapedDimOpInterface given the optional inferred ranges...
Type getTensorTypeFromMemRefType(Type type)
Return an unranked/ranked tensor type for the given unranked/ranked memref type.
OpFoldResult getMixedSize(OpBuilder &builder, Location loc, Value value, int64_t dim)
Return the dimension of the given memref value.
LogicalResult foldMemRefCast(Operation *op, Value inner=nullptr)
This is a common utility used for patterns of the form "someop(memref.cast) -> someop".
SmallVector< OpFoldResult > getMixedSizes(OpBuilder &builder, Location loc, Value value)
Return the dimensions of the given memref value.
Value createCanonicalRankReducingSubViewOp(OpBuilder &b, Location loc, Value memref, ArrayRef< int64_t > targetShape)
Create a rank-reducing SubViewOp @[0 .
Operation::operand_range getIndices(Operation *op)
Get the indices that the given load/store operation is operating on.
DynamicAPInt getIndex(const ConeV &cone)
Get the index of a cone, i.e., the volume of the parallelepiped spanned by its generators,...
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Value constantIndex(OpBuilder &builder, Location loc, int64_t i)
Generates a constant of index type.
bool canFoldIntoConsumerOp(CastOp castOp)
Determines whether tensor::CastOp casts to a more dynamic version of the source tensor.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
SliceVerificationResult
Enum that captures information related to verifier error conditions on slice insert/extract type of o...
constexpr StringRef getReassociationAttrName()
Attribute name for the ArrayAttr which encodes reassociation indices.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
LogicalResult reifyResultShapes(OpBuilder &b, Operation *op, ReifiedRankedShapedTypeDims &reifiedReturnShapes)
Reify the shape of the result of an operation (typically in terms of the shape of its operands).
SliceBoundsVerificationResult verifyInBoundsSlice(ArrayRef< int64_t > shape, ArrayRef< int64_t > staticOffsets, ArrayRef< int64_t > staticSizes, ArrayRef< int64_t > staticStrides, bool generateErrorMessage=false)
Verify that the offsets/sizes/strides-style access into the given shape is in-bounds.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
SmallVector< Range, 8 > getOrCreateRanges(OffsetSizeAndStrideOpInterface op, OpBuilder &b, Location loc)
Return the list of Range (i.e.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
SmallVector< AffineMap, 4 > getSymbolLessAffineMaps(ArrayRef< ReassociationExprs > reassociation)
Constructs affine maps out of Array<Array<AffineExpr>>.
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
bool hasValidSizesOffsets(SmallVector< int64_t > sizesOrOffsets)
Helper function to check whether the passed in sizes or offsets are valid.
bool isZeroInteger(OpFoldResult v)
Return true if v is an IntegerAttr with value 0.
bool hasValidStrides(SmallVector< int64_t > strides)
Helper function to check whether the passed in strides are valid.
void dispatchIndexOpFoldResults(ArrayRef< OpFoldResult > ofrs, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec)
Helper function to dispatch multiple OpFoldResults according to the behavior of dispatchIndexOpFoldRe...
SmallVector< SmallVector< AffineExpr, 2 >, 2 > convertReassociationIndicesToExprs(MLIRContext *context, ArrayRef< ReassociationIndices > reassociationIndices)
Convert reassociation indices to affine expressions.
std::optional< SmallVector< OpFoldResult > > inferExpandShapeOutputShape(OpBuilder &b, Location loc, ShapedType expandedType, ArrayRef< ReassociationIndices > reassociation, ArrayRef< OpFoldResult > inputShape)
Infer the output shape for a {memref|tensor}.expand_shape when it is possible to do so.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.
SmallVector< OpFoldResult > getMixedValues(ArrayRef< int64_t > staticValues, ValueRange dynamicValues, MLIRContext *context)
Return a vector of OpFoldResults with the same size a staticValues, but all elements for which Shaped...
std::optional< llvm::SmallDenseSet< unsigned > > computeRankReductionMask(ArrayRef< int64_t > originalShape, ArrayRef< int64_t > reducedShape, bool matchDynamic=false)
Given an originalShape and a reducedShape assumed to be a subset of originalShape with some 1 entries...
SliceVerificationResult isRankReducedType(ShapedType originalType, ShapedType candidateReducedType)
Check if originalType can be rank reduced to candidateReducedType type by dropping some dimensions wi...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
ArrayAttr getReassociationIndicesAttribute(Builder &b, ArrayRef< ReassociationIndices > reassociation)
Wraps a list of reassociations in an ArrayAttr.
bool isOneInteger(OpFoldResult v)
Return true if v is an IntegerAttr with value 1.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
std::pair< SmallVector< int64_t >, SmallVector< Value > > decomposeMixedValues(ArrayRef< OpFoldResult > mixedValues)
Decompose a vector of mixed static or dynamic values into the corresponding pair of arrays.
Move allocations into an allocation scope, if it is legal to move them (e.g.
LogicalResult matchAndRewrite(AllocaScopeOp op, PatternRewriter &rewriter) const override
Inline an AllocaScopeOp if either the direct parent is an allocation scope or it contains no allocati...
LogicalResult matchAndRewrite(AllocaScopeOp op, PatternRewriter &rewriter) const override
LogicalResult matchAndRewrite(CollapseShapeOp op, PatternRewriter &rewriter) const override
A canonicalizer wrapper to replace SubViewOps.
void operator()(PatternRewriter &rewriter, SubViewOp op, SubViewOp newOp)
Return the canonical type of the result of a subview.
MemRefType operator()(SubViewOp op, ArrayRef< OpFoldResult > mixedOffsets, ArrayRef< OpFoldResult > mixedSizes, ArrayRef< OpFoldResult > mixedStrides)
Pattern to compose collapse_shape(expand_shape(src, reassociation_1), reassociation_2).
Pattern to collapse producer/consumer reshape ops that are both collapsing dimensions or are both exp...
The following effect indicates that the operation allocates from some resource.
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.
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addTypes(ArrayRef< Type > newTypes)
SmallVector< std::unique_ptr< Region >, 1 > regions
Regions that the op will hold.
SmallVector< Type, 4 > types
Types of the results of this operation.
Region * addRegion()
Create a region that should be attached to the operation.
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...
static SaturatedInteger wrap(int64_t v)
Result for slice bounds verification;.
bool isValid
If set to "true", the slice bounds verification was successful.
std::string errorMessage
An error message that can be printed during op verification.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.