Skip to content

Commit 9014b0e

Browse files
ChengjunpPavelKopyl
authored andcommitted
[AA] Move Target Specific AA before BasicAA (#125965)
In this change, NVPTX AA is moved before Basic AA to potentially improve compile time. Additionally, it introduces a flag in the `ExternalAAWrapper` that allows other backends to run their target-specific AA passes before Basic AA, if desired. The change works for both New Pass Manager and Legacy Pass Manager. Original implementation by Princeton Ferro <[email protected]>
1 parent d3e3e7c commit 9014b0e

File tree

8 files changed

+77
-14
lines changed

8 files changed

+77
-14
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,18 @@ struct ExternalAAWrapperPass : ImmutablePass {
998998

999999
explicit ExternalAAWrapperPass(CallbackT CB);
10001000

1001+
/// Returns whether this external AA should run before Basic AA.
1002+
///
1003+
/// By default, external AA passes are run after Basic AA. If this returns
1004+
/// true, the external AA will be run before Basic AA during alias analysis.
1005+
///
1006+
/// For some targets, we prefer to run the external AA early to improve
1007+
/// compile time as it has more target-specific information. This is
1008+
/// particularly useful when the external AA can provide more precise results
1009+
/// than Basic AA so that Basic AA does not need to spend time recomputing
1010+
/// them.
1011+
virtual bool runEarly() { return false; }
1012+
10011013
void getAnalysisUsage(AnalysisUsage &AU) const override {
10021014
AU.setPreservesAll();
10031015
}

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ class TargetMachine {
371371
// TODO: Populate all pass names by using <Target>PassRegistry.def.
372372
virtual void registerPassBuilderCallbacks(PassBuilder &) {}
373373

374+
/// Allow the target to register early alias analyses (AA before BasicAA) with
375+
/// the AAManager for use with the new pass manager. Only affects the
376+
/// "default" AAManager.
377+
virtual void registerEarlyDefaultAliasAnalyses(AAManager &) {}
378+
374379
/// Allow the target to register alias analyses with the AAManager for use
375380
/// with the new pass manager. Only affects the "default" AAManager.
376381
virtual void registerDefaultAliasAnalyses(AAManager &) {}

llvm/lib/Analysis/AliasAnalysis.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,28 +837,49 @@ bool AAResultsWrapperPass::runOnFunction(Function &F) {
837837
AAR.reset(
838838
new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F)));
839839

840+
// Add any target-specific alias analyses that should be run early.
841+
auto *ExtWrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>();
842+
if (ExtWrapperPass && ExtWrapperPass->runEarly() && ExtWrapperPass->CB) {
843+
LLVM_DEBUG(dbgs() << "AAResults register Early ExternalAA: "
844+
<< ExtWrapperPass->getPassName() << "\n");
845+
ExtWrapperPass->CB(*this, F, *AAR);
846+
}
847+
840848
// BasicAA is always available for function analyses. Also, we add it first
841849
// so that it can trump TBAA results when it proves MustAlias.
842850
// FIXME: TBAA should have an explicit mode to support this and then we
843851
// should reconsider the ordering here.
844-
if (!DisableBasicAA)
852+
if (!DisableBasicAA) {
853+
LLVM_DEBUG(dbgs() << "AAResults register BasicAA\n");
845854
AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
855+
}
846856

847857
// Populate the results with the currently available AAs.
848-
if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
858+
if (auto *WrapperPass =
859+
getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) {
860+
LLVM_DEBUG(dbgs() << "AAResults register ScopedNoAliasAA\n");
849861
AAR->addAAResult(WrapperPass->getResult());
850-
if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
862+
}
863+
if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) {
864+
LLVM_DEBUG(dbgs() << "AAResults register TypeBasedAA\n");
851865
AAR->addAAResult(WrapperPass->getResult());
852-
if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
866+
}
867+
if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>()) {
868+
LLVM_DEBUG(dbgs() << "AAResults register GlobalsAA\n");
853869
AAR->addAAResult(WrapperPass->getResult());
854-
if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
870+
}
871+
if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>()) {
872+
LLVM_DEBUG(dbgs() << "AAResults register SCEVAA\n");
855873
AAR->addAAResult(WrapperPass->getResult());
874+
}
856875

