[clang] [CIR] Upstream extract op for VectorType (PR #138413)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Wed May 7 10:25:47 PDT 2025
AmrDeveloper 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`.
As far as I understood, they will not always return ConstVecAttr and IntAttr because if an index or a vector is coming from parameters, or other variables, they are not constants in that case, and I am thinking even if the vector is constant, we will use load or get_global when we use it in Extract and will not pass it directly like const int 🤔, not sure if there is a case that will trigger that fold
https://github.com/llvm/llvm-project/pull/138413
More information about the cfe-commits
mailing list