[CodeGen][NewPM] Port MachineCycleInfo to NPM (#114745)

This commit is contained in:
Akshat Oke
2025-03-03 11:26:17 +05:30
committed by GitHub
parent e11867039f
commit 69c8312c0a
7 changed files with 83 additions and 26 deletions

View File

@@ -16,6 +16,7 @@
#include "llvm/ADT/GenericCycleInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/MachineSSAContext.h"
namespace llvm {
@@ -46,6 +47,27 @@ public:
// version.
bool isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I);
class MachineCycleAnalysis : public AnalysisInfoMixin<MachineCycleAnalysis> {
friend AnalysisInfoMixin<MachineCycleAnalysis>;
static AnalysisKey Key;
public:
using Result = MachineCycleInfo;
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
};
class MachineCycleInfoPrinterPass
: public PassInfoMixin<MachineCycleInfoPrinterPass> {
raw_ostream &OS;
public:
explicit MachineCycleInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
static bool isRequired() { return true; }
};
} // end namespace llvm
#endif // LLVM_CODEGEN_MACHINECYCLEANALYSIS_H

View File

@@ -191,7 +191,7 @@ void initializeMachineCFGPrinterPass(PassRegistry &);
void initializeMachineCSELegacyPass(PassRegistry &);
void initializeMachineCombinerPass(PassRegistry &);
void initializeMachineCopyPropagationLegacyPass(PassRegistry &);
void initializeMachineCycleInfoPrinterPassPass(PassRegistry &);
void initializeMachineCycleInfoPrinterLegacyPass(PassRegistry &);
void initializeMachineCycleInfoWrapperPassPass(PassRegistry &);
void initializeMachineDominanceFrontierPass(PassRegistry &);
void initializeMachineDominatorTreeWrapperPassPass(PassRegistry &);

View File

