Skip to content

Commit 94d9336

Browse files
authored
[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 5d7e8ac commit 94d9336

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
@@ -1013,6 +1013,18 @@ struct ExternalAAWrapperPass : ImmutablePass {
10131013

10141014
explicit ExternalAAWrapperPass(CallbackT CB);
10151015

1016+
/// Returns whether this external AA should run before Basic AA.
1017+
///
1018+
/// By default, external AA passes are run after Basic AA. If this returns
1019+
/// true, the external AA will be run before Basic AA during alias analysis.
1020+
///
1021+
/// For some targets, we prefer to run the external AA early to improve
1022+
/// compile time as it has more target-specific information. This is
1023+
/// particularly useful when the external AA can provide more precise results
1024+
/// than Basic AA so that Basic AA does not need to spend time recomputing
1025+
/// them.
1026+
virtual bool runEarly() { return false; }
1027+
10161028
void getAnalysisUsage(AnalysisUsage &AU) const override {
10171029
AU.setPreservesAll();
10181030
}

llvm/include/llvm/Target/TargetMachine.h

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

399+
/// Allow the target to register early alias analyses (AA before BasicAA) with
400+
/// the AAManager for use with the new pass manager. Only affects the
401+
/// "default" AAManager.
402+
virtual void registerEarlyDefaultAliasAnalyses(AAManager &) {}
403+
399404
/// Allow the target to register alias analyses with the AAManager for use
400405
/// with the new pass manager. Only affects the "default" AAManager.
401406
virtual void registerDefaultAliasAnalyses(AAManager &) {}

llvm/lib/Analysis/AliasAnalysis.cpp

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

742+
// Add any target-specific alias analyses that should be run early.
743+
auto *ExtWrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>();
744+
if (ExtWrapperPass && ExtWrapperPass->runEarly() && ExtWrapperPass->CB) {
745+
LLVM_DEBUG(dbgs() << "AAResults register Early ExternalAA: "
746+
<< ExtWrapperPass->getPassName() << "\n");
747+
ExtWrapperPass->CB(*this, F, *AAR);
748+
}
749+
742750
// BasicAA is always available for function analyses. Also, we add it first
743751
// so that it can trump TBAA results when it proves MustAlias.
744752
// FIXME: TBAA should have an explicit mode to support this and then we
745753
// should reconsider the ordering here.
746-
if (!DisableBasicAA)
754+
if (!DisableBasicAA) {
755+
LLVM_DEBUG(dbgs() << "AAResults register BasicAA\n");
747756
AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
757+
}
748758

749759
// Populate the results with the currently available AAs.
750-
if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
760+
if (auto *WrapperPass =
761+
getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) {
762+
LLVM_DEBUG(dbgs() << "AAResults register ScopedNoAliasAA\n");
751763
AAR->addAAResult(WrapperPass->getResult());
752-
if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
764+
}
765+
if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) {
766+
LLVM_DEBUG(dbgs() << "AAResults register TypeBasedAA\n");
753767
AAR->addAAResult(WrapperPass->getResult());
754-
if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
768+
}
769+
if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>()) {
770+
LLVM_DEBUG(dbgs() << "AAResults register GlobalsAA\n");
755771
AAR->addAAResult(WrapperPass->getResult());
756-
if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
772+
}
773+
if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>()) {
774+
LLVM_DEBUG(dbgs() << "AAResults register SCEVAA\n");
757775
AAR->addAAResult(WrapperPass->getResult());
776+
}
758777

759778
// If available, run an external AA providing callback over the results as
760779
// well.
761-
if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
762-
if (WrapperPass->CB)
763-
WrapperPass->CB(*this, F, *AAR);
780+
if (ExtWrapperPass && !ExtWrapperPass->runEarly() && ExtWrapperPass->CB) {
781+
LLVM_DEBUG(dbgs() << "AAResults register Late ExternalAA: "
782+
<< ExtWrapperPass->getPassName() << "\n");
783+
ExtWrapperPass->CB(*this, F, *AAR);
784+
}
764785

765786
// Analyses don't mutate the IR, so return false.
766787
return false;

llvm/lib/Passes/PassBuilderPipelines.cpp

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

2323+
// Add any target-specific alias analyses that should be run early.
2324+
if (TM)
2325+
TM->registerEarlyDefaultAliasAnalyses(AA);
2326+
23232327
// First we register the basic alias analysis that provides the majority of
23242328
// per-function local AA logic. This is a stateless, on-demand local set of
23252329
// AA techniques.

llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.h

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

8686
// Wrapper around ExternalAAWrapperPass so that the default
8787
// constructor gets the callback.
88+
// Note that NVPTXAA will run before BasicAA for compile time considerations.
8889
class NVPTXExternalAAWrapper : public ExternalAAWrapperPass {
8990
public:
9091
static char ID;
9192

93+
bool runEarly() override { return true; }
94+
9295
NVPTXExternalAAWrapper()
9396
: ExternalAAWrapperPass([](Pass &P, Function &, AAResults &AAR) {
9497
if (auto *WrapperPass =
9598
P.getAnalysisIfAvailable<NVPTXAAWrapperPass>())
9699
AAR.addAAResult(WrapperPass->getResult());
97100
}) {}
101+
102+
StringRef getPassName() const override {
103+
return "NVPTX Address space based Alias Analysis Wrapper";
104+
}
98105
};
99106

100107
ImmutablePass *createNVPTXAAWrapperPass();

llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ MachineFunctionInfo *NVPTXTargetMachine::createMachineFunctionInfo(
234234
F, STI);
235235
}
236236

237-
void NVPTXTargetMachine::registerDefaultAliasAnalyses(AAManager &AAM) {
237+
void NVPTXTargetMachine::registerEarlyDefaultAliasAnalyses(AAManager &AAM) {
238238
AAM.registerFunctionAnalysis<NVPTXAA>();
239239
}
240240

@@ -349,10 +349,7 @@ void NVPTXPassConfig::addIRPasses() {
349349
disablePass(&RemoveLoadsIntoFakeUsesID);
350350

351351
addPass(createNVPTXAAWrapperPass());
352-
addPass(createExternalAAWrapperPass([](Pass &P, Function &, AAResults &AAR) {
353-
if (auto *WrapperPass = P.getAnalysisIfAvailable<NVPTXAAWrapperPass>())
354-
AAR.addAAResult(WrapperPass->getResult());
355-
}));
352+
addPass(createNVPTXExternalAAWrapperPass());
356353

357354
// NVVMReflectPass is added in addEarlyAsPossiblePasses, so hopefully running
358355
// 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 CodeGenTargetMachineImpl {
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)