Skip to content

Commit 80fa6e1

Browse files
authored
[DropAssumes] Drop dereferenceable assumptions after vectorization. (#166947)
This patch adds another run of DropUnnecessaryAssumes after vectorization, to clean up assumes that are not longer needed after this point. The main example of such an assume is currently dereferenceable assumptions. This complements #166945, which avoids sinking code if it would mean remove a dereferenceable assumption. There are a few additional cases where some unneeded assumes are left over after vectorization that also get cleaned up. The main motivation is to work together with #166945, but there may be a better solution. Adding another instance of this pass to the pipeline is not great, but compile-time impact seems in the noise: https://llvm-compile-time-tracker.com/compare.php?from=55e71fe08b6406ec7ce2c81ce042e48717acf204&to=85da4ee3a74126f557cdc74c7b40e048dacb3fc4&stat=instructions:u PR: #166947
1 parent 2d1d5fe commit 80fa6e1

File tree

14 files changed

+95
-79
lines changed

14 files changed

+95
-79
lines changed

llvm/include/llvm/Passes/PassBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ class PassBuilder {
742742
void addRequiredLTOPreLinkPasses(ModulePassManager &MPM);
743743

744744
void addVectorPasses(OptimizationLevel Level, FunctionPassManager &FPM,
745-
bool IsFullLTO);
745+
ThinOrFullLTOPhase LTOPhase);
746746

747747
static std::optional<std::vector<PipelineElement>>
748748
parsePipelineText(StringRef Text);

llvm/include/llvm/Transforms/Scalar/DropUnnecessaryAssumes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ namespace llvm {
1919

2020
struct DropUnnecessaryAssumesPass
2121
: public PassInfoMixin<DropUnnecessaryAssumesPass> {
22+
DropUnnecessaryAssumesPass(bool DropDereferenceable = false)
23+
: DropDereferenceable(DropDereferenceable) {}
24+
2225
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
26+
27+
private:
28+
bool DropDereferenceable;
2329
};
2430

2531
} // end namespace llvm

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,11 @@ Expected<bool> parseEntryExitInstrumenterPassOptions(StringRef Params) {
900900
"EntryExitInstrumenter");
901901
}
902902

903+
Expected<bool> parseDropUnnecessaryAssumesPassOptions(StringRef Params) {
904+
return PassBuilder::parseSinglePassOption(Params, "drop-deref",
905+
"DropUnnecessaryAssumes");
906+
}
907+
903908
Expected<bool> parseLoopExtractorPassOptions(StringRef Params) {
904909
return PassBuilder::parseSinglePassOption(Params, "single", "LoopExtractor");
905910
}

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,10 +1298,18 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
12981298

12991299
/// TODO: Should LTO cause any differences to this set of passes?
13001300
void PassBuilder::addVectorPasses(OptimizationLevel Level,
1301-
FunctionPassManager &FPM, bool IsFullLTO) {
1301+
FunctionPassManager &FPM,
1302+
ThinOrFullLTOPhase LTOPhase) {
1303+
const bool IsFullLTO = LTOPhase == ThinOrFullLTOPhase::FullLTOPostLink;
1304+
13021305
FPM.addPass(LoopVectorizePass(
13031306
LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization)));
13041307