857876
// If available, run an external AA providing callback over the results as
858877
// well.
859-
if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
860-
if (WrapperPass->CB)
861-
WrapperPass->CB(*this, F, *AAR);
878+
if (ExtWrapperPass && !ExtWrapperPass->runEarly() && ExtWrapperPass->CB) {
879+
LLVM_DEBUG(dbgs() << "AAResults register Late ExternalAA: "
880+
<< ExtWrapperPass->getPassName() << "\n");
881+
ExtWrapperPass->CB(*this, F, *AAR);
882+
}
862883

863884
// Analyses don't mutate the IR, so return false.
864885
return false;

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,10 @@ AAManager PassBuilder::buildDefaultAAPipeline() {
21812181
// The order in which these are registered determines their priority when
21822182
// being queried.
21832183

2184+
// Add any target-specific alias analyses that should be run early.
2185+
if (TM)
2186+
TM->registerEarlyDefaultAliasAnalyses(AA);
2187+
21842188
// First we register the basic alias analysis that provides the majority of
21852189
// per-function local AA logic. This is a stateless, on-demand local set of
21862190
// AA techniques.

llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,23 @@ class NVPTXAAWrapperPass : public ImmutablePass {
7979

8080
// Wrapper around ExternalAAWrapperPass so that the default
8181
// constructor gets the callback.
82+
// Note that NVPTXAA will run before BasicAA for compile time considerations.
8283
class NVPTXExternalAAWrapper : public ExternalAAWrapperPass {
8384
public:
8485
static char ID;
8586

87+
bool runEarly() override { return true; }
88+
8689
NVPTXExternalAAWrapper()
8790
: ExternalAAWrapperPass([](Pass &P, Function &, AAResults &AAR) {
8891
if (auto *WrapperPass =
8992
P.getAnalysisIfAvailable<NVPTXAAWrapperPass>())
9093
AAR.addAAResult(WrapperPass->getResult());
9194
}) {}
95+
96+
StringRef getPassName() const override {
97+
return "NVPTX Address space based Alias Analysis Wrapper";
98+
}
9299
};
93100

94101
ImmutablePass *createNVPTXAAWrapperPass();

llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ MachineFunctionInfo *NVPTXTargetMachine::createMachineFunctionInfo(
221221
F, STI);
222222
}
223223

224-
void NVPTXTargetMachine::registerDefaultAliasAnalyses(AAManager &AAM) {
224+
void NVPTXTargetMachine::registerEarlyDefaultAliasAnalyses(AAManager &AAM) {
225225
AAM.registerFunctionAnalysis<NVPTXAA>();
226226
}
227227

@@ -317,10 +317,7 @@ void NVPTXPassConfig::addIRPasses() {
317317
disablePass(&ShrinkWrapID);
318318

319319
addPass(createNVPTXAAWrapperPass());
320-
addPass(createExternalAAWrapperPass([](Pass &P, Function &, AAResults &AAR) {
321-
if (auto *WrapperPass = P.getAnalysisIfAvailable<NVPTXAAWrapperPass>())
322-
AAR.addAAResult(WrapperPass->getResult());
323-
}));
320+
addPass(createNVPTXExternalAAWrapperPass());
324321

325322
// NVVMReflectPass is added in addEarlyAsPossiblePasses, so hopefully running
326323
// it here does nothing. But since we need it for correctness when lowering

llvm/lib/Target/NVPTX/NVPTXTargetMachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class NVPTXTargetMachine : public LLVMTargetMachine {
6464
createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
6565
const TargetSubtargetInfo *STI) const override;
6666

67-
void registerDefaultAliasAnalyses(AAManager &AAM) override;
67+
void registerEarlyDefaultAliasAnalyses(AAManager &AAM) override;
6868

6969
void registerPassBuilderCallbacks(PassBuilder &PB) override;
7070

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -aa-pipeline=default -passes='require<aa>' -debug-pass-manager -disable-output -S < %s 2>&1 | FileCheck %s
3+
; RUN: llc --debug-only='aa' -o /dev/null %s 2>&1 | FileCheck %s -check-prefix=LEGACY
4+
5+
; In default AA pipeline, NVPTXAA should run before BasicAA to reduce compile time for NVPTX backend
6+
target triple = "nvptx64-nvidia-cuda"
7+
8+
; CHECK: Running analysis: NVPTXAA on foo
9+
; CHECK-NEXT: Running analysis: BasicAA on foo
10+
11+
; LEGACY: AAResults register Early ExternalAA: NVPTX Address space based Alias Analysis Wrapper
12+
; LEGACY-NEXT: AAResults register BasicAA
13+
define void @foo(){
14+
entry:
15+
ret void
16+
}
17+

0 commit comments

Comments
 (0)