[clang] [Clang][OpenCL] Mark scalar loads from constant address space invariant (PR #218356)

Keshav Vinayak Jha via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 23:31:13 PST 2026


https://github.com/keshavvinayak01 commented:

@efriedma-quic  `!invariant.load` does give IR optimizations information they don't reliably have from the IR address space alone:

- Language CAS aren't always preserved in IR. Lowering can collapse or cast constant storage into a shared/generic address space (e.g. OpenCL __constant → AS0 on flat-AS targets, or CUDA/HIP __constant__ cast to generic AS0 pointers), so the load's IR address space no longer identifies it as invariant.

- LLVM IR has no target-independent notion of a CAS. Only AMDGPU and NVPTX teach target AA that their constant address space is NoModRef; other targets (such as SPIR-V and DXIL/HLSL) don't.

> IR optimizations can trivially compute the address-space of a memory operation; if all addresses in a given address-space have this property, that should be trivially apparent to optimizations. Or is this language notion of "address-space" not apparent in the IR?

Several generic IR passes check MD_invariant_load directly without querying AA (e.g. InstCombine load sinking, EarlyCSE, Sink, LoadStoreVectorizer), so they miss invariance even when target AA knows the address space is constant.


Storing the flag in a spare bit of `Address::ElementType` (mirroring `KnownNonNull`, __current solution__) carries it through address-space casts and GEPs to load emission.

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


More information about the cfe-commits mailing list