[llvm] [NVPTX][AA] Traverse use-def chain to find non-generic addrspace (PR #106477)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 15:52:55 PDT 2024


================
@@ -47,6 +53,27 @@ void NVPTXAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.setPreservesAll();
 }
 
+static unsigned getAddressSpace(const Value *V, unsigned MaxLookup) {
+  // Find the first non-generic address space traversing the UD chain.
+  // It is undefined behaviour if a pointer belongs to more than one
+  // non-overlapping address spaces along a valid execution path.
+  for (unsigned Count = 0;; ++Count) {
+    const auto *PTy = dyn_cast<PointerType>(V->getType());
+    if (!PTy)
+      return AddressSpace::ADDRESS_SPACE_GENERIC;
+
+    const unsigned AS = PTy->getAddressSpace();
+    if (AS != AddressSpace::ADDRESS_SPACE_GENERIC || Count >= MaxLookup)
+      return AS;
+
+    // Continue traversing if address space is generic
+    const Value *VNext = getUnderlyingObject(V, 1);
+    if (VNext == V)
+      return AddressSpace::ADDRESS_SPACE_GENERIC;
+    V = VNext;
+  }
+}
----------------
Artem-B wrote:

The function does the job, but that exit on max depth in the middle of the function looks odd.

It should all be as readable as the comment at the top of the function.
I think lambda would work quite nicely here:

```
auto getAS = [](Value *V) {
  const auto *PTy = dyn_cast<PointerType>(V->getType());
    if (!PTy)
      return AddressSpace::ADDRESS_SPACE_GENERIC;

    const unsigned AS = PTy->getAddressSpace();
    if (AS != AddressSpace::ADDRESS_SPACE_GENERIC || Count >= MaxLookup)
      return AS;
}
while (MaxDepth-- && getAS(V) == ADDRESS_SPACE_GENERIC) {
  const Value *VNext = getUnderlyingObject(V, 1);
  if (VNext == V)
     break;
  V = VNext;
}
return getAS(V);
```

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


More information about the llvm-commits mailing list