[llvm] Inline: Fix handling of byval using non-alloca addrspace (PR #97306)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 05:31:14 PDT 2024


================
@@ -1674,8 +1674,9 @@ static Value *HandleByValArgument(Type *ByValType, Value *Arg,
   if (ByValAlignment)
     Alignment = std::max(Alignment, *ByValAlignment);
 
-  AllocaInst *NewAlloca = new AllocaInst(ByValType, DL.getAllocaAddrSpace(),
----------------
MrSidims wrote:

Digging a bit into git log the patch that added DL.getAllocaAddrSpace() was the following:
```
commit 3c1fc768ed6cf2b01463df036ae6ae3b1f0de632
Author: Matt Arsenault <Matthew.Arsenault at amd.com>
Date:   Mon Apr 10 22:27:50 2017 +0000

    Allow DataLayout to specify addrspace for allocas.
```
and reasoning behind that patch:
"    These are problems for AMDGPU because alloca is used to
    implement the private address space, which uses a 32-bit
    index as the pointer value. Other pointers are 64-bit
    and behave more like LLVM's notion of generic address
    space. By changing the address space used for allocas,
    we can change our generic pointer type to be LLVM's generic
    pointer type which does have similar properties."
 
 makes sense to me especially in OpenCL context, where default address space is 4 (generic) and LLVM passes must respect target's datalayout when picking possible types for the created instructions.
 
Current fix brings some complications though, if byval pointer of a function was is default aka genetic address space, then the alloca would be in generic. Meanwhile per SPIR-V (to which LLVM IR is being lowered for OpenCL 2.0 and earlier) specification OpVariable must have non-generic storage class. It can probably be workarounded in https://github.com/KhronosGroup/SPIRV-LLVM-Translator by translating alloca addrspace(4) to OpVariable with Private storage class bringing extra address space casts or by modifying clang, introducing an exception for byval pointers making them not be in default address space, but in private, but I wonder if https://github.com/llvm/llvm-project/issues/97086 can be solved differently - instead here if Arg->getType()->getPointerAddressSpace() != DL.getAllocaAddrSpace() - we still create an alloca in default datalayout address space and add an AS cast, WDYT? 
 


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


More information about the llvm-commits mailing list