[clang] [CIR] Upstream extract op for VectorType (PR #138413)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Wed May 7 04:57:37 PDT 2025


xlauko wrote:

> > Overall looks good. While here, can you please implement a folder for this operation? It should kick-in if both idx and input vector are constants.
> 
> @bcardosolopes I implement it like this snippet
> 
> ```
> 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];
> }
> ```
> 
> But I am thinking, is there a case that codegen will perform extractOp directly from ConstVec, not on load or get_global? I see a similar implementation in MLIR Vector Dialect 🤔 I will try to come up with a test case for testing

I think no need for dyn_casts here, those gets should already reaturn `cir::ConstVectorAttr` and `cir::IntAttr`.

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


More information about the cfe-commits mailing list