[Mlir-commits] [mlir] [mlir][vector][spirv] Lower `vector.to_elements` to SPIR-V (PR #146618)
Jakub Kuderski
llvmlistbot at llvm.org
Wed Jul 2 08:39:41 PDT 2025
================
@@ -1022,6 +1022,51 @@ struct VectorStepOpConvert final : OpConversionPattern<vector::StepOp> {
}
};
+struct VectorToElementOpConvert final
+ : public OpConversionPattern<vector::ToElementsOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(vector::ToElementsOp toElementsOp, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+
+ Type srcType =
+ getTypeConverter()->convertType(toElementsOp.getSource().getType());
+ if (!srcType)
+ return failure();
+
+ SmallVector<Value> results(toElementsOp->getNumResults());
+ Location loc = toElementsOp.getLoc();
+
+ // Input vectors of size 1 are converted to scalars by the type converter.
+ // We cannot use `spirv::CompositeExtractOp` directly in this case.
+ // For a scalar source, the result is just the scalar itself.
+ if (isa<spirv::ScalarType>(adaptor.getSource().getType())) {
+ results[0] = adaptor.getSource();
+ rewriter.replaceOp(toElementsOp, results);
+ return success();
+ }
+
+ for (auto [idx, element] : llvm::enumerate(toElementsOp.getElements())) {
+ // Create an CompositeExtract operation only for results that are not
+ // dead.
+ if (element.use_empty())
+ continue;
+
+ auto elementType = getTypeConverter()->convertType(element.getType());
+ if (!elementType)
+ return failure();
----------------
kuhar wrote:
Also, all extracted elements have the same type so we should be able to hoist this check outside of the loop
https://github.com/llvm/llvm-project/pull/146618
More information about the Mlir-commits
mailing list