Skip to content

[GC] Use MapVector for GCStrategyMap #132729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions llvm/include/llvm/CodeGen/GCMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do these names come from? Can this use StringRef? Cursory search seems to suggest these are already live in the LLVMContext

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];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::unique_ptr<GCStrategy> &operator[](const std::string &GCName) {
return Strategies[GCName];
}
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.
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/CodeGen/GCMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/ShadowStackGCLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading