Skip to content

[CIR] Backport VecShuffleOp folder #1707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -3234,7 +3234,9 @@ def VecShuffleOp : CIR_Op<"vec.shuffle",
`(` $vec1 `,` $vec2 `:` qualified(type($vec1)) `)` $indices `:`
qualified(type($result)) attr-dict
}];

let hasVerifier = 1;
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
34 changes: 34 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,40 @@ LogicalResult cir::VecTernaryOp::verify() {
// VecShuffle
//===----------------------------------------------------------------------===//

OpFoldResult cir::VecShuffleOp::fold(FoldAdaptor adaptor) {
auto vec1Attr =
mlir::dyn_cast_if_present<cir::ConstVectorAttr>(adaptor.getVec1());
auto vec2Attr =
mlir::dyn_cast_if_present<cir::ConstVectorAttr>(adaptor.getVec2());
if (!vec1Attr || !vec2Attr)
return {};

mlir::Type vec1ElemTy =
mlir::cast<cir::VectorType>(vec1Attr.getType()).getElementType();

mlir::ArrayAttr vec1Elts = vec1Attr.getElts();
mlir::ArrayAttr vec2Elts = vec2Attr.getElts();
mlir::ArrayAttr indicesElts = adaptor.getIndices();

SmallVector<mlir::Attribute, 16> elements;
elements.reserve(indicesElts.size());

uint64_t vec1Size = vec1Elts.size();
for (const auto &idxAttr : indicesElts.getAsRange<cir::IntAttr>()) {
if (idxAttr.getSInt() == -1) {
elements.push_back(cir::UndefAttr::get(vec1ElemTy));
continue;
}

uint64_t idxValue = idxAttr.getUInt();
elements.push_back(idxValue < vec1Size ? vec1Elts[idxValue]
: vec2Elts[idxValue - vec1Size]);
}

return cir::ConstVectorAttr::get(
getType(), mlir::ArrayAttr::get(getContext(), elements));
}

LogicalResult cir::VecShuffleOp::verify() {
// The number of elements in the indices array must match the number of
// elements in the result type.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void CIRCanonicalizePass::runOnOperation() {
// applyOpPatternsGreedily.
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp, TryOp, UnaryOp, SelectOp,
ComplexCreateOp, ComplexRealOp, ComplexImagOp, CallOp, VecCreateOp,
VecExtractOp>(op))
VecExtractOp, VecShuffleOp>(op))
ops.push_back(op);
});

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

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

module {
cir.func @fold_shuffle_vector_op_test() -> !cir.vector<!s32i x 4> {
%vec_1 = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<3> : !s32i, #cir.int<5> : !s32i, #cir.int<7> : !s32i]> : !cir.vector<!s32i x 4>
%vec_2 = cir.const #cir.const_vector<[#cir.int<2> : !s32i, #cir.int<4> : !s32i, #cir.int<6> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<!s32i x 4>
%new_vec = cir.vec.shuffle(%vec_1, %vec_2 : !cir.vector<!s32i x 4>) [#cir.int<0> : !s64i, #cir.int<4> : !s64i,
#cir.int<1> : !s64i, #cir.int<5> : !s64i] : !cir.vector<!s32i x 4>
cir.return %new_vec : !cir.vector<!s32i x 4>
}

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

// -----

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

module {
cir.func @fold_shuffle_vector_op_test() -> !cir.vector<!s32i x 6> {
%vec_1 = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<3> : !s32i, #cir.int<5> : !s32i, #cir.int<7> : !s32i]> : !cir.vector<!s32i x 4>
%vec_2 = cir.const #cir.const_vector<[#cir.int<2> : !s32i, #cir.int<4> : !s32i, #cir.int<6> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<!s32i x 4>
%new_vec = cir.vec.shuffle(%vec_1, %vec_2 : !cir.vector<!s32i x 4>) [#cir.int<0> : !s64i, #cir.int<4> : !s64i,
#cir.int<1> : !s64i, #cir.int<5> : !s64i, #cir.int<2> : !s64i, #cir.int<6> : !s64i] : !cir.vector<!s32i x 6>
cir.return %new_vec : !cir.vector<!s32i x 6>
}

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

// -----

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

module {
cir.func @fold_shuffle_vector_op_test() -> !cir.vector<!s32i x 4> {
%vec_1 = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<3> : !s32i, #cir.int<5> : !s32i, #cir.int<7> : !s32i]> : !cir.vector<!s32i x 4>
%vec_2 = cir.const #cir.const_vector<[#cir.int<2> : !s32i, #cir.int<4> : !s32i, #cir.int<6> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<!s32i x 4>
%new_vec = cir.vec.shuffle(%vec_1, %vec_2 : !cir.vector<!s32i x 4>) [#cir.int<-1> : !s64i, #cir.int<4> : !s64i,
#cir.int<1> : !s64i, #cir.int<5> : !s64i] : !cir.vector<!s32i x 4>
cir.return %new_vec : !cir.vector<!s32i x 4>
}

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