mirror of
https://github.com/intel/llvm.git
synced 2026-01-13 02:38:07 +08:00
Reapply of a22d1c2225. Using this PR for
pre-merge CI.
Instead of relying on any pass manager to schedule Polly's passes, add
Polly's own pipeline manager which is seen as a monolithic pass in
LLVM's pass manager. Polly's former passes are now phases of the new
PhaseManager component.
Relying on LLVM's pass manager (the legacy as well as the New Pass
Manager) to manage Polly's phases never was a good fit that the
PhaseManager resolves:
* Polly passes were modifying analysis results, in particular RegionInfo
and ScopInfo. This means that there was not just one unique and
"definite" analysis result, the actual result depended on which analyses
ran prior, and the pass manager was not allowed to throw away cached
analyses or prior SCoP optimizations would have been forgotten. The LLVM
pass manger's persistance of analysis results is not contractual but
designed for caching.
* Polly depends on a particular execution order of passes and regions
(e.g. regression tests, invalidation of consecutive SCoPs). LLVM's pass
manager does not guarantee any excecution order.
* Polly does not completely preserve DominatorTree, RegionInfo,
LoopInfo, or ScalarEvolution, but only as-needed for Polly's own uses.
Because the ScopDetection object stores references to those analyses, it
still had to lie to the pass manager that they would be preserved, or
the pass manager would have released and recomputed the invalidated
analysis objects that ScopDetection/ScopInfo was still referencing. To
ensure that no non-Polly pass would see these not-completely-preserved
analyses, all analyses still had to be thrown away after the
ScopPassManager, respectively with a BarrierNoopPass in case of the LPM.
* The NPM's PassInstrumentation wraps the IR unit into an `llvm::Any`
object, but implementations such as PrintIRInstrumentation call
llvm_unreachable on encountering an unknown IR unit, such as SCoPs, with
no extension points to add support. Hence LLVM crashes when dumping IR
between SCoP passes (such as `-print-before-changed` with Polly being
active).
The new PhaseManager uses some command line options that previously
belonged to Polly's legacy passes, such as `-polly-print-detect` (so the
option will continue to work). Hence the LPM support is incompatible
with the new approach and support for it is removed.
169 lines
5.7 KiB
C++
169 lines
5.7 KiB
C++
//===- DeadCodeElimination.cpp - Eliminate dead iteration ----------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// The polyhedral dead code elimination pass analyses a SCoP to eliminate
|
|
// statement instances that can be proven dead.
|
|
// As a consequence, the code generated for this SCoP may execute a statement
|
|
// less often. This means, a statement may be executed only in certain loop
|
|
// iterations or it may not even be part of the generated code at all.
|
|
//
|
|
// This code:
|
|
//
|
|
// for (i = 0; i < N; i++)
|
|
// arr[i] = 0;
|
|
// for (i = 0; i < N; i++)
|
|
// arr[i] = 10;
|
|
// for (i = 0; i < N; i++)
|
|
// arr[i] = i;
|
|
//
|
|
// is e.g. simplified to:
|
|
//
|
|
// for (i = 0; i < N; i++)
|
|
// arr[i] = i;
|
|
//
|
|
// The idea and the algorithm used was first implemented by Sven Verdoolaege in
|
|
// the 'ppcg' tool.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "polly/DeadCodeElimination.h"
|
|
#include "polly/DependenceInfo.h"
|
|
#include "polly/Options.h"
|
|
#include "polly/ScopInfo.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "isl/isl-noexceptions.h"
|
|
|
|
using namespace llvm;
|
|
using namespace polly;
|
|
|
|
namespace {
|
|
|
|
cl::opt<int> DCEPreciseSteps(
|
|
"polly-dce-precise-steps",
|
|
cl::desc("The number of precise steps between two approximating "
|
|
"iterations. (A value of -1 schedules another approximation stage "
|
|
"before the actual dead code elimination."),
|
|
cl::init(-1), cl::cat(PollyCategory));
|
|
|
|
/// Return the set of live iterations.
|
|
///
|
|
/// The set of live iterations are all iterations that write to memory and for
|
|
/// which we can not prove that there will be a later write that _must_
|
|
/// overwrite the same memory location and is consequently the only one that
|
|
/// is visible after the execution of the SCoP.
|
|
///
|
|
/// To compute the live outs, we compute for the data-locations that are
|
|
/// must-written to the last statement that touches these locations. On top of
|
|
/// this we add all statements that perform may-write accesses.
|
|
///
|
|
/// We could be more precise by removing may-write accesses for which we know
|
|
/// that they are overwritten by a must-write after. However, at the moment the
|
|
/// only may-writes we introduce access the full (unbounded) array, such that
|
|
/// bounded write accesses can not overwrite all of the data-locations. As
|
|
/// this means may-writes are in the current situation always live, there is
|
|
/// no point in trying to remove them from the live-out set.
|
|
static isl::union_set getLiveOut(Scop &S) {
|
|
isl::union_map Schedule = S.getSchedule();
|
|
isl::union_map MustWrites = S.getMustWrites();
|
|
isl::union_map WriteIterations = MustWrites.reverse();
|
|
isl::union_map WriteTimes = WriteIterations.apply_range(Schedule);
|
|
|
|
isl::union_map LastWriteTimes = WriteTimes.lexmax();
|
|
isl::union_map LastWriteIterations =
|
|
LastWriteTimes.apply_range(Schedule.reverse());
|
|
|
|
isl::union_set Live = LastWriteIterations.range();
|
|
isl::union_map MayWrites = S.getMayWrites();
|
|
Live = Live.unite(MayWrites.domain());
|
|
return Live.coalesce();
|
|
}
|
|
|
|
/// Performs polyhedral dead iteration elimination by:
|
|
/// o Assuming that the last write to each location is live.
|
|
/// o Following each RAW dependency from a live iteration backwards and adding
|
|
/// that iteration to the live set.
|
|
///
|
|
/// To ensure the set of live iterations does not get too complex we always
|
|
/// combine a certain number of precise steps with one approximating step that
|
|
/// simplifies the life set with an affine hull.
|
|
static bool runDeadCodeElimination(Scop &S, int PreciseSteps,
|
|
const Dependences &D) {
|
|
if (!D.hasValidDependences())
|
|
return false;
|
|
|
|
isl::union_set Live = getLiveOut(S);
|
|
isl::union_map Dep =
|
|
D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED);
|
|
Dep = Dep.reverse();
|
|
|
|
if (PreciseSteps == -1)
|
|
Live = Live.affine_hull();
|
|
|
|
isl::union_set OriginalDomain = S.getDomains();
|
|
int Steps = 0;
|
|
while (true) {
|
|
Steps++;
|
|
|
|
isl::union_set Extra = Live.apply(Dep);
|
|
|
|
if (Extra.is_subset(Live))
|
|
break;
|
|
|
|
Live = Live.unite(Extra);
|
|
|
|
if (Steps > PreciseSteps) {
|
|
Steps = 0;
|
|
Live = Live.affine_hull();
|
|
}
|
|
|
|
Live = Live.intersect(OriginalDomain);
|
|
}
|
|
|
|
Live = Live.coalesce();
|
|
|
|
return S.restrictDomains(Live);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool polly::runDeadCodeElim(Scop &S, DependenceAnalysis::Result &DA) {
|
|
const Dependences &Deps = DA.getDependences(Dependences::AL_Statement);
|
|
|
|
bool Changed = runDeadCodeElimination(S, DCEPreciseSteps, Deps);
|
|
|
|
// FIXME: We can probably avoid the recomputation of all dependences by
|
|
// updating them explicitly.
|
|
if (Changed)
|
|
DA.recomputeDependences(Dependences::AL_Statement);
|
|
|
|
return Changed;
|
|
}
|
|
|
|
llvm::PreservedAnalyses DeadCodeElimPass::run(Scop &S, ScopAnalysisManager &SAM,
|
|
ScopStandardAnalysisResults &SAR,
|
|
SPMUpdater &U) {
|
|
DependenceAnalysis::Result &DA = SAM.getResult<DependenceAnalysis>(S, SAR);
|
|
const Dependences &Deps = DA.getDependences(Dependences::AL_Statement);
|
|
|
|
bool Changed = runDeadCodeElimination(S, DCEPreciseSteps, Deps);
|
|
|
|
// FIXME: We can probably avoid the recomputation of all dependences by
|
|
// updating them explicitly.
|
|
if (Changed)
|
|
DA.recomputeDependences(Dependences::AL_Statement);
|
|
|
|
if (!Changed)
|
|
return PreservedAnalyses::all();
|
|
|
|
PreservedAnalyses PA;
|
|
PA.preserveSet<AllAnalysesOn<Module>>();
|
|
PA.preserveSet<AllAnalysesOn<Function>>();
|
|
PA.preserveSet<AllAnalysesOn<Loop>>();
|
|
return PA;
|
|
}
|