Skip to content

Commit f374e7b

Browse files
committed
[GC] Support bidirectional iterator in GCStrategyMap
1 parent b05ccaf commit f374e7b

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

llvm/include/llvm/CodeGen/GCMetadata.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,46 @@ class GCFunctionInfo {
151151
size_t live_size(const iterator &p) const { return roots_size(); }
152152
};
153153

154-
struct GCStrategyMap {
154+
class GCStrategyMap {
155155
StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
156+
SmallVector<GCStrategy *, 1> StrategyList; // For bidirectional iterator.
157+
using StrategyListT = SmallVector<GCStrategy *, 1>;
156158

159+
FunctionAnalysisManager *FAM = nullptr;
160+
161+
public:
157162
GCStrategyMap() = default;
158163
GCStrategyMap(GCStrategyMap &&) = default;
164+
GCStrategyMap(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
159165

160166
/// Handle invalidation explicitly.
161167
bool invalidate(Module &M, const PreservedAnalyses &PA,
162168
ModuleAnalysisManager::Invalidator &Inv);
169+
170+
GCFunctionInfo &getFunctionInfo(Function &F);
171+
172+
bool empty() const { return StrategyMap.empty(); }
173+
174+
bool contains(StringRef Name) const { return StrategyMap.contains(Name); }
175+
176+
/// Insert a new strategy if it is not existed, otherwise do nothing.
177+
void insert(StringRef Name, std::unique_ptr<GCStrategy> Strategy) {
178+
auto &S = StrategyMap[Name];
179+
if (!S) {
180+
S = std::move(Strategy);
181+
StrategyList.push_back(S.get());
182+
}
183+
}
184+
185+
GCStrategy &at(StringRef Name) { return *StrategyMap.at(Name); }
186+
187+
// This class must support bidirectional iterator which is used by AsmPrinter.
188+
using iterator = StrategyListT::iterator;
189+
iterator begin() { return StrategyList.begin(); }
190+
iterator end() { return StrategyList.end(); }
191+
using reverse_iterator = StrategyListT::reverse_iterator;
192+
reverse_iterator rbegin() { return StrategyList.rbegin(); }
193+
reverse_iterator rend() { return StrategyList.rend(); }
163194
};
164195

165196
/// An analysis pass which caches information about the entire Module.

llvm/include/llvm/CodeGen/GCMetadataPrinter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ class AsmPrinter;
2727
class GCMetadataPrinter;
2828
class GCModuleInfo;
2929
class GCStrategy;
30+
class GCStrategyMap;
3031
class Module;
3132
class StackMaps;
33+
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
34+
using ModuleAnalysisManager = AnalysisManager<Module>;
3235

3336
/// GCMetadataPrinterRegistry - The GC assembly printer registry uses all the
3437
/// defaults from Registry.
@@ -56,10 +59,12 @@ class GCMetadataPrinter {
5659
/// Called before the assembly for the module is generated by
5760
/// the AsmPrinter (but after target specific hooks.)
5861
virtual void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
62+
virtual void beginAssembly(Module &M, GCStrategyMap &Map, AsmPrinter &AP) {}
5963

6064
/// Called after the assembly for the module is generated by
6165
/// the AsmPrinter (but before target specific hooks)
6266
virtual void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
67+
virtual void finishAssembly(Module &M, GCStrategyMap &Map, AsmPrinter &AP) {}
6368

6469
/// Called when the stack maps are generated. Return true if
6570
/// stack maps with a custom format are generated. Otherwise

llvm/lib/CodeGen/GCMetadata.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ using namespace llvm;
2727

2828
bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
2929
ModuleAnalysisManager::Invalidator &) {
30+
auto PAC = PA.getChecker<CollectorMetadataAnalysis>();
31+
if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>())
32+
return false;
33+
3034
for (const auto &F : M) {
3135
if (F.isDeclaration() || !F.hasGC())
3236
continue;
@@ -40,17 +44,22 @@ AnalysisKey CollectorMetadataAnalysis::Key;
4044

4145
CollectorMetadataAnalysis::Result
4246
CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
43-
Result R;
44-
auto &Map = R.StrategyMap;
47+
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
48+
Result R(FAM);
4549
for (auto &F : M) {
4650
if (F.isDeclaration() || !F.hasGC())
4751
continue;
48-
if (auto GCName = F.getGC(); !Map.contains(GCName))
49-
Map[GCName] = getGCStrategy(GCName);
52+
auto GCName = F.getGC();
53+
R.insert(GCName, getGCStrategy(GCName));
5054
}
5155
return R;
5256
}
5357

58+
GCFunctionInfo &llvm::GCStrategyMap::getFunctionInfo(Function &F) {
59+
assert(FAM && "Need initialize!");
60+
return FAM->getResult<GCFunctionAnalysis>(F);
61+
}
62+
5463
AnalysisKey GCFunctionAnalysis::Key;
5564

5665
GCFunctionAnalysis::Result
@@ -63,9 +72,8 @@ GCFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
6372
MAMProxy.cachedResultExists<CollectorMetadataAnalysis>(*F.getParent()) &&
6473
"This pass need module analysis `collector-metadata`!");
6574
auto &Map =
66-
MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent())
67-
->StrategyMap;
68-
GCFunctionInfo Info(F, *Map[F.getGC()]);
75+
*MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent());
76+
GCFunctionInfo Info(F, Map.at(F.getGC()));
6977
return Info;
7078
}
7179

llvm/lib/CodeGen/ShadowStackGCLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class ShadowStackGCLowering : public FunctionPass {
110110
PreservedAnalyses ShadowStackGCLoweringPass::run(Module &M,
111111
ModuleAnalysisManager &MAM) {
112112
auto &Map = MAM.getResult<CollectorMetadataAnalysis>(M);
113-
if (Map.StrategyMap.contains("shadow-stack"))
113+
if (Map.contains("shadow-stack"))
114114
return PreservedAnalyses::all();
115115

116116
ShadowStackGCLoweringImpl Impl;

0 commit comments

Comments
 (0)