Skip to content

Commit 13fc48f

Browse files
committed
[NewPM][CodeGen] Add MachineFunctionAnalysis
1 parent b45c9c3 commit 13fc48f

16 files changed

+188
-51
lines changed

llvm/include/llvm/CodeGen/FreeMachineFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace llvm {
1515

16+
// TODO: Convert it to function pass.
1617
class FreeMachineFunctionPass : public PassInfoMixin<FreeMachineFunctionPass> {
1718
public:
1819
PreservedAnalyses run(MachineFunction &MF,

llvm/include/llvm/CodeGen/MIRParser/MIRParser.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class MachineModuleInfo;
3434
class SMDiagnostic;
3535
class StringRef;
3636

37+
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
38+
using ModuleAnalysisManager = AnalysisManager<Module>;
39+
3740
typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)>
3841
DataLayoutCallbackTy;
3942

@@ -60,6 +63,15 @@ class MIRParser {
6063
///
6164
/// \returns true if an error occurred.
6265
bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
66+
67+
/// Parses MachineFunctions in the MIR file and add them as the result
68+
/// of MachineFunctionAnalysis in ModulePassManager \p MAM.
69+
/// User should register at least MachineFunctionAnalysis,
70+
/// MachineModuleAnalysis, FunctionAnalysisManagerModuleProxy and
71+
/// PassInstrumentationAnalysis in \p MAM before parsing MIR.
72+
///
73+
/// \returns true if an error occurred.
74+
bool parseMachineFunctions(Module &M, ModuleAnalysisManager &MAM);
6375
};
6476

6577
/// This function is the main interface to the MIR serialization format parser.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===- llvm/CodeGen/MachineFunctionAnalysis.h -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares the MachineFunctionAnalysis class.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS
14+
#define LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS
15+
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
20+
class MachineFunction;
21+
class LLVMTargetMachine;
22+
23+
class MachineFunctionAnalysis
24+
: public AnalysisInfoMixin<MachineFunctionAnalysis> {
25+
friend AnalysisInfoMixin<MachineFunctionAnalysis>;
26+
27+
static AnalysisKey Key;
28+
29+
const LLVMTargetMachine *TM;
30+
31+
public:
32+
class Result {
33+
std::unique_ptr<MachineFunction> MF;
34+
35+
public:
36+
Result(std::unique_ptr<MachineFunction> MF) : MF(std::move(MF)) {}
37+
MachineFunction &getMF() { return *MF; };
38+
bool invalidate(Function &, const PreservedAnalyses &PA,
39+
FunctionAnalysisManager::Invalidator &);
40+
};
41+
42+
MachineFunctionAnalysis(const LLVMTargetMachine *TM) : TM(TM){};
43+
Result run(Function &F, FunctionAnalysisManager &FAM);
44+
};
45+
46+
} // namespace llvm
47+
48+
#endif // LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@ class MachineModuleInfo {
147147

148148
/// Returns the MachineFunction constructed for the IR function \p F.
149149
/// Creates a new MachineFunction if none exists yet.
150+
/// NOTE: New pass manager clients shall not use this method to get
151+
/// the `MachineFunction`, use `MachineFunctionAnalysis` instead.
150152
MachineFunction &getOrCreateMachineFunction(Function &F);
151153

152154
/// \brief Returns the MachineFunction associated to IR function \p F if there
153155
/// is one, otherwise nullptr.
156+
/// NOTE: New pass manager clients shall not use this method to get
157+
/// the `MachineFunction`, use `MachineFunctionAnalysis` instead.
154158
MachineFunction *getMachineFunction(const Function &F) const;
155159

156160
/// Delete the MachineFunction \p MF and reset the link in the IR Function to

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ struct MachinePassModel
7676
#endif
7777

7878
auto PA = this->Pass.run(IR, AM);
79+
// Machine function passes are not allowed to modify the LLVM
80+
// representation, therefore we should preserve all IR analyses.
81+
PA.template preserveSet<AllAnalysesOn<Module>>();
82+
PA.template preserveSet<AllAnalysesOn<Function>>();
7983

8084
if constexpr (is_detected<has_get_set_properties_t, PassT>::value)
8185
IR.getProperties().set(PassT::getSetProperties());

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ add_llvm_component_library(LLVMCodeGen
120120
MachineDominators.cpp
121121
MachineFrameInfo.cpp
122122
MachineFunction.cpp
123+
MachineFunctionAnalysis.cpp
123124
MachineFunctionPass.cpp
124125
MachineFunctionPrinterPass.cpp
125126
MachineFunctionSplitter.cpp

llvm/lib/CodeGen/FreeMachineFunction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
#include "llvm/CodeGen/FreeMachineFunction.h"
1010
#include "llvm/CodeGen/MachineFunction.h"
11+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
1112
#include "llvm/CodeGen/MachineModuleInfo.h"
1213

1314
using namespace llvm;
1415

1516
PreservedAnalyses
1617
FreeMachineFunctionPass::run(MachineFunction &MF,
1718
MachineFunctionAnalysisManager &MFAM) {
18-
auto &MMI = MF.getMMI();
19-
MFAM.invalidate(MF, PreservedAnalyses::none());
20-
MMI.deleteMachineFunctionFor(MF.getFunction()); // MF is dangling now.
21-
return PreservedAnalyses::none();
19+
PreservedAnalyses PA = PreservedAnalyses::none();
20+
PA.abandon<MachineFunctionAnalysis>();
21+
return PA;
2222
}

llvm/lib/CodeGen/MIRParser/MIRParser.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/CodeGen/MachineConstantPool.h"
2222
#include "llvm/CodeGen/MachineFrameInfo.h"
2323
#include "llvm/CodeGen/MachineFunction.h"
24+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
2425
#include "llvm/CodeGen/MachineModuleInfo.h"
2526
#include "llvm/CodeGen/MachineRegisterInfo.h"
2627
#include "llvm/CodeGen/TargetFrameLowering.h"
@@ -97,13 +98,15 @@ class MIRParserImpl {
9798
/// Create an empty function with the given name.
9899
Function *createDummyFunction(StringRef Name, Module &M);
99100

100-
bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
101+
bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI,
102+
ModuleAnalysisManager *FAM = nullptr);
101103

102104
/// Parse the machine function in the current YAML document.
103105
///
104106
///
105107
/// Return true if an error occurred.
106-
bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
108+
bool parseMachineFunction(Module &M, MachineModuleInfo &MMI,
109+
ModuleAnalysisManager *FAM);
107110

108111
/// Initialize the machine function to the state that's described in the MIR
109112
/// file.
@@ -275,13 +278,14 @@ MIRParserImpl::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) {
275278
return M;
276279
}
277280

278-
bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
281+
bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI,
282+
ModuleAnalysisManager *MAM) {
279283
if (NoMIRDocuments)
280284
return false;
281285

282286
// Parse the machine functions.
283287
do {
284-
if (parseMachineFunction(M, MMI))
288+
if (parseMachineFunction(M, MMI, MAM))
285289
return true;
286290
In.nextDocument();
287291
} while (In.setCurrentDocument());
@@ -303,7 +307,8 @@ Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
303307
return F;
304308
}
305309

