Skip to content

Commit 749e4bb

Browse files
committed
Add support for moving target specific AA before basic AA
1 parent b8ee0aa commit 749e4bb

File tree

8 files changed

+38
-5
lines changed

8 files changed

+38
-5
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ struct ExternalAAWrapperPass : ImmutablePass {
993993

994994
explicit ExternalAAWrapperPass(CallbackT CB);
995995

996+
virtual bool runEarly() { return false; }
997+
996998
void getAnalysisUsage(AnalysisUsage &AU) const override {
997999
AU.setPreservesAll();
9981000
}

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,11 @@ bool AAResultsWrapperPass::runOnFunction(Function &F) {
752752
AAR.reset(
753753
new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F)));
754754

755+
// Add any target-specific alias analyses that should be run early.
756+
auto *ExtWrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>();
757+
if (ExtWrapperPass && ExtWrapperPass->runEarly() && ExtWrapperPass->CB)
758+
ExtWrapperPass->CB(*this, F, *AAR);
759+
755760
// BasicAA is always available for function analyses. Also, we add it first
756761
// so that it can trump TBAA results when it proves MustAlias.
757762
// FIXME: TBAA should have an explicit mode to support this and then we
@@ -771,9 +776,8 @@ bool AAResultsWrapperPass::runOnFunction(Function &F) {
771776

772777
// If available, run an external AA providing callback over the results as
773778
// well.
774-
if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
775-
if (WrapperPass->CB)
776-
WrapperPass->CB(*this, F, *AAR);
779+
if (ExtWrapperPass && !ExtWrapperPass->runEarly() && ExtWrapperPass->CB)
780+
ExtWrapperPass->CB(*this, F, *AAR);
777781

778782
// Analyses don't mutate the IR, so return false.
779783
return false;

llvm/lib/Passes/PassBuilderPipelines.cpp

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

2205+
// Add any target-specific alias analyses that should be run early.
2206+
if (TM)
2207+
TM->registerEarlyDefaultAliasAnalyses(AA);
2208+
22052209
// First we register the basic alias analysis that provides the majority of
22062210
// per-function local AA logic. This is a stateless, on-demand local set of
22072211
// AA techniques.

llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ 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 to 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 =

llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
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

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: opt -aa-pipeline=default -passes='require<aa>' -debug-pass-manager -disable-output -S < %s 2>&1 | FileCheck %s
2+
; RUN: opt -O2 -debug-aa -disable-output -S < %s 2>&1 | FileCheck %s --check-prefix=LEGACY
3+
; LEGACY: NVPTX AA
4+
; LEGACY-NEXT: Basic Alias Analysis (stateless AA impl)
5+
6+
; In default AA pipeline, NVPTXAA should run before BasicAA to reduce compile time for NVPTX backend
7+
target triple = "nvptx64-nvidia-cuda"
8+
9+
; CHECK: Running analysis: NVPTXAA on foo
10+
; CHECK-NEXT: Running analysis: BasicAA on foo
11+
define void @foo(){
12+
entry:
13+
ret void
14+
}
15+

0 commit comments

Comments
 (0)