From 767e429a8088b1387a0b9e640db08ac0636014e2 Mon Sep 17 00:00:00 2001 From: Johannes Doerfert Date: Thu, 22 Jun 2023 13:11:33 -0700 Subject: [PATCH] [Attributor][NFC] Allow to restrict the Attributor to cached passes If the user wants to avoid running additional passes, they can now initialize the AnalysisGetter accordingly. --- llvm/include/llvm/Transforms/IPO/Attributor.h | 37 +++++++++++++++++------- llvm/lib/Transforms/IPO/Attributor.cpp | 4 --- llvm/lib/Transforms/IPO/AttributorAttributes.cpp | 3 +- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index 365301a..90ebe12 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -1125,25 +1125,42 @@ struct AnalysisGetter { template static constexpr bool HasLegacyWrapper = false; template - typename Analysis::Result *getAnalysis(const Function &F) { - if (FAM) + typename Analysis::Result *getAnalysis(const Function &F, + bool RequestCachedOnly = false) { + if (!LegacyPass && !FAM) + return nullptr; + if (FAM) { + if (CachedOnly || RequestCachedOnly) + return FAM->getCachedResult(const_cast(F)); return &FAM->getResult(const_cast(F)); - if constexpr (HasLegacyWrapper) - if (LegacyPass) + } + if constexpr (HasLegacyWrapper) { + if (!CachedOnly && !RequestCachedOnly) return &LegacyPass ->getAnalysis( const_cast(F)) .getResult(); + if (auto *P = + LegacyPass + ->getAnalysisIfAvailable()) + return &P->getResult(); + } return nullptr; } - AnalysisGetter(FunctionAnalysisManager &FAM) : FAM(&FAM) {} - AnalysisGetter(Pass *P) : LegacyPass(P) {} + AnalysisGetter(FunctionAnalysisManager &FAM, bool CachedOnly = false) + : FAM(&FAM), CachedOnly(CachedOnly) {} + AnalysisGetter(Pass *P, bool CachedOnly = false) + : LegacyPass(P), CachedOnly(CachedOnly) {} AnalysisGetter() = default; private: FunctionAnalysisManager *FAM = nullptr; Pass *LegacyPass = nullptr; + + /// If \p CachedOnly is true, no pass is created, just existing results are + /// used. Also available per request. + bool CachedOnly = false; }; template @@ -1288,9 +1305,6 @@ struct InformationCache { return AG.getAnalysis(F); } - /// Return AliasAnalysis Result for function \p F. - AAResults *getAAResultsForFunction(const Function &F); - /// Return true if \p Arg is involved in a must-tail call, thus the argument /// of the caller or callee. bool isInvolvedInMustTailCall(const Argument &Arg) { @@ -1304,8 +1318,9 @@ struct InformationCache { /// Return the analysis result from a pass \p AP for function \p F. template - typename AP::Result *getAnalysisResultForFunction(const Function &F) { - return AG.getAnalysis(F); + typename AP::Result *getAnalysisResultForFunction(const Function &F, + bool CachedOnly = false) { + return AG.getAnalysis(F, CachedOnly); } /// Return datalayout used in the module. diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index 1b51a11..f7d1f1f 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -3116,10 +3116,6 @@ void InformationCache::initializeInformationCache(const Function &CF, InlineableFunctions.insert(&F); } -AAResults *InformationCache::getAAResultsForFunction(const Function &F) { - return AG.getAnalysis(F); -} - InformationCache::FunctionInfo::~FunctionInfo() { // The instruction vectors are allocated using a BumpPtrAllocator, we need to // manually destroy them. diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index 58297b5..ce2255e 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -4016,7 +4016,8 @@ struct AANoAliasCallSiteArgument final : AANoAliasImpl { // We have to utilize actual alias analysis queries so we need the object. if (!AAR) - AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope()); + AAR = A.getInfoCache().getAnalysisResultForFunction( + *getAnchorScope()); // Try to rule it out at the call site. bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp); -- 2.7.4