306-
bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
310+
bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI,
311+
ModuleAnalysisManager *MAM) {
307312
// Parse the yaml.
308313
yaml::MachineFunction YamlMF;
309314
yaml::EmptyContext Ctx;
@@ -327,14 +332,28 @@ bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
327332
"' isn't defined in the provided LLVM IR");
328333
}
329334
}
330-
if (MMI.getMachineFunction(*F) != nullptr)
331-
return error(Twine("redefinition of machine function '") + FunctionName +
332-
"'");
333335

334-
// Create the MachineFunction.
335-
MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
336-
if (initializeMachineFunction(YamlMF, MF))
337-
return true;
336+
if (!MAM) {
337+
if (MMI.getMachineFunction(*F) != nullptr)
338+
return error(Twine("redefinition of machine function '") + FunctionName +
339+
"'");
340+
341+
// Create the MachineFunction.
342+
MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
343+
if (initializeMachineFunction(YamlMF, MF))
344+
return true;
345+
} else {
346+
auto &FAM =
347+
MAM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
348+
if (FAM.getCachedResult<MachineFunctionAnalysis>(*F))
349+
return error(Twine("redefinition of machine function '") + FunctionName +
350+
"'");
351+
352+
// Create the MachineFunction.
353+
MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(*F).getMF();
354+
if (initializeMachineFunction(YamlMF, MF))
355+
return true;
356+
}
338357

