[clang] [CIR] Implement foldder for VecExtractOp (PR #139304)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 9 11:34:38 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Amr Hesham (AmrDeveloper)
<details>
<summary>Changes</summary>
This change adds a folder for the VecExtractOp
Issue https://github.com/llvm/llvm-project/issues/136487
---
Full diff: https://github.com/llvm/llvm-project/pull/139304.diff
4 Files Affected:
- (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+2)
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+20)
- (modified) clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp (+3-2)
- (added) clang/test/CIR/Transforms/vector-extract-fold.cir (+20)
``````````diff
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>
+}
+
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/139304
More information about the cfe-commits
mailing list