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
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,8 @@ def VecExtractOp : CIR_Op<"vec.extract", [Pure,
let assemblyFormat = [{
$vec `[` $index `:` type($index) `]` attr-dict `:` qualified(type($vec))
}];

let hasFolder = 1;
}

#endif // CLANG_CIR_DIALECT_IR_CIROPS_TD
23 changes: 23 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,29 @@ LogicalResult cir::VecCreateOp::verify() {
return success();
}

//===----------------------------------------------------------------------===//
// VecExtractOp
//===----------------------------------------------------------------------===//

OpFoldResult cir::VecExtractOp::fold(FoldAdaptor adaptor) {
const auto vectorAttr =
llvm::dyn_cast_if_present<cir::ConstVectorAttr>(adaptor.getVec());
if (!vectorAttr)
return {};

const auto indexAttr =
llvm::dyn_cast_if_present<cir::IntAttr>(adaptor.getIndex());
if (!indexAttr)
return {};

const mlir::ArrayAttr elements = vectorAttr.getElts();
const uint64_t index = indexAttr.getUInt();
if (index >= elements.size())
return {};

return elements[index];
}

//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ void CIRCanonicalizePass::runOnOperation() {
assert(!cir::MissingFeatures::complexRealOp());
assert(!cir::MissingFeatures::complexImagOp());
assert(!cir::MissingFeatures::callOp());
// CastOp and UnaryOp are here to perform a manual `fold` in
// CastOp, UnaryOp and VecExtractOp are here to perform a manual `fold` in
// applyOpPatternsGreedily.
if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SelectOp, UnaryOp>(op))
if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SelectOp, UnaryOp, VecExtractOp>(
op))
ops.push_back(op);
});

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

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

module {
cir.func @fold_extract_vector_op_test() {
%init = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
%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>
%index = cir.const #cir.int<1> : !s32i
%ele = cir.vec.extract %const_vec[%index : !s32i] : !cir.vector<4 x !s32i>
cir.store %ele, %init : !s32i, !cir.ptr<!s32i>
cir.return
}

// CHECK: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
// CHECK: %[[VALUE:.*]] = cir.const #cir.int<2> : !s32i
// CHECK: cir.store %[[VALUE]], %[[INIT]] : !s32i, !cir.ptr<!s32i>

cir.func @fold_extract_vector_op_index_out_of_bounds_test() {
%init = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
%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>
%index = cir.const #cir.int<9> : !s32i
%ele = cir.vec.extract %const_vec[%index : !s32i] : !cir.vector<4 x !s32i>
cir.store %ele, %init : !s32i, !cir.ptr<!s32i>
cir.return
}

// CHECK: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
// CHECK: %[[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>
// CHECK: %[[INDEX:.*]] = cir.const #cir.int<9> : !s32i
// CHECK: %[[ELE:.*]] = cir.vec.extract %[[CONST_VEC]][%[[INDEX]] : !s32i] : !cir.vector<4 x !s32i>
// CHECK: cir.store %[[ELE]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
}