[llvm] [AMDGPU] Add dereferenceable retAttr to a call to llvm.amdgcn.implicitarg.ptr in amdgpu-codegenprepare (PR #182206)

Yoonseo Choi via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 19 11:30:37 PST 2026


yoonseoch wrote:

> I think this is the wrong place to handle this. You can either have clang introduce this on the callsite based on the known target ABI. But we could also get away with doing this in AMDGPUInstCombineIntrinsic

In the beginning, the call to implicitarg_ptr and the load from it were both resided in the inner loop's preheader. LICM in the middle end hoisted the call to implicitarg_ptr to outer loop's preheader (as condionally executable speculatable call), then dropped all UB attributes. Even if a call to implicitarg_ptr had dereferenceable added by clang, it would have been dropped at this point. Then, the load reading from implicitarg_ptr cannot be hoisted by LICM as its base pointer is no longer dereferenceable, which made this load is no longer unconditionally executable.

What this PR does is to add dereferenceable back to the call o implicitarg_ptr, after middle end LICMs are done but before the final LICM. So, the final LICM can hoist the remaining loads from implicitarg_ptr. 

Adding dereferenceable to call sites by clang when it emits LLVM IR won't work in the scenario above. I think the issue is related to phase ordering due to LICM's dropping of dereferenceable from hoisted calls.

However, now that I looked into Value::getPointerDerefernceableBytes again it seems if the function llvm.amdgcn.implicitarg.ptr itself had a retattr dereferenceable, LICM would hoisted the load looking the called function's dereferenceable. 
```
// InstrTypes.h - CallBase
uint64_t getRetDereferenceableBytes() const {
    uint64_t Bytes = Attrs.getRetDereferenceableBytes();
    if (const Function *F = getCalledFunction())
      Bytes = std::max(Bytes, F->getAttributes().getRetDereferenceableBytes()); // max of the two!
    return Bytes;
  }
```
My quick experiment of adding Dereferenceable<RetIndex, 256> to the function's signature made LICM hoist the load without changes in this PR. 
```
def int_amdgcn_implicitarg_ptr :
  ClangBuiltin<"__builtin_amdgcn_implicitarg_ptr">,
  DefaultAttrsIntrinsic<[LLVMQualPointerType<4>], [],
  [Dereferenceable<RetIndex, 256>, Align<RetIndex, 4>, NoUndef<RetIndex>,
   IntrNoMem, IntrSpeculatable]>;
```

Do you think it is better to add dereferenceable to the intrinsic's signature either through .td or from some other place? Or another suggestion?

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


More information about the llvm-commits mailing list