brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.2 KiB · af22b77 Raw
166 lines · cpp
1//===- PassManager.cpp - Infrastructure for managing & running IR passes --===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/IR/PassManager.h"10#include "llvm/IR/Module.h"11#include "llvm/IR/PassManagerImpl.h"12#include "llvm/Support/Compiler.h"13#include <optional>14 15using namespace llvm;16 17namespace llvm {18// Explicit template instantiations and specialization defininitions for core19// template typedefs.20template class LLVM_EXPORT_TEMPLATE AllAnalysesOn<Module>;21template class LLVM_EXPORT_TEMPLATE AllAnalysesOn<Function>;22template class LLVM_EXPORT_TEMPLATE PassManager<Module>;23template class LLVM_EXPORT_TEMPLATE PassManager<Function>;24template class LLVM_EXPORT_TEMPLATE AnalysisManager<Module>;25template class LLVM_EXPORT_TEMPLATE AnalysisManager<Function>;26template class LLVM_EXPORT_TEMPLATE27    InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;28template class LLVM_EXPORT_TEMPLATE29    OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;30 31template <>32bool FunctionAnalysisManagerModuleProxy::Result::invalidate(33    Module &M, const PreservedAnalyses &PA,34    ModuleAnalysisManager::Invalidator &Inv) {35  // If literally everything is preserved, we're done.36  if (PA.areAllPreserved())37    return false; // This is still a valid proxy.38 39  // If this proxy isn't marked as preserved, then even if the result remains40  // valid, the key itself may no longer be valid, so we clear everything.41  //42  // Note that in order to preserve this proxy, a module pass must ensure that43  // the FAM has been completely updated to handle the deletion of functions.44  // Specifically, any FAM-cached results for those functions need to have been45  // forcibly cleared. When preserved, this proxy will only invalidate results46  // cached on functions *still in the module* at the end of the module pass.47  auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>();48  if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {49    InnerAM->clear();50    return true;51  }52 53  // Directly check if the relevant set is preserved.54  bool AreFunctionAnalysesPreserved =55      PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>();56 57  // Now walk all the functions to see if any inner analysis invalidation is58  // necessary.59  for (Function &F : M) {60    std::optional<PreservedAnalyses> FunctionPA;61 62    // Check to see whether the preserved set needs to be pruned based on63    // module-level analysis invalidation that triggers deferred invalidation64    // registered with the outer analysis manager proxy for this function.65    if (auto *OuterProxy =66            InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))67      for (const auto &OuterInvalidationPair :68           OuterProxy->getOuterInvalidations()) {69        AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;70        const auto &InnerAnalysisIDs = OuterInvalidationPair.second;71        if (Inv.invalidate(OuterAnalysisID, M, PA)) {72          if (!FunctionPA)73            FunctionPA = PA;74          for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)75            FunctionPA->abandon(InnerAnalysisID);76        }77      }78 79    // Check if we needed a custom PA set, and if so we'll need to run the80    // inner invalidation.81    if (FunctionPA) {82      InnerAM->invalidate(F, *FunctionPA);83      continue;84    }85 86    // Otherwise we only need to do invalidation if the original PA set didn't87    // preserve all function analyses.88    if (!AreFunctionAnalysesPreserved)89      InnerAM->invalidate(F, PA);90  }91 92  // Return false to indicate that this result is still a valid proxy.93  return false;94}95} // namespace llvm96 97void ModuleToFunctionPassAdaptor::printPipeline(98    raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {99  OS << "function";100  if (EagerlyInvalidate)101    OS << "<eager-inv>";102  OS << '(';103  Pass->printPipeline(OS, MapClassName2PassName);104  OS << ')';105}106 107PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M,108                                                   ModuleAnalysisManager &AM) {109  FunctionAnalysisManager &FAM =110      AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();111 112  // Request PassInstrumentation from analysis manager, will use it to run113  // instrumenting callbacks for the passes later.114  PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);115 116  PreservedAnalyses PA = PreservedAnalyses::all();117  for (Function &F : M) {118    if (F.isDeclaration())119      continue;120 121    // Check the PassInstrumentation's BeforePass callbacks before running the122    // pass, skip its execution completely if asked to (callback returns123    // false).124    if (!PI.runBeforePass<Function>(*Pass, F))125      continue;126 127    PreservedAnalyses PassPA = Pass->run(F, FAM);128 129    // We know that the function pass couldn't have invalidated any other130    // function's analyses (that's the contract of a function pass), so131    // directly handle the function analysis manager's invalidation here.132    FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);133 134    PI.runAfterPass(*Pass, F, PassPA);135 136    // Then intersect the preserved set so that invalidation of module137    // analyses will eventually occur when the module pass completes.138    PA.intersect(std::move(PassPA));139  }140 141  // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)142  // the function passes we ran didn't add or remove any functions.143  //144  // We also preserve all analyses on Functions, because we did all the145  // invalidation we needed to do above.146  PA.preserveSet<AllAnalysesOn<Function>>();147  PA.preserve<FunctionAnalysisManagerModuleProxy>();148  return PA;149}150 151template <>152void llvm::printIRUnitNameForStackTrace<Module>(raw_ostream &OS,153                                                const Module &IR) {154  OS << "module \"" << IR.getName() << "\"";155}156 157template <>158void llvm::printIRUnitNameForStackTrace<Function>(raw_ostream &OS,159                                                  const Function &IR) {160  OS << "function \"" << IR.getName() << "\"";161}162 163AnalysisSetKey CFGAnalyses::SetKey;164 165AnalysisSetKey PreservedAnalyses::AllAnalysesKey;166