Skip to content
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
30 changes: 28 additions & 2 deletions clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ struct SimplifySwitch : public OpRewritePattern<SwitchOp> {
}
};

struct SimplifyVecSplat : public OpRewritePattern<VecSplatOp> {
using OpRewritePattern<VecSplatOp>::OpRewritePattern;
LogicalResult matchAndRewrite(VecSplatOp op,
PatternRewriter &rewriter) const override {
mlir::Value splatValue = op.getValue();
auto constant =
mlir::dyn_cast_if_present<cir::ConstantOp>(splatValue.getDefiningOp());
if (!constant)
return mlir::failure();

auto value = constant.getValue();
if (!mlir::isa_and_nonnull<cir::IntAttr>(value) &&
!mlir::isa_and_nonnull<cir::FPAttr>(value))
return mlir::failure();

cir::VectorType resultType = op.getResult().getType();
SmallVector<mlir::Attribute, 16> elements(resultType.getSize(), value);
auto constVecAttr = cir::ConstVectorAttr::get(
resultType, mlir::ArrayAttr::get(getContext(), elements));

rewriter.replaceOpWithNewOp<cir::ConstantOp>(op, constVecAttr);
return mlir::success();
}
};

//===----------------------------------------------------------------------===//
// CIRSimplifyPass
//===----------------------------------------------------------------------===//
Expand All @@ -275,7 +300,8 @@ void populateMergeCleanupPatterns(RewritePatternSet &patterns) {
patterns.add<
SimplifyTernary,
SimplifySelect,
SimplifySwitch
SimplifySwitch,
SimplifyVecSplat
>(patterns.getContext());
// clang-format on
}
Expand All @@ -288,7 +314,7 @@ void CIRSimplifyPass::runOnOperation() {
// Collect operations to apply patterns.
llvm::SmallVector<Operation *, 16> ops;
getOperation()->walk([&](Operation *op) {
if (isa<TernaryOp, SelectOp, SwitchOp>(op))
if (isa<TernaryOp, SelectOp, SwitchOp, VecSplatOp>(op))
ops.push_back(op);
});

Expand Down
4 changes: 1 addition & 3 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,7 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
}

attr = rewriter.getArrayAttr(components);
}

else {
} else {
return op.emitError() << "unsupported constant type " << op.getType();
}

Expand Down
16 changes: 16 additions & 0 deletions clang/test/CIR/Transforms/vector-splat.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: cir-opt %s -cir-simplify -o - | FileCheck %s

!s32i = !cir.int<s, 32>

module {
cir.func @fold_shuffle_vector_op_test() -> !cir.vector<4 x !s32i> {
%v = cir.const #cir.int<3> : !s32i
%vec = cir.vec.splat %v : !s32i, !cir.vector<4 x !s32i>
cir.return %vec : !cir.vector<4 x !s32i>
}

// CHECK: cir.func @fold_shuffle_vector_op_test() -> !cir.vector<4 x !s32i> {
// CHECK-NEXT: %0 = cir.const #cir.const_vector<[#cir.int<3> : !s32i, #cir.int<3> : !s32i,
// CHECK-SAME: #cir.int<3> : !s32i, #cir.int<3> : !s32i]> : !cir.vector<4 x !s32i>
// CHECK-NEXT: cir.return %0 : !cir.vector<4 x !s32i>
}
Loading