Skip to content

Commit 6a21dc2

Browse files
committed
[CIR] Implement foldder for VecExtractOp
1 parent a6385a8 commit 6a21dc2

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,8 @@ def VecExtractOp : CIR_Op<"vec.extract", [Pure,
19951995
let assemblyFormat = [{
19961996
$vec `[` $index `:` type($index) `]` attr-dict `:` qualified(type($vec))
19971997
}];
1998+
1999+
let hasFolder = 1;
19982000
}
19992001

20002002
#endif // CLANG_CIR_DIALECT_IR_CIROPS_TD

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,26 @@ LogicalResult cir::VecCreateOp::verify() {
13951395
return success();
13961396
}
13971397

1398+
//===----------------------------------------------------------------------===//
1399+
// VecExtractOp
1400+
//===----------------------------------------------------------------------===//
1401+
1402+
OpFoldResult cir::VecExtractOp::fold(FoldAdaptor adaptor) {
1403+
const auto vectorAttr =
1404+
llvm::dyn_cast_if_present<cir::ConstVectorAttr>(adaptor.getVec());
1405+
if (!vectorAttr)
1406+
return {};
1407+
1408+
const auto indexAttr =
1409+
llvm::dyn_cast_if_present<cir::IntAttr>(adaptor.getIndex());
1410+
if (!indexAttr)
1411+
return {};
1412+
1413+
const mlir::ArrayAttr elements = vectorAttr.getElts();
1414+
const int64_t index = indexAttr.getSInt();
1415+
return elements[index];
1416+
}
1417+
13981418
//===----------------------------------------------------------------------===//
13991419
// TableGen'd op method definitions
14001420
//===----------------------------------------------------------------------===//

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ void CIRCanonicalizePass::runOnOperation() {
125125
assert(!cir::MissingFeatures::complexRealOp());
126126
assert(!cir::MissingFeatures::complexImagOp());
127127
assert(!cir::MissingFeatures::callOp());
128-
// CastOp and UnaryOp are here to perform a manual `fold` in
128+
// CastOp, UnaryOp and VecExtractOp are here to perform a manual `fold` in
129129
// applyOpPatternsGreedily.
130-
if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SelectOp, UnaryOp>(op))
130+
if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SelectOp, UnaryOp, VecExtractOp>(
131+
op))
131132
ops.push_back(op);
132133
});
133134

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
2+
3+
!s32i = !cir.int<s, 32>
4+
5+
module {
6+
cir.func @fold_extract_vector_op_test() {
7+
%init = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
8+
%const_vec = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
9+
%index = cir.const #cir.int<1> : !s32i
10+
%ele = cir.vec.extract %const_vec[%index : !s32i] : !cir.vector<4 x !s32i>
11+
cir.store %ele, %init : !s32i, !cir.ptr<!s32i>
12+
cir.return
13+
}
14+
15+
// CHECK: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
16+
// CHECK: %[[VALUE:.*]] = cir.const #cir.int<2> : !s32i
17+
// CHECK: cir.store %[[VALUE]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
18+
}
19+
20+

0 commit comments

Comments
 (0)