[llvm] [GC] Use `MapVector` for `GCStrategyMap` (PR #132729)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 24 21:01:05 PDT 2025


https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/132729

>From 76d7df65dafb79acb5abb8a70eb2eef8938c3827 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 24 Mar 2025 20:26:43 +0800
Subject: [PATCH 1/2] [GC] Use `MapVector` for `GCStrategyMap`

Use `MapVector`, so `GCStrategyMap` can support forward and reverse iterator, which is required in `AsmPrinter`.
---
 llvm/include/llvm/CodeGen/GCMetadata.h     | 37 ++++++++++++++++++++--
 llvm/lib/CodeGen/GCMetadata.cpp            | 12 +++----
 llvm/lib/CodeGen/ShadowStackGCLowering.cpp |  2 +-
 3 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GCMetadata.h b/llvm/include/llvm/CodeGen/GCMetadata.h
index ca6a511185c7c..2a4e37930cbe1 100644
--- a/llvm/include/llvm/CodeGen/GCMetadata.h
+++ b/llvm/include/llvm/CodeGen/GCMetadata.h
@@ -33,6 +33,7 @@
 #define LLVM_CODEGEN_GCMETADATA_H
 
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
@@ -151,15 +152,47 @@ class GCFunctionInfo {
   size_t live_size(const iterator &p) const { return roots_size(); }
 };
 
-struct GCStrategyMap {
-  StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
+class GCStrategyMap {
+  using MapT =
+      MapVector<std::string, std::unique_ptr<GCStrategy>, StringMap<unsigned>>;
+  MapT Strategies;
 
+public:
   GCStrategyMap() = default;
   GCStrategyMap(GCStrategyMap &&) = default;
 
   /// Handle invalidation explicitly.
   bool invalidate(Module &M, const PreservedAnalyses &PA,
                   ModuleAnalysisManager::Invalidator &Inv);
+
+  using iterator = MapT::iterator;
+  using const_iterator = MapT::const_iterator;
+  using reverse_iterator = MapT::reverse_iterator;
+  using const_reverse_iterator = MapT::const_reverse_iterator;
+
+  iterator begin() { return Strategies.begin(); }
+  const_iterator begin() const { return Strategies.begin(); }
+  iterator end() { return Strategies.end(); }
+  const_iterator end() const { return Strategies.end(); }
+
+  reverse_iterator rbegin() { return Strategies.rbegin(); }
+  const_reverse_iterator rbegin() const { return Strategies.rbegin(); }
+  reverse_iterator rend() { return Strategies.rend(); }
+  const_reverse_iterator rend() const { return Strategies.rend(); }
+
+  bool empty() const { return Strategies.empty(); }
+
+  std::unique_ptr<GCStrategy> &operator[](const std::string &GCName) {
+    return Strategies[GCName];
+  }
+
+  std::pair<iterator, bool> try_emplace(const std::string &GCName) {
+    return Strategies.try_emplace(GCName);
+  }
+
+  bool contains(const std::string &GCName) const {
+    return Strategies.find(GCName) != Strategies.end();
+  }
 };
 
 /// An analysis pass which caches information about the entire Module.
diff --git a/llvm/lib/CodeGen/GCMetadata.cpp b/llvm/lib/CodeGen/GCMetadata.cpp
index fa87b14e708e1..aec67a8d2e936 100644
--- a/llvm/lib/CodeGen/GCMetadata.cpp
+++ b/llvm/lib/CodeGen/GCMetadata.cpp
@@ -26,7 +26,7 @@ bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
   for (const auto &F : M) {
     if (F.isDeclaration() || !F.hasGC())
       continue;
-    if (!StrategyMap.contains(F.getGC()))
+    if (!contains(F.getGC()))
       return true;
   }
   return false;
@@ -36,17 +36,16 @@ AnalysisKey CollectorMetadataAnalysis::Key;
 
 CollectorMetadataAnalysis::Result
 CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
-  Result R;
-  auto &Map = R.StrategyMap;
+  Result StrategyMap;
   for (auto &F : M) {
     if (F.isDeclaration() || !F.hasGC())
       continue;
     auto GCName = F.getGC();
-    auto [It, Inserted] = Map.try_emplace(GCName);
+    auto [It, Inserted] = StrategyMap.try_emplace(GCName);
     if (Inserted)
       It->second = getGCStrategy(GCName);
   }
-  return R;
+  return StrategyMap;
 }
 
 AnalysisKey GCFunctionAnalysis::Key;
@@ -61,8 +60,7 @@ GCFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
       MAMProxy.cachedResultExists<CollectorMetadataAnalysis>(*F.getParent()) &&
       "This pass need module analysis `collector-metadata`!");
   auto &Map =