@@ -106,6 +106,7 @@ MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-block-freq", MachineBlockFrequencyAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-branch-prob",
MachineBranchProbabilityAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-cycles", MachineCycleAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopAnalysis())
MACHINE_FUNCTION_ANALYSIS("machine-opt-remark-emitter",
@@ -162,6 +163,7 @@ MACHINE_FUNCTION_PASS("print<machine-block-freq>",
MachineBlockFrequencyPrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<machine-branch-prob>",
MachineBranchProbabilityPrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<machine-cycles>", MachineCycleInfoPrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<machine-dom-tree>",
MachineDominatorTreePrinterPass(errs()))
MACHINE_FUNCTION_PASS("print<machine-loops>", MachineLoopPrinterPass(errs()))
@@ -263,7 +265,6 @@ DUMMY_MACHINE_FUNCTION_PASS("mirfs-discriminators", MIRAddFSDiscriminatorsPass)
DUMMY_MACHINE_FUNCTION_PASS("patchable-function", PatchableFunctionPass)
DUMMY_MACHINE_FUNCTION_PASS("postra-machine-sink", PostRAMachineSinkingPass)
DUMMY_MACHINE_FUNCTION_PASS("postrapseudos", ExpandPostRAPseudosPass)
DUMMY_MACHINE_FUNCTION_PASS("print-machine-cycles", MachineCycleInfoPrinterPass)
DUMMY_MACHINE_FUNCTION_PASS("print-machine-uniformity", MachineUniformityInfoPrinterPass)
DUMMY_MACHINE_FUNCTION_PASS("processimpdefs", ProcessImplicitDefsPass)
DUMMY_MACHINE_FUNCTION_PASS("prologepilog", PrologEpilogInserterPass)

View File

@@ -78,7 +78,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeMachineCSELegacyPass(Registry);
initializeMachineCombinerPass(Registry);
initializeMachineCopyPropagationLegacyPass(Registry);
initializeMachineCycleInfoPrinterPassPass(Registry);
initializeMachineCycleInfoPrinterLegacyPass(Registry);
initializeMachineCycleInfoWrapperPassPass(Registry);
initializeMachineDominatorTreeWrapperPassPass(Registry);
initializeMachineFunctionPrinterPassPass(Registry);

View File

@@ -54,43 +54,61 @@ void MachineCycleInfoWrapperPass::releaseMemory() {
F = nullptr;
}
AnalysisKey MachineCycleAnalysis::Key;
MachineCycleInfo
MachineCycleAnalysis::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
MachineCycleInfo MCI;
MCI.compute(MF);
return MCI;
}
namespace {
class MachineCycleInfoPrinterPass : public MachineFunctionPass {
class MachineCycleInfoPrinterLegacy : public MachineFunctionPass {
public:
static char ID;
MachineCycleInfoPrinterPass();
MachineCycleInfoPrinterLegacy();
bool runOnMachineFunction(MachineFunction &F) override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
};
} // namespace
char MachineCycleInfoPrinterPass::ID = 0;
char MachineCycleInfoPrinterLegacy::ID = 0;
MachineCycleInfoPrinterPass::MachineCycleInfoPrinterPass()
MachineCycleInfoPrinterLegacy::MachineCycleInfoPrinterLegacy()
: MachineFunctionPass(ID) {
initializeMachineCycleInfoPrinterPassPass(*PassRegistry::getPassRegistry());
initializeMachineCycleInfoPrinterLegacyPass(*PassRegistry::getPassRegistry());
}
INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterPass, "print-machine-cycles",
INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
"Print Machine Cycle Info Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
INITIALIZE_PASS_END(MachineCycleInfoPrinterPass, "print-machine-cycles",
INITIALIZE_PASS_END(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
"Print Machine Cycle Info Analysis", true, true)
void MachineCycleInfoPrinterPass::getAnalysisUsage(AnalysisUsage &AU) const {
void MachineCycleInfoPrinterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<MachineCycleInfoWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}
bool MachineCycleInfoPrinterPass::runOnMachineFunction(MachineFunction &F) {
bool MachineCycleInfoPrinterLegacy::runOnMachineFunction(MachineFunction &F) {
auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
CI.print(errs());
return false;
}
PreservedAnalyses
MachineCycleInfoPrinterPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
auto &MCI = MFAM.getResult<MachineCycleAnalysis>(MF);
MCI.print(OS);
return PreservedAnalyses::all();
}
bool llvm::isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I) {
MachineFunction *MF = I.getParent()->getParent();
MachineRegisterInfo *MRI = &MF->getRegInfo();

View File

@@ -111,6 +111,7 @@
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
#include "llvm/CodeGen/MachineCSE.h"
#include "llvm/CodeGen/MachineCopyPropagation.h"
#include "llvm/CodeGen/MachineCycleAnalysis.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineLICM.h"

View File

@@ -1,8 +1,11 @@
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=print-machine-cycles -o - %s 2>&1 | FileCheck %s
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=print-machine-cycles -o - %s 2>&1 | FileCheck %s --check-prefixes=LEGACY,CHECK
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -passes="machine-function(print,print<machine-cycles>)" -o - %s 2>&1 | FileCheck %s --check-prefixes=NPM,CHECK
...
---
# CHECK-LABEL: MachineCycleInfo for function: empty
# LEGACY-LABEL: MachineCycleInfo for function: empty
# NPM-LABEL: name: empty
name: empty
alignment: 16
tracksRegLiveness: true
@@ -15,7 +18,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: simple
# LEGACY-LABEL: MachineCycleInfo for function: simple
# NPM-LABEL: name: simple
# CHECK: depth=1: entries(bb.1)
name: simple
alignment: 16
@@ -40,7 +44,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: two_latches
# LEGACY-LABEL: MachineCycleInfo for function: two_latches
# NPM-LABEL: name: two_latches
# CHECK: depth=1: entries(bb.1) bb.2
name: two_latches
alignment: 16
@@ -72,7 +77,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: nested_simple
# LEGACY-LABEL: MachineCycleInfo for function: nested_simple
# NPM-LABEL: name: nested_simple
# CHECK: depth=1: entries(bb.1) bb.3 bb.2
# CHECK: depth=2: entries(bb.2)
name: nested_simple
@@ -108,7 +114,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: nested_outer_latch_in_inner_loop
# LEGACY-LABEL: MachineCycleInfo for function: nested_outer_latch_in_inner_loop
# NPM-LABEL: name: nested_outer_latch_in_inner_loop
# CHECK: depth=1: entries(bb.1) bb.2 bb.3
# CHECK: depth=2: entries(bb.2) bb.3
name: nested_outer_latch_in_inner_loop
@@ -144,7 +151,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: sibling_loops
# LEGACY-LABEL: MachineCycleInfo for function: sibling_loops
# NPM-LABEL: name: sibling_loops
# CHECK: depth=1: entries(bb.1)
# CHECK: depth=1: entries(bb.2)
name: sibling_loops
@@ -181,7 +189,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: serial_loops
# LEGACY-LABEL: MachineCycleInfo for function: serial_loops
# NPM-LABEL: name: serial_loops
# CHECK: depth=1: entries(bb.2)
# CHECK: depth=1: entries(bb.1)
name: serial_loops
@@ -214,7 +223,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: nested_sibling_loops
# LEGACY-LABEL: MachineCycleInfo for function: nested_sibling_loops
# NPM-LABEL: name: nested_sibling_loops
# CHECK: depth=1: entries(bb.1) bb.4 bb.5 bb.3 bb.2
# CHECK: depth=2: entries(bb.4) bb.5
# CHECK: depth=2: entries(bb.2)
@@ -277,7 +287,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: deeper_nest
# LEGACY-LABEL: MachineCycleInfo for function: deeper_nest
# NPM-LABEL: name: deeper_nest
# CHECK: depth=1: entries(bb.1) bb.5 bb.2 bb.3 bb.4
# CHECK: depth=2: entries(bb.2) bb.3 bb.4
# CHECK: depth=3: entries(bb.3) bb.4
@@ -324,7 +335,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: irreducible_basic
# LEGACY-LABEL: MachineCycleInfo for function: irreducible_basic
# NPM-LABEL: name: irreducible_basic
# CHECK: depth=1: entries(bb.2 bb.1)
name: irreducible_basic
alignment: 16
@@ -360,7 +372,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: irreducible_mess
# LEGACY-LABEL: MachineCycleInfo for function: irreducible_mess
# NPM-LABEL: name: irreducible_mess
# CHECK: depth=1: entries(bb.2 bb.1) bb.6 bb.5 bb.3 bb.4
# CHECK: depth=2: entries(bb.5 bb.3 bb.1) bb.4
# CHECK: depth=3: entries(bb.3 bb.1) bb.4
@@ -436,7 +449,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: irreducible_into_simple_cycle
# LEGACY-LABEL: MachineCycleInfo for function: irreducible_into_simple_cycle
# NPM-LABEL: name: irreducible_into_simple_cycle
# CHECK: depth=1: entries(bb.2 bb.7 bb.4) bb.6 bb.5 bb.3
name: irreducible_into_simple_cycle
alignment: 16
@@ -495,7 +509,8 @@ body: |
...
---
# CHECK-LABEL: MachineCycleInfo for function: irreducible_mountain_bug
# LEGACY-LABEL: MachineCycleInfo for function: irreducible_mountain_bug
# NPM-LABEL: name: irreducible_mountain_bug
# CHECK: depth=1: entries(bb.6) bb.11 bb.10 bb.8 bb.7 bb.9 bb.12
# CHECK: depth=2: entries(bb.10 bb.7) bb.8 bb.9
# CHECK: depth=3: entries(bb.8 bb.7) bb.9