Skip to content

Commit 347c26b

Browse files
committed
[NewPM][CodeGen] Add MachineFunctionAnalysis
1 parent a9d7ad2 commit 347c26b

23 files changed

+294
-195
lines changed

llvm/include/llvm/CodeGen/FreeMachineFunction.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
/// This analysis create MachineFunction for given Function.
24+
/// To release the MachineFunction, users should invalidate it explicitly.
25+
class MachineFunctionAnalysis
26+
: public AnalysisInfoMixin<MachineFunctionAnalysis> {
27+
friend AnalysisInfoMixin<MachineFunctionAnalysis>;
28+
29+
static AnalysisKey Key;
30+
31+
const LLVMTargetMachine *TM;
32+
33+
public:
34+
class Result {
35+
std::unique_ptr<MachineFunction> MF;
36+
37+
public:
38+
Result(std::unique_ptr<MachineFunction> MF) : MF(std::move(MF)) {}
39+
MachineFunction &getMF() { return *MF; };
40+
bool invalidate(Function &, const PreservedAnalyses &PA,
41+
FunctionAnalysisManager::Invalidator &);
42+
};
43+
44+
MachineFunctionAnalysis(const LLVMTargetMachine *TM) : TM(TM){};
45+
Result run(Function &F, FunctionAnalysisManager &FAM);
46+
};
47+
48+
} // namespace llvm
49+
50+
#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: 22 additions & 19 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());
@@ -108,6 +112,15 @@ bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
108112
ModuleAnalysisManager::Invalidator &Inv);
109113
extern template class InnerAnalysisManagerProxy<MachineFunctionAnalysisManager,
110114
Module>;
115+
using MachineFunctionAnalysisManagerFunctionProxy =
116+
InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, Function>;
117+
118+
template <>
119+
bool MachineFunctionAnalysisManagerFunctionProxy::Result::invalidate(
120+
Function &F, const PreservedAnalyses &PA,
121+
FunctionAnalysisManager::Invalidator &Inv);
122+
extern template class InnerAnalysisManagerProxy<MachineFunctionAnalysisManager,
123+
Function>;
111124

112125
extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
113126
MachineFunction>;
@@ -129,16 +142,6 @@ class FunctionAnalysisManagerMachineFunctionProxy
129142
Arg.FAM = nullptr;
130143
}
131144

132-
~Result() {
133-
// FAM is cleared in a moved from state where there is nothing to do.
134-
if (!FAM)
135-
return;
136-
137-
// Clear out the analysis manager if we're being destroyed -- it means we
138-
// didn't even see an invalidate call when we got invalidated.
139-
FAM->clear();
140-
}
141-
142145
Result &operator=(Result &&RHS) {
143146
FAM = RHS.FAM;
144147
// We have to null out the analysis manager in the moved-from state
@@ -187,18 +190,18 @@ class FunctionAnalysisManagerMachineFunctionProxy
187190
FunctionAnalysisManager *FAM;
188191
};
189192

