[clang] [CIR] Implement folder for VecExtractOp (PR #139304)

Bruno Cardoso Lopes via cfe-commits cfe-commits at lists.llvm.org
Fri May 9 11:51:21 PDT 2025


================
@@ -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];
----------------
bcardosolopes wrote:

If the index is out of bounds we don't want to try accessing the elements. We have couple options here: (a) make the verifier look at input constants and verify out of bounds and/or (b) return `{}` in case it's not bound. If you add a verifier, than the check could be a helper function used by both it and `::fold`.

https://github.com/llvm/llvm-project/pull/139304


More information about the cfe-commits mailing list