[Mlir-commits] [mlir] [mlir][linalg] Add maps-based inferConvolutionDims overload (PR #203323)
Federico Bruzzone
llvmlistbot at llvm.org
Fri Jun 12 02:33:31 PDT 2026
================
@@ -115,41 +115,52 @@ MLIR_CAPI_EXPORTED bool mlirLinalgIsAConvolutionOp(MlirOperation op) {
return linalg::isaConvolutionOpInterface(linalgOp);
}
+static MlirLinalgConvolutionDimensions
+toConvolutionDimensions(MLIRContext *ctx,
+ const linalg::ConvolutionDimensions &dims) {
+ auto toI32Attr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
+ return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
+ };
+ auto toI64Attr = [ctx](ArrayRef<int64_t> vals) -> MlirAttribute {
+ return wrap(DenseI64ArrayAttr::get(ctx, vals));
+ };
+ return {toI32Attr(dims.batch), toI32Attr(dims.outputImage),
+ toI32Attr(dims.outputChannel), toI32Attr(dims.filterLoop),
+ toI32Attr(dims.inputChannel), toI32Attr(dims.depth),
+ toI64Attr(dims.strides), toI64Attr(dims.dilations)};
+}
+
MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions
mlirLinalgInferConvolutionDimensions(MlirOperation op) {
- MlirLinalgConvolutionDimensions result{};
auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
if (!linalgOp)
- return result;
+ return MlirLinalgConvolutionDimensions{};
FailureOr<linalg::ConvolutionDimensions> maybeDims =
linalg::inferConvolutionDims(linalgOp);
if (failed(maybeDims))
- return result;
+ return MlirLinalgConvolutionDimensions{};
- const linalg::ConvolutionDimensions &dims = *maybeDims;
- MLIRContext *ctx = linalgOp.getContext();
+ return toConvolutionDimensions(linalgOp.getContext(), *maybeDims);
+}
- auto toI32Attr =
- [&ctx](const SmallVector<unsigned, 2> &vals) -> MlirAttribute {
- return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
- };
+MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions
+mlirLinalgInferConvolutionDimensionsFromMaps(const MlirAffineMap *indexingMaps,
+ size_t numMaps) {
+ if (!indexingMaps || numMaps == 0)
+ return MlirLinalgConvolutionDimensions{};
- auto toI64Attr =
- [&ctx](const SmallVector<int64_t, 2> &vals) -> MlirAttribute {
- return wrap(DenseI64ArrayAttr::get(ctx, vals));
- };
+ SmallVector<AffineMap, 3> maps;
+ maps.reserve(numMaps);
----------------
FedericoBruzzone wrote:
Correct me If I'm wrong but here the invariant is `numMaps == 3`, isn't it?
If so, I don't see the point of doing `.reserve()` (geniunely speaking) :D
1. it assumes that it makes semantic sense at that point for `numMaps` to be greater than 3
2. we potentially allocate memory and then fail immediately afterward with `inferConvolutionDims` (with `if (indexingMaps.size() != 3)`).
Again, If I'm not missing something, couldn't we just check in the first `if` that numMaps is 3?
https://github.com/llvm/llvm-project/pull/203323
More information about the Mlir-commits
mailing list