Skip to content

Commit 034501e

Browse files
committed
AMDGPU/NewPM: Port SIAnnotateControlFlow to new pass manager
Does not yet add it to the pass pipeline. Somehow it causes 2 tests to assert in SelectionDAG, in functions without any control flow.
1 parent 9f3ff8d commit 034501e

File tree

6 files changed

+95
-42
lines changed

6 files changed

+95
-42
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace llvm {
1919

2020
class AMDGPUTargetMachine;
21+
class GCNTargetMachine;
2122
class TargetMachine;
2223

2324
// GlobalISel passes
@@ -32,7 +33,7 @@ void initializeAMDGPURegBankSelectPass(PassRegistry &);
3233

3334
// SI Passes
3435
FunctionPass *createGCNDPPCombinePass();
35-
FunctionPass *createSIAnnotateControlFlowPass();
36+
FunctionPass *createSIAnnotateControlFlowLegacyPass();
3637
FunctionPass *createSIFoldOperandsPass();
3738
FunctionPass *createSIPeepholeSDWAPass();
3839
FunctionPass *createSILowerI1CopiesPass();
@@ -343,8 +344,18 @@ class AMDGPURewriteUndefForPHIPass
343344
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
344345
};
345346

346-
void initializeSIAnnotateControlFlowPass(PassRegistry&);
347-
extern char &SIAnnotateControlFlowPassID;
347+
class SIAnnotateControlFlowPass
348+
: public PassInfoMixin<SIAnnotateControlFlowPass> {
349+
private:
350+
const AMDGPUTargetMachine &TM;
351+
352+
public:
353+
SIAnnotateControlFlowPass(const AMDGPUTargetMachine &TM) : TM(TM) {}
354+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
355+
};
356+
357+
void initializeSIAnnotateControlFlowLegacyPass(PassRegistry &);
358+
extern char &SIAnnotateControlFlowLegacyPassID;
348359

349360
void initializeSIMemoryLegalizerPass(PassRegistry&);
350361
extern char &SIMemoryLegalizerID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ FUNCTION_PASS("amdgpu-rewrite-undef-for-phi", AMDGPURewriteUndefForPHIPass())
4646
FUNCTION_PASS("amdgpu-unify-divergent-exit-nodes",
4747
AMDGPUUnifyDivergentExitNodesPass())
4848
FUNCTION_PASS("amdgpu-usenative", AMDGPUUseNativeCallsPass())
49+
FUNCTION_PASS("si-annotate-control-flow", SIAnnotateControlFlowPass(*static_cast<const GCNTargetMachine *>(this)))
4950
#undef FUNCTION_PASS
5051

