Skip to content

Commit 197b28c

Browse files
committed
[NewPM] Introduce MFAnalysisGetter for a common analysis getter
1 parent 68bcb36 commit 197b28c

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@
2121
#ifndef LLVM_CODEGEN_MACHINEPASSMANAGER_H
2222
#define LLVM_CODEGEN_MACHINEPASSMANAGER_H
2323

24+
#include "llvm/ADT/DenseMap.h"
2425
#include "llvm/ADT/FunctionExtras.h"
26+
#include "llvm/ADT/STLExtras.h"
2527
#include "llvm/ADT/SmallVector.h"
2628
#include "llvm/CodeGen/MachineFunction.h"
29+
#include "llvm/IR/Function.h"
2730
#include "llvm/IR/PassManager.h"
2831
#include "llvm/IR/PassManagerInternal.h"
32+
#include "llvm/Pass.h"
2933
#include "llvm/Support/Error.h"
34+
#include <memory>
35+
#include <type_traits>
36+
#include <utility>
3037

3138
namespace llvm {
3239
class Module;
@@ -236,6 +243,79 @@ using MachineFunctionPassManager = PassManager<MachineFunction>;
236243
/// preserve.
237244
PreservedAnalyses getMachineFunctionPassPreservedAnalyses();
238245

246+
/// For migrating to new pass manager
247+
/// Provides a common interface to fetch analyses instead of doing it twice in
248+
/// the *LegacyPass::runOnMachineFunction and NPM Pass::run NPM analyses must
249+
/// have the LegacyWrapper type to indicate which legacy analysis to run.
250+
///
251+
/// Outer analyses passes(Module or Function) can also be requested through
252+
/// `getAnalysis` or `getCachedAnalysis`.
253+
class MFAnalysisGetter {
254+
private:
255+
Pass *LegacyPass;
256+
MachineFunctionAnalysisManager *MFAM;
257+
258+
template <typename T>
259+
using type_of_run =
260+
typename function_traits<decltype(&T::run)>::template arg_t<0>;
261+
262+
template <typename T>
263+
static constexpr bool IsFunctionAnalysis =
264+
std::is_same_v<Function, type_of_run<T>>;
265+
266+
template <typename T>
267+
static constexpr bool IsModuleAnalysis =
268+
std::is_same_v<Module, type_of_run<T>>;
269+
270+
public:
271+
MFAnalysisGetter(Pass *LegacyPass) : LegacyPass(LegacyPass) {}
272+
MFAnalysisGetter(MachineFunctionAnalysisManager *MFAM) : MFAM(MFAM) {}
273+
274+
/// Outer analyses requested from NPM will be cached results and can be null
275+
template <typename AnalysisT>
276+
typename AnalysisT::Result *getAnalysis(MachineFunction &MF) {
277+
if (MFAM) {
278+
// need a proxy to get the result for outer analyses
279+
// this can return null
280+
if constexpr (IsModuleAnalysis<AnalysisT>)
281+
return MFAM->getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
282+
.getCachedResult<AnalysisT>(*MF.getFunction().getParent());
283+
else if constexpr (IsFunctionAnalysis<AnalysisT>) {
284+
return &MFAM->getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF)
285+
.getManager()
286+
.getResult<AnalysisT>(MF.getFunction());
287+
}
288+
return &MFAM->getResult<AnalysisT>(MF);
289+
}
290+
return &LegacyPass->getAnalysis<typename AnalysisT::LegacyWrapper>()
291+
.getResult();
292+
}
293+
294+
template <typename AnalysisT>
295+
typename AnalysisT::Result *getCachedAnalysis(MachineFunction &MF) {
296+
if (MFAM) {
297+
if constexpr (IsFunctionAnalysis<AnalysisT>) {
298+
return MFAM->getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF)
299+
.getManager()
300+
.getCachedResult<AnalysisT>(MF.getFunction());
301+
} else if constexpr (IsModuleAnalysis<AnalysisT>)
302+
return MFAM->getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
303+
.getCachedResult<AnalysisT>(*MF.getFunction().getParent());
304+
305+
return &MFAM->getCachedResult<AnalysisT>(MF);
306+
}
307+
308+
if (auto *P =
309+
LegacyPass->getAnalysisIfAvailable<AnalysisT::LegacyWrapper>())
310+
return &P->getResult();
311+
return nullptr;
312+
}
313+
314+
/// This is not intended to be used to invoke getAnalysis()
315+
Pass *getLegacyPass() const { return LegacyPass; }
316+
MachineFunctionAnalysisManager *getMFAM() const { return MFAM; }
317+
};
318+
239319
} // end namespace llvm
240320

241321
#endif // LLVM_CODEGEN_MACHINEPASSMANAGER_H

0 commit comments

Comments
 (0)