1308+
// Drop dereferenceable assumes after vectorization, as they are no longer
1309+
// needed and can inhibit further optimization.
1310+
if (!isLTOPreLink(LTOPhase))
1311+
FPM.addPass(DropUnnecessaryAssumesPass(/*DropDereferenceable=*/true));
1312+
13051313
FPM.addPass(InferAlignmentPass());
13061314
if (IsFullLTO) {
13071315
// The vectorizer may have significantly shortened a loop body; unroll
@@ -1572,7 +1580,7 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
15721580
// from the TargetLibraryInfo.
15731581
OptimizePM.addPass(InjectTLIMappings());
15741582

1575-
addVectorPasses(Level, OptimizePM, /* IsFullLTO */ false);
1583+
addVectorPasses(Level, OptimizePM, LTOPhase);
15761584

15771585
invokeVectorizerEndEPCallbacks(OptimizePM, Level);
15781586

@@ -2162,7 +2170,7 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
21622170

21632171
MainFPM.addPass(LoopDistributePass());
21642172

2165-
addVectorPasses(Level, MainFPM, /* IsFullLTO */ true);
2173+
addVectorPasses(Level, MainFPM, ThinOrFullLTOPhase::FullLTOPostLink);
21662174

21672175
invokeVectorizerEndEPCallbacks(MainFPM, Level);
21682176

llvm/lib/Passes/PassRegistry.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ FUNCTION_PASS("dot-post-dom", PostDomPrinter())
432432
FUNCTION_PASS("dot-post-dom-only", PostDomOnlyPrinter())
433433
FUNCTION_PASS("dse", DSEPass())
434434
FUNCTION_PASS("dwarf-eh-prepare", DwarfEHPreparePass(*TM))
435-
FUNCTION_PASS("drop-unnecessary-assumes", DropUnnecessaryAssumesPass())
436435
FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass(*TM))
437436
FUNCTION_PASS("expand-memcmp", ExpandMemCmpPass(*TM))
438437
FUNCTION_PASS("expand-reductions", ExpandReductionsPass())
@@ -584,6 +583,10 @@ FUNCTION_PASS_WITH_PARAMS(
584583
"early-cse", "EarlyCSEPass",
585584
[](bool UseMemorySSA) { return EarlyCSEPass(UseMemorySSA); },
586585
parseEarlyCSEPassOptions, "memssa")
586+
FUNCTION_PASS_WITH_PARAMS(
587+
"drop-unnecessary-assumes", "DropUnnecessaryAssumesPass",
588+
[](bool DropDereferenceable) { return DropUnnecessaryAssumesPass(DropDereferenceable); },
589+
parseDropUnnecessaryAssumesPassOptions, "drop-deref")
587590
FUNCTION_PASS_WITH_PARAMS(
588591
"ee-instrument", "EntryExitInstrumenterPass",
589592
[](bool PostInlining) { return EntryExitInstrumenterPass(PostInlining); },

llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ DropUnnecessaryAssumesPass::run(Function &F, FunctionAnalysisManager &FAM) {
7878
SmallVector<OperandBundleDef> KeptBundles;
7979
unsigned NumBundles = Assume->getNumOperandBundles();
8080
for (unsigned I = 0; I != NumBundles; ++I) {
81-
auto IsDead = [](OperandBundleUse Bundle) {
81+
auto IsDead = [&](OperandBundleUse Bundle) {
8282
// "ignore" operand bundles are always dead.
8383
if (Bundle.getTagName() == "ignore")
8484
return true;
8585

86+
// "dereferenceable" operand bundles are only dropped if requested
87+
// (e.g., after loop vectorization has run).
88+
if (Bundle.getTagName() == "dereferenceable")
89+
return DropDereferenceable;
90+
8691
// Bundles without arguments do not affect any specific values.
8792
// Always keep them for now.
8893
if (Bundle.Inputs.empty())

llvm/test/Other/new-pm-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
; CHECK-O-NEXT: Running analysis: LoopAccessAnalysis on foo
262262
; CHECK-O-NEXT: Running pass: InjectTLIMappings
263263
; CHECK-O-NEXT: Running pass: LoopVectorizePass
264+
; CHECK-DEFAULT-NEXT: Running pass: DropUnnecessaryAssumesPass
264265
; CHECK-O-NEXT: Running pass: InferAlignmentPass
265266
; CHECK-O-NEXT: Running pass: LoopLoadEliminationPass
266267
; CHECK-O-NEXT: Running pass: InstCombinePass

llvm/test/Other/new-pm-lto-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
; CHECK-O23SZ-NEXT: Running analysis: LoopAccessAnalysis on foo
130130
; CHECK-O23SZ-NEXT: Running pass: LoopVectorizePass on foo
131131
; CHECK-O23SZ-NEXT: Running analysis: DemandedBitsAnalysis on foo
132+
; CHECK-O23SZ-NEXT: Running pass: DropUnnecessaryAssumesPass on foo
132133
; CHECK-O23SZ-NEXT: Running pass: InferAlignmentPass on foo
133134
; CHECK-O23SZ-NEXT: Running pass: LoopUnrollPass on foo
134135
; CHECK-O23SZ-NEXT: WarnMissedTransformationsPass on foo

llvm/test/Other/new-pm-thinlto-postlink-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
; CHECK-POSTLINK-O-NEXT: Running analysis: LoopAccessAnalysis on foo
181181
; CHECK-POSTLINK-O-NEXT: Running pass: InjectTLIMappings
182182
; CHECK-POSTLINK-O-NEXT: Running pass: LoopVectorizePass
183+
; CHECK-POSTLINK-O-NEXT: Running pass: DropUnnecessaryAssumesPass
183184
; CHECK-POSTLINK-O-NEXT: Running pass: InferAlignmentPass
184185
; CHECK-POSTLINK-O-NEXT: Running pass: LoopLoadEliminationPass
185186
; CHECK-POSTLINK-O-NEXT: Running pass: InstCombinePass

llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
; CHECK-O-NEXT: Running analysis: LoopAccessAnalysis on foo
166166
; CHECK-O-NEXT: Running pass: InjectTLIMappings
167167
; CHECK-O-NEXT: Running pass: LoopVectorizePass
168+
; CHECK-O-NEXT: Running pass: DropUnnecessaryAssumesPass
168169
; CHECK-O-NEXT: Running pass: InferAlignmentPass
169170
; CHECK-O-NEXT: Running pass: LoopLoadEliminationPass
170171
; CHECK-O-NEXT: Running pass: InstCombinePass

0 commit comments

Comments
 (0)