[llvm] 6e2a720 - [AMDGPU][Uniformity][TTI] Make Uniformity Analysis Operand-Aware via Custom Uniformity Checks (#137639)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 24 09:55:01 PDT 2026
Author: Pankaj Dwivedi
Date: 2026-03-24T22:24:57+05:30
New Revision: 6e2a720dd1890d49afef6ab5165cb6e7fd346048
URL: https://github.com/llvm/llvm-project/commit/6e2a720dd1890d49afef6ab5165cb6e7fd346048
DIFF: https://github.com/llvm/llvm-project/commit/6e2a720dd1890d49afef6ab5165cb6e7fd346048.diff
LOG: [AMDGPU][Uniformity][TTI] Make Uniformity Analysis Operand-Aware via Custom Uniformity Checks (#137639)
See: https://github.com/llvm/llvm-project/issues/131779
Extends uniformity analysis to support instructions whose uniformity
depends on which specific operands are uniform. Introduces
`InstructionUniformity::Custom` and a target hook `TTI::isUniform(I,
UniformArgs)` that allows targets to define custom uniformity rules.
During propagation, custom candidates are checked via the target hook.
If we can prove they are uniform, we skip marking them divergent and let
iterative propagation re-evaluate as operands change.
Implements AMDGPU's `llvm.amdgcn.wave.shuffle` rules (uniform when
either operand is uniform, divergent only when both are divergent) as
the motivating example.
This inverted-logic approach is critical for correctness: proving
uniformity early during propagation would be unsafe, as operands can
transition from uniform to divergent during divergence propagation.
---------
Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
Added:
llvm/test/Analysis/UniformityAnalysis/AMDGPU/uniform_intrinsic.ll
Modified:
llvm/include/llvm/ADT/GenericUniformityImpl.h
llvm/include/llvm/ADT/Uniformity.h
llvm/include/llvm/Analysis/TargetTransformInfo.h
llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
llvm/lib/Analysis/TargetTransformInfo.cpp
llvm/lib/Analysis/UniformityAnalysis.cpp
llvm/lib/CodeGen/MachineUniformityAnalysis.cpp
llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/GenericUniformityImpl.h b/llvm/include/llvm/ADT/GenericUniformityImpl.h
index 2db76a1ad9b13..6089f69b393bd 100644
--- a/llvm/include/llvm/ADT/GenericUniformityImpl.h
+++ b/llvm/include/llvm/ADT/GenericUniformityImpl.h
@@ -407,6 +407,13 @@ template <typename ContextT> class GenericUniformityAnalysisImpl {
void recordTemporalDivergence(ConstValueRefT, const InstructionT *,
const CycleT *);
+ /// Check if an instruction with Custom uniformity can be proven uniform
+ /// based on its operands. This queries the target-specific callback.
+ bool isCustomUniform(const InstructionT &I) const;
+
+ /// \brief Add an instruction that requires custom uniformity analysis.
+ void addCustomUniformityCandidate(const InstructionT *I);
+
protected:
const ContextT &Context;
const FunctionT &F;
@@ -420,6 +427,10 @@ template <typename ContextT> class GenericUniformityAnalysisImpl {
// Internal worklist for divergence propagation.
std::vector<const InstructionT *> Worklist;
+ // Set of instructions that require custom uniformity analysis based on
+ // operand uniformity.
+ SmallPtrSet<const InstructionT *, 8> CustomUniformityCandidates;
+
/// \brief Mark \p Term as divergent and push all Instructions that become
/// divergent as a result on the worklist.
void analyzeControlDivergence(const InstructionT &Term);
@@ -783,6 +794,13 @@ void GenericUniformityAnalysisImpl<ContextT>::markDivergent(
const InstructionT &I) {
if (isAlwaysUniform(I))
return;
+ // For custom uniformity candidates, check if the instruction can be
+ // proven uniform based on which operands are uniform/divergent.
+ // The candidate will be re-evaluated as operands become divergent.
+ if (CustomUniformityCandidates.contains(&I)) {
+ if (isCustomUniform(I))
+ return;
+ }
bool Marked = false;
if (I.isTerminator()) {
Marked = DivergentTermBlocks.insert(I.getParent()).second;
@@ -814,6 +832,12 @@ void GenericUniformityAnalysisImpl<ContextT>::addUniformOverride(
UniformOverrides.insert(&Instr);
}
+template <typename ContextT>
+void GenericUniformityAnalysisImpl<ContextT>::addCustomUniformityCandidate(
+ const InstructionT *I) {
+ CustomUniformityCandidates.insert(I);
+}
+
// Mark as divergent all external uses of values defined in \p DefCycle.
//
// A value V defined by a block B inside \p DefCycle may be used outside the
diff --git a/llvm/include/llvm/ADT/Uniformity.h b/llvm/include/llvm/ADT/Uniformity.h
index 21ca106b80be3..0adbdf99c6d2a 100644
--- a/llvm/include/llvm/ADT/Uniformity.h
+++ b/llvm/include/llvm/ADT/Uniformity.h
@@ -23,7 +23,12 @@ enum class InstructionUniformity {
AlwaysUniform,
/// The result values can never be assumed to be uniform.
- NeverUniform
+ NeverUniform,
+
+ /// The result values require a custom uniformity check. A target-specific
+ /// callback determines whether the result is uniform based on which
+ /// operands are uniform.
+ Custom
};
} // namespace llvm
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 9cb4a97f9459c..b5069da74f3e6 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -2084,6 +2084,17 @@ class TargetTransformInfo {
/// target.
LLVM_ABI bool allowVectorElementIndexingUsingGEP() const;
+ /// Determine if an instruction with Custom uniformity can be proven uniform
+ /// based on which operands are uniform.
+ ///
+ /// \param I The instruction to check.
+ /// \param UniformArgs A bitvector indicating which operands are known to be
+ /// uniform (bit N corresponds to operand N).
+ /// \returns true if the instruction result can be proven uniform given the
+ /// uniform operands, false otherwise.
+ LLVM_ABI bool isUniform(const Instruction *I,
+ const SmallBitVector &UniformArgs) const;
+
private:
std::unique_ptr<const TargetTransformInfoImplBase> TTIImpl;
};
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 410b9187a5fed..419f2eedd6d8f 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -1228,6 +1228,11 @@ class TargetTransformInfoImplBase {
virtual bool allowVectorElementIndexingUsingGEP() const { return true; }
+ virtual bool isUniform(const Instruction *I,
+ const SmallBitVector &UniformArgs) const {
+ llvm_unreachable("target must implement isUniform for Custom uniformity");
+ }
+
protected:
// Obtain the minimum required size to hold the value (without the sign)
// In case of a vector it returns the min required size for one element.
diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp
index b0afba2c78884..9345595555c2d 100644
--- a/llvm/lib/Analysis/TargetTransformInfo.cpp
+++ b/llvm/lib/Analysis/TargetTransformInfo.cpp
@@ -1551,6 +1551,11 @@ bool TargetTransformInfo::allowVectorElementIndexingUsingGEP() const {
return TTIImpl->allowVectorElementIndexingUsingGEP();
}
+bool TargetTransformInfo::isUniform(const Instruction *I,
+ const SmallBitVector &UniformArgs) const {
+ return TTIImpl->isUniform(I, UniformArgs);
+}
+
TargetTransformInfoImplBase::~TargetTransformInfoImplBase() = default;
TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
diff --git a/llvm/lib/Analysis/UniformityAnalysis.cpp b/llvm/lib/Analysis/UniformityAnalysis.cpp
index b56534935d7c2..805f4894f3634 100644
--- a/llvm/lib/Analysis/UniformityAnalysis.cpp
+++ b/llvm/lib/Analysis/UniformityAnalysis.cpp
@@ -8,6 +8,7 @@
#include "llvm/Analysis/UniformityAnalysis.h"
#include "llvm/ADT/GenericUniformityImpl.h"
+#include "llvm/ADT/SmallBitVector.h"
#include "llvm/Analysis/CycleAnalysis.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Dominators.h"
@@ -35,10 +36,13 @@ template <> void llvm::GenericUniformityAnalysisImpl<SSAContext>::initialize() {
switch (IU) {
case InstructionUniformity::AlwaysUniform:
addUniformOverride(I);
- continue;
+ break;
case InstructionUniformity::NeverUniform:
markDivergent(I);
- continue;
+ break;
+ case InstructionUniformity::Custom:
+ addCustomUniformityCandidate(&I);
+ break;
case InstructionUniformity::Default:
break;
}
@@ -108,6 +112,15 @@ bool llvm::GenericUniformityAnalysisImpl<SSAContext>::isDivergentUse(
return false;
}
+template <>
+bool GenericUniformityAnalysisImpl<SSAContext>::isCustomUniform(
+ const Instruction &I) const {
+ SmallBitVector UniformArgs(I.getNumOperands());
+ for (auto [Idx, Use] : enumerate(I.operands()))
+ UniformArgs[Idx] = !isDivergentUse(Use);
+ return TTI->isUniform(&I, UniformArgs);
+}
+
// This ensures explicit instantiation of
// GenericUniformityAnalysisImpl::ImplDeleter::operator()
template class llvm::GenericUniformityInfo<SSAContext>;
diff --git a/llvm/lib/CodeGen/MachineUniformityAnalysis.cpp b/llvm/lib/CodeGen/MachineUniformityAnalysis.cpp
index dbadb67e1e6d2..af1c448497a52 100644
--- a/llvm/lib/CodeGen/MachineUniformityAnalysis.cpp
+++ b/llvm/lib/CodeGen/MachineUniformityAnalysis.cpp
@@ -61,6 +61,8 @@ void llvm::GenericUniformityAnalysisImpl<MachineSSAContext>::initialize() {
case InstructionUniformity::NeverUniform:
markDivergent(instr);
break;
+ case InstructionUniformity::Custom:
+ break;
case InstructionUniformity::Default:
break;
}
@@ -151,6 +153,12 @@ bool llvm::GenericUniformityAnalysisImpl<MachineSSAContext>::isDivergentUse(
return isTemporalDivergent(*UseInstr->getParent(), *DefInstr);
}
+template <>
+bool GenericUniformityAnalysisImpl<MachineSSAContext>::isCustomUniform(
+ const MachineInstr &MI) const {
+ llvm_unreachable("no MIR instructions use Custom uniformity yet");
+}
+
// This ensures explicit instantiation of
// GenericUniformityAnalysisImpl::ImplDeleter::operator()
template class llvm::GenericUniformityInfo<MachineSSAContext>;
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
index e81ce4aaf6fe9..d02bc45bc14f6 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
@@ -19,6 +19,7 @@
#include "AMDGPUTargetMachine.h"
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
#include "SIModeRegisterDefaults.h"
+#include "llvm/ADT/SmallBitVector.h"
#include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ValueTracking.h"
@@ -1729,6 +1730,15 @@ unsigned GCNTTIImpl::getNumberOfParts(Type *Tp) const {
InstructionUniformity
GCNTTIImpl::getInstructionUniformity(const Value *V) const {
+ if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
+ switch (Intrinsic->getIntrinsicID()) {
+ case Intrinsic::amdgcn_wave_shuffle:
+ return InstructionUniformity::Custom;
+ default:
+ break;
+ }
+ }
+
if (isAlwaysUniform(V))
return InstructionUniformity::AlwaysUniform;
@@ -1784,3 +1794,16 @@ bool GCNTTIImpl::shouldDropLSRSolutionIfLessProfitable() const {
// Prefer the baseline when LSR cannot clearly reduce per-iteration work.
return true;
}
+
+bool GCNTTIImpl::isUniform(const Instruction *I,
+ const SmallBitVector &UniformArgs) const {
+ const IntrinsicInst *Intrinsic = cast<IntrinsicInst>(I);
+ switch (Intrinsic->getIntrinsicID()) {
+ case Intrinsic::amdgcn_wave_shuffle:
+ // wave_shuffle(Value, Index): result is uniform when either Value or Index
+ // is uniform.
+ return UniformArgs[0] || UniformArgs[1];
+ default:
+ llvm_unreachable("unexpected intrinsic in isUniform");
+ }
+}
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
index dc7d01533da02..ea2bf72836199 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
@@ -321,6 +321,9 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
const TTI::LSRCost &B) const override;
bool isNumRegsMajorCostOfLSR() const override;
bool shouldDropLSRSolutionIfLessProfitable() const override;
+
+ bool isUniform(const Instruction *I,
+ const SmallBitVector &UniformArgs) const override;
};
} // end namespace llvm
diff --git a/llvm/test/Analysis/UniformityAnalysis/AMDGPU/uniform_intrinsic.ll b/llvm/test/Analysis/UniformityAnalysis/AMDGPU/uniform_intrinsic.ll
new file mode 100644
index 0000000000000..e3367235c9f6d
--- /dev/null
+++ b/llvm/test/Analysis/UniformityAnalysis/AMDGPU/uniform_intrinsic.ll
@@ -0,0 +1,50 @@
+; RUN: opt -mtriple amdgcn-- -passes='print<uniformity>' -disable-output %s 2>&1 | FileCheck %s
+
+; wave_shuffle(Value, Index): result is divergent only when both Value and
+; Index are divergent. A uniform Value read from any lane yields the same
+; result, and a uniform Index makes all lanes read the same source lane.
+
+; All kernel args are uniform => result is uniform.
+; CHECK-LABEL: UniformityInfo for function 'wave_shuffle_all_uniform':
+; CHECK: ALL VALUES UNIFORM
+define amdgpu_kernel void @wave_shuffle_all_uniform(ptr addrspace(1) %out, i32 %val, i32 %idx) {
+ %v = call i32 @llvm.amdgcn.wave.shuffle(i32 %val, i32 %idx)
+ store i32 %v, ptr addrspace(1) %out
+ ret void
+}
+
+; Value is divergent, Index is uniform => result is uniform.
+; All lanes read from the same source lane, so the result is the same.
+; CHECK-LABEL: UniformityInfo for function 'wave_shuffle_divergent_val_uniform_idx':
+; CHECK-NOT: DIVERGENT: {{.*}}wave.shuffle
+define amdgpu_kernel void @wave_shuffle_divergent_val_uniform_idx(ptr addrspace(1) %out, i32 %idx) {
+ %tid = call i32 @llvm.amdgcn.workitem.id.x()
+ %v = call i32 @llvm.amdgcn.wave.shuffle(i32 %tid, i32 %idx)
+ store i32 %v, ptr addrspace(1) %out
+ ret void
+}
+
+; Value is uniform, Index is divergent => result is uniform.
+; Each lane may read from a
diff erent source lane, but Value is the same
+; across all lanes so the result is still uniform.
+; CHECK-LABEL: UniformityInfo for function 'wave_shuffle_uniform_val_divergent_idx':
+; CHECK-NOT: DIVERGENT: {{.*}}wave.shuffle
+define amdgpu_kernel void @wave_shuffle_uniform_val_divergent_idx(ptr addrspace(1) %out, i32 %val) {
+ %tid = call i32 @llvm.amdgcn.workitem.id.x()
+ %v = call i32 @llvm.amdgcn.wave.shuffle(i32 %val, i32 %tid)
+ store i32 %v, ptr addrspace(1) %out
+ ret void
+}
+
+; Both Value and Index are divergent => result is divergent.
+; CHECK-LABEL: UniformityInfo for function 'wave_shuffle_both_divergent':
+; CHECK: DIVERGENT: %v = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %tid, i32 %tid)
+define amdgpu_kernel void @wave_shuffle_both_divergent(ptr addrspace(1) %out) {
+ %tid = call i32 @llvm.amdgcn.workitem.id.x()
+ %v = call i32 @llvm.amdgcn.wave.shuffle(i32 %tid, i32 %tid)
+ store i32 %v, ptr addrspace(1) %out
+ ret void
+}
+
+declare i32 @llvm.amdgcn.wave.shuffle(i32, i32)
+declare i32 @llvm.amdgcn.workitem.id.x()
More information about the llvm-commits
mailing list