Skip to content

Conversation

@J-MR-T
Copy link
Contributor

@J-MR-T J-MR-T commented Jul 11, 2025

Currently, Uniformity Analysis is preserved in the Legacy PM when a pass sets setPreservesCFG(). This is incorrect: any values' uniformity not only depends on the CFG, but also on the uniformity of other values, so a CFG-preserving change in many cases doesn't preserve uniformity analysis.

This corrected behavior also matches the behavior of the new PM.


On its own, this change does not affect the pass pipeline because of the happenstance of pass ordering. I also created a PR to change AMDGPULateCodeGenPrepare (#148167), this will have an actual impact on pass executions. That PR also includes changes to the amdgpu/llc-pipeline.ll test in order to check that this change works, but if this is preferred, I would also be happy to try to extend this PR to add an isolated test case; though my personal opinion is that the test in #148167 should suffice, as it should also accurately pinpoint failures related to this change.


I ran git-clang-format on my changes. I tested them using the check-llvm target; no unexpected failures occurred.

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jul 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Jim M. R. Teichgräber (J-MR-T)

Changes

Currently, Uniformity Analysis is preserved in the Legacy PM when a pass sets setPreservesCFG(). This is incorrect: any values' uniformity not only depends on the CFG, but also on the uniformity of other values, so a CFG-preserving change in many cases doesn't preserve uniformity analysis.

This corrected behavior also matches the behavior of the new PM.


On its own, this change does not affect the pass pipeline because of the happenstance of pass ordering. In a minute, I'll also create a PR to change AMDGPULateCodeGenPrepare and link this here, this will have an actual impact on pass executions. That PR will also include changes to the amdgpu/llc-pipeline.ll test in order to check that this change works, but if this is preferred, I would also be happy to try to extend this PR to add an isolated test case; though my personal opinion is that the test in the other PR should suffice, as it should also accurately pinpoint failures related to this change.


I ran git-clang-format on my changes. I tested them using the check-llvm target; no unexpected failures occurred.


Full diff: https://github.com/llvm/llvm-project/pull/148165.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/UniformityAnalysis.cpp (+4-1)
diff --git a/llvm/lib/Analysis/UniformityAnalysis.cpp b/llvm/lib/Analysis/UniformityAnalysis.cpp
index 2101fdfacfc8f..0daf4041a72f3 100644
--- a/llvm/lib/Analysis/UniformityAnalysis.cpp
+++ b/llvm/lib/Analysis/UniformityAnalysis.cpp
@@ -150,8 +150,11 @@ INITIALIZE_PASS_BEGIN(UniformityInfoWrapperPass, "uniformity",
 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(CycleInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
+// Though Uniformity Analysis depends on the CFG,
+// it also needs to be invalidated if values are changed, so isCFGOnly: false.
+// See NOTE on updatability at the start of GenericUniformityImpl.h
 INITIALIZE_PASS_END(UniformityInfoWrapperPass, "uniformity",
-                    "Uniformity Analysis", true, true)
+                    "Uniformity Analysis", false, true)
 
 void UniformityInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.setPreservesAll();

@J-MR-T
Copy link
Contributor Author

J-MR-T commented Jul 11, 2025

CC @nhaehnle

Copy link
Collaborator

@nhaehnle nhaehnle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BEGIN macro use should also be updated.

And personally, I'd say you should drop the comment. It only make sense to me with the historical context, which isn't visible when reading the post-change version of the code.

@J-MR-T
Copy link
Contributor Author

J-MR-T commented Jul 11, 2025

The BEGIN macro use should also be updated.

Oops, I didn't notice that that bool is duplicated there (my clangd only seems to show an inlay hint for the END version of the macro, as the BEGIN cfg/analysis arguments don't seem to be used).

And personally, I'd say you should drop the comment.

I was also on the fence on that comment, so I'm also happy to get rid of it :)

@J-MR-T
Copy link
Contributor Author

J-MR-T commented Jul 11, 2025

Sidenote: Is there a reason that INITIALIZE_PASS_BEGIN has the arg, name, cfg, and analysis arguments?

#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
static void initialize##passName##PassOnce(PassRegistry &Registry) {

It seems redundant.

@nhaehnle nhaehnle merged commit e129570 into llvm:main Jul 11, 2025
9 checks passed
@nhaehnle
Copy link
Collaborator

Sidenote: Is there a reason that INITIALIZE_PASS_BEGIN has the arg, name, cfg, and analysis arguments?

#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
static void initialize##passName##PassOnce(PassRegistry &Registry) {

It seems redundant.

I assume this was done for symmetry and in case the implementation of the macros would ever have to change.

I don't know if you have write access, so I took the liberty of merging it.

arsenm pushed a commit that referenced this pull request Aug 12, 2025
…()` with `setPreservesCFG()` (#148167)

This PR depends on #148165; the first commit
(90f1d0a) belongs to that PR. The
changes are distinct, so separate PRs seemed like the best option. I
don't have commit access, so I couldn't use user-branches to mark the
dependency.

As AMDGPULateCodeGenPrepare actually performs changes that invalidate
Uniformity Analysis; use `setPreservesCFG()` to mark this, instead of
`setPreservesAll()` which wrongly includes preserving Uniformity
Analysis.

Note that before #148165, this would still have preserved Uniformity
Analysis, hence the dependency. In addition, `amdgpu/llc-pipeline.cc`
needs to be changed when both changes are in effect, but those changes
would make the test fail if the PRs weren't based on one another.

Note on why this hasn't caused issues so far:
It just so happens that AMDGPULateCodeGenPrepare is always immediately
followed by AMDGPUUnifyDivergentExitNodes, which *does* invalidate most
analyses, including Uniformity. And because UnifyDivergentExitNodes only
looks at terminators, and LateCGP seemingly does not replace uniform
values with divergent values, or divergent values with uniform values,
and it only *inserts new values that are not looked at by
UnifyDivergentExitNodes*, this bug remained hidden.

---

I ran `git-clang-format` on my changes. I tested them using the
`check-llvm` target; no unexpected failures occurred after I made the
change to `amdgpu/llc-pipeline.ll`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants