LLVM 20.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"
35#include <functional>
36
37namespace llvm {
38
39/// This class is used to track the last run of a set of module/function passes.
40/// Invalidation are conservatively handled by the pass manager if a pass
41/// doesn't explicitly preserve the result.
42/// If we want to skip a pass, we should define a unique ID \p PassID to
43/// identify the pass, which is usually a pointer to a static member. If a pass
44/// has parameters, they should be stored in a struct \p OptionT with a method
45/// bool isCompatibleWith(const OptionT& LastOpt) const to check compatibility.
47public:
48 using PassID = const void *;
49 using OptionPtr = const void *;
50 // CompatibilityCheckFn is a closure that stores the parameters of last run.
51 using CompatibilityCheckFn = std::function<bool(OptionPtr)>;
52
53 /// Check if we should skip a pass.
54 /// \param ID The unique ID of the pass.
55 /// \param Opt The parameters of the pass. If the pass has no parameters, use
56 /// shouldSkip(PassID ID) instead.
57 /// \return True if we should skip the pass.
58 /// \sa shouldSkip(PassID ID)
59 template <typename OptionT>
60 bool shouldSkip(PassID ID, const OptionT &Opt) const {
61 return shouldSkipImpl(ID, &Opt);
62 }
63 bool shouldSkip(PassID ID) const { return shouldSkipImpl(ID, nullptr); }
64
65 /// Update the tracking info.
66 /// \param ID The unique ID of the pass.
67 /// \param Changed Whether the pass makes changes.
68 /// \param Opt The parameters of the pass. It must have the same type as the
69 /// parameters of the last run. If the pass has no parameters, use
70 /// update(PassID ID, bool Changed) instead.
71 /// \sa update(PassID ID, bool Changed)
72 template <typename OptionT>
73 void update(PassID ID, bool Changed, const OptionT &Opt) {
74 updateImpl(ID, Changed, [Opt](OptionPtr Ptr) {
75 return static_cast<const OptionT *>(Ptr)->isCompatibleWith(Opt);
76 });
77 }
78 void update(PassID ID, bool Changed) {
79 updateImpl(ID, Changed, CompatibilityCheckFn{});
80 }
81
82private:
83 bool shouldSkipImpl(PassID ID, OptionPtr Ptr) const;
84 void updateImpl(PassID ID, bool Changed, CompatibilityCheckFn CheckFn);
85
87};
88
89/// A function/module analysis which provides an empty \c LastRunTrackingInfo.
91 : public AnalysisInfoMixin<LastRunTrackingAnalysis> {
93 static AnalysisKey Key;
94
95public:
98 return LastRunTrackingInfo();
99 }
101 return LastRunTrackingInfo();
102 }
103};
104
105} // namespace llvm
106
107#endif // LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition: MD5.cpp:55
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
A function/module analysis which provides an empty LastRunTrackingInfo.
LastRunTrackingInfo run(Module &M, ModuleAnalysisManager &)
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
bool shouldSkip(PassID ID) const
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:65
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28