Skip to content

Commit 30c83b8

Browse files
committed
[Attributor] Use more appropriate approach to check flat address space
1 parent 8db0d52 commit 30c83b8

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ struct InformationCache {
13321332
bool stackIsAccessibleByOtherThreads() { return !targetIsGPU(); }
13331333

13341334
/// Return true if the target is a GPU.
1335-
bool targetIsGPU() {
1335+
bool targetIsGPU() const {
13361336
return TargetTriple.isAMDGPU() || TargetTriple.isNVPTX();
13371337
}
13381338

@@ -6267,8 +6267,8 @@ struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> {
62676267
return (AA->getIdAddr() == &ID);
62686268
}
62696269

6270-
// No address space which indicates the associated value is dead.
6271-
static const uint32_t NoAddressSpace = ~0U;
6270+
// Invalid address space which indicates the associated value is dead.
6271+
static const uint32_t InvalidAddressSpace = ~0U;
62726272

62736273
/// Unique ID (due to the unique address)
62746274
static const char ID;

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Analysis/InlineCost.h"
2727
#include "llvm/Analysis/MemoryBuiltins.h"
2828
#include "llvm/Analysis/MustExecute.h"
29+
#include "llvm/Analysis/TargetTransformInfo.h"
2930
#include "llvm/IR/AttributeMask.h"
3031
#include "llvm/IR/Attributes.h"
3132
#include "llvm/IR/Constant.h"

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12571,8 +12571,19 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1257112571
void initialize(Attributor &A) override {
1257212572
assert(getAssociatedType()->isPtrOrPtrVectorTy() &&
1257312573
"Associated value is not a pointer");
12574-
if (getAssociatedType()->getPointerAddressSpace())
12574+
12575+
unsigned FlatAS = A.getInfoCache().getDL().getFlatAddressSpace();
12576+
if (FlatAS == InvalidAddressSpace) {
12577+
indicatePessimisticFixpoint();
12578+
return;
12579+
}
12580+
12581+
unsigned AS = getAssociatedType()->getPointerAddressSpace();
12582+
if (AS != FlatAS) {
12583+
[[maybe_unused]] bool R = takeAddressSpace(AS);
12584+
assert(R && "The take should happen");
1257512585
indicateOptimisticFixpoint();
12586+
}
1257612587
}
1257712588

1257812589
ChangeStatus updateImpl(Attributor &A) override {
@@ -12594,12 +12605,13 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1259412605

1259512606
/// See AbstractAttribute::manifest(...).
1259612607
ChangeStatus manifest(Attributor &A) override {
12597-
Value *AssociatedValue = &getAssociatedValue();
12598-
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12599-
if (getAddressSpace() == NoAddressSpace ||
12608+
if (getAddressSpace() == InvalidAddressSpace ||
1260012609
getAddressSpace() == getAssociatedType()->getPointerAddressSpace())
1260112610
return ChangeStatus::UNCHANGED;
1260212611

12612+
Value *AssociatedValue = &getAssociatedValue();
12613+
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12614+
1260312615
PointerType *NewPtrTy =
1260412616
PointerType::get(getAssociatedType()->getContext(), getAddressSpace());
1260512617
bool UseOriginalValue =
@@ -12646,17 +12658,17 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1264612658
if (!isValidState())
1264712659
return "addrspace(<invalid>)";
1264812660
return "addrspace(" +
12649-
(AssumedAddressSpace == NoAddressSpace
12661+
(AssumedAddressSpace == InvalidAddressSpace
1265012662
? "none"
1265112663
: std::to_string(AssumedAddressSpace)) +
1265212664
")";
1265312665
}
1265412666

1265512667
private:
12656-
uint32_t AssumedAddressSpace = NoAddressSpace;
12668+
uint32_t AssumedAddressSpace = InvalidAddressSpace;
1265712669

1265812670
bool takeAddressSpace(uint32_t AS) {
12659-
if (AssumedAddressSpace == NoAddressSpace) {
12671+
if (AssumedAddressSpace == InvalidAddressSpace) {
1266012672
AssumedAddressSpace = AS;
1266112673
return true;
1266212674
}

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)