File changed.
Preview size limit exceeded, changes collapsed.
Loading
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`
File changed.
Preview size limit exceeded, changes collapsed.