339358
return false;
340359
}
@@ -1101,6 +1120,11 @@ bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
11011120
return Impl->parseMachineFunctions(M, MMI);
11021121
}
11031122

1123+
bool MIRParser::parseMachineFunctions(Module &M, ModuleAnalysisManager &MAM) {
1124+
auto &MMI = MAM.getResult<MachineModuleAnalysis>(M).getMMI();
1125+
return Impl->parseMachineFunctions(M, MMI, &MAM);
1126+
}
1127+
11041128
std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(
11051129
StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
11061130
std::function<void(Function &)> ProcessIRFunction) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- MachineFunctionAnalysis.cpp ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains the definitions of the MachineFunctionAnalysis members.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
14+
#include "llvm/CodeGen/MachineFunction.h"
15+
#include "llvm/CodeGen/MachineModuleInfo.h"
16+
#include "llvm/Target/TargetMachine.h"
17+
18+
using namespace llvm;
19+
20+
AnalysisKey MachineFunctionAnalysis::Key;
21+
22+
bool MachineFunctionAnalysis::Result::invalidate(
23+
Function &, const PreservedAnalyses &PA,
24+
FunctionAnalysisManager::Invalidator &) {
25+
// Unless it is invalidated explicitly, it should remain preserved.
26+
auto PAC = PA.getChecker<MachineFunctionAnalysis>();
27+
return !PAC.preservedWhenStateless();
28+
}
29+
30+
MachineFunctionAnalysis::Result
31+
MachineFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
32+
/// Next unique number available for a MachineFunction.
33+
static unsigned NextFnNum = 0;
34+
35+
const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(F);
36+
auto &MMI = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
37+
.getCachedResult<MachineModuleAnalysis>(*F.getParent())
38+
->getMMI();
39+
auto MF = std::make_unique<MachineFunction>(F, *TM, STI, NextFnNum++, MMI);
40+
MF->initTargetMachineFunctionInfo(STI);
41+
42+
// MRI callback for target specific initializations.
43+
TM->registerMachineRegisterInfoCallback(*MF);
44+
45+
return Result(std::move(MF));
46+
}

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/CodeGen/MachinePassManager.h"
14+
#include "llvm/CodeGen/FreeMachineFunction.h"
1415
#include "llvm/CodeGen/MachineFunction.h"
16+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
1517
#include "llvm/CodeGen/MachineModuleInfo.h"
1618
#include "llvm/IR/PassManagerImpl.h"
1719

@@ -71,7 +73,9 @@ bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
7173

7274
PreservedAnalyses
7375
ModuleToMachineFunctionPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
74-
auto &MMI = AM.getResult<MachineModuleAnalysis>(M).getMMI();
76+
// Ensure we have a MachineModuleInfo
77+
AM.getResult<MachineModuleAnalysis>(M).getMMI();
78+
auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
7579
MachineFunctionAnalysisManager &MFAM =
7680
AM.getResult<MachineFunctionAnalysisManagerModuleProxy>(M).getManager();
7781
PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);
@@ -82,19 +86,20 @@ ModuleToMachineFunctionPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
8286
if (F.isDeclaration() || F.hasAvailableExternallyLinkage())
8387
continue;
8488

85-
MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
89+
MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(F).getMF();
8690

