[Mlir-commits] [mlir] [MLIR][Linalg] Expose linalg.matmul and linalg.contract via Python API (PR #126377)

Rolf Morel llvmlistbot at llvm.org
Sun Feb 9 09:59:45 PST 2025


================
@@ -606,7 +622,7 @@ def MatmulOp : LinalgStructuredBase_Op<"matmul", [
     let arguments = (ins
       Variadic<AnyType>:$inputs,
       Variadic<AnyShaped>:$outputs,
-      DefaultValuedOptionalAttr<AffineMapArrayAttr, "{}">:$indexing_maps,
+      DefaultValuedMatmulIndexingMapsAttr:$indexing_maps, // DONOTMERGE(rolfmorel): explain why this is necessary
----------------
rolfmorel wrote:

The thing is, as far as I understand now no builder (even worse, no op-specific C++ code at all) is called at all when an op gets constructed from Python (see from [here](https://github.com/llvm/llvm-project/blob/3d140004c70e2bc79416825e43207e8b711c56d9/mlir/lib/Bindings/Python/IRCore.cpp#L3330) to [here](https://github.com/llvm/llvm-project/blob/3d140004c70e2bc79416825e43207e8b711c56d9/mlir/lib/Bindings/Python/IRCore.cpp#L2073) to  [here](https://github.com/llvm/llvm-project/blob/3d140004c70e2bc79416825e43207e8b711c56d9/mlir/lib/Bindings/Python/IRCore.cpp#L1590) to [here](https://github.com/llvm/llvm-project/blob/3d140004c70e2bc79416825e43207e8b711c56d9/mlir/lib/CAPI/IR/IR.cpp#L510) to finally [here](https://github.com/llvm/llvm-project/blob/3d140004c70e2bc79416825e43207e8b711c56d9/mlir/lib/IR/Operation.cpp#L118) ). The only thing one can do is pass in default values for attributes. Could you point me to an example of specifying "the default builder" for an op?

And yes, preferably I would just have `DefaultValuedOptionalAttr<AffineMapArrayAttr, "MatmulOp::getDefaultIndexingMaps($_builder.getContext())">:$indexing_maps` in MatmulOp's `arguments` (note, to my knowledge, we cannot specify custom "builders" in this list), but that doesn't work. The problem is that `DefaultValuedOptionalAttr` assumes that the `defaultValue` can be given without access to the context. However, that is not true in this case (that is, attempting the above leads to `error: use of undeclared identifier '$_builder'`). The workaround is to just expand what `DefaultValuedOptionalAttr` does to get `DefaultValuedMatmulIndexingMapsAttr` in which we _can_ modify `constBuilderCall` so that we can pass the context in case of the `defaultValue`. That the resulting attr is not "anonymous" is rather unfortunate though.

If you have suggestions for a cleaner approach, I am all ears!

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


More information about the Mlir-commits mailing list