Skip to content

Commit 8df7be7

Browse files
committed
Passes] Generalize ShouldRunExtraVectorPasses to allow re-use (NFCI).
1 parent 81d82ca commit 8df7be7

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- ExtraPassManager.h - Loop pass management -----------------*- C++
2+
//-*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
/// \file
10+
///
11+
/// This file provides a pass manager that only runs its passes if the
12+
/// provided marker analysis has been preserved, together with a class to
13+
/// define such a marker analysis.
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
17+
#define LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
18+
19+
#include "llvm/IR/PassManager.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+
37+
Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
38+
};
39+
40+
/// A pass manager to run a set of extra function passes if the
41+
/// ShouldRunExtraPasses marker analysis is present. This allows passes to
42+
/// request additional transformations on demand. An example is extra
43+
/// simplifications after loop-vectorization, if runtime checks have been added.
44+
template <typename MarkerTy>
45+
struct ExtraPassManager : public FunctionPassManager {
46+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
47+
auto PA = PreservedAnalyses::all();
48+
if (AM.getCachedResult<MarkerTy>(F))
49+
PA.intersect(FunctionPassManager::run(F, AM));
50+
PA.abandon<MarkerTy>();
51+
return PA;
52+
}
53+
};
54+
} // namespace llvm
55+
56+
#endif // LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H

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

Lines changed: 8 additions & 32 deletions
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 analyss 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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
#include "llvm/Transforms/Utils/DXILUpgrade.h"
311311
#include "llvm/Transforms/Utils/Debugify.h"
312312
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
313+
#include "llvm/Transforms/Utils/ExtraPassManager.h"
313314
#include "llvm/Transforms/Utils/FixIrreducible.h"
314315
#include "llvm/Transforms/Utils/HelloWorld.h"
315316
#include "llvm/Transforms/Utils/IRNormalizer.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 2 additions & 1 deletion
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"
@@ -1307,7 +1308,7 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
13071308
FPM.addPass(InstCombinePass());
13081309

13091310
if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) {
1310-
ExtraVectorPassManager ExtraPasses;
1311+
ExtraPassManager<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

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
#include "llvm/Support/NativeFormatting.h"
135135
#include "llvm/Support/raw_ostream.h"
136136
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
137+
#include "llvm/Transforms/Utils/ExtraPassManager.h"
137138
#include "llvm/Transforms/Utils/InjectTLIMappings.h"
138139
#include "llvm/Transforms/Utils/Local.h"
139140
#include "llvm/Transforms/Utils/LoopSimplify.h"

0 commit comments

Comments
 (0)