LLVM 24.0.0git
LastRunTrackingAnalysis.h
Go to the documentation of this file.
1//===- LastRunTrackingAnalysis.h - Avoid running redundant pass -*- 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 is an analysis pass to track a set of passes that have been run, so that
10// we can avoid running a pass again if there is no change since the last run of
11// the pass.
12//
13// In this analysis we track a set of passes S for each function with the
14// following transition rules:
15// 1. If pass P makes changes, set S = {P}.
16// 2. If pass P doesn't make changes, set S = S + {P}.
17//
18// Before running a pass P which satisfies P(P(x)) == P(x), we check if P is in
19// S. If so, we skip this pass since we know that there will be no change.
20//
21// Notes:
22// 1. Some transform passes have parameters that may vary in the optimization
23// pipeline. We should check if parameters in current run is compatible with
24// that in the last run.
25// 2. This pass only tracks at the module/function level. Loop passes are not
26// supported for now.
27//
28//===----------------------------------------------------------------------===//
29
30#ifndef LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
31#define LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
32
33#include "llvm/ADT/DenseMap.h"
34#include "llvm/IR/PassManager.h"
36#include <functional>
37
38namespace llvm {
39
40struct AnalysisOptions;
41
42/// This class is used to track the last run of a set of module/function passes.
43/// Invalidation are conservatively handled by the pass manager if a pass
44/// doesn't explicitly preserve the result.
45/// If we want to skip a pass, we should define a unique ID \p PassID to
46/// identify the pass, which is usually a pointer to a static member. If a pass
47/// has parameters, they should be stored in a struct \p OptionT with a method
48/// bool isCompatibleWith(const OptionT& LastOpt) const to check compatibility.
49class LastRunTrackingInfo {
50public:
51 using PassID = const void *;
52 using OptionPtr = const void *;
53 // CompatibilityCheckFn is a closure that stores the parameters of last run.
54 using CompatibilityCheckFn = std::function<bool(OptionPtr)>;
55
56 /// Check if we should skip a pass.
57 /// \param ID The unique ID of the pass.
58 /// \param Opt The parameters of the pass. If the pass has no parameters, use
59 /// shouldSkip(PassID ID) instead.
60 /// \return True if we should skip the pass.
61 /// \sa shouldSkip(PassID ID)
62 template <typename OptionT>
63 bool shouldSkip(PassID ID, const OptionT &Opt) const {
64 return shouldSkipImpl(ID, &Opt);
65 }
66 bool shouldSkip(PassID ID) const { return shouldSkipImpl(ID, nullptr); }
67
68 /// Update the tracking info.
69 /// \param ID The unique ID of the pass.
70 /// \param Changed Whether the pass makes changes.
71 /// \param Opt The parameters of the pass. It must have the same type as the
72 /// parameters of the last run. If the pass has no parameters, use
73 /// update(PassID ID, bool Changed) instead.
74 /// \sa update(PassID ID, bool Changed)
75 template <typename OptionT>
76 void update(PassID ID, bool Changed, const OptionT &Opt) {
77 updateImpl(ID, Changed, [Opt](OptionPtr Ptr) {
78 return static_cast<const OptionT *>(Ptr)->isCompatibleWith(Opt);
79 });
80 }
81 void update(PassID ID, bool Changed) {
82 updateImpl(ID, Changed, CompatibilityCheckFn{});
83 }
84
85private:
87 explicit LastRunTrackingInfo(const AnalysisOptions &Opts) : Opts(Opts) {}
88
89 LLVM_ABI bool shouldSkipImpl(PassID ID, OptionPtr Ptr) const;
90 LLVM_ABI void updateImpl(PassID ID, bool Changed,
91 CompatibilityCheckFn CheckFn);
92
93 const AnalysisOptions &Opts;
95};
96
97/// A function/module analysis which provides an empty \c LastRunTrackingInfo.
108
109} // namespace llvm
110
111#endif // LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
A function/module analysis which provides an empty LastRunTrackingInfo.
LLVM_ABI LastRunTrackingInfo run(Function &F, FunctionAnalysisManager &)
This class is used to track the last run of a set of module/function passes.
std::function< bool(OptionPtr)> CompatibilityCheckFn
void update(PassID ID, bool Changed, const OptionT &Opt)
Update the tracking info.
bool shouldSkip(PassID ID, const OptionT &Opt) const
Check if we should skip a pass.
void update(PassID ID, bool Changed)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29