[mlir][memref] Enforce consistent reinterpret_cast metadata (#217338)
This PR implements [[RFC] Clarify `memref.reinterpret_cast` Verification
of Dynamic
Metadata](https://discourse.llvm.org/t/rfc-clarify-memref-reinterpret-cast-verification-of-dynamic-metadata).
## Context
`memref.reinterpret_cast` describes its result through both its mixed
offset/size/stride metadata and its result `MemRefType`. Currently,
verification allowed a dynamic result-type correspond to static
operation metadata, introducing inconsistent descriptors.
For example, the verifier permits:
```mlir
%r = memref.reinterpret_cast %base
to offset: [0], sizes: [%n, 2], strides: [%s, 1] : memref<f32>
to memref<?x?xf32, strided<[?, ?], offset: ?>>
```
where the operation constructs a descriptor with a statically known
second size and stride, but the result type does not expose those facts.
## Changes
This PR consolidates the verifier to require `reinterpret_cast` result
type’s sizes, offset, and strides to exactly match the static/dynamic
representation and values of the operation metadata.
Transforms that create `memref.reinterpret_cast`, including GPU memref
decomposition and strided-metadata expansion for `subview` and
`collapse_shape`, now derive the result type from the final, potentially
folded descriptor instead of inheriting the original operation’s result
type.
When this descriptor-derived type differs from the original result type,
the rewrite inserts a `memref.cast` back to the original type. These
rewrites are not type-conversion patterns, so their existing
users—including function returns and calls—must retain their original
types. Identity stride layouts are canonicalized when possible to avoid
unnecessary explicit layouts and casts.
Tests are updated to use result types consistent with their
`reinterpret_cast` metadata. In the SPIR-V tests, the size `10` is
represented by an SSA value because those cases intentionally remain
dynamically shaped and lower to element pointers; making the size static
would change the SPIR-V pointee type and prevent the existing
`reinterpret_cast` lowering from applying.
A regression test also verifies that folded `subview` offsets and
strides are reflected in the generated `reinterpret_cast` type.
---
Assisted-by: Codex (refine implementation + tests). I reviewed all code
and tests before submission.