Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RemoveSingleElem] Update the pattern for ExtractElementOp #998

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/Transforms/RemoveSingleElemVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ struct VectorExtractElementOpConversion final
mlir::LogicalResult
matchAndRewrite(mlir::vector::ExtractElementOp extractOp, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {

if (adaptor.getVector().getType() == extractOp.getType()) {
rewriter.replaceOp(extractOp, adaptor.getVector());
return mlir::success();
}

auto vector = extractOp.getVector();
auto vecTy = vector.getType();
auto constOp = vector.getDefiningOp<mlir::arith::ConstantOp>();
Expand All @@ -57,6 +63,7 @@ struct VectorExtractElementOpConversion final
rewriter.replaceOp(extractOp, newVal);
return mlir::success();
}

return mlir::failure();
}
};
Expand Down Expand Up @@ -207,26 +214,34 @@ struct RemoveSingleElemVectorPass final
void runOnOperation() override {
auto *context = &getContext();
mlir::TypeConverter typeConverter;

// convert a value from vector<1xT> to T using vector::ExtractElementOp
auto materializeCast = [](mlir::OpBuilder &builder, mlir::Type resultType,
mlir::ValueRange inputs, mlir::Location loc) {
if (inputs.size() != 1)
return mlir::Value();

auto vecTy = mlir::dyn_cast<mlir::VectorType>(inputs[0].getType());
if (!vecTy || vecTy.getNumElements() != 1)
return mlir::Value();

auto zero =
builder.create<mlir::arith::ConstantOp>(loc, builder.getIndexAttr(0));
return builder
.create<mlir::UnrealizedConversionCastOp>(loc, resultType, inputs)
.getResult(0);
.create<mlir::vector::ExtractElementOp>(loc, inputs[0], zero)
.getResult();
};

typeConverter.addArgumentMaterialization(materializeCast);
typeConverter.addSourceMaterialization(materializeCast);
typeConverter.addTargetMaterialization(materializeCast);

typeConverter.addConversion([](mlir::Type type) {
auto vecTy = mlir::dyn_cast<mlir::VectorType>(type);
if (vecTy && vecTy.getNumElements() == 1)
return vecTy.getElementType();
return type;
});

// typeConverter.addConversion([](mlir::Type type) { return type; });

mlir::ConversionTarget target(*context);
target.addLegalOp<mlir::arith::ConstantOp>();
target.addLegalOp<mlir::vector::InsertElementOp>();
Expand Down
Loading
Loading