Hi,
I’m working on PR #177908, which extends the LoadStoreVectorizer (LSV) to merge contiguous loads/stores of different scalar types (e.g. {ptr, i16, i16} or {ptr, <32 x i1>}) into a single vector memory operation. This resolves #97715.
Most of the PR review feedback has been addressed, but one item remains open: whether the pointer-recovery strategy in mixed-type chains is IR-legal and provenance-safe. I’m posting here to get broader input before merging.
Background
LSV currently groups instructions by base pointer and address space. This PR removes scalar element type from the equivalence-class key so mixed-type contiguous accesses can be vectorized together.
For a heterogeneous chain, the vector lane width is the GCD of the scalar bitwidths. Original types are recovered after the vector load/store using IRBuilder::CreateBitPreservingCastChain.
Homogeneous chains (e.g. {i32, i32, i32, i32}) keep their original scalar element type to avoid unnecessary churn.
The pointer concern
Mixing pointers with narrower types is the hard case. An early approach normalized chains to integer GCD lanes (iN / <K x iN>) and used inttoptr/ptrtoint to recover pointers. That is incorrect when the pointer is wider than the GCD:
; GCD = 32, ptr = 64-bit — loses high bits
%trunc = trunc i64 %p to i32
%ptr = inttoptr i32 %trunc to ptr ; unsound
To avoid that, the PR now always normalizes heterogeneous chains to byte lanes (b<GCD> / <K x b<GCD>>), including sub-byte widths (b1, b2, b4). LangRef permits pointer↔byte bitcasts of the same size, and byte types represent raw memory bits without integer provenance.
Example from llvm/test/Transforms/LoadStoreVectorizer/mixed-ptr-and-subbyte-int.ll:
; Before
%x0 = load ptr addrspace(3), ptr addrspace(3) %o0
%x1 = load <32 x i1>, ptr addrspace(3) %o4
; After LSV
%v = load <64 x b1>, ptr addrspace(3) %o0
%p0 = shufflevector <64 x b1> %v, ..., <32 x i32> <0, 1, ..., 31>
%p = bitcast <32 x b1> %p0 to ptr addrspace(3)
%i1 = bitcast <32 x b1> %p1 to <32 x i1>
Similar coverage exists in mixed-ptr-and-subbyte.ll and AMDGPU/mixed-ptr-and-narrow-int.ll.
Where inttoptr/ptrtoint still appears
Reviewers flagged that CreateBitPreservingCastChain may still route some conversions through inttoptr/ptrtoint rather than a plain bitcast. Related work landed separately in #209557 (merged): byte and byte-vector types are now treated as “int-like” in CreateBitPreservingCastChain, so <N x b32> → ptr goes through bitcast to i64 + inttoptr instead of a direct (and invalid) bitcast <2 x b32> to ptr.
For the LSV sub-byte cases above, the tests currently expect direct same-sized bitcasts such as bitcast <32 x b1> to ptr. After rebasing fully onto main (with #209557), some of these may become inttoptr (bitcast … to iN) to ptr instead.
The question is whether that is acceptable in this context:
- Provenance: Does recovering a pointer from a byte vector (after a mixed-type vector load of raw memory) require preserving provenance via bitcast, or is
inttoptrof the same bit pattern sound? - LangRef: Is
bitcast <N x bK> to ptr(same total bitwidth) the preferred form, or should we always go throughinttoptr/ptrtointfor byte→pointer conversions? - Sub-byte lanes: For GCD = 1/2/4, is normalizing to
b1/b2/b4(rather thani1/i2/i4) the right long-term approach?
What we’d like feedback on
- Is the byte-normalization strategy for heterogeneous LSV chains reasonable?
- For pointer recovery from byte vectors of the same total size, is
bitcastvsinttoptr/ptrtointthe correct lowering? - Any objections to merging #177908 once this is resolved?
Related PRs / context
- #177908 — mixed-type LSV (this work)
- #209557 —
CreateBitPreservingCastChainbyte-type handling (merged) - #203356 —
stripAndAccumulateConstantOffsetsbitcast walk fix (merged) - Byte type RFC
- Earlier LSV RFC
NVIDIA review (@dakersnar) is tentatively positive pending this item; the original inttoptr concern on the sub-byte tests was resolved once we switched to byte normalization, but a second opinion from IR/provenance folks was requested.
cc @nikic @efriedma-quic @arsenm @dakersnar — any thoughts appreciated.
Thanks,
Anshil Gandhi