[Mlir-commits] [mlir] [mlir] Fix correct memset range in `OwningMemRef` zero-init (PR #158200)

Ryan Kim llvmlistbot at llvm.org
Fri Sep 12 06:11:19 PDT 2025


================
@@ -174,9 +174,7 @@ class OwningMemRef {
            it != end; ++it)
         init(*it, it.getIndices());
     } else {
-      memset(descriptor.data, 0,
-             nElements * sizeof(T) +
-                 alignment.value_or(detail::nextPowerOf2(sizeof(T))));
+      memset(descriptor.data, 0, nElements * sizeof(T));
----------------
chokobole wrote:

Thanks—that makes sense. To keep this patch focused and avoid churn, I’ll make the naming self-consistent in this function and zero only the logical payload from the aligned start. Concretely:

Rename the local unpack to `allocatedPtr` / `alignedData` to avoid confusion.

When zero-init is requested, use `alignedData` with the payload size only.

```diff
- auto [data, alignedData] = allocateWithOverprovision(...);
+ auto [allocatedPtr, alignedData] = allocateWithOverprovision(...);
- descriptor = detail::makeStridedMemRefDescriptor<Rank>(data, alignedData,
+ descriptor = detail::makeStridedMemRefDescriptor<Rank>(allocatedPtr, alignedData,
                                                         shape, shapeAlloc);

  if (init) {

  } else {
-   memset(descriptor.data, 0, size + desiredAlignment); // could overrun
+   // Zero only the logical payload; do not write past the allocation.
+   memset(alignedData, 0, nElements * sizeof(T));
  }
```

This keeps the change scoped to this function, removes the overrun, and matches the mental model you suggested. If you’re good with this, I’ll push it here.

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


More information about the Mlir-commits mailing list