diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h --- a/llvm/include/llvm/IR/PassInstrumentation.h +++ b/llvm/include/llvm/IR/PassInstrumentation.h @@ -35,7 +35,7 @@ /// executed and IRUnit it works on. There can be different schemes of /// providing names in future, currently it is just a name() of the pass. /// -/// - PassInstrumentation wraps address of IRUnit into llvm::Any and passes +/// - PassInstrumentation wraps address of IRUnit into std::any and passes /// control to all the registered callbacks. Note that we specifically wrap /// 'const IRUnitT*' so as to avoid any accidental changes to IR in /// instrumenting callbacks. @@ -49,10 +49,10 @@ #ifndef LLVM_IR_PASSINSTRUMENTATION_H #define LLVM_IR_PASSINSTRUMENTATION_H -#include "llvm/ADT/Any.h" #include "llvm/ADT/FunctionExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" +#include #include #include @@ -66,7 +66,7 @@ class PassInstrumentationCallbacks { public: // Before/After callbacks accept IRUnits whenever appropriate, so they need - // to take them as constant pointers, wrapped with llvm::Any. + // to take them as constant pointers, wrapped with std::any. // For the case when IRUnit has been invalidated there is a different // callback to use - AfterPassInvalidated. // We call all BeforePassFuncs to determine if a pass should run or not. @@ -75,14 +75,14 @@ // already invalidated IRUnit is unsafe. There are ways to handle invalidated // IRUnits in a safe way, and we might pursue that as soon as there is a // useful instrumentation that needs it. - using BeforePassFunc = bool(StringRef, Any); - using BeforeSkippedPassFunc = void(StringRef, Any); - using BeforeNonSkippedPassFunc = void(StringRef, Any); - using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &); + using BeforePassFunc = bool(StringRef, std::any); + using BeforeSkippedPassFunc = void(StringRef, std::any); + using BeforeNonSkippedPassFunc = void(StringRef, std::any); + using AfterPassFunc = void(StringRef, std::any, const PreservedAnalyses &); using AfterPassInvalidatedFunc = void(StringRef, const PreservedAnalyses &); - using BeforeAnalysisFunc = void(StringRef, Any); - using AfterAnalysisFunc = void(StringRef, Any); - using AnalysisInvalidatedFunc = void(StringRef, Any); + using BeforeAnalysisFunc = void(StringRef, std::any); + using AfterAnalysisFunc = void(StringRef, std::any); + using AnalysisInvalidatedFunc = void(StringRef, std::any); using AnalysesClearedFunc = void(StringRef); public: @@ -221,15 +221,15 @@ bool ShouldRun = true; if (!isRequired(Pass)) { for (auto &C : Callbacks->ShouldRunOptionalPassCallbacks) - ShouldRun &= C(Pass.name(), llvm::Any(&IR)); + ShouldRun &= C(Pass.name(), std::any(&IR)); } if (ShouldRun) { for (auto &C : Callbacks->BeforeNonSkippedPassCallbacks) - C(Pass.name(), llvm::Any(&IR)); + C(Pass.name(), std::any(&IR)); } else { for (auto &C : Callbacks->BeforeSkippedPassCallbacks) - C(Pass.name(), llvm::Any(&IR)); + C(Pass.name(), std::any(&IR)); } return ShouldRun; @@ -243,7 +243,7 @@ const PreservedAnalyses &PA) const { if (Callbacks) for (auto &C : Callbacks->AfterPassCallbacks) - C(Pass.name(), llvm::Any(&IR), PA); + C(Pass.name(), std::any(&IR), PA); } /// AfterPassInvalidated instrumentation point - takes \p Pass instance @@ -263,7 +263,7 @@ void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const { if (Callbacks) for (auto &C : Callbacks->BeforeAnalysisCallbacks) - C(Analysis.name(), llvm::Any(&IR)); + C(Analysis.name(), std::any(&IR)); } /// AfterAnalysis instrumentation point - takes \p Analysis instance @@ -272,7 +272,7 @@ void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const { if (Callbacks) for (auto &C : Callbacks->AfterAnalysisCallbacks) - C(Analysis.name(), llvm::Any(&IR)); + C(Analysis.name(), std::any(&IR)); } /// AnalysisInvalidated instrumentation point - takes \p Analysis instance @@ -282,7 +282,7 @@ void runAnalysisInvalidated(const PassT &Analysis, const IRUnitT &IR) const { if (Callbacks) for (auto &C : Callbacks->AnalysisInvalidatedCallbacks) - C(Analysis.name(), llvm::Any(&IR)); + C(Analysis.name(), std::any(&IR)); } /// AnalysesCleared instrumentation point - takes name of IR that analyses diff --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp --- a/llvm/lib/CodeGen/MachinePassManager.cpp +++ b/llvm/lib/CodeGen/MachinePassManager.cpp @@ -15,6 +15,8 @@ #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/PassManagerImpl.h" +#include + using namespace llvm; namespace llvm { @@ -40,7 +42,7 @@ // No need to pop this callback later since MIR pipeline is flat which means // current pipeline is the top-level pipeline. Callbacks are not used after // current pipeline. - PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) { + PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, std::any IR) { assert(any_isa(IR)); const MachineFunction *MF = any_cast(IR); assert(MF && "Machine function should be valid for printing"); diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -17,7 +17,6 @@ #include "ConstantsContext.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" -#include "llvm/ADT/Any.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" diff --git a/llvm/lib/IR/PassTimingInfo.cpp b/llvm/lib/IR/PassTimingInfo.cpp --- a/llvm/lib/IR/PassTimingInfo.cpp +++ b/llvm/lib/IR/PassTimingInfo.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/Mutex.h" #include "llvm/Support/TypeName.h" #include "llvm/Support/raw_ostream.h" +#include #include using namespace llvm; @@ -275,9 +276,9 @@ return; PIC.registerBeforeNonSkippedPassCallback( - [this](StringRef P, Any) { this->runBeforePass(P); }); + [this](StringRef P, std::any) { this->runBeforePass(P); }); PIC.registerAfterPassCallback( - [this](StringRef P, Any, const PreservedAnalyses &) { + [this](StringRef P, std::any, const PreservedAnalyses &) { this->runAfterPass(P); }); PIC.registerAfterPassInvalidatedCallback( @@ -285,9 +286,9 @@ this->runAfterPass(P); }); PIC.registerBeforeAnalysisCallback( - [this](StringRef P, Any) { this->runBeforePass(P); }); + [this](StringRef P, std::any) { this->runBeforePass(P); }); PIC.registerAfterAnalysisCallback( - [this](StringRef P, Any) { this->runAfterPass(P); }); + [this](StringRef P, std::any) { this->runAfterPass(P); }); } } // namespace llvm diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp --- a/llvm/lib/Passes/StandardInstrumentations.cpp +++ b/llvm/lib/Passes/StandardInstrumentations.cpp @@ -13,7 +13,6 @@ //===----------------------------------------------------------------------===// #include "llvm/Passes/StandardInstrumentations.h" -#include "llvm/ADT/Any.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" #include "llvm/Analysis/CallGraphSCCPass.h" @@ -37,6 +36,7 @@ #include "llvm/Support/Regex.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" +#include #include #include #include @@ -113,7 +113,7 @@ /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match /// certain global filters. Will never return nullptr if \p Force is true. -const Module *unwrapModule(Any IR, bool Force = false) { +const Module *unwrapModule(std::any IR, bool Force = false) { if (any_isa(IR)) return any_cast(IR); @@ -180,7 +180,7 @@ printLoop(const_cast(*L), OS); } -std::string getIRName(Any IR) { +std::string getIRName(std::any IR) { if (any_isa(IR)) return "[module]"; @@ -221,7 +221,7 @@ isFunctionInPrintList("*"); } -bool shouldPrintIR(Any IR) { +bool shouldPrintIR(std::any IR) { if (any_isa(IR)) { const Module *M = any_cast(IR); return moduleContainsFilterPrintFunc(*M); @@ -245,8 +245,8 @@ } /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into -/// llvm::Any and does actual print job. -void unwrapAndPrint(raw_ostream &OS, Any IR) { +/// std::any and does actual print job. +void unwrapAndPrint(raw_ostream &OS, std::any IR) { if (!shouldPrintIR(IR)) return; @@ -306,7 +306,7 @@ } // Return the module when that is the appropriate level of comparison for \p IR. -const Module *getModuleForComparison(Any IR) { +const Module *getModuleForComparison(std::any IR) { if (any_isa(IR)) return any_cast(IR); if (any_isa(IR)) @@ -332,7 +332,7 @@ // Return true when this is a pass on IR for which printing // of changes is desired. -bool isInteresting(Any IR, StringRef PassID) { +bool isInteresting(std::any IR, StringRef PassID) { if (!isInterestingPass(PassID)) return false; if (any_isa(IR)) @@ -347,7 +347,7 @@ } template -void ChangeReporter::saveIRBeforePass(Any IR, StringRef PassID) { +void ChangeReporter::saveIRBeforePass(std::any IR, StringRef PassID) { // Always need to place something on the stack because invalidated passes // are not given the IR so it cannot be determined whether the pass was for // something that was filtered out. @@ -368,7 +368,7 @@ } template -void ChangeReporter::handleIRAfterPass(Any IR, StringRef PassID) { +void ChangeReporter::handleIRAfterPass(std::any IR, StringRef PassID) { assert(!BeforeStack.empty() && "Unexpected empty stack encountered."); std::string Name = getIRName(IR); @@ -413,10 +413,10 @@ void ChangeReporter::registerRequiredCallbacks( PassInstrumentationCallbacks &PIC) { PIC.registerBeforeNonSkippedPassCallback( - [this](StringRef P, Any IR) { saveIRBeforePass(IR, P); }); + [this](StringRef P, std::any IR) { saveIRBeforePass(IR, P); }); PIC.registerAfterPassCallback( - [this](StringRef P, Any IR, const PreservedAnalyses &) { + [this](StringRef P, std::any IR, const PreservedAnalyses &) { handleIRAfterPass(IR, P); }); PIC.registerAfterPassInvalidatedCallback( @@ -429,7 +429,7 @@ TextChangeReporter::TextChangeReporter(bool Verbose) : ChangeReporter(Verbose), Out(dbgs()) {} -template void TextChangeReporter::handleInitialIR(Any IR) { +template void TextChangeReporter::handleInitialIR std::any IR) { // Always print the module. // Unwrap and print directly to avoid filtering problems in general routines. auto *M = unwrapModule(IR, /*Force=*/true); @@ -470,7 +470,7 @@ TextChangeReporter::registerRequiredCallbacks(PIC); } -void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID, +void IRChangedPrinter::generateIRRepresentation(std::any IR, StringRef PassID, std::string &Output) { raw_string_ostream OS(Output); unwrapAndPrint(OS, IR); @@ -479,7 +479,7 @@ void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name, const std::string &Before, - const std::string &After, Any) { + const std::string &After, std::any) { // Report the IR before the changes when requested. if (PrintChangedBefore) Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n" @@ -595,7 +595,8 @@ }); } -template void IRComparer::analyzeIR(Any IR, IRDataT &Data) { +template +void IRComparer::analyzeIR(std::any IR, IRDataT &Data) { if (const Module *M = getModuleForComparison(IR)) { // Create data for each existing/interesting function in the module. for (const Function &F : *M) @@ -640,7 +641,7 @@ assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit"); } -void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) { +void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, std::any IR) { const Module *M = unwrapModule(IR); ModuleDescStack.emplace_back(M, getIRName(IR), PassID); } @@ -653,7 +654,7 @@ return ModuleDesc; } -void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) { +void PrintIRInstrumentation::printBeforePass(StringRef PassID, std::any IR) { if (isIgnored(PassID)) return; @@ -675,7 +676,7 @@ unwrapAndPrint(dbgs(), IR); } -void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) { +void PrintIRInstrumentation::printAfterPass(StringRef PassID, std::any IR) { if (isIgnored(PassID)) return; @@ -743,11 +744,11 @@ // for later use in AfterPassInvalidated. if (shouldPrintBeforeSomePass() || shouldPrintAfterSomePass()) PIC.registerBeforeNonSkippedPassCallback( - [this](StringRef P, Any IR) { this->printBeforePass(P, IR); }); + [this](StringRef P, std::any IR) { this->printBeforePass(P, IR); }); if (shouldPrintAfterSomePass()) { PIC.registerAfterPassCallback( - [this](StringRef P, Any IR, const PreservedAnalyses &) { + [this](StringRef P, std::any IR, const PreservedAnalyses &) { this->printAfterPass(P, IR); }); PIC.registerAfterPassInvalidatedCallback( @@ -760,10 +761,10 @@ void OptNoneInstrumentation::registerCallbacks( PassInstrumentationCallbacks &PIC) { PIC.registerShouldRunOptionalPassCallback( - [this](StringRef P, Any IR) { return this->shouldRun(P, IR); }); + [this](StringRef P, std::any IR) { return this->shouldRun(P, IR); }); } -bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) { +bool OptNoneInstrumentation::shouldRun(StringRef PassID, std::any IR) { const Function *F = nullptr; if (any_isa(IR)) { F = any_cast(IR); @@ -782,7 +783,7 @@ PassInstrumentationCallbacks &PIC) { if (!getOptBisector().isEnabled()) return; - PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, Any IR) { + PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, std::any IR) { return isIgnored(PassID) || getOptBisector().checkPass(PassID, getIRName(IR)); }); @@ -808,14 +809,14 @@ } PIC.registerBeforeSkippedPassCallback([this, SpecialPasses](StringRef PassID, - Any IR) { + std::any IR) { assert(!isSpecialPass(PassID, SpecialPasses) && "Unexpectedly skipping special pass"); print() << "Skipping pass: " << PassID << " on " << getIRName(IR) << "\n"; }); PIC.registerBeforeNonSkippedPassCallback([this, SpecialPasses]( - StringRef PassID, Any IR) { + StringRef PassID, std::any IR) { if (isSpecialPass(PassID, SpecialPasses)) return; @@ -838,7 +839,7 @@ Indent += 2; }); PIC.registerAfterPassCallback( - [this, SpecialPasses](StringRef PassID, Any IR, + [this, SpecialPasses](StringRef PassID, std::any IR, const PreservedAnalyses &) { if (isSpecialPass(PassID, SpecialPasses)) return; @@ -846,7 +847,7 @@ Indent -= 2; }); PIC.registerAfterPassInvalidatedCallback( - [this, SpecialPasses](StringRef PassID, Any IR) { + [this, SpecialPasses](StringRef PassID, std::any IR) { if (isSpecialPass(PassID, SpecialPasses)) return; @@ -854,14 +855,15 @@ }); if (!Opts.SkipAnalyses) { - PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) { + PIC.registerBeforeAnalysisCallback([this](StringRef PassID, std::any IR) { print() << "Running analysis: " << PassID << " on " << getIRName(IR) << "\n"; Indent += 2; }); PIC.registerAfterAnalysisCallback( - [this](StringRef PassID, Any IR) { Indent -= 2; }); - PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) { + [this](StringRef PassID, std::any IR) { Indent -= 2; }); + PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, + std::any IR) { print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR) << "\n"; }); @@ -1026,18 +1028,19 @@ report_fatal_error(Twine("CFG unexpectedly changed by ", Pass)); }; - PIC.registerBeforeNonSkippedPassCallback([this, &FAM](StringRef P, Any IR) { + PIC.registerBeforeNonSkippedPassCallback( + [this, &FAM](StringRef P, std::any IR) { #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS - assert(&PassStack.emplace_back(P)); + assert(&PassStack.emplace_back(P)); #endif - (void)this; - if (!any_isa(IR)) - return; + (void)this; + if (!any_isa(IR)) + return; - const auto *F = any_cast(IR); - // Make sure a fresh CFG snapshot is available before the pass. - FAM.getResult(*const_cast(F)); - }); + const auto *F = any_cast(IR); + // Make sure a fresh CFG snapshot is available before the pass. + FAM.getResult(*const_cast(F)); + }); PIC.registerAfterPassInvalidatedCallback( [this](StringRef P, const PreservedAnalyses &PassPA) { @@ -1049,7 +1052,7 @@ }); PIC.registerAfterPassCallback([this, &FAM, - checkCFG](StringRef P, Any IR, + checkCFG](StringRef P, std::any IR, const PreservedAnalyses &PassPA) { #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS assert(PassStack.pop_back_val() == P && @@ -1075,7 +1078,7 @@ void VerifyInstrumentation::registerCallbacks( PassInstrumentationCallbacks &PIC) { PIC.registerAfterPassCallback( - [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) { + [this](StringRef P, std::any IR, const PreservedAnalyses &PassPA) { if (isIgnored(P) || P == "VerifierPass") return; if (any_isa(IR) || any_isa(IR)) { @@ -1110,14 +1113,16 @@ InLineChangePrinter::~InLineChangePrinter() = default; -void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID, +void InLineChangePrinter::generateIRRepresentation(std::any IR, + StringRef PassID, IRDataT &D) { IRComparer::analyzeIR(IR, D); } void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name, const IRDataT &Before, - const IRDataT &After, Any IR) { + const IRDataT &After, + std::any IR) { SmallString<20> Banner = formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name); Out << Banner; @@ -1848,7 +1853,7 @@ return S.c_str(); } -void DotCfgChangeReporter::handleInitialIR(Any IR) { +void DotCfgChangeReporter::handleInitialIR(std::any IR) { assert(HTML && "Expected outstream to be set"); *HTML << "\n" @@ -1872,7 +1877,8 @@ ++N; } -void DotCfgChangeReporter::generateIRRepresentation(Any IR, StringRef PassID, +void DotCfgChangeReporter::generateIRRepresentation(std::any IR, + StringRef PassID, IRDataT &Data) { IRComparer::analyzeIR(IR, Data); } @@ -1888,7 +1894,8 @@ void DotCfgChangeReporter::handleAfter(StringRef PassID, std::string &Name, const IRDataT &Before, - const IRDataT &After, Any IR) { + const IRDataT &After, + std::any IR) { assert(HTML && "Expected outstream to be set"); IRComparer(Before, After) .compare(getModuleForComparison(IR), @@ -2047,7 +2054,8 @@ sys::AddSignalHandler(SignalHandler, nullptr); CrashReporter = this; - PIC.registerBeforeNonSkippedPassCallback([this](StringRef PassID, Any IR) { + PIC.registerBeforeNonSkippedPassCallback([this](StringRef PassID, + std::any IR) { SavedIR.clear(); raw_string_ostream OS(SavedIR); OS << formatv("*** Dump of {0}IR Before Last Pass {1}", diff --git a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp --- a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp +++ b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp @@ -7,10 +7,10 @@ //===----------------------------------------------------------------------===// #include "llvm/Testing/Support/Error.h" +#include #include #include #include -#include #include #include #include @@ -281,7 +281,7 @@ } /// Helper for HasName matcher that returns getName both for IRUnit and -/// for IRUnit pointer wrapper into llvm::Any (wrapped by PassInstrumentation). +/// for IRUnit pointer wrapper into std::any (wrapped by PassInstrumentation). template std::string getName(const IRUnitT &IR) { return std::string(IR.getName()); } @@ -290,7 +290,7 @@ return std::string(name); } -template <> std::string getName(const llvm::Any &WrappedIR) { +template <> std::string getName(const std::any &WrappedIR) { if (any_isa(WrappedIR)) return any_cast(WrappedIR)->getName().str(); if (any_isa(WrappedIR)) @@ -346,30 +346,30 @@ void registerPassInstrumentation() { Callbacks.registerShouldRunOptionalPassCallback( - [this](StringRef P, llvm::Any IR) { + [this](StringRef P, std::any IR) { return this->runBeforePass(P, IR); }); Callbacks.registerBeforeSkippedPassCallback( - [this](StringRef P, llvm::Any IR) { + [this](StringRef P, std::any IR) { this->runBeforeSkippedPass(P, IR); }); Callbacks.registerBeforeNonSkippedPassCallback( - [this](StringRef P, llvm::Any IR) { + [this](StringRef P, std::any IR) { this->runBeforeNonSkippedPass(P, IR); }); Callbacks.registerAfterPassCallback( - [this](StringRef P, llvm::Any IR, const PreservedAnalyses &PA) { + [this](StringRef P, std::any IR, const PreservedAnalyses &PA) { this->runAfterPass(P, IR, PA); }); Callbacks.registerAfterPassInvalidatedCallback( [this](StringRef P, const PreservedAnalyses &PA) { this->runAfterPassInvalidated(P, PA); }); - Callbacks.registerBeforeAnalysisCallback([this](StringRef P, llvm::Any IR) { + Callbacks.registerBeforeAnalysisCallback([this](StringRef P, std::any IR) { return this->runBeforeAnalysis(P, IR); }); Callbacks.registerAfterAnalysisCallback( - [this](StringRef P, llvm::Any IR) { this->runAfterAnalysis(P, IR); }); + [this](StringRef P, std::any IR) { this->runAfterAnalysis(P, IR); }); } void ignoreNonMockPassInstrumentation(StringRef IRName) {