[llvm] bb7940e - [llvm] Make llvm::Any similar to std::any

Sebastian Neubauer via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 20 04:31:46 PST 2022


Author: Sebastian Neubauer
Date: 2022-12-20T13:28:30+01:00
New Revision: bb7940e25f6ca201ca57943544016390f1d2e504

URL: https://github.com/llvm/llvm-project/commit/bb7940e25f6ca201ca57943544016390f1d2e504
DIFF: https://github.com/llvm/llvm-project/commit/bb7940e25f6ca201ca57943544016390f1d2e504.diff

LOG: [llvm] Make llvm::Any similar to std::any

This facilitates replacing llvm::Any with std::any.
- Deprecate any_isa in favor of using any_cast(Any*) and checking for
  nullptr because C++17 has no any_isa.
- Remove the assert from any_cast(Any*), so it returns nullptr if the
  type is not correct. This aligns it with std::any_cast(any*).

Use any_cast(Any*) throughout LLVM instead of checks with any_isa.

This is the first part outlined in
https://discourse.llvm.org/t/rfc-switching-from-llvm-any-to-std-any/67176

Differential Revision: https://reviews.llvm.org/D139973

Added: 
    

Modified: 
    clang/unittests/Tooling/RefactoringTest.cpp
    clang/unittests/Tooling/TransformerTest.cpp
    lldb/include/lldb/Core/RichManglingContext.h
    llvm/include/llvm/ADT/Any.h
    llvm/lib/CodeGen/MachinePassManager.cpp
    llvm/lib/IR/LLVMContextImpl.h
    llvm/lib/Passes/StandardInstrumentations.cpp
    llvm/lib/Transforms/IPO/SampleProfileProbe.cpp
    llvm/lib/Transforms/Scalar/LoopPassManager.cpp
    llvm/lib/Transforms/Utils/Debugify.cpp
    llvm/unittests/ADT/AnyTest.cpp
    llvm/unittests/IR/PassBuilderCallbacksTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp
index e38afc148a7b8..c7a02e195d04a 100644
--- a/clang/unittests/Tooling/RefactoringTest.cpp
+++ b/clang/unittests/Tooling/RefactoringTest.cpp
@@ -1288,7 +1288,7 @@ TEST_F(AtomicChangeTest, InsertAfterWithInvalidLocation) {
 TEST_F(AtomicChangeTest, Metadata) {
   AtomicChange Change(Context.Sources, DefaultLoc, 17);
   const llvm::Any &Metadata = Change.getMetadata();
-  ASSERT_TRUE(llvm::any_isa<int>(Metadata));
+  ASSERT_TRUE(llvm::any_cast<int>(&Metadata));
   EXPECT_EQ(llvm::any_cast<int>(Metadata), 17);
 }
 

diff  --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index 6d6ed33d8fd67..4d5e22d944988 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -806,7 +806,7 @@ TEST_F(TransformerTest, WithMetadata) {
       "clang-tool", std::make_shared<PCHContainerOperations>(), {}));
   ASSERT_EQ(Changes.size(), 1u);
   const llvm::Any &Metadata = Changes[0].getMetadata();
-  ASSERT_TRUE(llvm::any_isa<int>(Metadata));
+  ASSERT_TRUE(llvm::any_cast<int>(&Metadata));
   EXPECT_THAT(llvm::any_cast<int>(Metadata), 5);
 }
 

diff  --git a/lldb/include/lldb/Core/RichManglingContext.h b/lldb/include/lldb/Core/RichManglingContext.h
index 9636b7898f8f5..3b79924e88a9a 100644
--- a/lldb/include/lldb/Core/RichManglingContext.h
+++ b/lldb/include/lldb/Core/RichManglingContext.h
@@ -87,8 +87,8 @@ class RichManglingContext {
   /// can't access CPlusPlusLanguage::MethodName from within the header.
   template <class ParserT> static ParserT *get(llvm::Any parser) {
     assert(parser.has_value());
-    assert(llvm::any_isa<ParserT *>(parser));
-    return llvm::any_cast<ParserT *>(parser);
+    assert(llvm::any_cast<ParserT *>(&parser));
+    return *llvm::any_cast<ParserT *>(&parser);
   }
 };
 

