-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[CIR] Implement folder for VecExtractOp #139304
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
Conversation
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Amr Hesham (AmrDeveloper) ChangesThis change adds a folder for the VecExtractOp Issue #136487 Full diff: https://github.com/llvm/llvm-project/pull/139304.diff 4 Files Affected:
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 7aff5edb88167..f7f84bb715846 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -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
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index b131edaf403ed..9ddb1b1dd60b2 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1395,6 +1395,26 @@ 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 int64_t index = indexAttr.getSInt();
+ return elements[index];
+}
+
//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index 3b4c7bc613133..798bc0dab9384 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -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);
});
diff --git a/clang/test/CIR/Transforms/vector-extract-fold.cir b/clang/test/CIR/Transforms/vector-extract-fold.cir
new file mode 100644
index 0000000000000..5e1d6e1ddb629
--- /dev/null
+++ b/clang/test/CIR/Transforms/vector-extract-fold.cir
@@ -0,0 +1,20 @@
+// 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>
+}
+
+
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for following up on this!
Once it lands we should probably backport to incubator too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
This change adds a folder for the VecExtractOp
Issue #136487