-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CIR] Implement folder for VecShuffleDynamicOp #142315
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
#include "clang/CIR/Dialect/IR/CIROpsDialect.cpp.inc" | ||
#include "clang/CIR/Dialect/IR/CIROpsEnums.cpp.inc" | ||
#include "clang/CIR/MissingFeatures.h" | ||
|
||
#include <numeric> | ||
|
||
using namespace mlir; | ||
|
@@ -1579,6 +1580,37 @@ OpFoldResult cir::VecExtractOp::fold(FoldAdaptor adaptor) { | |
// VecShuffleDynamicOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
OpFoldResult cir::VecShuffleDynamicOp::fold(FoldAdaptor adaptor) { | ||
mlir::Attribute vec = adaptor.getVec(); | ||
mlir::Attribute indices = adaptor.getIndices(); | ||
if (mlir::isa_and_nonnull<cir::ConstVectorAttr>(vec) && | ||
mlir::isa_and_nonnull<cir::ConstVectorAttr>(indices)) { | ||
auto vecAttr = mlir::cast<cir::ConstVectorAttr>(vec); | ||
auto indicesAttr = mlir::cast<cir::ConstVectorAttr>(indices); | ||
auto vecTy = mlir::cast<cir::VectorType>(vecAttr.getType()); | ||
|
||
mlir::ArrayAttr vecElts = vecAttr.getElts(); | ||
mlir::ArrayAttr indicesElts = indicesAttr.getElts(); | ||
|
||
const uint64_t numElements = vecElts.size(); | ||
|
||
SmallVector<mlir::Attribute, 16> elements; | ||
elements.reserve(numElements); | ||
|
||
const uint64_t maskBits = llvm::NextPowerOf2(numElements - 1) - 1; | ||
for (const auto &idxAttr : indicesElts.getAsRange<cir::IntAttr>()) { | ||
uint64_t idxValue = idxAttr.getUInt(); | ||
uint64_t newIdx = idxValue & maskBits; | ||
elements.push_back(vecElts[newIdx]); | ||
} | ||
|
||
return cir::ConstVectorAttr::get( | ||
vecTy, mlir::ArrayAttr::get(getContext(), elements)); | ||
} | ||
|
||
return {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this branch have a 'not yet implemented' thing here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understood, no, it should be null because that means we can't fold |
||
} | ||
|
||
LogicalResult cir::VecShuffleDynamicOp::verify() { | ||
// The number of elements in the two input vectors must match. | ||
if (getVec().getType().getSize() != | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s | ||
|
||
!s32i = !cir.int<s, 32> | ||
|
||
module { | ||
cir.func @fold_shuffle_dynamic_vector_op_test() -> !cir.vector<4 x !s32i> { | ||
%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> | ||
%indices = cir.const #cir.const_vector<[#cir.int<8> : !s32i, #cir.int<7> : !s32i, #cir.int<6> : !s32i, #cir.int<5> : !s32i]> : !cir.vector<4 x !s32i> | ||
%new_vec = cir.vec.shuffle.dynamic %vec : !cir.vector<4 x !s32i>, %indices : !cir.vector<4 x !s32i> | ||
cir.return %new_vec : !cir.vector<4 x !s32i> | ||
} | ||
|
||
// Masking indices [8, 7, 6, 5] AND 3 = [0, 3, 2, 1] | ||
// CHECK: cir.func @fold_shuffle_dynamic_vector_op_test() -> !cir.vector<4 x !s32i> { | ||
// CHECK-NEXT: %[[NEW_VEC:.*]] = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<4> : !s32i, #cir.int<3> : !s32i, #cir.int<2> : !s32i]> : !cir.vector<4 x !s32i> | ||
// CHECK-NEXT: cir.return %[[NEW_VEC:.*]] : !cir.vector<4 x !s32i> | ||
|
||
cir.func @fold_shuffle_dynamic_vector_op_test_2() -> !cir.vector<4 x !s32i> { | ||
%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> | ||
%indices = cir.const #cir.const_vector<[#cir.int<3> : !s32i, #cir.int<2> : !s32i, #cir.int<1> : !s32i, #cir.int<0> : !s32i]> : !cir.vector<4 x !s32i> | ||
%new_vec = cir.vec.shuffle.dynamic %vec : !cir.vector<4 x !s32i>, %indices : !cir.vector<4 x !s32i> | ||
cir.return %new_vec : !cir.vector<4 x !s32i> | ||
} | ||
|
||
// Masking indices [3, 2, 1, 0] AND 3 = [3, 2, 1, 0] | ||
// CHECK: cir.func @fold_shuffle_dynamic_vector_op_test_2() -> !cir.vector<4 x !s32i> { | ||
// CHECK-NEXT: %[[NEW_VEC:.*]] = cir.const #cir.const_vector<[#cir.int<4> : !s32i, #cir.int<3> : !s32i, #cir.int<2> : !s32i, #cir.int<1> : !s32i]> : !cir.vector<4 x !s32i> | ||
// CHECK-NEXT: cir.return %[[NEW_VEC:.*]] : !cir.vector<4 x !s32i> | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.