diff  --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h
index 934dfc142d376..acb7101a5145f 100644
--- a/llvm/include/llvm/ADT/Any.h
+++ b/llvm/include/llvm/ADT/Any.h
@@ -107,6 +107,13 @@ class LLVM_EXTERNAL_VISIBILITY Any {
   void reset() { Storage.reset(); }
 
 private:
+  // Only used for the internal llvm::Any implementation
+  template <typename T> bool isa() const {
+    if (!Storage)
+      return false;
+    return Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
+  }
+
   template <class T> friend T any_cast(const Any &Value);
   template <class T> friend T any_cast(Any &Value);
   template <class T> friend T any_cast(Any &&Value);
@@ -119,36 +126,37 @@ class LLVM_EXTERNAL_VISIBILITY Any {
 
 template <typename T> char Any::TypeId<T>::Id = 0;
 
-template <typename T> bool any_isa(const Any &Value) {
-  if (!Value.Storage)
-    return false;
-  return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
+template <typename T>
+LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")
+bool any_isa(const Any &Value) {
+  return Value.isa<T>();
 }
 
 template <class T> T any_cast(const Any &Value) {
+  assert(Value.isa<T>() && "Bad any cast!");
   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
 }
 
 template <class T> T any_cast(Any &Value) {
+  assert(Value.isa<T>() && "Bad any cast!");
   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
 }
 
 template <class T> T any_cast(Any &&Value) {
+  assert(Value.isa<T>() && "Bad any cast!");
   return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
 }
 
 template <class T> const T *any_cast(const Any *Value) {
   using U = remove_cvref_t<T>;
-  assert(Value && any_isa<T>(*Value) && "Bad any cast!");
-  if (!Value || !any_isa<U>(*Value))
+  if (!Value || !Value->isa<U>())
     return nullptr;
   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
 }
 
 template <class T> T *any_cast(Any *Value) {
   using U = std::decay_t<T>;
-  assert(Value && any_isa<U>(*Value) && "Bad any cast!");
-  if (!Value || !any_isa<U>(*Value))
+  if (!Value || !Value->isa<U>())
     return nullptr;
   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
 }

diff  --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp
index 476dc059d2b5d..039634f3d0477 100644
--- a/llvm/lib/CodeGen/MachinePassManager.cpp
+++ b/llvm/lib/CodeGen/MachinePassManager.cpp
@@ -41,7 +41,7 @@ Error MachineFunctionPassManager::run(Module &M,
     // current pipeline is the top-level pipeline. Callbacks are not used after
     // current pipeline.
     PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) {
-      assert(any_isa<const MachineFunction *>(IR));
+      assert(any_cast<const MachineFunction *>(&IR));
       const MachineFunction *MF = any_cast<const MachineFunction *>(IR);
       assert(MF && "Machine function should be valid for printing");
       std::string Banner = std::string("After ") + std::string(PassID);

diff  --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h
index eadb3b7fba775..5a87a025e3c39 100644
--- 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/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 0ebf14fd6c975..ad2504eca2fbb 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -123,20 +123,18 @@ static cl::opt<std::string>
 /// 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) {
-  if (any_isa<const Module *>(IR))
-    return any_cast<const Module *>(IR);
+  if (const auto **M = any_cast<const Module *>(&IR))
+    return *M;
 
-  if (any_isa<const Function *>(IR)) {
-    const Function *F = any_cast<const Function *>(IR);
-    if (!Force && !isFunctionInPrintList(F->getName()))
+  if (const auto **F = any_cast<const Function *>(&IR)) {
+    if (!Force && !isFunctionInPrintList((*F)->getName()))
       return nullptr;
 
-    return F->getParent();
+    return (*F)->getParent();
   }
 
-  if (any_isa<const LazyCallGraph::SCC *>(IR)) {
-    const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
-    for (const LazyCallGraph::Node &N : *C) {
+  if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
+    for (const LazyCallGraph::Node &N : **C) {
       const Function &F = N.getFunction();
       if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
         return F.getParent();
@@ -146,9 +144,8 @@ const Module *unwrapModule(Any IR, bool Force = false) {
     return nullptr;
   }
 
-  if (any_isa<const Loop *>(IR)) {
-    const Loop *L = any_cast<const Loop *>(IR);
-    const Function *F = L->getHeader()->getParent();
+  if (const auto **L = any_cast<const Loop *>(&IR)) {
+    const Function *F = (*L)->getHeader()->getParent();
     if (!Force && !isFunctionInPrintList(F->getName()))
       return nullptr;
     return F->getParent();
@@ -190,23 +187,17 @@ void printIR(raw_ostream &OS, const Loop *L) {
 }
 
 std::string getIRName(Any IR) {
-  if (any_isa<const Module *>(IR))
+  if (any_cast<const Module *>(&IR))
     return "[module]";
 
-  if (any_isa<const Function *>(IR)) {
-    const Function *F = any_cast<const Function *>(IR);
-    return F->getName().str();
-  }
+  if (const auto **F = any_cast<const Function *>(&IR))
+    return (*F)->getName().str();
 
-  if (any_isa<const LazyCallGraph::SCC *>(IR)) {
-    const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
-    return C->getName();
-  }
+  if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+    return (*C)->getName();
 
-  if (any_isa<const Loop *>(IR)) {
-    const Loop *L = any_cast<const Loop *>(IR);
-    return L->getName().str();
-  }
+  if (const auto **L = any_cast<const Loop *>(&IR))
+    return (*L)->getName().str();
 
   llvm_unreachable("Unknown wrapped IR type");
 }
@@ -228,30 +219,22 @@ bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
 }
 
 bool shouldPrintIR(Any IR) {
-  if (any_isa<const Module *>(IR)) {
-    const Module *M = any_cast<const Module *>(IR);
-    return moduleContainsFilterPrintFunc(*M);
-  }
+  if (const auto **M = any_cast<const Module *>(&IR))
+    return moduleContainsFilterPrintFunc(**M);
 
-  if (any_isa<const Function *>(IR)) {
-    const Function *F = any_cast<const Function *>(IR);
-    return isFunctionInPrintList(F->getName());
-  }
+  if (const auto **F = any_cast<const Function *>(&IR))
+    return isFunctionInPrintList((*F)->getName());
 
-  if (any_isa<const LazyCallGraph::SCC *>(IR)) {
-    const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
-    return sccContainsFilterPrintFunc(*C);
-  }
+  if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+    return sccContainsFilterPrintFunc(**C);
 
-  if (any_isa<const Loop *>(IR)) {
-    const Loop *L = any_cast<const Loop *>(IR);
-    return isFunctionInPrintList(L->getHeader()->getParent()->getName());
-  }
+  if (const auto **L = any_cast<const Loop *>(&IR))
+    return isFunctionInPrintList((*L)->getHeader()->getParent()->getName());
   llvm_unreachable("Unknown wrapped IR type");
 }
 
 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
-/// llvm::Any and does actual print job.
+/// Any and does actual print job.
 void unwrapAndPrint(raw_ostream &OS, Any IR) {
   if (!shouldPrintIR(IR))
     return;
@@ -263,27 +246,23 @@ void unwrapAndPrint(raw_ostream &OS, Any IR) {
     return;
   }
 
-  if (any_isa<const Module *>(IR)) {
-    const Module *M = any_cast<const Module *>(IR);
-    printIR(OS, M);
+  if (const auto **M = any_cast<const Module *>(&IR)) {
+    printIR(OS, *M);
     return;
   }
 
-  if (any_isa<const Function *>(IR)) {
-    const Function *F = any_cast<const Function *>(IR);
-    printIR(OS, F);
+  if (const auto **F = any_cast<const Function *>(&IR)) {
+    printIR(OS, *F);
     return;
   }
 
-  if (any_isa<const LazyCallGraph::SCC *>(IR)) {
-    const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
-    printIR(OS, C);
+  if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
+    printIR(OS, *C);
     return;
   }
 
-  if (any_isa<const Loop *>(IR)) {
-    const Loop *L = any_cast<const Loop *>(IR);
-    printIR(OS, L);
+  if (const auto **L = any_cast<const Loop *>(&IR)) {
+    printIR(OS, *L);
     return;
   }
   llvm_unreachable("Unknown wrapped IR type");
@@ -313,10 +292,10 @@ std::string makeHTMLReady(StringRef SR) {
 
 // Return the module when that is the appropriate level of comparison for \p IR.
 const Module *getModuleForComparison(Any IR) {
-  if (any_isa<const Module *>(IR))
-    return any_cast<const Module *>(IR);
-  if (any_isa<const LazyCallGraph::SCC *>(IR))
-    return any_cast<const LazyCallGraph::SCC *>(IR)
+  if (const auto **M = any_cast<const Module *>(&IR))
+    return *M;
+  if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+    return (*C)
         ->begin()
         ->getFunction()
         .getParent();
@@ -332,8 +311,8 @@ bool isInterestingFunction(const Function &F) {
 bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
   if (isIgnored(PassID) || !isPassInPrintList(PassName))
     return false;
-  if (any_isa<const Function *>(IR))
-    return isInterestingFunction(*any_cast<const Function *>(IR));
+  if (const auto **F = any_cast<const Function *>(&IR))
+    return isInterestingFunction(**F);
   return true;
 }
 
@@ -655,13 +634,12 @@ template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
     return;
   }
 
-  const Function *F = nullptr;
-  if (any_isa<const Function *>(IR))
-    F = any_cast<const Function *>(IR);
-  else {
-    assert(any_isa<const Loop *>(IR) && "Unknown IR unit.");
-    const Loop *L = any_cast<const Loop *>(IR);
-    F = L->getHeader()->getParent();
+  const Function **FPtr = any_cast<const Function *>(&IR);
+  const Function *F = FPtr ? *FPtr : nullptr;
+  if (!F) {
+    const Loop **L = any_cast<const Loop *>(&IR);
+    assert(L && "Unknown IR unit.");
+    F = (*L)->getHeader()->getParent();
   }
   assert(F && "Unknown IR unit.");
   generateFunctionData(Data, *F);
@@ -816,11 +794,11 @@ void OptNoneInstrumentation::registerCallbacks(
 }
 
 bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
-  const Function *F = nullptr;
-  if (any_isa<const Function *>(IR)) {
-    F = any_cast<const Function *>(IR);
-  } else if (any_isa<const Loop *>(IR)) {
-    F = any_cast<const Loop *>(IR)->getHeader()->getParent();
+  const Function **FPtr = any_cast<const Function *>(&IR);
+  const Function *F = FPtr ? *FPtr : nullptr;
+  if (!F) {
+    if (const auto **L = any_cast<const Loop *>(&IR))
+      F = (*L)->getHeader()->getParent();
   }
   bool ShouldRun = !(F && F->hasOptNone());
   if (!ShouldRun && DebugLogging) {
@@ -895,14 +873,14 @@ void PrintPassInstrumentation::registerCallbacks(
 
     auto &OS = print();
     OS << "Running pass: " << PassID << " on " << getIRName(IR);
-    if (any_isa<const Function *>(IR)) {
-      unsigned Count = any_cast<const Function *>(IR)->getInstructionCount();
+    if (const auto **F = any_cast<const Function *>(&IR)) {
+      unsigned Count = (*F)->getInstructionCount();
       OS << " (" << Count << " instruction";
       if (Count != 1)
         OS << 's';
       OS << ')';
-    } else if (any_isa<const LazyCallGraph::SCC *>(IR)) {
-      int Count = any_cast<const LazyCallGraph::SCC *>(IR)->size();
+    } else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
+      int Count = (*C)->size();
       OS << " (" << Count << " node";
       if (Count != 1)
         OS << 's';
@@ -1100,18 +1078,19 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
     report_fatal_error(Twine("CFG unexpectedly changed by ", Pass));
   };
 
-  PIC.registerBeforeNonSkippedPassCallback([this, &FAM](StringRef P, Any IR) {
+  PIC.registerBeforeNonSkippedPassCallback(
+      [this, &FAM](StringRef P, Any IR) {
 #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
-    assert(&PassStack.emplace_back(P));
+        assert(&PassStack.emplace_back(P));
 #endif
-    (void)this;
-    if (!any_isa<const Function *>(IR))
-      return;
+        (void)this;
+        const auto **F = any_cast<const Function *>(&IR);
+        if (!F)
+          return;
 
-    const auto *F = any_cast<const Function *>(IR);
-    // Make sure a fresh CFG snapshot is available before the pass.
-    FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(F));
-  });
+        // Make sure a fresh CFG snapshot is available before the pass.
+        FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(*F));
+      });
 
   PIC.registerAfterPassInvalidatedCallback(
       [this](StringRef P, const PreservedAnalyses &PassPA) {
@@ -1131,18 +1110,18 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
 #endif
     (void)this;
 
-    if (!any_isa<const Function *>(IR))
+    const auto **F = any_cast<const Function *>(&IR);
+    if (!F)
       return;
 
     if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
         !PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
       return;
 
-    const auto *F = any_cast<const Function *>(IR);
     if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>(
-            *const_cast<Function *>(F)))
-      checkCFG(P, F->getName(), *GraphBefore,
-               CFG(F, /* TrackBBLifetime */ false));
+            *const_cast<Function *>(*F)))
+      checkCFG(P, (*F)->getName(), *GraphBefore,
+               CFG(*F, /* TrackBBLifetime */ false));
   });
 }
 
@@ -1152,46 +1131,50 @@ void VerifyInstrumentation::registerCallbacks(
       [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
         if (isIgnored(P) || P == "VerifierPass")
           return;
-        if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) {
-          const Function *F;
-          if (any_isa<const Loop *>(IR))
-            F = any_cast<const Loop *>(IR)->getHeader()->getParent();
-          else
-            F = any_cast<const Function *>(IR);
+        const Function **FPtr = any_cast<const Function *>(&IR);
+        const Function *F = FPtr ? *FPtr : nullptr;
+        if (!F) {
+          if (const auto **L = any_cast<const Loop *>(&IR))
+            F = (*L)->getHeader()->getParent();
+        }
+
+        if (F) {
           if (DebugLogging)
             dbgs() << "Verifying function " << F->getName() << "\n";
 
           if (verifyFunction(*F, &errs()))
             report_fatal_error("Broken function found, compilation aborted!");
-        } else if (any_isa<const Module *>(IR) ||
-                   any_isa<const LazyCallGraph::SCC *>(IR)) {
-          const Module *M;
-          if (any_isa<const LazyCallGraph::SCC *>(IR))
-            M = any_cast<const LazyCallGraph::SCC *>(IR)
-                    ->begin()
-                    ->getFunction()
-                    .getParent();
-          else
-            M = any_cast<const Module *>(IR);
-          if (DebugLogging)
-            dbgs() << "Verifying module " << M->getName() << "\n";
-
-          if (verifyModule(*M, &errs()))
-            report_fatal_error("Broken module found, compilation aborted!");
+        } else {
+          const Module **MPtr = any_cast<const Module *>(&IR);
+          const Module *M = MPtr ? *MPtr : nullptr;
+          if (!M) {
+            if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+              M = (*C)->begin()->getFunction().getParent();
+          }
+
+          if (M) {
+            if (DebugLogging)
+              dbgs() << "Verifying module " << M->getName() << "\n";
+
+            if (verifyModule(*M, &errs()))
+              report_fatal_error("Broken module found, compilation aborted!");
+          }
         }
       });
 }
 
 InLineChangePrinter::~InLineChangePrinter() = default;
 
-void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID,
+void InLineChangePrinter::generateIRRepresentation(Any IR,
+                                                   StringRef PassID,
                                                    IRDataT<EmptyData> &D) {
   IRComparer<EmptyData>::analyzeIR(IR, D);
 }
 
 void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name,
                                       const IRDataT<EmptyData> &Before,
-                                      const IRDataT<EmptyData> &After, Any IR) {
+                                      const IRDataT<EmptyData> &After,
+                                      Any IR) {
   SmallString<20> Banner =
       formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
   Out << Banner;

diff  --git a/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp b/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp
index d1edea9cba770..c4844dbe7f3c9 100644
--- a/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfileProbe.cpp
@@ -98,14 +98,14 @@ void PseudoProbeVerifier::runAfterPass(StringRef PassID, Any IR) {
   std::string Banner =
       "\n*** Pseudo Probe Verification After " + PassID.str() + " ***\n";
   dbgs() << Banner;
-  if (any_isa<const Module *>(IR))
-    runAfterPass(any_cast<const Module *>(IR));
-  else if (any_isa<const Function *>(IR))
-    runAfterPass(any_cast<const Function *>(IR));
-  else if (any_isa<const LazyCallGraph::SCC *>(IR))
-    runAfterPass(any_cast<const LazyCallGraph::SCC *>(IR));
-  else if (any_isa<const Loop *>(IR))
-    runAfterPass(any_cast<const Loop *>(IR));
+  if (const auto **M = any_cast<const Module *>(&IR))
+    runAfterPass(*M);
+  else if (const auto **F = any_cast<const Function *>(&IR))
+    runAfterPass(*F);
+  else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
+    runAfterPass(*C);
+  else if (const auto **L = any_cast<const Loop *>(&IR))
+    runAfterPass(*L);
   else
     llvm_unreachable("Unknown IR unit");
 }

diff  --git a/llvm/lib/Transforms/Scalar/LoopPassManager.cpp b/llvm/lib/Transforms/Scalar/LoopPassManager.cpp
index fadd0707d66be..c98b94b56e484 100644
--- a/llvm/lib/Transforms/Scalar/LoopPassManager.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopPassManager.cpp
@@ -269,10 +269,11 @@ PreservedAnalyses FunctionToLoopPassAdaptor::run(Function &F,
   PI.pushBeforeNonSkippedPassCallback([&LAR, &LI](StringRef PassID, Any IR) {
     if (isSpecialPass(PassID, {"PassManager"}))
       return;
-    assert(any_isa<const Loop *>(IR) || any_isa<const LoopNest *>(IR));
-    const Loop *L = any_isa<const Loop *>(IR)
-                        ? any_cast<const Loop *>(IR)
-                        : &any_cast<const LoopNest *>(IR)->getOutermostLoop();
+    assert(any_cast<const Loop *>(&IR) || any_cast<const LoopNest *>(&IR));
+    const Loop **LPtr = any_cast<const Loop *>(&IR);
+    const Loop *L = LPtr ? *LPtr : nullptr;
+    if (!L)
+      L = &any_cast<const LoopNest *>(IR)->getOutermostLoop();
     assert(L && "Loop should be valid for printing");
 
     // Verify the loop structure and LCSSA form before visiting the loop.

diff  --git a/llvm/lib/Transforms/Utils/Debugify.cpp b/llvm/lib/Transforms/Utils/Debugify.cpp
index f4d9bce72dc3d..b11435343b3fb 100644
--- a/llvm/lib/Transforms/Utils/Debugify.cpp
+++ b/llvm/lib/Transforms/Utils/Debugify.cpp
@@ -1031,19 +1031,19 @@ void DebugifyEachInstrumentation::registerCallbacks(
   PIC.registerBeforeNonSkippedPassCallback([this](StringRef P, Any IR) {
     if (isIgnoredPass(P))
       return;
-    if (any_isa<const Function *>(IR))
-      applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR)),
+    if (const auto **F = any_cast<const Function *>(&IR))
+      applyDebugify(*const_cast<Function *>(*F),
                     Mode, DebugInfoBeforePass, P);
-    else if (any_isa<const Module *>(IR))
-      applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR)),
+    else if (const auto **M = any_cast<const Module *>(&IR))
+      applyDebugify(*const_cast<Module *>(*M),
                     Mode, DebugInfoBeforePass, P);
   });
   PIC.registerAfterPassCallback([this](StringRef P, Any IR,
                                        const PreservedAnalyses &PassPA) {
     if (isIgnoredPass(P))
       return;
-    if (any_isa<const Function *>(IR)) {
-      auto &F = *const_cast<Function *>(any_cast<const Function *>(IR));
+    if (const auto **CF = any_cast<const Function *>(&IR)) {
+      auto &F = *const_cast<Function *>(*CF);
       Module &M = *F.getParent();
       auto It = F.getIterator();
       if (Mode == DebugifyMode::SyntheticDebugInfo)
@@ -1054,8 +1054,8 @@ void DebugifyEachInstrumentation::registerCallbacks(
           M, make_range(It, std::next(It)), *DebugInfoBeforePass,
           "CheckModuleDebugify (original debuginfo)",
           P, OrigDIVerifyBugsReportFilePath);
-    } else if (any_isa<const Module *>(IR)) {
-      auto &M = *const_cast<Module *>(any_cast<const Module *>(IR));
+    } else if (const auto **CM = any_cast<const Module *>(&IR)) {
+      auto &M = *const_cast<Module *>(*CM);
       if (Mode == DebugifyMode::SyntheticDebugInfo)
        checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
                             /*Strip=*/true, DIStatsMap);

diff  --git a/llvm/unittests/ADT/AnyTest.cpp b/llvm/unittests/ADT/AnyTest.cpp
index 312cd745251e7..f76cae516d049 100644
--- a/llvm/unittests/ADT/AnyTest.cpp
+++ b/llvm/unittests/ADT/AnyTest.cpp
@@ -24,55 +24,55 @@ TEST(AnyTest, ConstructionAndAssignment) {
 
   // An empty Any is not anything.
   EXPECT_FALSE(A.has_value());
-  EXPECT_FALSE(any_isa<int>(A));
+  EXPECT_FALSE(any_cast<int>(&A));
 
   // An int is an int but not something else.
   EXPECT_TRUE(B.has_value());
-  EXPECT_TRUE(any_isa<int>(B));
-  EXPECT_FALSE(any_isa<float>(B));
+  EXPECT_TRUE(any_cast<int>(&B));
+  EXPECT_FALSE(any_cast<float>(&B));
 
   EXPECT_TRUE(C.has_value());
-  EXPECT_TRUE(any_isa<int>(C));
+  EXPECT_TRUE(any_cast<int>(&C));
 
   // A const char * is a const char * but not an int.
   EXPECT_TRUE(D.has_value());
-  EXPECT_TRUE(any_isa<const char *>(D));
-  EXPECT_FALSE(any_isa<int>(D));
+  EXPECT_TRUE(any_cast<const char *>(&D));
+  EXPECT_FALSE(any_cast<int>(&D));
 
   // A double is a double but not a float.
   EXPECT_TRUE(E.has_value());
-  EXPECT_TRUE(any_isa<double>(E));
-  EXPECT_FALSE(any_isa<float>(E));
+  EXPECT_TRUE(any_cast<double>(&E));
+  EXPECT_FALSE(any_cast<float>(&E));
 
   // After copy constructing from an int, the new item and old item are both
   // ints.
   llvm::Any F(B);
   EXPECT_TRUE(B.has_value());
   EXPECT_TRUE(F.has_value());
-  EXPECT_TRUE(any_isa<int>(F));
-  EXPECT_TRUE(any_isa<int>(B));
+  EXPECT_TRUE(any_cast<int>(&F));
+  EXPECT_TRUE(any_cast<int>(&B));
 
   // After move constructing from an int, the new item is an int and the old one
   // isn't.
   llvm::Any G(std::move(C));
   EXPECT_FALSE(C.has_value());
   EXPECT_TRUE(G.has_value());
-  EXPECT_TRUE(any_isa<int>(G));
-  EXPECT_FALSE(any_isa<int>(C));
+  EXPECT_TRUE(any_cast<int>(&G));
+  EXPECT_FALSE(any_cast<int>(&C));
 
   // After copy-assigning from an int, the new item and old item are both ints.
   A = F;
   EXPECT_TRUE(A.has_value());
   EXPECT_TRUE(F.has_value());
-  EXPECT_TRUE(any_isa<int>(A));
-  EXPECT_TRUE(any_isa<int>(F));
+  EXPECT_TRUE(any_cast<int>(&A));
+  EXPECT_TRUE(any_cast<int>(&F));
 
   // After move-assigning from an int, the new item and old item are both ints.
   B = std::move(G);
   EXPECT_TRUE(B.has_value());
   EXPECT_FALSE(G.has_value());
-  EXPECT_TRUE(any_isa<int>(B));
-  EXPECT_FALSE(any_isa<int>(G));
+  EXPECT_TRUE(any_cast<int>(&B));
+  EXPECT_FALSE(any_cast<int>(&G));
 }
 
 TEST(AnyTest, GoodAnyCast) {

diff  --git a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp
index c5947cfc381b7..befdf03fce7cd 100644
--- a/llvm/unittests/IR/PassBuilderCallbacksTest.cpp
+++ b/llvm/unittests/IR/PassBuilderCallbacksTest.cpp
@@ -290,17 +290,17 @@ template <> std::string getName(const StringRef &name) {
   return std::string(name);
 }
 
-template <> std::string getName(const llvm::Any &WrappedIR) {
-  if (any_isa<const Module *>(WrappedIR))
-    return any_cast<const Module *>(WrappedIR)->getName().str();
-  if (any_isa<const Function *>(WrappedIR))
-    return any_cast<const Function *>(WrappedIR)->getName().str();
-  if (any_isa<const Loop *>(WrappedIR))
-    return any_cast<const Loop *>(WrappedIR)->getName().str();
-  if (any_isa<const LoopNest *>(WrappedIR))
-    return any_cast<const LoopNest *>(WrappedIR)->getName().str();
-  if (any_isa<const LazyCallGraph::SCC *>(WrappedIR))
-    return any_cast<const LazyCallGraph::SCC *>(WrappedIR)->getName();
+template <> std::string getName(const Any &WrappedIR) {
+  if (const auto *const *M = any_cast<const Module *>(&WrappedIR))
+    return (*M)->getName().str();
+  if (const auto *const *F = any_cast<const Function *>(&WrappedIR))
+    return (*F)->getName().str();
+  if (const auto *const *L = any_cast<const Loop *>(&WrappedIR))
+    return (*L)->getName().str();
+  if (const auto *const *L = any_cast<const LoopNest *>(&WrappedIR))
+    return (*L)->getName().str();
+  if (const auto *const *C = any_cast<const LazyCallGraph::SCC *>(&WrappedIR))
+    return (*C)->getName();
   return "<UNKNOWN>";
 }
 /// Define a custom matcher for objects which support a 'getName' method.


        


More information about the llvm-commits mailing list