Skip to content

Commit

Permalink
TTI: Add function to hasBranchDivergence
Browse files Browse the repository at this point in the history
It my be possible to contextually ignore divergence in a function if
it's known to run single threaded.
  • Loading branch information
arsenm committed Jun 16, 2023
1 parent d61cba6 commit 12c12c5
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 12 deletions.
12 changes: 9 additions & 3 deletions llvm/include/llvm/Analysis/TargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ class TargetTransformInfo {
/// Branch divergence has a significantly negative impact on GPU performance
/// when threads in the same wavefront take different paths due to conditional
/// branches.
bool hasBranchDivergence() const;
///
/// If \p F is passed, provides a context function. If \p F is known to only
/// execute in a single threaded environment, the target may choose to skip
/// uniformity analysis and assume all values are uniform.
bool hasBranchDivergence(const Function *F = nullptr) const;

/// Returns whether V is a source of divergence.
///
Expand Down Expand Up @@ -1689,7 +1693,7 @@ class TargetTransformInfo::Concept {
ArrayRef<const Value *> Operands,
TargetCostKind CostKind) = 0;
virtual BranchProbability getPredictableBranchThreshold() = 0;
virtual bool hasBranchDivergence() = 0;
virtual bool hasBranchDivergence(const Function *F = nullptr) = 0;
virtual bool isSourceOfDivergence(const Value *V) = 0;
virtual bool isAlwaysUniform(const Value *V) = 0;
virtual bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const = 0;
Expand Down Expand Up @@ -2066,7 +2070,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
BranchProbability getPredictableBranchThreshold() override {
return Impl.getPredictableBranchThreshold();
}
bool hasBranchDivergence() override { return Impl.hasBranchDivergence(); }
bool hasBranchDivergence(const Function *F = nullptr) override {
return Impl.hasBranchDivergence(F);
}
bool isSourceOfDivergence(const Value *V) override {
return Impl.isSourceOfDivergence(V);
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TargetTransformInfoImplBase {
return BranchProbability(99, 100);
}

bool hasBranchDivergence() const { return false; }
bool hasBranchDivergence(const Function *F = nullptr) const { return false; }

bool isSourceOfDivergence(const Value *V) const { return false; }

Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/BasicTTIImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
E, AddressSpace, Alignment, MachineMemOperand::MONone, Fast);
}

bool hasBranchDivergence() { return false; }
bool hasBranchDivergence(const Function *F = nullptr) { return false; }

bool isSourceOfDivergence(const Value *V) { return false; }

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ BranchProbability TargetTransformInfo::getPredictableBranchThreshold() const {
: TTIImpl->getPredictableBranchThreshold();
}

bool TargetTransformInfo::hasBranchDivergence() const {
return TTIImpl->hasBranchDivergence();
bool TargetTransformInfo::hasBranchDivergence(const Function *F) const {
return TTIImpl->hasBranchDivergence(F);
}

bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ GCNTTIImpl::GCNTTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
HasFP64FP16Denormals = Mode.allFP64FP16Denormals();
}

bool GCNTTIImpl::hasBranchDivergence(const Function *F) const {
return true;
}

unsigned GCNTTIImpl::getNumberOfRegisters(unsigned RCID) const {
// NB: RCID is not an RCID. In fact it is 0 or 1 for scalar or vector
// registers. See getRegisterClassForType for the implementation.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
public:
explicit GCNTTIImpl(const AMDGPUTargetMachine *TM, const Function &F);

bool hasBranchDivergence() { return true; }
bool hasBranchDivergence(const Function *F = nullptr) const;

void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
TTI::UnrollingPreferences &UP,
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ class HexagonTTIImpl : public BasicTTIImplBase<HexagonTTIImpl> {
return true;
}
bool supportsEfficientVectorElementLoadStore() { return false; }
bool hasBranchDivergence() {
return false;
}
bool hasBranchDivergence(const Function *F = nullptr) { return false; }
bool enableAggressiveInterleaving(bool LoopHasReductions) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/NVPTX/NVPTXTargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NVPTXTTIImpl : public BasicTTIImplBase<NVPTXTTIImpl> {
: BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl()),
TLI(ST->getTargetLowering()) {}

bool hasBranchDivergence() { return true; }
bool hasBranchDivergence(const Function *F = nullptr) { return true; }

bool isSourceOfDivergence(const Value *V);

Expand Down

0 comments on commit 12c12c5

Please sign in to comment.