Skip to content

Commit cd8face

Browse files
authored
[CIR] Implement folder for VecCreateOp (#143355)
This change adds a folder for the VecCreateOp Issue #136487
1 parent 31daed8 commit cd8face

File tree

7 files changed

+128
-172
lines changed

7 files changed

+128
-172
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,7 @@ def VecCreateOp : CIR_Op<"vec.create", [Pure]> {
20592059
}];
20602060

20612061
let hasVerifier = 1;
2062+
let hasFolder = 1;
20622063
}
20632064

20642065
//===----------------------------------------------------------------------===//

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,16 @@ LogicalResult cir::GetMemberOp::verify() {
15331533
// VecCreateOp
15341534
//===----------------------------------------------------------------------===//
15351535

1536+
OpFoldResult cir::VecCreateOp::fold(FoldAdaptor adaptor) {
1537+
if (llvm::any_of(getElements(), [](mlir::Value value) {
1538+
return !mlir::isa<cir::ConstantOp>(value.getDefiningOp());
1539+
}))
1540+
return {};
1541+
1542+
return cir::ConstVectorAttr::get(
1543+
getType(), mlir::ArrayAttr::get(getContext(), adaptor.getElements()));
1544+
}
1545+
15361546
LogicalResult cir::VecCreateOp::verify() {
15371547
// Verify that the number of arguments matches the number of elements in the
15381548
// vector, and that the type of all the arguments matches the type of the

clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ void CIRCanonicalizePass::runOnOperation() {
142142
// Many operations are here to perform a manual `fold` in
143143
// applyOpPatternsGreedily.
144144
if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SwitchOp, SelectOp, UnaryOp,
145-
VecExtractOp, VecShuffleOp, VecShuffleDynamicOp, VecTernaryOp>(op))
145+
VecCreateOp, VecExtractOp, VecShuffleOp, VecShuffleDynamicOp,
146+
VecTernaryOp>(op))
146147
ops.push_back(op);
147148
});
148149

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,10 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
901901
rewriter.eraseOp(op);
902902
return mlir::success();
903903
}
904+
} else if (const auto vecTy = mlir::dyn_cast<cir::VectorType>(op.getType())) {
905+
rewriter.replaceOp(op, lowerCirAttrAsValue(op, op.getValue(), rewriter,
906+
getTypeConverter()));
907+
return mlir::success();
904908
} else {
905909
return op.emitError() << "unsupported constant type " << op.getType();
906910
}

clang/test/CIR/CodeGen/vector-ext.cpp

Lines changed: 46 additions & 90 deletions
Large diffs are not rendered by default.

clang/test/CIR/CodeGen/vector.cpp

Lines changed: 46 additions & 81 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
2+
3+
!s32i = !cir.int<s, 32>
4+
5+
module {
6+
cir.func @fold_create_vector_op_test() -> !cir.vector<4 x !s32i> {
7+
%2 = cir.const #cir.int<1> : !s32i
8+
%3 = cir.const #cir.int<2> : !s32i
9+
%4 = cir.const #cir.int<3> : !s32i
10+
%5 = cir.const #cir.int<4> : !s32i
11+
%vec = cir.vec.create(%2, %3, %4, %5 : !s32i, !s32i, !s32i, !s32i) : !cir.vector<4 x !s32i>
12+
cir.return %vec : !cir.vector<4 x !s32i>
13+
}
14+
15+
// CHECK: cir.func @fold_create_vector_op_test() -> !cir.vector<4 x !s32i> {
16+
// CHECK-NEXT: %[[VEC:.*]] = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i,
17+
// CHECK-SAME: #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
18+
// CHECK-NEXT: cir.return %[[VEC]] : !cir.vector<4 x !s32i>
19+
}

0 commit comments

Comments
 (0)