-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NVPTX][AA] Traverse use-def chain to find non-generic addrspace #106477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-backend-nvptx Author: Alex MacLean (AlexMaclean) ChangesAddress space information may be encoded anywhere along the use-def chain. take advantage of this by traversing the chain until we find a non-generic addrspace. Full diff: https://github.com/llvm/llvm-project/pull/106477.diff 2 Files Affected:
diff --git a/llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp b/llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp
index 4f106584eb0a94..e86dfee875fd89 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp
@@ -15,11 +15,17 @@
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/Support/CommandLine.h"
using namespace llvm;
#define DEBUG_TYPE "NVPTX-aa"
+static cl::opt<unsigned> TraverseAddressSpacesLimit(
+ "nvptx-traverse-address-aliasing-limit", cl::Hidden,
+ cl::desc("Depth limit for finding address space through traversal"),
+ cl::init(6));
+
AnalysisKey NVPTXAA::Key;
char NVPTXAAWrapperPass::ID = 0;
@@ -47,6 +53,28 @@ 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; MaxLookup == 0 || Count < MaxLookup; ++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)
+ 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;
+ }
+ return AddressSpace::ADDRESS_SPACE_GENERIC;
+}
+
static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC))
return AliasResult::MayAlias;
@@ -70,8 +98,8 @@ static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
AliasResult NVPTXAAResult::alias(const MemoryLocation &Loc1,
const MemoryLocation &Loc2, AAQueryInfo &AAQI,
const Instruction *) {
- unsigned AS1 = Loc1.Ptr->getType()->getPointerAddressSpace();
- unsigned AS2 = Loc2.Ptr->getType()->getPointerAddressSpace();
+ unsigned AS1 = getAddressSpace(Loc1.Ptr, TraverseAddressSpacesLimit);
+ unsigned AS2 = getAddressSpace(Loc2.Ptr, TraverseAddressSpacesLimit);
return getAliasResult(AS1, AS2);
}
@@ -87,11 +115,7 @@ static bool isConstOrParam(unsigned AS) {
ModRefInfo NVPTXAAResult::getModRefInfoMask(const MemoryLocation &Loc,
AAQueryInfo &AAQI,
bool IgnoreLocals) {
- if (isConstOrParam(Loc.Ptr->getType()->getPointerAddressSpace()))
- return ModRefInfo::NoModRef;
-
- const Value *Base = getUnderlyingObject(Loc.Ptr);
- if (isConstOrParam(Base->getType()->getPointerAddressSpace()))
+ if (isConstOrParam(getAddressSpace(Loc.Ptr, TraverseAddressSpacesLimit)))
return ModRefInfo::NoModRef;
return ModRefInfo::ModRef;
diff --git a/llvm/test/CodeGen/NVPTX/nvptx-aa.ll b/llvm/test/CodeGen/NVPTX/nvptx-aa.ll
index 79bd06ee677ac0..074e741dc3e949 100644
--- a/llvm/test/CodeGen/NVPTX/nvptx-aa.ll
+++ b/llvm/test/CodeGen/NVPTX/nvptx-aa.ll
@@ -111,3 +111,40 @@ loop:
done:
ret i8 %v2
}
+
+;; Address space information may be encoded anywhere along the UD chain.
+;; We define a set of tests that:
+;; 1. Perform some number of address space casts on pointer A and B
+;; 2. Store a value to address A
+;; 3. Store a value to address B (that we know does not alias with A)
+
+;; generic->space
+; CHECK-ALIAS-LABEL: Function: test_traversal_gen_space
+; CHECK-ALIAS: NoAlias: i32 addrspace(1)* %global, i32 addrspace(5)* %local
+define void @test_traversal_gen_space(ptr %gen, ptr addrspace(1) %global) {
+ %local = addrspacecast ptr %gen to ptr addrspace(5)
+ store i32 1, ptr addrspace(5) %local, align 8
+ store i32 5, ptr addrspace(1) %global, align 8
+ ret void
+}
+
+;; space->generic
+; CHECK-ALIAS-LABEL: Function: test_traversal_space_gen
+; CHECK-ALIAS: NoAlias: i32* %gen, i32 addrspace(1)* %global
+define void @test_traversal_space_gen(ptr addrspace(5) %local, ptr addrspace(1) %global) {
+ %gen = addrspacecast ptr addrspace(5) %local to ptr
+ store i32 2, ptr %gen, align 8
+ store i32 5, ptr addrspace(1) %global, align 8
+ ret void
+}
+
+;; generic->space->generic
+; CHECK-ALIAS-LABEL: Function: test_traversal_gen_space_gen
+; CHECK-ALIAS: NoAlias: i32* %gen2, i32 addrspace(1)* %global
+define void @test_traversal_gen_space_gen(ptr %gen1, ptr addrspace(1) %global) {
+ %local = addrspacecast ptr %gen1 to ptr addrspace(5)
+ %gen2 = addrspacecast ptr addrspace(5) %local to ptr
+ store i32 3, ptr %gen2, align 8
+ store i32 5, ptr addrspace(1) %global, align 8
+ ret void
+}
|
d8dcccf
to
8781e91
Compare
return AddressSpace::ADDRESS_SPACE_GENERIC; | ||
V = VNext; | ||
} | ||
return AddressSpace::ADDRESS_SPACE_GENERIC; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we assuming that we're starting with a generic AS? If so, I'd add an assertion. Otherwise we should probably return V's AS when we bail out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is V's AS is non-generic it will be returned above. I've added an assert to confirm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the loop logic is flawed. If V
is set in the last loop iteration to something in non-generic AS, the assertion will fail.
I think you may want to extract "get Value's AS" into a helper lambda, use it as part of the loop condition, and apply it to the last V's value if we terminate the loop without returning early.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apologies, you're correct. I've update the loops logic.
return AddressSpace::ADDRESS_SPACE_GENERIC; | ||
V = VNext; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably have llvm_unreachable()
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) {
if (const auto *PTy = dyn_cast<PointerType>(V->getType()))
return PTy->getAddressSpace();
return AddressSpace::ADDRESS_SPACE_GENERIC);
}
while (MaxDepth-- && getAS(V) == ADDRESS_SPACE_GENERIC) {
const Value *VNext = getUnderlyingObject(V, 1);
if (VNext == V)
break;
V = VNext;
}
return getAS(V);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I've adopted the lambda approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/66/builds/3253 Here is the relevant piece of the build log for the reference
|
Address space information may be encoded anywhere along the use-def chain. take advantage of this by traversing the chain until we find a non-generic addrspace.