[all-commits] [llvm/llvm-project] aa04bc: [memref] Simplify loads from reinterpret_cast of 1...

ioana ghiban via All-commits all-commits at lists.llvm.org
Mon Apr 27 02:46:14 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: aa04bcfd2f368405490a6ef45a8b7d6fd6ee964c
      https://github.com/llvm/llvm-project/commit/aa04bcfd2f368405490a6ef45a8b7d6fd6ee964c
  Author: ioana ghiban <ioana.ghiban at arm.com>
  Date:   2026-04-27 (Mon, 27 Apr 2026)

  Changed paths:
    M mlir/lib/Dialect/MemRef/Transforms/ElideReinterpretCast.cpp
    M mlir/test/Dialect/MemRef/elide-reinterpret-cast.mlir

  Log Message:
  -----------
  [memref] Simplify loads from reinterpret_cast of 1D contiguous memrefs (#188459)

Rewrite `memref.load` operations whose source is a `reinterpret_cast` that
represents a rank reshape of a 1D contiguous `memref` with a single  non-unit
dimension.

Assisted-by: ChatGPT (refine implementation + tests). I reviewed all code and
tests before submission.

## Example

Before:
```mlir
%reinterpret_cast = memref.reinterpret_cast %src
  to offset: [0], sizes: [1, 1, 999], strides: [999, 999, 1]
  : memref<999xi64> to memref<1x1x999xi64>
%0 = memref.load %reinterpret_cast[%c0, %c0, %i]
  : memref<1x1x999xi64>
```

After:
```mlir
%0 = memref.load %src[%i] : memref<999xi64>
```

## Motivation

This simplifies the IR, makes indexing explicit, and reduces  indirection,
which in turn improves downstream transformations and  lowerings (e.g. EmitC).

## Scope

This rewrite is intentionally narrow:
- Applies only to rank-expansion and rank-collapsing of a contiguous 1D buffer
  (at most one non-unit dimension).
- Requires `reinterpret_cast` with zero offset and fully static sizes and
  strides.
- Requires the non-unit dimension to be at a boundary (first or last).
- Requires any dropped indices (from size-1 dimensions) to be statically zero.

It does **not** handle:
- general `memref.reinterpret_cast` with arbitrary strides or offsets
- multiple non-unit dimensions
- cases where index dropping would change semantics
For example:
```mlir
%reinterpret_cast = memref.reinterpret_cast %src
to offset: [0], sizes: [1, 1, 1, 108], strides: [108, 108, 108, 1]
: memref<1x108xf32> to memref<1x1x1x108xf32>
%0 = memref.load %reinterpret_cast[%c0, %c1, %c0, %c0]
: memref<1x1x1x108xf32>
```
The pattern would skip `%c1` when forming the indices for the replacement load,
since it cuts the dimensions that were added to the left, including the
dimension where the non-zero index is:
```mlir
%0 = memref.load %src[%c0, %c0]
: memref<1x108xf32>
```
causing the rewrite to discard a non-zero index on a size-1 dimension, which is
not semantics-preserving.

## Correctness

In the accepted cases, the cast is a pure view that does not alter memory
layout. Size-1 dimensions do not contribute to address computation, and the
single non-unit dimension determines the access.

Dropping indices for size-1 dimensions (or inserting zeros when collapsing
rank) preserves the computed address. The rewrite is only applied when such
indices are statically zero, ensuring in-bounds semantics.

Therefore, the rewritten load is equivalent to the original load through the
`reinterpret_cast`



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list