5152
#ifndef FUNCTION_ANALYSIS

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
438438
initializeAMDGPURewriteOutArgumentsPass(*PR);
439439
initializeAMDGPURewriteUndefForPHILegacyPass(*PR);
440440
initializeAMDGPUUnifyMetadataPass(*PR);
441-
initializeSIAnnotateControlFlowPass(*PR);
441+
initializeSIAnnotateControlFlowLegacyPass(*PR);
442442
initializeAMDGPUInsertSingleUseVDSTPass(*PR);
443443
initializeAMDGPUInsertDelayAluPass(*PR);
444444
initializeSIInsertHardClausesPass(*PR);
@@ -1220,7 +1220,7 @@ bool GCNPassConfig::addPreISel() {
12201220
}
12211221
addPass(createAMDGPUAnnotateUniformValues());
12221222
if (!LateCFGStructurize && !DisableStructurizer) {
1223-
addPass(createSIAnnotateControlFlowPass());
1223+
addPass(createSIAnnotateControlFlowLegacyPass());
12241224
// TODO: Move this right after structurizeCFG to avoid extra divergence
12251225
// analysis. This depends on stopping SIAnnotateControlFlow from making
12261226
// control flow modifications.

llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "AMDGPU.h"
15+
#include "AMDGPUTargetMachine.h"
1516
#include "GCNSubtarget.h"
1617
#include "llvm/Analysis/LoopInfo.h"
1718
#include "llvm/Analysis/UniformityAnalysis.h"
@@ -36,7 +37,8 @@ namespace {
3637
using StackEntry = std::pair<BasicBlock *, Value *>;
3738
using StackVector = SmallVector<StackEntry, 16>;
3839

39-
class SIAnnotateControlFlow : public FunctionPass {
40+
class SIAnnotateControlFlow {
41+
private:
4042
UniformityInfo *UA;
4143

4244
Type *Boolean;
@@ -89,37 +91,17 @@ class SIAnnotateControlFlow : public FunctionPass {
8991
bool closeControlFlow(BasicBlock *BB);
9092

9193
public:
92-
static char ID;
93-
94-
SIAnnotateControlFlow() : FunctionPass(ID) {}
95-
96-
bool runOnFunction(Function &F) override;
97-
98-
StringRef getPassName() const override { return "SI annotate control flow"; }
99-
100-
void getAnalysisUsage(AnalysisUsage &AU) const override {
101-
AU.addRequired<LoopInfoWrapperPass>();
102-
AU.addRequired<DominatorTreeWrapperPass>();
103-
AU.addRequired<UniformityInfoWrapperPass>();
104-
AU.addPreserved<LoopInfoWrapperPass>();
105-
AU.addPreserved<DominatorTreeWrapperPass>();
106-
AU.addRequired<TargetPassConfig>();
107-
FunctionPass::getAnalysisUsage(AU);
94+
SIAnnotateControlFlow(Module &M, const GCNSubtarget &ST, DominatorTree &DT,
95+
LoopInfo &LI, UniformityInfo &UA)
96+
: UA(&UA), DT(&DT), LI(&LI) {
97+
initialize(M, ST);
10898
}
99+
100+
bool run(Function &F);
109101
};
110102

111103
} // end anonymous namespace
112104

113-
INITIALIZE_PASS_BEGIN(SIAnnotateControlFlow, DEBUG_TYPE,
114-
"Annotate SI Control Flow", false, false)
115-
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
116-
INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass)
117-
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
118-
INITIALIZE_PASS_END(SIAnnotateControlFlow, DEBUG_TYPE,
119-
"Annotate SI Control Flow", false, false)
120-
121-
char SIAnnotateControlFlow::ID = 0;
122-
123105
/// Initialize all the types and constants used in the pass
124106
void SIAnnotateControlFlow::initialize(Module &M, const GCNSubtarget &ST) {
125107
LLVMContext &Context = M.getContext();
@@ -349,15 +331,9 @@ bool SIAnnotateControlFlow::closeControlFlow(BasicBlock *BB) {
349331

350332
/// Annotate the control flow with intrinsics so the backend can
351333
/// recognize if/then/else and loops.
352-
bool SIAnnotateControlFlow::runOnFunction(Function &F) {
353-
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
354-
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
355-
UA = &getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
356-
TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
357-
const TargetMachine &TM = TPC.getTM<TargetMachine>();
358-
334+
bool SIAnnotateControlFlow::run(Function &F) {
359335
bool Changed = false;
360-
initialize(*F.getParent(), TM.getSubtarget<GCNSubtarget>(F));
336+
361337
for (df_iterator<BasicBlock *> I = df_begin(&F.getEntryBlock()),
362338
E = df_end(&F.getEntryBlock()); I != E; ++I) {
363339
BasicBlock *BB = *I;
@@ -401,7 +377,70 @@ bool SIAnnotateControlFlow::runOnFunction(Function &F) {
401377
return Changed;
402378
}
403379

380+
PreservedAnalyses SIAnnotateControlFlowPass::run(Function &F,
381+
FunctionAnalysisManager &FAM) {
382+
const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
383+
384+
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
385+
UniformityInfo &UI = FAM.getResult<UniformityInfoAnalysis>(F);
386+
LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
387+
388+
SIAnnotateControlFlow Impl(*F.getParent(), ST, DT, LI, UI);
389+
390+
// FIXME: We introduce dead declarations of intrinsics even if never used.
391+
bool Changed = Impl.run(F);
392+
if (!Changed)
393+
return PreservedAnalyses::none();
394+
395+
// TODO: Is LoopInfo preserved?
396+
PreservedAnalyses PA = PreservedAnalyses::none();
397+
PA.preserve<DominatorTreeAnalysis>();
398+
return PA;
399+
}
400+
401+
class SIAnnotateControlFlowLegacy : public FunctionPass {
402+
public:
403+
static char ID;
404+
405+
SIAnnotateControlFlowLegacy() : FunctionPass(ID) {}
406+
407+
StringRef getPassName() const override { return "SI annotate control flow"; }
408+
409+
void getAnalysisUsage(AnalysisUsage &AU) const override {
410+
AU.addRequired<LoopInfoWrapperPass>();
411+
AU.addRequired<DominatorTreeWrapperPass>();
412+
AU.addRequired<UniformityInfoWrapperPass>();
413+
AU.addPreserved<LoopInfoWrapperPass>();
414+
AU.addPreserved<DominatorTreeWrapperPass>();
415+
AU.addRequired<TargetPassConfig>();
416+
FunctionPass::getAnalysisUsage(AU);
417+
}
418+
419+
bool runOnFunction(Function &F) override {
420+
DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
421+
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
422+
UniformityInfo &UI =
423+
getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
424+
TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
425+
const TargetMachine &TM = TPC.getTM<TargetMachine>();
426+
const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
427+
428+
SIAnnotateControlFlow Impl(*F.getParent(), ST, DT, LI, UI);
429+
return Impl.run(F);
430+
}
431+
};
432+
433+
INITIALIZE_PASS_BEGIN(SIAnnotateControlFlowLegacy, DEBUG_TYPE,
434+
"Annotate SI Control Flow", false, false)
435+
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
436+
INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass)
437+
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
438+
INITIALIZE_PASS_END(SIAnnotateControlFlowLegacy, DEBUG_TYPE,
439+
"Annotate SI Control Flow", false, false)
440+
441+
char SIAnnotateControlFlowLegacy::ID = 0;
442+
404443
/// Create the annotation pass
405-
FunctionPass *llvm::createSIAnnotateControlFlowPass() {
406-
return new SIAnnotateControlFlow();
444+
FunctionPass *llvm::createSIAnnotateControlFlowLegacyPass() {
445+
return new SIAnnotateControlFlowLegacy();
407446
}

llvm/test/CodeGen/AMDGPU/si-annotate-cf-noloop.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -mtriple=amdgcn-- -S -structurizecfg -si-annotate-control-flow -simplifycfg-require-and-preserve-domtree=1 %s | FileCheck -check-prefix=OPT %s
2+
; RUN: opt -mtriple=amdgcn-- -S -passes=structurizecfg,si-annotate-control-flow -simplifycfg-require-and-preserve-domtree=1 %s | FileCheck -check-prefix=OPT %s
23
; RUN: llc -mtriple=amdgcn -verify-machineinstrs -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck -check-prefix=GCN %s
34

45

llvm/test/CodeGen/AMDGPU/si-annotate-cf-unreachable.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -mtriple=amdgcn-- -S -structurizecfg -si-annotate-control-flow %s | FileCheck -check-prefix=OPT %s
2+
; RUN: opt -mtriple=amdgcn-- -S -passes=structurizecfg,si-annotate-control-flow %s | FileCheck -check-prefix=OPT %s
23
; RUN: llc -mtriple=amdgcn -verify-machineinstrs < %s | FileCheck -check-prefix=GCN %s
34

45

0 commit comments

Comments
 (0)