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

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 24 07:05:43 PDT 2025


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

>From e3b7620abbf8d2d76479e8f9bd37eda12ae24df2 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 24 Mar 2025 20:26:43 +0800
Subject: [PATCH] [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 |  7 ++++---
 llvm/lib/CodeGen/GCMetadata.cpp        | 12 +++++-------
 2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GCMetadata.h b/llvm/include/llvm/CodeGen/GCMetadata.h
index ca6a511185c7c..c19bea9531ddd 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,9 +152,9 @@ class GCFunctionInfo {
   size_t live_size(const iterator &p) const { return roots_size(); }
 };
 
-struct GCStrategyMap {
-  StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
-
+class GCStrategyMap : public MapVector<std::string, std::unique_ptr<GCStrategy>,
+                                       StringMap<unsigned>> {
+public:
   GCStrategyMap() = default;
   GCStrategyMap(GCStrategyMap &&) = default;
 
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;
 }



More information about the llvm-commits mailing list