8791
if (!PI.runBeforePass<MachineFunction>(*Pass, MF))
8892
continue;
8993
PreservedAnalyses PassPA = Pass->run(MF, MFAM);
90-
if (MMI.getMachineFunction(F)) {
91-
MFAM.invalidate(MF, PassPA);
94+
MFAM.invalidate(MF, PassPA);
95+
if (Pass->name() != FreeMachineFunctionPass::name()) {
9296
PI.runAfterPass(*Pass, MF, PassPA);
97+
PA.intersect(std::move(PassPA));
9398
} else {
94-
MFAM.clear(MF, F.getName());
95-
PI.runAfterPassInvalidated<MachineFunction>(*Pass, PassPA);
99+
PA.intersect(std::move(PassPA));
100+
FAM.invalidate(F, PA);
101+
PI.runAfterPassInvalidated<MachineFunction>(*Pass, PA);
96102
}
97-
PA.intersect(std::move(PassPA));
98103
}
99104

100105
return PA;
@@ -112,25 +117,24 @@ PreservedAnalyses
112117
PassManager<MachineFunction>::run(MachineFunction &MF,
113118
AnalysisManager<MachineFunction> &MFAM) {
114119
PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF);
115-
Function &F = MF.getFunction();
116-
MachineModuleInfo &MMI =
117-
MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
118-
.getCachedResult<MachineModuleAnalysis>(*F.getParent())
119-
->getMMI();
120+
FunctionAnalysisManager &FAM =
121+
MFAM.getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF)
122+
.getManager();
120123
PreservedAnalyses PA = PreservedAnalyses::all();
121124
for (auto &Pass : Passes) {
122125
if (!PI.runBeforePass<MachineFunction>(*Pass, MF))
123126
continue;
124127

125128
PreservedAnalyses PassPA = Pass->run(MF, MFAM);
126-
if (MMI.getMachineFunction(F)) {
127-
MFAM.invalidate(MF, PassPA);
129+
MFAM.invalidate(MF, PassPA);
130+
if (Pass->name() != FreeMachineFunctionPass::name()) {
128131
PI.runAfterPass(*Pass, MF, PassPA);
132+
PA.intersect(std::move(PassPA));
129133
} else {
130-
MFAM.clear(MF, F.getName());
131-
PI.runAfterPassInvalidated<MachineFunction>(*Pass, PassPA);
134+
PA.intersect(std::move(PassPA));
135+
FAM.invalidate(MF.getFunction(), PA);
136+
PI.runAfterPassInvalidated<MachineFunction>(*Pass, PA);
132137
}
133-
PA.intersect(std::move(PassPA));
134138
}
135139
return PA;
136140
}

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#include "llvm/CodeGen/JMCInstrumenter.h"
9393
#include "llvm/CodeGen/LowerEmuTLS.h"
9494
#include "llvm/CodeGen/MIRPrinter.h"
95+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
9596
#include "llvm/CodeGen/MachinePassManager.h"
9697
#include "llvm/CodeGen/SafeStack.h"
9798
#include "llvm/CodeGen/SelectOptimize.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ FUNCTION_ANALYSIS("gc-function", GCFunctionAnalysis())
258258
FUNCTION_ANALYSIS("inliner-size-estimator", InlineSizeEstimatorAnalysis())
259259
FUNCTION_ANALYSIS("lazy-value-info", LazyValueAnalysis())
260260
FUNCTION_ANALYSIS("loops", LoopAnalysis())
261+
FUNCTION_ANALYSIS(
262+
"machine-function-info",
263+
MachineFunctionAnalysis(static_cast<const LLVMTargetMachine *>(TM)))
261264
FUNCTION_ANALYSIS("memdep", MemoryDependenceAnalysis())
262265
FUNCTION_ANALYSIS("memoryssa", MemorySSAAnalysis())
263266
FUNCTION_ANALYSIS("no-op-function", NoOpFunctionAnalysis())

0 commit comments

Comments
 (0)