Skip to content

Commit 75979ba

Browse files
shiltianpuja2196
authored andcommitted
[Attributor] Use more appropriate approach to check flat address space (#108713)
1 parent a499b7b commit 75979ba

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

llvm/include/llvm/Transforms/IPO/Attributor.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,9 @@ struct InformationCache {
13411341
const ArrayRef<Function *>
13421342
getIndirectlyCallableFunctions(Attributor &A) const;
13431343

1344+
/// Return the flat address space if the associated target has.
1345+
std::optional<unsigned> getFlatAddressSpace() const;
1346+
13441347
private:
13451348
struct FunctionInfo {
13461349
~FunctionInfo();
@@ -6267,11 +6270,12 @@ struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> {
62676270
return (AA->getIdAddr() == &ID);
62686271
}
62696272

6270-
// No address space which indicates the associated value is dead.
6271-
static const uint32_t NoAddressSpace = ~0U;
6272-
62736273
/// Unique ID (due to the unique address)
62746274
static const char ID;
6275+
6276+
protected:
6277+
// Invalid address space which indicates the associated value is dead.
6278+
static const uint32_t InvalidAddressSpace = ~0U;
62756279
};
62766280

62776281
struct AAAllocationInfo : public StateWrapper<BooleanState, AbstractAttribute> {

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,6 +3294,12 @@ InformationCache::getIndirectlyCallableFunctions(Attributor &A) const {
32943294
return IndirectlyCallableFunctions;
32953295
}
32963296

3297+
std::optional<unsigned> InformationCache::getFlatAddressSpace() const {
3298+
if (TargetTriple.isAMDGPU() || TargetTriple.isNVPTX())
3299+
return 0;
3300+
return std::nullopt;
3301+
}
3302+
32973303
void Attributor::recordDependence(const AbstractAttribute &FromAA,
32983304
const AbstractAttribute &ToAA,
32993305
DepClassTy DepClass) {

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12580,8 +12580,19 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1258012580
void initialize(Attributor &A) override {
1258112581
assert(getAssociatedType()->isPtrOrPtrVectorTy() &&
1258212582
"Associated value is not a pointer");
12583-
if (getAssociatedType()->getPointerAddressSpace())
12583+
12584+
if (!A.getInfoCache().getFlatAddressSpace().has_value()) {
12585+
indicatePessimisticFixpoint();
12586+
return;
12587+
}
12588+
12589+
unsigned FlatAS = A.getInfoCache().getFlatAddressSpace().value();
12590+
unsigned AS = getAssociatedType()->getPointerAddressSpace();
12591+
if (AS != FlatAS) {
12592+
[[maybe_unused]] bool R = takeAddressSpace(AS);
12593+
assert(R && "The take should happen");
1258412594
indicateOptimisticFixpoint();
12595+
}
1258512596
}
1258612597

1258712598
ChangeStatus updateImpl(Attributor &A) override {
@@ -12603,12 +12614,13 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1260312614

1260412615
/// See AbstractAttribute::manifest(...).
1260512616
ChangeStatus manifest(Attributor &A) override {
12606-
Value *AssociatedValue = &getAssociatedValue();
12607-
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12608-
if (getAddressSpace() == NoAddressSpace ||
12617+
if (getAddressSpace() == InvalidAddressSpace ||
1260912618
getAddressSpace() == getAssociatedType()->getPointerAddressSpace())
1261012619
return ChangeStatus::UNCHANGED;
1261112620

12621+
Value *AssociatedValue = &getAssociatedValue();
12622+
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12623+
1261212624
PointerType *NewPtrTy =
1261312625
PointerType::get(getAssociatedType()->getContext(), getAddressSpace());
1261412626
bool UseOriginalValue =
@@ -12655,17 +12667,17 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1265512667
if (!isValidState())
1265612668
return "addrspace(<invalid>)";
1265712669
return "addrspace(" +
12658-
(AssumedAddressSpace == NoAddressSpace
12670+
(AssumedAddressSpace == InvalidAddressSpace
1265912671
? "none"
1266012672
: std::to_string(AssumedAddressSpace)) +
1266112673
")";
1266212674
}
1266312675

1266412676
private:
12665-
uint32_t AssumedAddressSpace = NoAddressSpace;
12677+
uint32_t AssumedAddressSpace = InvalidAddressSpace;
1266612678

1266712679
bool takeAddressSpace(uint32_t AS) {
12668-
if (AssumedAddressSpace == NoAddressSpace) {
12680+
if (AssumedAddressSpace == InvalidAddressSpace) {
1266912681
AssumedAddressSpace = AS;
1267012682
return true;
1267112683
}

llvm/test/Transforms/Attributor/address_space_info.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals --prefix-filecheck-ir-name true
2-
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK
2+
; RUN: opt -mtriple=amdgcn-amd-amdhsa -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefix=CHECK
3+
4+
; REQUIRES: amdgpu-registered-target
35

46
@dst = dso_local addrspace(1) externally_initialized global i32 0, align 4
57
@g1 = dso_local addrspace(1) externally_initialized global ptr null, align 4

llvm/test/Transforms/Attributor/nocapture-1.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ define i32 @nc1_addrspace(ptr %q, ptr addrspace(1) %p, i1 %b) {
257257
; TUNIT-NEXT: [[TMP:%.*]] = addrspacecast ptr addrspace(1) [[P]] to ptr
258258
; TUNIT-NEXT: [[TMP2:%.*]] = select i1 [[B]], ptr [[TMP]], ptr [[Q]]
259259
; TUNIT-NEXT: [[VAL:%.*]] = load i32, ptr [[TMP2]], align 4
260-
; TUNIT-NEXT: store i32 0, ptr addrspace(1) [[P]], align 4
260+
; TUNIT-NEXT: store i32 0, ptr [[TMP]], align 4
261261
; TUNIT-NEXT: store ptr [[Q]], ptr @g, align 8
262262
; TUNIT-NEXT: ret i32 [[VAL]]
263263
;
@@ -272,7 +272,7 @@ define i32 @nc1_addrspace(ptr %q, ptr addrspace(1) %p, i1 %b) {
272272
; CGSCC-NEXT: [[TMP:%.*]] = addrspacecast ptr addrspace(1) [[P]] to ptr
273273
; CGSCC-NEXT: [[TMP2:%.*]] = select i1 [[B]], ptr [[TMP]], ptr [[Q]]
274274
; CGSCC-NEXT: [[VAL:%.*]] = load i32, ptr [[TMP2]], align 4
275-
; CGSCC-NEXT: store i32 0, ptr addrspace(1) [[P]], align 4
275+
; CGSCC-NEXT: store i32 0, ptr [[TMP]], align 4
276276
; CGSCC-NEXT: store ptr [[Q]], ptr @g, align 8
277277
; CGSCC-NEXT: ret i32 [[VAL]]
278278
;

llvm/test/Transforms/Attributor/value-simplify.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ define void @user() {
838838
; TUNIT: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write)
839839
; TUNIT-LABEL: define {{[^@]+}}@user
840840
; TUNIT-SAME: () #[[ATTR5]] {
841-
; TUNIT-NEXT: [[TMP1:%.*]] = addrspacecast ptr addrspacecast (ptr addrspace(3) @ConstAS3Ptr to ptr) to ptr addrspace(3)
842-
; TUNIT-NEXT: store i32 0, ptr addrspace(3) [[TMP1]], align 4
841+
; TUNIT-NEXT: store i32 0, ptr addrspacecast (ptr addrspace(3) @ConstAS3Ptr to ptr), align 4
843842
; TUNIT-NEXT: ret void
844843
;
845844
; CGSCC: Function Attrs: mustprogress nofree nosync nounwind willreturn memory(write)

0 commit comments

Comments
 (0)