-      MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent())
-          ->StrategyMap;
+      *MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent());
   GCFunctionInfo Info(F, *Map[F.getGC()]);
   return Info;
 }
diff --git a/llvm/lib/CodeGen/ShadowStackGCLowering.cpp b/llvm/lib/CodeGen/ShadowStackGCLowering.cpp
index 60c8372577a93..1f9beb84ef62d 100644
--- a/llvm/lib/CodeGen/ShadowStackGCLowering.cpp
+++ b/llvm/lib/CodeGen/ShadowStackGCLowering.cpp
@@ -109,7 +109,7 @@ class ShadowStackGCLowering : public FunctionPass {
 PreservedAnalyses ShadowStackGCLoweringPass::run(Module &M,
                                                  ModuleAnalysisManager &MAM) {
   auto &Map = MAM.getResult<CollectorMetadataAnalysis>(M);
-  if (Map.StrategyMap.contains("shadow-stack"))
+  if (!Map.contains("shadow-stack"))
     return PreservedAnalyses::all();
 
   ShadowStackGCLoweringImpl Impl;

>From 6ab0128dddf43e0888c53738cd7db9873315fe33 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Tue, 25 Mar 2025 12:00:50 +0800
Subject: [PATCH 2/2] address comments

---
 llvm/include/llvm/CodeGen/GCMetadata.h | 10 ++++------
 llvm/lib/CodeGen/GCMetadata.cpp        |  2 +-
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GCMetadata.h b/llvm/include/llvm/CodeGen/GCMetadata.h
index 2a4e37930cbe1..a3b284ef0ca30 100644
--- a/llvm/include/llvm/CodeGen/GCMetadata.h
+++ b/llvm/include/llvm/CodeGen/GCMetadata.h
@@ -154,7 +154,7 @@ class GCFunctionInfo {
 
 class GCStrategyMap {
   using MapT =
-      MapVector<std::string, std::unique_ptr<GCStrategy>, StringMap<unsigned>>;
+      MapVector<StringRef, std::unique_ptr<GCStrategy>, StringMap<unsigned>>;
   MapT Strategies;
 
 public:
@@ -182,15 +182,13 @@ class GCStrategyMap {
 
   bool empty() const { return Strategies.empty(); }
 
-  std::unique_ptr<GCStrategy> &operator[](const std::string &GCName) {
-    return Strategies[GCName];
-  }
+  GCStrategy &operator[](StringRef GCName) { return *Strategies[GCName]; }
 
-  std::pair<iterator, bool> try_emplace(const std::string &GCName) {
+  std::pair<iterator, bool> try_emplace(StringRef GCName) {
     return Strategies.try_emplace(GCName);
   }
 
-  bool contains(const std::string &GCName) const {
+  bool contains(StringRef GCName) const {
     return Strategies.find(GCName) != Strategies.end();
   }
 };
diff --git a/llvm/lib/CodeGen/GCMetadata.cpp b/llvm/lib/CodeGen/GCMetadata.cpp
index aec67a8d2e936..09c328cafe406 100644
--- a/llvm/lib/CodeGen/GCMetadata.cpp
+++ b/llvm/lib/CodeGen/GCMetadata.cpp
@@ -40,7 +40,7 @@ CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
   for (auto &F : M) {
     if (F.isDeclaration() || !F.hasGC())
       continue;
-    auto GCName = F.getGC();
+    StringRef GCName = F.getGC();
     auto [It, Inserted] = StrategyMap.try_emplace(GCName);
     if (Inserted)
       It->second = getGCStrategy(GCName);



More information about the llvm-commits mailing list