Skip to content

Commit 3928d94

Browse files
committed
Add target hook for automatic histogram vectorization
Now, each target, depending on the load/store instruction, can indicate whether it is safe to vectorize histogram operations. For example, this allows distinguishing between different pointer address spaces.
1 parent 2071ea2 commit 3928d94

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,9 @@ class TargetTransformInfo {
805805
bool isLegalMaskedScatter(Type *DataType, Align Alignment) const;
806806
/// Return true if the target supports masked gather.
807807
bool isLegalMaskedGather(Type *DataType, Align Alignment) const;
808+
/// Return true if the target supports vectorization of histograms.
809+
bool isLegalForHistogramVectorization(const LoadInst *LI,
810+
const StoreInst *SI) const;
808811
/// Return true if the target forces scalarizing of llvm.masked.gather
809812
/// intrinsics.
810813
bool forceScalarizeMaskedGather(VectorType *Type, Align Alignment) const;
@@ -2028,6 +2031,8 @@ class TargetTransformInfo::Concept {
20282031
ElementCount NumElements) const = 0;
20292032
virtual bool isLegalMaskedScatter(Type *DataType, Align Alignment) = 0;
20302033
virtual bool isLegalMaskedGather(Type *DataType, Align Alignment) = 0;
2034+
virtual bool isLegalForHistogramVectorization(const LoadInst *LI,
2035+
const StoreInst *SI) = 0;
20312036
virtual bool forceScalarizeMaskedGather(VectorType *DataType,
20322037
Align Alignment) = 0;
20332038
virtual bool forceScalarizeMaskedScatter(VectorType *DataType,
@@ -2589,6 +2594,10 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
25892594
bool isLegalMaskedGather(Type *DataType, Align Alignment) override {
25902595
return Impl.isLegalMaskedGather(DataType, Alignment);
25912596
}
2597+
bool isLegalForHistogramVectorization(const LoadInst *LI,
2598+
const StoreInst *SI) override {
2599+
return Impl.isLegalForHistogramVectorization(LI, SI);
2600+
}
25922601
bool forceScalarizeMaskedGather(VectorType *DataType,
25932602
Align Alignment) override {
25942603
return Impl.forceScalarizeMaskedGather(DataType, Alignment);

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ class TargetTransformInfoImplBase {
310310
return false;
311311
}
312312

313+
bool isLegalForHistogramVectorization(const LoadInst *LI,
314+
const StoreInst *SI) const {
315+
// TODO: Currently, when a target wants to use histogram intrinsics, it adds
316+
// the
317+
// `-enable-histogram-loop-vectorization` flag to Clang.
318+
//
319+
// Once the histogram option is enabled by default, we will need to update
320+
// the default hook to return `false`, and each target that wants automatic
321+
// histogram vectorization will need to override it to return `true`.
322+
//
323+
// Additionally, we will need to deprecate the
324+
// `-enable-histogram-loop-vectorization` flag, as it will no longer be
325+
// necessary.
326+
return true;
327+
}
328+
313329
bool forceScalarizeMaskedGather(VectorType *DataType, Align Alignment) const {
314330
return false;
315331
}

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,11 @@ bool TargetTransformInfo::isLegalMaskedGather(Type *DataType,
492492
return TTIImpl->isLegalMaskedGather(DataType, Alignment);
493493
}
494494

495+
bool TargetTransformInfo::isLegalForHistogramVectorization(
496+
const LoadInst *LI, const StoreInst *SI) const {
497+
return TTIImpl->isLegalForHistogramVectorization(LI, SI);
498+
}
499+
495500
bool TargetTransformInfo::isLegalAltInstr(
496501
VectorType *VecTy, unsigned Opcode0, unsigned Opcode1,
497502
const SmallBitVector &OpcodeMask) const {

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,9 @@ bool LoopVectorizationLegality::canVectorizeIndirectUnsafeDependences() {
11931193
if (!LI || !SI)
11941194
return false;
11951195

1196+
if (!TTI->isLegalForHistogramVectorization(LI, SI))
1197+
return false;
1198+
11961199
LLVM_DEBUG(dbgs() << "LV: Checking for a histogram on: " << *SI << "\n");
11971200
return findHistogram(LI, SI, TheLoop, LAI->getPSE(), Histograms);
11981201
}

0 commit comments

Comments
 (0)