Skip to content

Commit 18e3c77

Browse files
committed
[CIR] Backport VecCreateOp Folder
1 parent 32e3971 commit 18e3c77

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

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

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

31093109
let hasVerifier = 1;
3110+
let hasFolder = 1;
31103111
}
31113112

31123113
//===----------------------------------------------------------------------===//

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,16 @@ void cir::StoreOp::setAtomic(cir::MemOrder order) {
10111011
// VecCreateOp
10121012
//===----------------------------------------------------------------------===//
10131013

1014+
OpFoldResult cir::VecCreateOp::fold(FoldAdaptor adaptor) {
1015+
if (llvm::any_of(getElements(), [](mlir::Value value) {
1016+
return !mlir::isa<cir::ConstantOp>(value.getDefiningOp());
1017+
}))
1018+
return {};
1019+
1020+
return cir::ConstVectorAttr::get(
1021+
getType(), mlir::ArrayAttr::get(getContext(), adaptor.getElements()));
1022+
}
1023+
10141024
LogicalResult cir::VecCreateOp::verify() {
10151025
// Verify that the number of arguments matches the number of elements in the
10161026
// vector, and that the type of all the arguments matches the type of the

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void CIRCanonicalizePass::runOnOperation() {
179179
// CastOp, UnaryOp and VecExtractOp are here to perform a manual `fold` in
180180
// applyOpPatternsGreedily.
181181
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp, TryOp, UnaryOp, SelectOp,
182-
ComplexCreateOp, ComplexRealOp, ComplexImagOp, CallOp,
182+
ComplexCreateOp, ComplexRealOp, ComplexImagOp, CallOp, VecCreateOp,
183183
VecExtractOp>(op))
184184
ops.push_back(op);
185185
});
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<!s32i x 4> {
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<!s32i x 4>
12+
cir.return %vec : !cir.vector<!s32i x 4>
13+
}
14+
15+
// CHECK: cir.func @fold_create_vector_op_test() -> !cir.vector<!s32i x 4> {
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<!s32i x 4>
18+
// CHECK-NEXT: cir.return %[[VEC]] : !cir.vector<!s32i x 4>
19+
}

0 commit comments

Comments
 (0)