Skip to content

Commit c06fe68

Browse files
committed
[Attributor] Use more appropriate approach to check flat address space
1 parent 1e4e1ce commit c06fe68

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

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

Lines changed: 8 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

@@ -1341,6 +1341,8 @@ struct InformationCache {
13411341
const ArrayRef<Function *>
13421342
getIndirectlyCallableFunctions(Attributor &A) const;
13431343

1344+
unsigned getFlatAddressSpace(const Function *F);
1345+
13441346
private:
13451347
struct FunctionInfo {
13461348
~FunctionInfo();
@@ -1383,6 +1385,9 @@ struct InformationCache {
13831385
/// through the information cache interface *prior* to looking at them.
13841386
void initializeInformationCache(const Function &F, FunctionInfo &FI);
13851387

1388+
/// Return the assumed flat address space.
1389+
unsigned getAssumedFlatAddressSpace() const;
1390+
13861391
/// The datalayout used in the module.
13871392
const DataLayout &DL;
13881393

@@ -6267,8 +6272,8 @@ struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> {
62676272
return (AA->getIdAddr() == &ID);
62686273
}
62696274

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

62736278
/// Unique ID (due to the unique address)
62746279
static const char ID;

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 28 additions & 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"
@@ -179,6 +180,12 @@ static cl::opt<bool> CloseWorldAssumption(
179180
"attributor-assume-closed-world", cl::Hidden,
180181
cl::desc("Should a closed world be assumed, or not. Default if not set."));
181182

183+
static cl::opt<unsigned> AssumedFlatAddressSpace(
184+
"attributor-assume-flat-address-space", cl::Hidden,
185+
cl::desc(
186+
"The assumed flat address space. This is mainly for test purpose."),
187+
cl::init(~0U));
188+
182189
/// Logic operators for the change status enum class.
183190
///
184191
///{
@@ -3294,6 +3301,27 @@ InformationCache::getIndirectlyCallableFunctions(Attributor &A) const {
32943301
return IndirectlyCallableFunctions;
32953302
}
32963303

3304+
unsigned InformationCache::getFlatAddressSpace(const Function *F) {
3305+
if (!F)
3306+
return getAssumedFlatAddressSpace();
3307+
auto *TTI = getAnalysisResultForFunction<TargetIRAnalysis>(*F);
3308+
if (!TTI)
3309+
return getAssumedFlatAddressSpace();
3310+
return TTI->getFlatAddressSpace();
3311+
}
3312+
3313+
unsigned InformationCache::getAssumedFlatAddressSpace() const {
3314+
if (targetIsGPU()) {
3315+
if (TargetTriple.isAMDGPU() || TargetTriple.isNVPTX()) {
3316+
// We use 0 here directly instead of enumeration such that we don't need
3317+
// to include the target headers.
3318+
return 0;
3319+
}
3320+
llvm_unreachable("unknown GPU target");
3321+
}
3322+
return AssumedFlatAddressSpace;
3323+
}
3324+
32973325
void Attributor::recordDependence(const AbstractAttribute &FromAA,
32983326
const AbstractAttribute &ToAA,
32993327
DepClassTy DepClass) {

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12571,8 +12571,20 @@ 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 =
12576+
A.getInfoCache().getFlatAddressSpace(getAssociatedFunction());
12577+
if (FlatAS == InvalidAddressSpace) {
12578+
indicatePessimisticFixpoint();
12579+
return;
12580+
}
12581+
12582+
unsigned AS = getAssociatedType()->getPointerAddressSpace();
12583+
if (AS != FlatAS) {
12584+
[[maybe_unused]] bool R = takeAddressSpace(AS);
12585+
assert(R && "The take should happen");
1257512586
indicateOptimisticFixpoint();
12587+
}
1257612588
}
1257712589

1257812590
ChangeStatus updateImpl(Attributor &A) override {
@@ -12594,12 +12606,13 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1259412606

1259512607
/// See AbstractAttribute::manifest(...).
1259612608
ChangeStatus manifest(Attributor &A) override {
12597-
Value *AssociatedValue = &getAssociatedValue();
12598-
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12599-
if (getAddressSpace() == NoAddressSpace ||
12609+
if (getAddressSpace() == InvalidAddressSpace ||
1260012610
getAddressSpace() == getAssociatedType()->getPointerAddressSpace())
1260112611
return ChangeStatus::UNCHANGED;
1260212612

12613+
Value *AssociatedValue = &getAssociatedValue();
12614+
Value *OriginalValue = peelAddrspacecast(AssociatedValue);
12615+
1260312616
PointerType *NewPtrTy =
1260412617
PointerType::get(getAssociatedType()->getContext(), getAddressSpace());
1260512618
bool UseOriginalValue =
@@ -12646,17 +12659,17 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1264612659
if (!isValidState())
1264712660
return "addrspace(<invalid>)";
1264812661
return "addrspace(" +
12649-
(AssumedAddressSpace == NoAddressSpace
12662+
(AssumedAddressSpace == InvalidAddressSpace
1265012663
? "none"
1265112664
: std::to_string(AssumedAddressSpace)) +
1265212665
")";
1265312666
}
1265412667

1265512668
private:
12656-
uint32_t AssumedAddressSpace = NoAddressSpace;
12669+
uint32_t AssumedAddressSpace = InvalidAddressSpace;
1265712670

1265812671
bool takeAddressSpace(uint32_t AS) {
12659-
if (AssumedAddressSpace == NoAddressSpace) {
12672+
if (AssumedAddressSpace == InvalidAddressSpace) {
1266012673
AssumedAddressSpace = AS;
1266112674
return true;
1266212675
}

llvm/test/Transforms/Attributor/address_space_info.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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 -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -attributor-assume-flat-address-space=0 -S < %s | FileCheck %s --check-prefixes=CHECK
33

44
@dst = dso_local addrspace(1) externally_initialized global i32 0, align 4
55
@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)