Skip to content

Commit dfccdac

Browse files
committed
[GC] Support bidirectional iterator in GCStrategyMap
1 parent dd3edc8 commit dfccdac

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.
@@ -58,10 +61,12 @@ class GCMetadataPrinter {
5861
/// Called before the assembly for the module is generated by
5962
/// the AsmPrinter (but after target specific hooks.)
6063
virtual void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
64+
virtual void beginAssembly(Module &M, GCStrategyMap &Map, AsmPrinter &AP) {}
6165

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

6671
/// Called when the stack maps are generated. Return true if
6772
/// 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
@@ -23,6 +23,10 @@ using namespace llvm;
2323

2424
bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
2525
ModuleAnalysisManager::Invalidator &) {
26+
auto PAC = PA.getChecker<CollectorMetadataAnalysis>();
27+
if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>())
28+
return false;
29+
2630
for (const auto &F : M) {
2731
if (F.isDeclaration() || !F.hasGC())
2832
continue;
@@ -36,17 +40,22 @@ AnalysisKey CollectorMetadataAnalysis::Key;
3640

3741
CollectorMetadataAnalysis::Result
3842
CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
39-
Result R;
40-
auto &Map = R.StrategyMap;
43+
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
44+
Result R(FAM);
4145
for (auto &F : M) {
4246
if (F.isDeclaration() || !F.hasGC())
4347
continue;
44-
if (auto GCName = F.getGC(); !Map.contains(GCName))
45-
Map[GCName] = getGCStrategy(GCName);
48+
auto GCName = F.getGC();
49+
R.insert(GCName, getGCStrategy(GCName));
4650
}
4751
return R;
4852
}
4953

54+
GCFunctionInfo &llvm::GCStrategyMap::getFunctionInfo(Function &F) {
55+
assert(FAM && "Need initialize!");
56+
return FAM->getResult<GCFunctionAnalysis>(F);
57+
}
58+
5059
AnalysisKey GCFunctionAnalysis::Key;
5160

5261
GCFunctionAnalysis::Result
@@ -59,9 +68,8 @@ GCFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
5968
MAMProxy.cachedResultExists<CollectorMetadataAnalysis>(*F.getParent()) &&
6069
"This pass need module analysis `collector-metadata`!");
6170
auto &Map =
62-
MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent())
63-
->StrategyMap;
64-
GCFunctionInfo Info(F, *Map[F.getGC()]);
71+
*MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent());
72+
GCFunctionInfo Info(F, Map.at(F.getGC()));
6573
return Info;
6674
}
6775

llvm/lib/CodeGen/ShadowStackGCLowering.cpp

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

115115
ShadowStackGCLoweringImpl Impl;

0 commit comments

Comments
 (0)