Hello everyone.
At the moment, the SPIR-V back-end uses an address space map (apparently inherited from SPIR) that maps the private (Function in SPIR-V parlance, local in CUDA parlance) language AS to numerical 0. This is, unfortunately, rather problematic, because LLVM implicitly (but very strongly) assumes that AS0 is a safe / portable default which yields portable / generic pointers, whereas private is very constrained and has pretty specific semantics in offload languages and in SPIR-V. More explicitly, we use PointerType::getUnqual and PointerType::get(T) all over the place in CodeGen, and whilst it could be argued that with careful thought and analysis one could always “correct” these uses, that’s a very major and probably unrealistic task. For some related discussion, please see https://github.com/llvm/llvm-project/pull/108786
Given the above, I would like to propose that we switch the BE’s AS map to something like the following:
SPIRVASMap[]  = {
static const unsigned SPIRDefIsGenMap[] = {
    0, // opencl_generic (Default)
    1, // opencl_global
    3, // opencl_local
    2, // opencl_constant
    4, // opencl_private
    5, // opencl_global_device
    6, // opencl_global_host
    // And more
}
We should delete the DefIsPrivMap hack, which seems inherited from AMDGPU - it is a bad solution to an OpenCL peculiarity, which should be handled in the FE. At the same time, there’s s bit of heavy lifting going on around HIPSPV and how it handles CUDA/HIP __constant__, which also seems misplaced in the AS map. This might be a bit noisy for downstream tools, so we will have to work with the Translator to make sure it gracefully absorbs the change, as it currently hardcodes assumptions about address space numbering (I have some ongoing work around making the Translator more flexible in that regard and will update this RFC once the respective PRs/RFCs go up). Overall, whilst this might appear painful at a glance, it is very likely to spare us considerable future pain (for context, we had to go through something similar with AMDGPU, except this was past the point of being Experimental).
@VyacheslavLevytskyy @michalpaszkowski @Keenuts @arsenm @gonzalobg @shiltian