[llvm] Attributor: Add noalias.addrspace attribute for store and load (PR #136553)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 21 20:39:21 PDT 2025
================
@@ -12784,6 +12785,172 @@ struct AAAddressSpaceCallSiteArgument final : AAAddressSpaceImpl {
};
} // namespace
+/// ------------------------ No Alias Address Space ---------------------------
+namespace {
+struct AANoAliasAddrSpaceImpl : public AANoAliasAddrSpace {
+ AANoAliasAddrSpaceImpl(const IRPosition &IRP, Attributor &A)
+ : AANoAliasAddrSpace(IRP, A) {}
+
+ void initialize(Attributor &A) override {
+ assert(getAssociatedType()->isPtrOrPtrVectorTy() &&
+ "Associated value is not a pointer");
+
+ if (!A.getInfoCache().getFlatAddressSpace().has_value()) {
+ indicatePessimisticFixpoint();
+ return;
+ }
+
+ unsigned FlatAS = A.getInfoCache().getFlatAddressSpace().value();
+ unsigned AS = getAssociatedType()->getPointerAddressSpace();
+ if (AS != FlatAS) {
+ removeAssumedBits(1 << AS);
+ indicateOptimisticFixpoint();
+ }
+ }
+
+ ChangeStatus updateImpl(Attributor &A) override {
+ unsigned FlatAS = A.getInfoCache().getFlatAddressSpace().value();
+ uint32_t OrigAssumed = getAssumed();
+
+ auto CheckAddressSpace = [&](Value &Obj) {
+ if (isa<UndefValue>(&Obj))
+ return true;
+ // Handle argument in flat address space only has addrspace cast uses
+ if (auto *Arg = dyn_cast<Argument>(&Obj)) {
+ if (Arg->getType()->getPointerAddressSpace() == FlatAS) {
+ for (auto *U : Arg->users()) {
+ auto *ASCI = dyn_cast<AddrSpaceCastInst>(U);
+ if (!ASCI)
+ return false;
----------------
shiltian wrote:
We have a very specific pattern introduced in the kernel argument promotion pass where a kernel argument pointer is cast from AS0 to AS1, then cast back to AS0, and all uses of the original pointer are replaced with the final cast AS0 version. The pass relies on the InferAddressSpace pass to detect this pattern and perform the necessary rewrites, rather than modifying the kernel signature directly.
We briefly discussed this back when I implemented `AAAddressSpace`, and you mentioned kernel arguments should ideally be in AS1 in the first place instead of patching things up later in the middle end. We didn’t come to a conclusion at the time since someone pointed out that changing the address space in the kernel signature might break the ABI.
That said, I agree that the cleanest solution would be to have kernel arguments in AS1 to begin with. It’s probably worth adding this to our project list.
As for the handling in the current context, I think it's reasonable to special-case this pattern, just like I’ve done in `AAAddressSpace`.
https://github.com/llvm/llvm-project/pull/136553
More information about the llvm-commits
mailing list