@@ -762,6 +762,42 @@ struct LinearizeVectorStore final
762762 }
763763};
764764
765+ // / This pattern linearizes `vector.from_elements` operations by converting
766+ // / the result type to a 1-D vector while preserving all element values.
767+ // / The transformation creates a linearized `vector.from_elements` followed by
768+ // / a `vector.shape_cast` to restore the original multidimensional shape.
769+ // /
770+ // / Example:
771+ // /
772+ // / %0 = vector.from_elements %a, %b, %c, %d : vector<2x2xf32>
773+ // /
774+ // / is converted to:
775+ // /
776+ // / %0 = vector.from_elements %a, %b, %c, %d : vector<4xf32>
777+ // / %1 = vector.shape_cast %0 : vector<4xf32> to vector<2x2xf32>
778+ // /
779+ struct LinearizeVectorFromElements final
780+ : public OpConversionPattern<vector::FromElementsOp> {
781+ using OpConversionPattern::OpConversionPattern;
782+ LinearizeVectorFromElements (const TypeConverter &typeConverter,
783+ MLIRContext *context, PatternBenefit benefit = 1 )
784+ : OpConversionPattern(typeConverter, context, benefit) {}
785+ LogicalResult
786+ matchAndRewrite (vector::FromElementsOp fromElementsOp, OpAdaptor adaptor,
787+ ConversionPatternRewriter &rewriter) const override {
788+ VectorType dstTy =
789+ getTypeConverter ()->convertType <VectorType>(fromElementsOp.getType ());
790+ assert (dstTy && " vector type destination expected." );
791+
792+ auto elements = fromElementsOp.getElements ();
793+ assert (elements.size () == static_cast <size_t >(dstTy.getNumElements ()) &&
794+ " expected same number of elements" );
795+ rewriter.replaceOpWithNewOp <vector::FromElementsOp>(fromElementsOp, dstTy,
796+ elements);
797+ return success ();
798+ }
799+ };
800+
765801} // namespace
766802
767803// / This method defines the set of operations that are linearizable, and hence
@@ -854,7 +890,8 @@ void mlir::vector::populateVectorLinearizeBasePatterns(
854890 patterns
855891 .add <LinearizeConstantLike, LinearizeVectorizable, LinearizeVectorBitCast,
856892 LinearizeVectorSplat, LinearizeVectorCreateMask, LinearizeVectorLoad,
857- LinearizeVectorStore>(typeConverter, patterns.getContext ());
893+ LinearizeVectorStore, LinearizeVectorFromElements>(
894+ typeConverter, patterns.getContext ());
858895}
859896
860897void mlir::vector::populateVectorLinearizeShuffleLikeOpsPatterns (
0 commit comments