[llvm] [UniformityAnalysis] Replace DivergentValues with UniformValues for conservative divergence queries (PR #180509)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 26 05:26:17 PDT 2026


================
@@ -73,6 +49,58 @@ void llvm::GenericUniformityAnalysisImpl<SSAContext>::pushUsers(
   pushUsers(cast<Value>(&Instr));
 }
 
+template <>
+bool llvm::GenericUniformityAnalysisImpl<SSAContext>::printDivergentArgs(
+    raw_ostream &OS) const {
+  bool haveDivergentArgs = false;
+  for (const auto &Arg : F.args()) {
+    if (isDivergent(&Arg)) {
+      if (!haveDivergentArgs) {
+        OS << "DIVERGENT ARGUMENTS:\n";
+        haveDivergentArgs = true;
+      }
+      OS << "  DIVERGENT: " << Context.print(&Arg) << '\n';
+    }
+  }
+  return haveDivergentArgs;
+}
+
+template <> void llvm::GenericUniformityAnalysisImpl<SSAContext>::initialize() {
+  // Pre-populate UniformValues with all values, then seed divergence.
+  // Values are removed from UniformValues as divergence is propagated.
+  SmallVector<const Value *, 4> DivergentArgs;
+  for (auto &Arg : F.args()) {
+    UniformValues.insert(&Arg);
+    if (TTI->getInstructionUniformity(&Arg) ==
+        InstructionUniformity::NeverUniform) {
+      markDivergent(&Arg);
+      DivergentArgs.push_back(&Arg);
+    }
+  }
+  for (auto &I : instructions(F)) {
+    UniformValues.insert(&I);
+    InstructionUniformity IU = TTI->getInstructionUniformity(&I);
+    switch (IU) {
+    case InstructionUniformity::AlwaysUniform:
+      addUniformOverride(I);
+      continue;
+    case InstructionUniformity::NeverUniform:
+      markDivergent(I);
+      continue;
+    case InstructionUniformity::Custom:
+      addCustomUniformityCandidate(&I);
+      continue;
+    case InstructionUniformity::Default:
+      break;
+    }
+  }
+  // Push users of divergent arguments after all instructions are in
+  // UniformValues, so markDivergent (called inside pushUsers) can
+  // successfully erase the user instruction from the set.
+  for (const Value *Arg : DivergentArgs)
+    pushUsers(Arg);
----------------
jayfoad wrote:

Well I still don't really understand the flow, but I think you're saying that it works correctly? I'm happy with that.

https://github.com/llvm/llvm-project/pull/180509


More information about the llvm-commits mailing list