In C++ based languages targeting GPUGPU, e.g. HCC, OpenMP or Cuda, it is desirable to keep the AST free of explicit address space qualifier for simplified parsing and type checking. We can assume that all the pointers are generic pointers which can pointing to any real address spaces in the target IR. This results in a consistent and clean AST and therefore IR since every pointer is a generic pointer.
In Clang codegen, when we need to emit global variables or allocas in real address space, we cast the resulting pointer to a generic pointer and put them in the address map. Later on, when we need to refer to the address, we always get a generic pointer. In this way, we can keep the translation of AST consistent.
Currently Clang assumes that the address space for a generic pointer (a pointer which can pointing to any real memory region) is 0, which is correct for x86 and nvptx target, but is not correct for some other targets, e.g., amdgcn target, for which the generic address space is 4.
This patch introduce a virtual function TargetInfo::getDefaultTargetAddressSpace which allows each target to specify the default target address space based on language options. It also introduces ASTContext::getTargetGlobalAddressSpace and ASTContext::getTargetConstantAddressSpace as an abstraction for a unified approach to support different languages on address space aware targets, e.g. amdgcn and nvptx.
It also fixes various places in codegen for incorrect assumptions about default address space.
With this change, Clang will be able to generate correct IR for C++ based kernel languages e.g. HCC, OpenMP and Cuda for a greater range of targets.
It will not change the emitted IR for targets using 0 as generic address space. For these targets, since alloca returns generic pointer, they usually require some LLVM passes to cast alloca to real address space. This patch can help those targets generate correct alloca in Clang codegen.
This patch was co-authored by Yaxun (Sam) Liu and Wen-Heng (Jack) Chung.