Skip to content

[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

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +53,24 @@ 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.
auto GetAS = [](const Value *V) -> unsigned {
if (const auto *PTy = dyn_cast<PointerType>(V->getType()))
return PTy->getAddressSpace();
return ADDRESS_SPACE_GENERIC;
};
while (MaxLookup-- && GetAS(V) == ADDRESS_SPACE_GENERIC) {
const Value *NewV = getUnderlyingObject(V, 1);
if (NewV == V)
break;
V = NewV;
}
return GetAS(V);
}
Copy link
Member

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.

Copy link
Member

@Artem-B Artem-B Aug 29, 2024

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);

Copy link
Member Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!


static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC))
return AliasResult::MayAlias;
Expand All @@ -70,8 +94,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);
}
Expand All @@ -87,11 +111,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;
Expand Down
37 changes: 37 additions & 0 deletions llvm/test/CodeGen/NVPTX/nvptx-aa.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading