Skip to content

Commit 39c58b7

Browse files
committed
Works on simple testcase
1 parent c701bea commit 39c58b7

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ extern char &AMDGPUPreloadKernArgPrologLegacyID;
248248
void initializeAMDGPUPreloadKernelArgumentsLegacyPass(PassRegistry &);
249249
extern char &AMDGPUPreloadKernelArgumentsLegacyID;
250250

251+
void initializeSIRestoreNormalEpilogLegacyPass(PassRegistry &);
252+
extern char &SIRestoreNormalEpilogLegacyID;
253+
251254
// Passes common to R600 and SI
252255
FunctionPass *createAMDGPUPromoteAlloca();
253256
void initializeAMDGPUPromoteAllocaPass(PassRegistry&);

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
597597
initializeSIOptimizeExecMaskingLegacyPass(*PR);
598598
initializeSIPreAllocateWWMRegsLegacyPass(*PR);
599599
initializeSIFormMemoryClausesLegacyPass(*PR);
600+
initializeSIRestoreNormalEpilogLegacyPass(*PR);
600601
initializeSIPostRABundlerLegacyPass(*PR);
601602
initializeGCNCreateVOPDLegacyPass(*PR);
602603
initializeAMDGPUUnifyDivergentExitNodesPass(*PR);
@@ -1595,6 +1596,8 @@ void GCNPassConfig::addOptimizedRegAlloc() {
15951596

15961597
if (EnableRewritePartialRegUses)
15971598
insertPass(&RenameIndependentSubregsID, &GCNRewritePartialRegUsesID);
1599+
1600+
insertPass(&RenameIndependentSubregsID,&SIRestoreNormalEpilogLegacyID);
15981601

15991602
if (isPassEnabled(EnablePreRAOptimizations))
16001603
insertPass(&MachineSchedulerID, &GCNPreRAOptimizationsID);

llvm/lib/Target/AMDGPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ add_llvm_target(AMDGPUCodeGen
183183
SIPreEmitPeephole.cpp
184184
SIProgramInfo.cpp
185185
SIRegisterInfo.cpp
186+
SIRestoreNormalEpilog.cpp
186187
SIShrinkInstructions.cpp
187188
SIWholeQuadMode.cpp
188189

llvm/lib/Target/AMDGPU/SICustomBranchBundles.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ get_epilog_for_successor(MachineBasicBlock& pred_MBB, MachineBasicBlock& succ_MB
9191

9292
for (MachineInstr &branch_MI : reverse(pred_MBB.instrs()))
9393
if (branch_MI.isBranch() && TII.getBranchDestBlock(branch_MI) == &succ_MBB)
94-
return std::next(branch_MI.getIterator());
95-
else
96-
assert(branch_MI.isBranch() && "Shouldn't have fall-throughs here.");
94+
return ++Epilog_Iterator(branch_MI.getIterator());
9795

9896
llvm_unreachable("There should always be a branch to succ_MBB.");
9997
}
@@ -132,8 +130,8 @@ static inline void normalize_ir_post_phi_elimination(MachineFunction &MF) {
132130
for (MachineBasicBlock &MBB : MF) {
133131
CFG_Rewrite_Entry to_insert = {{}, &MBB, {}};
134132
for (MachineBasicBlock *pred_MBB : MBB.predecessors()) {
135-
MachineBasicBlock::instr_iterator ep_it =
136-
get_epilog_for_successor(*pred_MBB, MBB)->getIterator();
133+
Epilog_Iterator ep_it =
134+
get_epilog_for_successor(*pred_MBB, MBB);
137135

138136
vector<MachineInstr *> epilog;
139137
while (!ep_it.isEnd())
@@ -175,6 +173,7 @@ static inline void normalize_ir_post_phi_elimination(MachineFunction &MF) {
175173
// Perform the journaled rewrites.
176174
for (auto &entry : cfg_rewrite_entries) {
177175
MachineBasicBlock *mezzanine_MBB = MF.CreateMachineBasicBlock();
176+
MF.insert(MF.end(),mezzanine_MBB);
178177

179178
// Deal with mezzanine to successor succession.
180179
BuildMI(mezzanine_MBB, DebugLoc(), TII.get(AMDGPU::S_BRANCH)).addMBB(entry.succ_MBB);
@@ -192,7 +191,7 @@ static inline void normalize_ir_post_phi_elimination(MachineFunction &MF) {
192191
pred_MBB->replaceSuccessor(entry.succ_MBB, mezzanine_MBB);
193192

194193
// Delete instructions that were lowered from epilog
195-
auto epilog_it = get_epilog_for_successor(*pred_MBB, *entry.succ_MBB);
194+
auto epilog_it = ++Epilog_Iterator(branch_ins.getIterator());
196195
while (!epilog_it.isEnd())
197196
epilog_it++->eraseFromBundle();
198197
}
@@ -228,7 +227,8 @@ static inline void hoist_unrelated_copies(MachineFunction &MF) {
228227
while (!copy_move_it.isEnd()) {
229228
Epilog_Iterator next = copy_move_it; ++next;
230229
if (copy_move_it->getOpcode() == AMDGPU::COPY &&
231-
!related_copy_sources.count(copy_move_it->getOperand(1).getReg())) {
230+
!related_copy_sources.count(copy_move_it->getOperand(1).getReg())
231+
|| copy_move_it->getOpcode() == AMDGPU::IMPLICIT_DEF) {
232232
MachineInstr &MI_to_move = *copy_move_it;
233233
MI_to_move.removeFromBundle();
234234
MBB.insert(branch_MI.getIterator(),&MI_to_move);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "SICustomBranchBundles.h"
2+
#include "AMDGPU.h"
3+
#include "GCNSubtarget.h"
4+
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
5+
#include "llvm/ADT/SmallSet.h"
6+
#include "llvm/CodeGen/LiveIntervals.h"
7+
#include "llvm/CodeGen/LiveVariables.h"
8+
#include "llvm/CodeGen/MachineDominators.h"
9+
#include "llvm/CodeGen/MachineFunctionPass.h"
10+
#include "llvm/CodeGen/MachinePostDominators.h"
11+
#include "llvm/Target/TargetMachine.h"
12+
13+
#define DEBUG_TYPE "si-restore-normal-epilog"
14+
15+
namespace
16+
{
17+
18+
class SIRestoreNormalEpilogLegacy : public MachineFunctionPass {
19+
public:
20+
static char ID;
21+
22+
SIRestoreNormalEpilogLegacy() : MachineFunctionPass(ID) {}
23+
24+
bool runOnMachineFunction(MachineFunction &MF) override {
25+
hoist_unrelated_copies(MF);
26+
normalize_ir_post_phi_elimination(MF);
27+
return true;
28+
}
29+
30+
StringRef getPassName() const override {
31+
return "SI Restore Normal Epilog Post PHI Elimination";
32+
}
33+
34+
MachineFunctionProperties getRequiredProperties() const override {
35+
return MachineFunctionProperties().setNoPHIs();
36+
}
37+
38+
};
39+
40+
} // namespace
41+
42+
INITIALIZE_PASS(SIRestoreNormalEpilogLegacy, DEBUG_TYPE,
43+
"SI restore normal epilog", false, false)
44+
45+
char SIRestoreNormalEpilogLegacy::ID;
46+
char &llvm::SIRestoreNormalEpilogLegacyID = SIRestoreNormalEpilogLegacy::ID;

0 commit comments

Comments
 (0)