[PATCH] D100559: [GC][NFC] Make getGCStrategy by name available in Analysis

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 15 06:52:41 PDT 2021


mkazantsev created this revision.
mkazantsev added reviewers: reames, apilipenko, anna.
Herald added a subscriber: hiraditya.
mkazantsev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We might want to use info from GC strategy in middle end analysis.
The motivation for this is provided in D99135 <https://reviews.llvm.org/D99135>: we may want to ask
a GC if it's going to work with a given pointer (currently this code
makes naive check by the method name).


https://reviews.llvm.org/D100559

Files:
  llvm/include/llvm/Analysis/GCStrategy.h
  llvm/lib/Analysis/GCStrategy.cpp
  llvm/lib/CodeGen/GCMetadata.cpp


Index: llvm/lib/CodeGen/GCMetadata.cpp
===================================================================
--- llvm/lib/CodeGen/GCMetadata.cpp
+++ llvm/lib/CodeGen/GCMetadata.cpp
@@ -145,24 +145,9 @@
   if (NMI != GCStrategyMap.end())
     return NMI->getValue();
 
-  for (auto& Entry : GCRegistry::entries()) {
-    if (Name == Entry.getName()) {
-      std::unique_ptr<GCStrategy> S = Entry.instantiate();
-      S->Name = std::string(Name);
-      GCStrategyMap[Name] = S.get();
-      GCStrategyList.push_back(std::move(S));
-      return GCStrategyList.back().get();
-    }
-  }
-
-  if (GCRegistry::begin() == GCRegistry::end()) {
-    // In normal operation, the registry should not be empty.  There should
-    // be the builtin GCs if nothing else.  The most likely scenario here is
-    // that we got here without running the initializers used by the Registry
-    // itself and it's registration mechanism.
-    const std::string error = ("unsupported GC: " + Name).str() +
-      " (did you remember to link and initialize the CodeGen library?)";
-    report_fatal_error(error);
-  } else
-    report_fatal_error(std::string("unsupported GC: ") + Name);
+  std::unique_ptr<GCStrategy> S = llvm::getGCStrategy(Name);
+  S->Name = std::string(Name);
+  GCStrategyMap[Name] = S.get();
+  GCStrategyList.push_back(std::move(S));
+  return GCStrategyList.back().get();
 }
Index: llvm/lib/Analysis/GCStrategy.cpp
===================================================================
--- llvm/lib/Analysis/GCStrategy.cpp
+++ llvm/lib/Analysis/GCStrategy.cpp
@@ -18,3 +18,21 @@
 LLVM_INSTANTIATE_REGISTRY(GCRegistry)
 
 GCStrategy::GCStrategy() = default;
+
+std::unique_ptr<GCStrategy> llvm::getGCStrategy(const StringRef Name) {
+  for (auto &S : GCRegistry::entries())
+    if (S.getName() == Name)
+      return S.instantiate();
+
+  if (GCRegistry::begin() == GCRegistry::end()) {
+    // In normal operation, the registry should not be empty.  There should
+    // be the builtin GCs if nothing else.  The most likely scenario here is
+    // that we got here without running the initializers used by the Registry
+    // itself and it's registration mechanism.
+    const std::string error =
+        std::string("unsupported GC: ") + Name.str() +
+        " (did you remember to link and initialize the Analysis library?)";
+    report_fatal_error(error);
+  } else
+    report_fatal_error(std::string("unsupported GC: ") + Name.str());
+}
Index: llvm/include/llvm/Analysis/GCStrategy.h
===================================================================
--- llvm/include/llvm/Analysis/GCStrategy.h
+++ llvm/include/llvm/Analysis/GCStrategy.h
@@ -131,6 +131,9 @@
 /// GCMetadataPrinterRegistery as well.
 using GCRegistry = Registry<GCStrategy>;
 
+/// Lookup the GCStrategy object associated with the given gc name.
+std::unique_ptr<GCStrategy> getGCStrategy(const StringRef Name);
+
 } // end namespace llvm
 
 #endif // LLVM_ANALYSIS_GCSTRATEGY_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100559.337744.patch
Type: text/x-patch
Size: 2962 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210415/b4063c00/attachment.bin>


More information about the llvm-commits mailing list