================
@@ -1883,6 +1884,60 @@ mlir::LogicalResult
CIRToLLVMVecCmpOpLowering::matchAndRewrite(
return mlir::success();
}
+mlir::LogicalResult CIRToLLVMVecSplatOpLowering::matchAndRewrite(
+ cir::VecSplatOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ // Vector splat can be implemented with an `insertelement` and a
+ // `shufflevector`, which is better than an `insertelement` for each
+ // element in the vector. Start with an undef vector. Insert the value into
+ // the first element. Then use a `shufflevector` with a mask of all 0 to
+ // fill out the entire vector with that value.
+ const auto vecTy = mlir::cast<cir::VectorType>(op.getType());
+ const mlir::Type llvmTy = typeConverter->convertType(vecTy);
+ const mlir::Location loc = op.getLoc();
+ const mlir::Value poison = rewriter.create<mlir::LLVM::PoisonOp>(loc,
llvmTy);
+
+ const mlir::Value elementValue = adaptor.getValue();
+ if (mlir::isa<mlir::LLVM::PoisonOp>(elementValue.getDefiningOp())) {
+ // If the splat value is poison, then we can just use poison value
+ // for the entire vector.
+ rewriter.replaceOp(op, poison);
+ return mlir::success();
+ }
+
+ if (auto constValue =
+ dyn_cast<mlir::LLVM::ConstantOp>(elementValue.getDefiningOp())) {
+ if (auto intAttr = dyn_cast<mlir::IntegerAttr>(constValue.getValue())) {
+ mlir::DenseIntElementsAttr denseVec = mlir::DenseIntElementsAttr::get(
+ mlir::cast<mlir::ShapedType>(llvmTy), intAttr.getValue());
+
+ const mlir::Value indexValue = rewriter.create<mlir::LLVM::ConstantOp>(
+ loc, denseVec.getType(), denseVec);
+ rewriter.replaceOp(op, indexValue);
+ return mlir::success();
+ }
+
+ if (auto fpAttr = dyn_cast<mlir::FloatAttr>(constValue.getValue())) {
+ mlir::DenseFPElementsAttr denseVec = mlir::DenseFPElementsAttr::get(
+ mlir::cast<mlir::ShapedType>(llvmTy), fpAttr.getValue());
+
+ const mlir::Value indexValue = rewriter.create<mlir::LLVM::ConstantOp>(
+ loc, denseVec.getType(), denseVec);
+ rewriter.replaceOp(op, indexValue);
----------------
xlauko wrote:
why not `replaceOpWithNewOp`?
https://github.com/llvm/llvm-project/pull/139827
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits