[Mlir-commits] [mlir] [mlir][bufferization] Convert tensor enconding -> memref layout (PR #161166)
    Andrei Golubev 
    llvmlistbot at llvm.org
       
    Mon Sep 29 03:28:33 PDT 2025
    
    
  
================
@@ -244,6 +244,15 @@ AllocTensorOp::getBufferType(Value value, const BufferizationOptions &options,
     return getOperation()->emitError("could not infer memory space");
   }
 
+  // Note: Only rely on TensorLikeType::getBufferType() if memref layout is
+  // explicitly specified by the user. Otherwise, the default behavior is to
+  // return a fully dynamic layout map which is the opposite of the default
+  // behavior of this function.
+  if (options.constructMemRefLayoutFn) {
+    return cast<TensorLikeType>(getType()).getBufferType(
+        options, [&]() { return emitError(); });
+  }
+
----------------
andrey-golubev wrote:
note: right now, this is pretty much a crutch. I'm thinking that a good design could be this:
* default `constructMemRefLayoutFn` returns fully dynamic layout map
* user can specify their own behavior by setting a different callable
* *questionable*: `AllocTensorOp::getBufferType()` would copy bufferization options and set:
  * options.defaultMemorySpaceFn (see right above the added code for what happens)
  * options.constructMemRefLayoutFn to return `nullptr` layout
Additionally, I think we should revisit [`TensorType::getBufferType()`](https://github.com/llvm/llvm-project/blob/ce70773cff2ab6b5d9b8d7a97ee62c75762a51d2/mlir/lib/Dialect/Bufferization/IR/BufferizationDialect.cpp#L69-L70): instead of calling unknownTypeConversionFn we can just utilize constructMemRefLayoutFn introduced by this patch, rendering unknown type conversion unnecessary? (can delete that option completely). And thus making the ::getBufferType() look something like:
```cpp
// for RankedTensorType
return MemRefType::get(
  tensor.getShape(), // as before
  tensor.getElementType(), // as before
  options.constructMemRefLayoutFn(tensor), // this is "new" and universal
  options.defaultMemorySpaceFn(tensor) // as before
);
```
Perhaps then we could get rid of getMemRefType / getMemRefTypeWithFullyDynamicLayout / getMemRefTypeWithStaticIdentityLayout triplet (at least there's no longer "no difference" since layout specification is controlled via options).
https://github.com/llvm/llvm-project/pull/161166
    
    
More information about the Mlir-commits
mailing list