190-
class ModuleToMachineFunctionPassAdaptor
191-
: public PassInfoMixin<ModuleToMachineFunctionPassAdaptor> {
193+
class FunctionToMachineFunctionPassAdaptor
194+
: public PassInfoMixin<FunctionToMachineFunctionPassAdaptor> {
192195
public:
193196
using PassConceptT =
194197
detail::PassConcept<MachineFunction, MachineFunctionAnalysisManager>;
195198

196-
explicit ModuleToMachineFunctionPassAdaptor(
199+
explicit FunctionToMachineFunctionPassAdaptor(
197200
std::unique_ptr<PassConceptT> Pass)
198201
: Pass(std::move(Pass)) {}
199202

200-
/// Runs the function pass across every function in the module.
201-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
203+
/// Runs the function pass across every function in the function.
204+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
202205
void printPipeline(raw_ostream &OS,
203206
function_ref<StringRef(StringRef)> MapClassName2PassName);
204207

@@ -209,14 +212,14 @@ class ModuleToMachineFunctionPassAdaptor
209212
};
210213

211214
template <typename MachineFunctionPassT>
212-
ModuleToMachineFunctionPassAdaptor
213-
createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
215+
FunctionToMachineFunctionPassAdaptor
216+
createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
214217
using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
215218
MachineFunctionAnalysisManager>;
216219
// Do not use make_unique, it causes too many template instantiations,
217220
// causing terrible compile times.
218-
return ModuleToMachineFunctionPassAdaptor(
219-
std::unique_ptr<ModuleToMachineFunctionPassAdaptor::PassConceptT>(
221+
return FunctionToMachineFunctionPassAdaptor(
222+
std::unique_ptr<FunctionToMachineFunctionPassAdaptor::PassConceptT>(
220223
new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
221224
}
222225

llvm/include/llvm/IR/LLVMContext.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ class LLVMContext {
155155
void enableDebugTypeODRUniquing();
156156
void disableDebugTypeODRUniquing();
157157

158+
/// generateMachineFunctionNum - Get a unique number for MachineFunction
159+
/// that associated with the given Function.
160+
unsigned generateMachineFunctionNum(Function &);
161+
158162
/// Defines the type of a yield callback.
159163
/// \see LLVMContext::setYieldCallback.
160164
using YieldCallbackTy = void (*)(LLVMContext *Context, void *OpaqueHandle);
@@ -332,7 +336,7 @@ class LLVMContext {
332336
void addModule(Module*);
333337

334338
/// removeModule - Unregister a module from this context.
335-
void removeModule(Module*);
339+
void removeModule(Module *);
336340
};
337341

338342
// Create wrappers for C Binding types (see CBindingWrapping.h).

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "llvm/CodeGen/DwarfEHPrepare.h"
3030
#include "llvm/CodeGen/ExpandMemCmp.h"
3131
#include "llvm/CodeGen/ExpandReductions.h"
32-
#include "llvm/CodeGen/FreeMachineFunction.h"
3332
#include "llvm/CodeGen/GCMetadata.h"
3433
#include "llvm/CodeGen/GlobalMerge.h"
3534
#include "llvm/CodeGen/IndirectBrExpand.h"
@@ -38,6 +37,7 @@
3837
#include "llvm/CodeGen/JMCInstrumenter.h"
3938
#include "llvm/CodeGen/LowerEmuTLS.h"
4039
#include "llvm/CodeGen/MIRPrinter.h"
40+
#include "llvm/CodeGen/MachineModuleInfo.h"
4141
#include "llvm/CodeGen/MachinePassManager.h"
4242
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
4343
#include "llvm/CodeGen/ReplaceWithVeclib.h"
@@ -200,7 +200,8 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
200200
: MPM(MPM), PB(PB) {}
201201
~AddMachinePass() {
202202
if (!MFPM.isEmpty())
203-
MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
203+
MPM.addPass(createModuleToFunctionPassAdaptor(
204+
createFunctionToMachineFunctionPassAdaptor(std::move(MFPM))));
204205
}
205206

206207
template <typename PassT>
@@ -219,8 +220,8 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
219220
} else {
220221
// Add Module Pass
221222
if (!MFPM.isEmpty()) {
222-
MPM.addPass(
223-
createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
223+
MPM.addPass(createModuleToFunctionPassAdaptor(
224+
createFunctionToMachineFunctionPassAdaptor(std::move(MFPM))));
224225
MFPM = MachineFunctionPassManager();
225226
}
226227

@@ -512,6 +513,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
512513

513514
{
514515
AddIRPass addIRPass(MPM, derived());
516+
addIRPass(RequireAnalysisPass<MachineModuleAnalysis, Module>());
515517
addIRPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
516518
addIRPass(RequireAnalysisPass<CollectorMetadataAnalysis, Module>());
517519
addISelPasses(addIRPass);
@@ -538,7 +540,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
538540
if (PrintMIR)
539541
addPass(PrintMIRPass(Out), /*Force=*/true);
540542

541-
addPass(FreeMachineFunctionPass());
543+
// TODO: invalidate MachineFunctionAnalysis
542544
return verifyStartStop(*StartStopInfo);
543545
}
544546

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ add_llvm_component_library(LLVMCodeGen
6565
FEntryInserter.cpp
6666
FinalizeISel.cpp
6767
FixupStatepointCallerSaved.cpp
68-
FreeMachineFunction.cpp
6968
FuncletLayout.cpp
69+
MachineFunctionAnalysis.cpp
7070
GCMetadata.cpp
7171
GCMetadataPrinter.cpp
7272
GCRootLowering.cpp

llvm/lib/CodeGen/FreeMachineFunction.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)