Skip to content

Commit 0562cf4

Browse files
committed
Allow data prefetch into non-default address space
I am playing with the LoopDataPrefetch pass and found out that it bails to work with a pointer in a non-zero address space. This patch adds the target callback to check if an address space is to be considered for prefetching. Default implementation still only allows address space 0, so this is NFCI. This does not currently affect any known targets, but seems to be generally useful for the future. Differential Revision: https://reviews.llvm.org/D129795
1 parent 7ce39d8 commit 0562cf4

File tree

7 files changed

+30
-2
lines changed

7 files changed

+30
-2
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,9 @@ class TargetTransformInfo {
10421042
/// \return True if prefetching should also be done for writes.
10431043
bool enableWritePrefetching() const;
10441044

1045+
/// \return if target want to issue a prefetch in address space \p AS.
1046+
bool shouldPrefetchAddressSpace(unsigned AS) const;
1047+
10451048
/// \return The maximum interleave factor that any transform should try to
10461049
/// perform for this target. This number depends on the level of parallelism
10471050
/// and the number of execution units in the CPU.
@@ -1705,6 +1708,9 @@ class TargetTransformInfo::Concept {
17051708
/// \return True if prefetching should also be done for writes.
17061709
virtual bool enableWritePrefetching() const = 0;
17071710

1711+
/// \return if target want to issue a prefetch in address space \p AS.
1712+
virtual bool shouldPrefetchAddressSpace(unsigned AS) const = 0;
1713+
17081714
virtual unsigned getMaxInterleaveFactor(unsigned VF) = 0;
17091715
virtual InstructionCost getArithmeticInstrCost(
17101716
unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
@@ -2231,6 +2237,11 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
22312237
return Impl.enableWritePrefetching();
22322238
}
22332239

2240+
/// \return if target want to issue a prefetch in address space \p AS.
2241+
bool shouldPrefetchAddressSpace(unsigned AS) const override {
2242+
return Impl.shouldPrefetchAddressSpace(AS);
2243+
}
2244+
22342245
unsigned getMaxInterleaveFactor(unsigned VF) override {
22352246
return Impl.getMaxInterleaveFactor(VF);
22362247
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ class TargetTransformInfoImplBase {
475475
}
476476
unsigned getMaxPrefetchIterationsAhead() const { return UINT_MAX; }
477477
bool enableWritePrefetching() const { return false; }
478+
bool shouldPrefetchAddressSpace(unsigned AS) const { return !AS; }
478479

479480
unsigned getMaxInterleaveFactor(unsigned VF) const { return 1; }
480481

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,10 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
683683
return getST()->enableWritePrefetching();
684684
}
685685

686+
virtual bool shouldPrefetchAddressSpace(unsigned AS) const {
687+
return getST()->shouldPrefetchAddressSpace(AS);
688+
}
689+
686690
/// @}
687691

688692
/// \name Vector TTI Implementations

llvm/include/llvm/MC/MCSubtargetInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ class MCSubtargetInfo {
282282
unsigned NumStridedMemAccesses,
283283
unsigned NumPrefetches,
284284
bool HasCall) const;
285+
286+
/// \return if target want to issue a prefetch in address space \p AS.
287+
virtual bool shouldPrefetchAddressSpace(unsigned AS) const;
285288
};
286289

287290
} // end namespace llvm

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,10 @@ bool TargetTransformInfo::enableWritePrefetching() const {
704704
return TTIImpl->enableWritePrefetching();
705705
}
706706

707+
bool TargetTransformInfo::shouldPrefetchAddressSpace(unsigned AS) const {
708+
return TTIImpl->shouldPrefetchAddressSpace(AS);
709+
}
710+
707711
unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
708712
return TTIImpl->getMaxInterleaveFactor(VF);
709713
}

llvm/lib/MC/MCSubtargetInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,7 @@ unsigned MCSubtargetInfo::getMinPrefetchStride(unsigned NumMemAccesses,
366366
bool HasCall) const {
367367
return 1;
368368
}
369+
370+
bool MCSubtargetInfo::shouldPrefetchAddressSpace(unsigned AS) const {
371+
return !AS;
372+
}

llvm/lib/Transforms/Scalar/LoopDataPrefetch.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ bool LoopDataPrefetch::runOnLoop(Loop *L) {
338338
} else continue;
339339

340340
unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace();
341-
if (PtrAddrSpace)
341+
if (!TTI->shouldPrefetchAddressSpace(PtrAddrSpace))
342342
continue;
343343
NumMemAccesses++;
344344
if (L->isLoopInvariant(PtrValue))
@@ -398,7 +398,8 @@ bool LoopDataPrefetch::runOnLoop(Loop *L) {
398398
if (!SCEVE.isSafeToExpand(NextLSCEV))
399399
continue;
400400

401-
Type *I8Ptr = Type::getInt8PtrTy(BB->getContext(), 0/*PtrAddrSpace*/);
401+
unsigned PtrAddrSpace = NextLSCEV->getType()->getPointerAddressSpace();
402+
Type *I8Ptr = Type::getInt8PtrTy(BB->getContext(), PtrAddrSpace);
402403
Value *PrefPtrValue = SCEVE.expandCodeFor(NextLSCEV, I8Ptr, P.InsertPt);
403404

404405
IRBuilder<> Builder(P.InsertPt);

0 commit comments

Comments
 (0)