@@ -2033,20 +2033,21 @@ static Value extractInsertFoldConstantOp(OpType op, AdaptorType adaptor,
20332033static Attribute foldPoisonIndexInsertExtractOp (MLIRContext *context,
20342034 ArrayRef<int64_t > staticPos,
20352035 int64_t poisonVal) {
2036- if (!llvm:: is_contained (staticPos, poisonVal))
2036+ if (!is_contained (staticPos, poisonVal))
20372037 return {};
20382038
20392039 return ub::PoisonAttr::get (context);
20402040}
20412041
20422042// / Fold a vector extract from is a poison source.
20432043static Attribute foldPoisonSrcExtractOp (Attribute srcAttr) {
2044- if (llvm:: isa_and_nonnull<ub::PoisonAttr>(srcAttr))
2044+ if (isa_and_nonnull<ub::PoisonAttr>(srcAttr))
20452045 return srcAttr;
20462046
20472047 return {};
20482048}
20492049
2050+ // / Fold a vector extract extracting from a DenseElementsAttr.
20502051static Attribute foldDenseElementsAttrSrcExtractOp (ExtractOp extractOp,
20512052 Attribute srcAttr) {
20522053 auto denseAttr = dyn_cast_if_present<DenseElementsAttr>(srcAttr);
@@ -2056,30 +2057,37 @@ static Attribute foldDenseElementsAttrSrcExtractOp(ExtractOp extractOp,
20562057
20572058 if (denseAttr.isSplat ()) {
20582059 Attribute newAttr = denseAttr.getSplatValue <Attribute>();
2059- if (auto vecDstType = llvm:: dyn_cast<VectorType>(extractOp.getType ()))
2060+ if (auto vecDstType = dyn_cast<VectorType>(extractOp.getType ()))
20602061 newAttr = DenseElementsAttr::get (vecDstType, newAttr);
20612062 return newAttr;
20622063 }
20632064
2064- auto vecTy = llvm:: cast<VectorType>(extractOp.getSourceVectorType ());
2065+ auto vecTy = cast<VectorType>(extractOp.getSourceVectorType ());
20652066 if (vecTy.isScalable ())
20662067 return {};
20672068
20682069 if (extractOp.hasDynamicPosition ()) {
20692070 return {};
20702071 }
20712072
2073+ // Materializing subsets of a large constant array can generally lead to
2074+ // explosion in IR size because of different combination of subsets that
2075+ // can exist. However, vector.extract is a restricted form of subset
2076+ // extract where you can only extract non-overlapping (or the same) subset for
2077+ // a given rank of the subset. Because of this property, the IR size can only
2078+ // increase at most by `rank * size(array)` from a single constant array being
2079+ // extracted by multiple extracts.
2080+
20722081 // Calculate the linearized position of the continuous chunk of elements to
20732082 // extract.
2074- llvm:: SmallVector<int64_t > completePositions (vecTy.getRank (), 0 );
2083+ SmallVector<int64_t > completePositions (vecTy.getRank (), 0 );
20752084 copy (extractOp.getStaticPosition (), completePositions.begin ());
2076- int64_t elemBeginPosition =
2085+ int64_t startPos =
20772086 linearize (completePositions, computeStrides (vecTy.getShape ()));
2078- auto denseValuesBegin =
2079- denseAttr.value_begin <TypedAttr>() + elemBeginPosition;
2087+ auto denseValuesBegin = denseAttr.value_begin <TypedAttr>() + startPos;
20802088
20812089 TypedAttr newAttr;
2082- if (auto resVecTy = llvm:: dyn_cast<VectorType>(extractOp.getType ())) {
2090+ if (auto resVecTy = dyn_cast<VectorType>(extractOp.getType ())) {
20832091 SmallVector<Attribute> elementValues (
20842092 denseValuesBegin, denseValuesBegin + resVecTy.getNumElements ());
20852093 newAttr = DenseElementsAttr::get (resVecTy, elementValues);
0 commit comments