Skip to content

Commit 9e66206

Browse files
authored
[Passes] Generalize ShouldRunExtraVectorPasses to allow re-use (NFCI). (#118323)
Generalize ShouldRunExtraVectorPasses to ShouldRunExtraPasses, to allow re-use for other transformations. PR: #118323
1 parent 06c831d commit 9e66206

File tree

5 files changed

+106
-68
lines changed

5 files changed

+106
-68
lines changed

llvm/include/llvm/Transforms/Scalar/SimpleLoopUnswitch.h

+9-34
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/Analysis/LoopAnalysisManager.h"
1414
#include "llvm/IR/PassManager.h"
1515
#include "llvm/Transforms/Scalar/LoopPassManager.h"
16+
#include "llvm/Transforms/Utils/ExtraPassManager.h"
1617

1718
namespace llvm {
1819

@@ -21,40 +22,6 @@ class Loop;
2122
class StringRef;
2223
class raw_ostream;
2324

24-
struct ShouldRunExtraSimpleLoopUnswitch
25-
: public AnalysisInfoMixin<ShouldRunExtraSimpleLoopUnswitch> {
26-
static AnalysisKey Key;
27-
struct Result {
28-
bool invalidate(Loop &L, const PreservedAnalyses &PA,
29-
LoopAnalysisManager::Invalidator &) {
30-
// Check whether the analysis has been explicitly invalidated. Otherwise,
31-
// it remains preserved.
32-
auto PAC = PA.getChecker<ShouldRunExtraSimpleLoopUnswitch>();
33-
return !PAC.preservedWhenStateless();
34-
}
35-
};
36-
37-
Result run(Loop &L, LoopAnalysisManager &AM,
38-
LoopStandardAnalysisResults &AR) {
39-
return Result();
40-
}
41-
42-
static bool isRequired() { return true; }
43-
};
44-
45-
struct ExtraSimpleLoopUnswitchPassManager : public LoopPassManager {
46-
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
47-
LoopStandardAnalysisResults &AR, LPMUpdater &U) {
48-
auto PA = PreservedAnalyses::all();
49-
if (AM.getCachedResult<ShouldRunExtraSimpleLoopUnswitch>(L))
50-
PA.intersect(LoopPassManager::run(L, AM, AR, U));
51-
PA.abandon<ShouldRunExtraSimpleLoopUnswitch>();
52-
return PA;
53-
}
54-
55-
static bool isRequired() { return true; }
56-
};
57-
5825
/// This pass transforms loops that contain branches or switches on loop-
5926
/// invariant conditions to have multiple loops. For example, it turns the left
6027
/// into the right code:
@@ -113,6 +80,14 @@ class SimpleLoopUnswitchPass : public PassInfoMixin<SimpleLoopUnswitchPass> {
11380
function_ref<StringRef(StringRef)> MapClassName2PassName);
11481
};
11582

83+
/// A marker analysis to determine if SimpleLoopUnswitch should run again on a
84+
/// given loop.
85+
struct ShouldRunExtraSimpleLoopUnswitch
86+
: public ShouldRunExtraPasses<ShouldRunExtraSimpleLoopUnswitch>,
87+
public AnalysisInfoMixin<ShouldRunExtraSimpleLoopUnswitch> {
88+
static AnalysisKey Key;
89+
};
90+
11691
} // end namespace llvm
11792

11893
#endif // LLVM_TRANSFORMS_SCALAR_SIMPLELOOPUNSWITCH_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===- ExtraFunctionPassManager.h - Run Optimizations on Demand -*- 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+
/// \file
9+
///
10+
/// This file provides a pass manager that only runs its passes if the
11+
/// provided marker analysis has been preserved, together with a class to
12+
/// define such a marker analysis.
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
16+
#define LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
17+
18+
#include "llvm/IR/PassManager.h"
19+
#include "llvm/Transforms/Scalar/LoopPassManager.h"
20+
21+
namespace llvm {
22+
23+
/// A marker analysis to determine if extra passes should be run on demand.
24+
/// Passes requesting extra transformations to run need to request and preserve
25+
/// this analysis.
26+
template <typename MarkerTy> struct ShouldRunExtraPasses {
27+
struct Result {
28+
bool invalidate(Function &F, const PreservedAnalyses &PA,
29+
FunctionAnalysisManager::Invalidator &) {
30+
// Check whether the analysis has been explicitly invalidated. Otherwise,
31+
// it remains preserved.
32+
auto PAC = PA.getChecker<MarkerTy>();
33+
return !PAC.preservedWhenStateless();
34+
}
35+
36+
bool invalidate(Loop &L, const PreservedAnalyses &PA,
37+
LoopAnalysisManager::Invalidator &) {
38+
// Check whether the analysis has been explicitly invalidated. Otherwise,
39+
// it remains preserved.
40+
auto PAC = PA.getChecker<MarkerTy>();
41+
return !PAC.preservedWhenStateless();
42+
}
43+
};
44+
45+
Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
46+
47+
Result run(Loop &L, LoopAnalysisManager &AM,
48+
LoopStandardAnalysisResults &AR) {
49+
return Result();
50+
}
51+
};
52+
53+
/// A pass manager to run a set of extra function passes if the
54+
/// ShouldRunExtraPasses marker analysis is present. This allows passes to
55+
/// request additional transformations on demand. An example is extra
56+
/// simplifications after loop-vectorization, if runtime checks have been added.
57+
template <typename MarkerTy>
58+
struct ExtraFunctionPassManager : public FunctionPassManager {
59+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
60+
auto PA = PreservedAnalyses::all();
61+
if (AM.getCachedResult<MarkerTy>(F))
62+
PA.intersect(FunctionPassManager::run(F, AM));
63+
PA.abandon<MarkerTy>();
64+
return PA;
65+
}
66+
};
67+
68+
/// A pass manager to run a set of extra loop passes if the MarkerTy analysis is
69+
/// present. This allows passes to request additional transformations on demand.
70+
/// An example is doing additional runs of SimpleLoopUnswitch.
71+
template <typename MarkerTy>
72+
struct ExtraLoopPassManager : public LoopPassManager {
73+
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
74+
LoopStandardAnalysisResults &AR, LPMUpdater &U) {
75+
auto PA = PreservedAnalyses::all();
76+
if (AM.getCachedResult<MarkerTy>(L))
77+
PA.intersect(LoopPassManager::run(L, AM, AR, U));
78+
PA.abandon<MarkerTy>();
79+
return PA;
80+
}
81+
};
82+
83+
} // namespace llvm
84+
85+
#endif // LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H

llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h

+8-32
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
#include "llvm/IR/PassManager.h"
6060
#include "llvm/Support/CommandLine.h"
61+
#include "llvm/Transforms/Utils/ExtraPassManager.h"
6162
#include <functional>
6263

6364
namespace llvm {
@@ -80,38 +81,6 @@ class TargetTransformInfo;
8081
extern cl::opt<bool> EnableLoopInterleaving;
8182
extern cl::opt<bool> EnableLoopVectorization;
8283

83-
/// A marker to determine if extra passes after loop vectorization should be
84-
/// run.
85-
struct ShouldRunExtraVectorPasses
86-
: public AnalysisInfoMixin<ShouldRunExtraVectorPasses> {
87-
static AnalysisKey Key;
88-
struct Result {
89-
bool invalidate(Function &F, const PreservedAnalyses &PA,
90-
FunctionAnalysisManager::Invalidator &) {
91-
// Check whether the analysis has been explicitly invalidated. Otherwise,
92-
// it remains preserved.
93-
auto PAC = PA.getChecker<ShouldRunExtraVectorPasses>();
94-
return !PAC.preservedWhenStateless();
95-
}
96-
};
97-
98-
Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
99-
};
100-
101-
/// A pass manager to run a set of extra function simplification passes after
102-
/// vectorization, if requested. LoopVectorize caches the
103-
/// ShouldRunExtraVectorPasses analysis to request extra simplifications, if
104-
/// they could be beneficial.
105-
struct ExtraVectorPassManager : public FunctionPassManager {
106-
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
107-
auto PA = PreservedAnalyses::all();
108-
if (AM.getCachedResult<ShouldRunExtraVectorPasses>(F))
109-
PA.intersect(FunctionPassManager::run(F, AM));
110-
PA.abandon<ShouldRunExtraVectorPasses>();
111-
return PA;
112-
}
113-
};
114-
11584
struct LoopVectorizeOptions {
11685
/// If false, consider all loops for interleaving.
11786
/// If true, only loops that explicitly request interleaving are considered.
@@ -201,6 +170,13 @@ void reportVectorizationFailure(const StringRef DebugMsg,
201170
const StringRef OREMsg, const StringRef ORETag,
202171
OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr);
203172

173+
/// A marker analysis to determine if extra passes should be run after loop
174+
/// vectorization.
175+
struct ShouldRunExtraVectorPasses
176+
: public ShouldRunExtraPasses<ShouldRunExtraVectorPasses>,
177+
public AnalysisInfoMixin<ShouldRunExtraVectorPasses> {
178+
static AnalysisKey Key;
179+
};
204180
} // end namespace llvm
205181

206182
#endif // LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H

llvm/lib/Passes/PassBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
#include "llvm/Transforms/Utils/DXILUpgrade.h"
312312
#include "llvm/Transforms/Utils/Debugify.h"
313313
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
314+
#include "llvm/Transforms/Utils/ExtraPassManager.h"
314315
#include "llvm/Transforms/Utils/FixIrreducible.h"
315316
#include "llvm/Transforms/Utils/HelloWorld.h"
316317
#include "llvm/Transforms/Utils/IRNormalizer.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
135135
#include "llvm/Transforms/Utils/CountVisits.h"
136136
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
137+
#include "llvm/Transforms/Utils/ExtraPassManager.h"
137138
#include "llvm/Transforms/Utils/InjectTLIMappings.h"
138139
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
139140
#include "llvm/Transforms/Utils/Mem2Reg.h"
@@ -660,7 +661,7 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
660661
LPM2.addPass(IndVarSimplifyPass());
661662

662663
{
663-
ExtraSimpleLoopUnswitchPassManager ExtraPasses;
664+
ExtraLoopPassManager<ShouldRunExtraSimpleLoopUnswitch> ExtraPasses;
664665
ExtraPasses.addPass(SimpleLoopUnswitchPass(/* NonTrivial */ Level ==
665666
OptimizationLevel::O3));
666667
LPM2.addPass(std::move(ExtraPasses));
@@ -1307,7 +1308,7 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
13071308
FPM.addPass(InstCombinePass());
13081309

13091310
if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) {
1310-
ExtraVectorPassManager ExtraPasses;
1311+
ExtraFunctionPassManager<ShouldRunExtraVectorPasses> ExtraPasses;
13111312
// At higher optimization levels, try to clean up any runtime overlap and
13121313
// alignment checks inserted by the vectorizer. We want to track correlated
13131314
// runtime checks for two inner loops in the same outer loop, fold any

0 commit comments

Comments
 (0)