Skip to content

Commit 4f56200

Browse files
committed
[Attributor] Take the address space from addrspacecast directly
If the value to be analyzed is directly from addrspacecast, we take the source address space directly. This is to improve the case where in `AMDGPUPromoteKernelArgumentsPass`, the kernel argument is promoted by insertting an addrspacecast directly from a generic pointer. However, during the analysis, the underlying object will be the generic pointer, instead of the addrspacecast, thus the inferred address space is the generic one, which is not ideal.
1 parent fb2ed73 commit 4f56200

File tree

2 files changed

+79
-14
lines changed

2 files changed

+79
-14
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12587,16 +12587,37 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1258712587
}
1258812588

1258912589
ChangeStatus updateImpl(Attributor &A) override {
12590+
assert(A.getInfoCache().getDL().getFlatAddressSpace().has_value());
12591+
unsigned FlatAS = A.getInfoCache().getDL().getFlatAddressSpace().value();
1259012592
uint32_t OldAddressSpace = AssumedAddressSpace;
12591-
auto *AUO = A.getOrCreateAAFor<AAUnderlyingObjects>(getIRPosition(), this,
12592-
DepClassTy::REQUIRED);
12593-
auto Pred = [&](Value &Obj) {
12593+
12594+
auto CheckAddressSpace = [&](Value &Obj) {
1259412595
if (isa<UndefValue>(&Obj))
1259512596
return true;
12597+
// If an argument in flat address space only has addrspace cast uses, and
12598+
// those casts are same, then we take the dst addrspace.
12599+
if (auto *Arg = dyn_cast<Argument>(&Obj)) {
12600+
if (Arg->getType()->getPointerAddressSpace() == FlatAS) {
12601+
unsigned CastAddrSpace = FlatAS;
12602+
for (auto *U : Arg->users()) {
12603+
auto *ASCI = dyn_cast<AddrSpaceCastInst>(U);
12604+
if (!ASCI)
12605+
return takeAddressSpace(Obj.getType()->getPointerAddressSpace());
12606+
if (CastAddrSpace != FlatAS &&
12607+
CastAddrSpace != ASCI->getDestAddressSpace())
12608+
return false;
12609+
CastAddrSpace = ASCI->getDestAddressSpace();
12610+
}
12611+
if (CastAddrSpace != FlatAS)
12612+
return takeAddressSpace(CastAddrSpace);
12613+
}
12614+
}
1259612615
return takeAddressSpace(Obj.getType()->getPointerAddressSpace());
1259712616
};
1259812617

12599-
if (!AUO->forallUnderlyingObjects(Pred))
12618+
auto *AUO = A.getOrCreateAAFor<AAUnderlyingObjects>(getIRPosition(), this,
12619+
DepClassTy::REQUIRED);
12620+
if (!AUO->forallUnderlyingObjects(CheckAddressSpace))
1260012621
return indicatePessimisticFixpoint();
1260112622

1260212623
return OldAddressSpace == AssumedAddressSpace ? ChangeStatus::UNCHANGED
@@ -12605,17 +12626,21 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1260512626

1260612627
/// See AbstractAttribute::manifest(...).
1260712628
ChangeStatus manifest(Attributor &A) override {
12608-
if (getAddressSpace() == InvalidAddressSpace ||
12609-
getAddressSpace() == getAssociatedType()->getPointerAddressSpace())
12629+
unsigned NewAS = getAddressSpace();
12630+
12631+
if (NewAS == InvalidAddressSpace ||
12632+
NewAS == getAssociatedType()->getPointerAddressSpace())
1261012633
return ChangeStatus::UNCHANGED;
1261112634

12635+
unsigned FlatAS = A.getInfoCache().getDL().getFlatAddressSpace().value();
12636+
1261212637
Value *AssociatedValue = &getAssociatedValue();
12613-
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12638+
Value *OriginalValue = peelAddrspacecast(AssociatedValue, FlatAS);
1261412639

1261512640
PointerType *NewPtrTy =
12616-
PointerType::get(getAssociatedType()->getContext(), getAddressSpace());
12641+
PointerType::get(getAssociatedType()->getContext(), NewAS);
1261712642
bool UseOriginalValue =
12618-
OriginalValue->getType()->getPointerAddressSpace() == getAddressSpace();
12643+
OriginalValue->getType()->getPointerAddressSpace() == NewAS;
1261912644

1262012645
bool Changed = false;
1262112646

@@ -12675,12 +12700,19 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1267512700
return AssumedAddressSpace == AS;
1267612701
}
1267712702

12678-
static Value *peelAddrspacecast(Value *V) {
12679-
if (auto *I = dyn_cast<AddrSpaceCastInst>(V))
12680-
return peelAddrspacecast(I->getPointerOperand());
12703+
static Value *peelAddrspacecast(Value *V, unsigned FlatAS) {
12704+
if (auto *I = dyn_cast<AddrSpaceCastInst>(V)) {
12705+
assert(I->getSrcAddressSpace() != FlatAS &&
12706+
"there should not be flat AS -> non-flat AS");
12707+
return I->getPointerOperand();
12708+
}
1268112709
if (auto *C = dyn_cast<ConstantExpr>(V))
12682-
if (C->getOpcode() == Instruction::AddrSpaceCast)
12683-
return peelAddrspacecast(C->getOperand(0));
12710+
if (C->getOpcode() == Instruction::AddrSpaceCast) {
12711+
assert(C->getOperand(0)->getType()->getPointerAddressSpace() !=
12712+
FlatAS &&
12713+
"there should not be flat AS -> non-flat AS X");
12714+
return C->getOperand(0);
12715+
}
1268412716
return V;
1268512717
}
1268612718
};

llvm/test/CodeGen/AMDGPU/aa-as-infer.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,36 @@ define void @foo(ptr addrspace(3) %val) {
243243
ret void
244244
}
245245

246+
define void @kernel_argument_promotion_pattern_intra_procedure(ptr %p, i32 %val) {
247+
; CHECK-LABEL: define void @kernel_argument_promotion_pattern_intra_procedure(
248+
; CHECK-SAME: ptr [[P:%.*]], i32 [[VAL:%.*]]) #[[ATTR0]] {
249+
; CHECK-NEXT: store i32 [[VAL]], ptr [[P]], align 4
250+
; CHECK-NEXT: ret void
251+
;
252+
%p.cast.0 = addrspacecast ptr %p to ptr addrspace(1)
253+
%p.cast.1 = addrspacecast ptr addrspace(1) %p.cast.0 to ptr
254+
store i32 %val, ptr %p.cast.1
255+
ret void
256+
}
257+
258+
define internal void @use_argument_after_promotion(ptr %p, i32 %val) {
259+
; CHECK-LABEL: define internal void @use_argument_after_promotion(
260+
; CHECK-SAME: ptr [[P:%.*]], i32 [[VAL:%.*]]) #[[ATTR0]] {
261+
; CHECK-NEXT: store i32 [[VAL]], ptr [[P]], align 4
262+
; CHECK-NEXT: ret void
263+
;
264+
store i32 %val, ptr %p
265+
ret void
266+
}
267+
268+
define void @kernel_argument_promotion_pattern_inter_procedure(ptr %p, i32 %val) {
269+
; CHECK-LABEL: define void @kernel_argument_promotion_pattern_inter_procedure(
270+
; CHECK-SAME: ptr [[P:%.*]], i32 [[VAL:%.*]]) #[[ATTR0]] {
271+
; CHECK-NEXT: call void @use_argument_after_promotion(ptr [[P]], i32 [[VAL]])
272+
; CHECK-NEXT: ret void
273+
;
274+
%p.cast.0 = addrspacecast ptr %p to ptr addrspace(1)
275+
%p.cast.1 = addrspacecast ptr addrspace(1) %p.cast.0 to ptr
276+
call void @use_argument_after_promotion(ptr %p.cast.1, i32 %val)
277+
ret void
278+
}

0 commit comments

Comments
 (0)