Skip to content

Commit

Permalink
[AMDGPU] Use UniformityAnalysis in RewriteUndefsForPHI
Browse files Browse the repository at this point in the history
Reviewed By: foad

Differential Revision: https://reviews.llvm.org/D145359
  • Loading branch information
Pierre-vh committed Mar 6, 2023
1 parent 665e32e commit 7a5d850
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
21 changes: 11 additions & 10 deletions llvm/lib/Target/AMDGPU/AMDGPURewriteUndefForPHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// StructurizedCFG pass, and this pass has some additional limitation that make
// it can only run after SIAnnotateControlFlow.
//
// To achieve optimal code generation for AMDGPU, we assume that divergence
// To achieve optimal code generation for AMDGPU, we assume that uniformity
// analysis reports the PHI in join block of divergent branch as uniform if
// it has one unique uniform value plus additional undefined/poisoned incoming
// value. That is to say the later compiler pipeline will ensure such PHI always
Expand Down Expand Up @@ -56,7 +56,7 @@
// \---

#include "AMDGPU.h"
#include "llvm/Analysis/LegacyDivergenceAnalysis.h"
#include "llvm/Analysis/UniformityAnalysis.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
Expand All @@ -81,11 +81,11 @@ class AMDGPURewriteUndefForPHI : public FunctionPass {
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<LegacyDivergenceAnalysis>();
AU.addRequired<UniformityInfoWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();

AU.addPreserved<DominatorTreeWrapperPass>();
AU.addPreserved<LegacyDivergenceAnalysis>();
AU.addPreserved<UniformityInfoWrapperPass>();
AU.setPreservesCFG();
}
};
Expand All @@ -95,17 +95,17 @@ char AMDGPURewriteUndefForPHI::ID = 0;

INITIALIZE_PASS_BEGIN(AMDGPURewriteUndefForPHI, DEBUG_TYPE,
"Rewrite undef for PHI", false, false)
INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_END(AMDGPURewriteUndefForPHI, DEBUG_TYPE,
"Rewrite undef for PHI", false, false)

bool rewritePHIs(Function &F, LegacyDivergenceAnalysis *DA, DominatorTree *DT) {
bool rewritePHIs(Function &F, UniformityInfo &UA, DominatorTree *DT) {
bool Changed = false;
SmallVector<PHINode *> ToBeDeleted;
for (auto &BB : F) {
for (auto &PHI : BB.phis()) {
if (DA->isDivergent(&PHI))
if (UA.isDivergent(&PHI))
continue;

// The unique incoming value except undef/poison for the PHI node.
Expand Down Expand Up @@ -147,7 +147,7 @@ bool rewritePHIs(Function &F, LegacyDivergenceAnalysis *DA, DominatorTree *DT) {
// TODO: We should still be able to replace undef value if the unique
// value is a Constant.
if (!UniqueDefinedIncoming || Undefs.empty() ||
!DA->isDivergent(DominateBB->getTerminator()))
!UA.isDivergent(DominateBB->getTerminator()))
continue;

// We only replace the undef when DominateBB truly dominates all the
Expand All @@ -171,9 +171,10 @@ bool rewritePHIs(Function &F, LegacyDivergenceAnalysis *DA, DominatorTree *DT) {
}

bool AMDGPURewriteUndefForPHI::runOnFunction(Function &F) {
LegacyDivergenceAnalysis *DA = &getAnalysis<LegacyDivergenceAnalysis>();
UniformityInfo &UA =
getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
return rewritePHIs(F, DA, DT);
return rewritePHIs(F, UA, DT);
}

FunctionPass *llvm::createAMDGPURewriteUndefForPHIPass() {
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/CodeGen/AMDGPU/llc-pipeline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
; GCN-O0-NEXT: AMDGPU Annotate Uniform Values
; GCN-O0-NEXT: Natural Loop Information
; GCN-O0-NEXT: SI annotate control flow
; GCN-O0-NEXT: Post-Dominator Tree Construction
; GCN-O0-NEXT: Legacy Divergence Analysis
; GCN-O0-NEXT: Cycle Info Analysis
; GCN-O0-NEXT: Uniformity Analysis
; GCN-O0-NEXT: AMDGPU Rewrite Undef for PHI
; GCN-O0-NEXT: LCSSA Verifier
; GCN-O0-NEXT: Loop-Closed SSA Form Pass
Expand Down Expand Up @@ -282,8 +282,8 @@
; GCN-O1-NEXT: AMDGPU Annotate Uniform Values
; GCN-O1-NEXT: Natural Loop Information
; GCN-O1-NEXT: SI annotate control flow
; GCN-O1-NEXT: Post-Dominator Tree Construction
; GCN-O1-NEXT: Legacy Divergence Analysis
; GCN-O1-NEXT: Cycle Info Analysis
; GCN-O1-NEXT: Uniformity Analysis
; GCN-O1-NEXT: AMDGPU Rewrite Undef for PHI
; GCN-O1-NEXT: LCSSA Verifier
; GCN-O1-NEXT: Loop-Closed SSA Form Pass
Expand Down Expand Up @@ -581,8 +581,8 @@
; GCN-O1-OPTS-NEXT: AMDGPU Annotate Uniform Values
; GCN-O1-OPTS-NEXT: Natural Loop Information
; GCN-O1-OPTS-NEXT: SI annotate control flow
; GCN-O1-OPTS-NEXT: Post-Dominator Tree Construction
; GCN-O1-OPTS-NEXT: Legacy Divergence Analysis
; GCN-O1-OPTS-NEXT: Cycle Info Analysis
; GCN-O1-OPTS-NEXT: Uniformity Analysis
; GCN-O1-OPTS-NEXT: AMDGPU Rewrite Undef for PHI
; GCN-O1-OPTS-NEXT: LCSSA Verifier
; GCN-O1-OPTS-NEXT: Loop-Closed SSA Form Pass
Expand Down Expand Up @@ -888,8 +888,8 @@
; GCN-O2-NEXT: AMDGPU Annotate Uniform Values
; GCN-O2-NEXT: Natural Loop Information
; GCN-O2-NEXT: SI annotate control flow
; GCN-O2-NEXT: Post-Dominator Tree Construction
; GCN-O2-NEXT: Legacy Divergence Analysis
; GCN-O2-NEXT: Cycle Info Analysis
; GCN-O2-NEXT: Uniformity Analysis
; GCN-O2-NEXT: AMDGPU Rewrite Undef for PHI
; GCN-O2-NEXT: LCSSA Verifier
; GCN-O2-NEXT: Loop-Closed SSA Form Pass
Expand Down Expand Up @@ -1208,8 +1208,8 @@
; GCN-O3-NEXT: AMDGPU Annotate Uniform Values
; GCN-O3-NEXT: Natural Loop Information
; GCN-O3-NEXT: SI annotate control flow
; GCN-O3-NEXT: Post-Dominator Tree Construction
; GCN-O3-NEXT: Legacy Divergence Analysis
; GCN-O3-NEXT: Cycle Info Analysis
; GCN-O3-NEXT: Uniformity Analysis
; GCN-O3-NEXT: AMDGPU Rewrite Undef for PHI
; GCN-O3-NEXT: LCSSA Verifier
; GCN-O3-NEXT: Loop-Closed SSA Form Pass
Expand Down

0 comments on commit 7a5d850

Please sign in to comment.