Skip to content

Commit 06aa8b1

Browse files
authored
[CodeGen] Add analyses to help for porting GC passes (#74972)
- `CollectorMetadataAnalysis` provides `GCStrategyMap`. - `GCFunctionAnalysis` provides `GCFunctionInfo`. `GCStrategyMap` owns `GCStrategy` pointers and this pass is used by `AsmPrinter` to iterate all GC strategies. Most passes that require `GCModuleInfo` actually require the `GCFunctionInfo`, so add `GCFunctionAnalysis` for convenience.
1 parent dbf67ea commit 06aa8b1

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/CodeGen/CallBrPrepare.h"
2727
#include "llvm/CodeGen/DwarfEHPrepare.h"
2828
#include "llvm/CodeGen/ExpandReductions.h"
29+
#include "llvm/CodeGen/GCMetadata.h"
2930
#include "llvm/CodeGen/InterleavedAccess.h"
3031
#include "llvm/CodeGen/InterleavedLoadCombine.h"
3132
#include "llvm/CodeGen/JMCInstrumenter.h"

llvm/include/llvm/CodeGen/GCMetadata.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/ADT/StringRef.h"
3939
#include "llvm/IR/DebugLoc.h"
4040
#include "llvm/IR/GCStrategy.h"
41+
#include "llvm/IR/PassManager.h"
4142
#include "llvm/Pass.h"
4243
#include <algorithm>
4344
#include <cstddef>
@@ -101,6 +102,10 @@ class GCFunctionInfo {
101102
GCFunctionInfo(const Function &F, GCStrategy &S);
102103
~GCFunctionInfo();
103104

105+
/// Handle invalidation explicitly.
106+
bool invalidate(Function &F, const PreservedAnalyses &PA,
107+
FunctionAnalysisManager::Invalidator &Inv);
108+
104109
/// getFunction - Return the function to which this metadata applies.
105110
const Function &getFunction() const { return F; }
106111

@@ -146,6 +151,41 @@ class GCFunctionInfo {
146151
size_t live_size(const iterator &p) const { return roots_size(); }
147152
};
148153

154+
struct GCStrategyMap {
155+
StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
156+
157+
GCStrategyMap() = default;
158+
GCStrategyMap(GCStrategyMap &&) = default;
159+
160+
/// Handle invalidation explicitly.
161+
bool invalidate(Module &M, const PreservedAnalyses &PA,
162+
ModuleAnalysisManager::Invalidator &Inv);
163+
};
164+
165+
/// An analysis pass which caches information about the entire Module.
166+
/// Records a cache of the 'active' gc strategy objects for the current Module.
167+
class CollectorMetadataAnalysis
168+
: public AnalysisInfoMixin<CollectorMetadataAnalysis> {
169+
friend struct AnalysisInfoMixin<CollectorMetadataAnalysis>;
170+
static AnalysisKey Key;
171+
172+
public:
173+
using Result = GCStrategyMap;
174+
Result run(Module &M, ModuleAnalysisManager &MAM);
175+
};
176+
177+
/// An analysis pass which caches information about the Function.
178+
/// Records the function level information used by GCRoots.
179+
/// This pass depends on `CollectorMetadataAnalysis`.
180+
class GCFunctionAnalysis : public AnalysisInfoMixin<GCFunctionAnalysis> {
181+
friend struct AnalysisInfoMixin<GCFunctionAnalysis>;
182+
static AnalysisKey Key;
183+
184+
public:
185+
using Result = GCFunctionInfo;
186+
Result run(Function &F, FunctionAnalysisManager &FAM);
187+
};
188+
149189
/// An analysis pass which caches information about the entire Module.
150190
/// Records both the function level information used by GCRoots and a
151191
/// cache of the 'active' gc strategy objects for the current Module.

llvm/include/llvm/CodeGen/MachinePassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef MODULE_ANALYSIS
1717
#define MODULE_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR)
1818
#endif
19+
MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis, ())
1920
MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis, (PIC))
2021
#undef MODULE_ANALYSIS
2122

@@ -29,6 +30,7 @@ MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass, ())
2930
#ifndef FUNCTION_ANALYSIS
3031
#define FUNCTION_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR)
3132
#endif
33+
FUNCTION_ANALYSIS("gc-function", GCFunctionAnalysis, ())
3234
FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis, (PIC))
3335
FUNCTION_ANALYSIS("targetir", TargetIRAnalysis,
3436
(std::move(TM.getTargetIRAnalysis())))

llvm/lib/CodeGen/GCMetadata.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,50 @@
2424

2525
using namespace llvm;
2626

27+
bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
28+
ModuleAnalysisManager::Invalidator &) {
29+
for (const auto &F : M) {
30+
if (F.isDeclaration() || !F.hasGC())
31+
continue;
32+
if (!StrategyMap.contains(F.getGC()))
33+
return true;
34+
}
35+
return false;
36+
}
37+
38+
AnalysisKey CollectorMetadataAnalysis::Key;
39+
40+
CollectorMetadataAnalysis::Result
41+
CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
42+
Result R;
43+
auto &Map = R.StrategyMap;
44+
for (auto &F : M) {
45+
if (F.isDeclaration() || !F.hasGC())
46+
continue;
47+
if (auto GCName = F.getGC(); !Map.contains(GCName))
48+
Map[GCName] = getGCStrategy(GCName);
49+
}
50+
return R;
51+
}
52+
53+
AnalysisKey GCFunctionAnalysis::Key;
54+
55+
GCFunctionAnalysis::Result
56+
GCFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
57+
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
58+
assert(F.hasGC() && "Function doesn't have GC!");
59+
60+
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
61+
assert(
62+
MAMProxy.cachedResultExists<CollectorMetadataAnalysis>(*F.getParent()) &&
63+
"This pass need module analysis `collector-metadata`!");
64+
auto &Map =
65+
MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent())
66+
->StrategyMap;
67+
GCFunctionInfo Info(F, *Map[F.getGC()]);
68+
return Info;
69+
}
70+
2771
INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
2872
"Create Garbage Collector Module Metadata", false, false)
2973

@@ -34,6 +78,12 @@ GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
3478

3579
GCFunctionInfo::~GCFunctionInfo() = default;
3680

81+
bool GCFunctionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
82+
FunctionAnalysisManager::Invalidator &) {
83+
auto PAC = PA.getChecker<GCFunctionAnalysis>();
84+
return !PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>();
85+
}
86+
3787
// -----------------------------------------------------------------------------
3888

3989
char GCModuleInfo::ID = 0;

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "llvm/CodeGen/DwarfEHPrepare.h"
7777
#include "llvm/CodeGen/ExpandLargeDivRem.h"
7878
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
79+
#include "llvm/CodeGen/GCMetadata.h"
7980
#include "llvm/CodeGen/HardwareLoops.h"
8081
#include "llvm/CodeGen/InterleavedAccess.h"
8182
#include "llvm/CodeGen/InterleavedLoadCombine.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define MODULE_ANALYSIS(NAME, CREATE_PASS)
2020
#endif
2121
MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
22+
MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
2223
MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
2324
MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
2425
MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
@@ -235,6 +236,7 @@ FUNCTION_ANALYSIS("demanded-bits", DemandedBitsAnalysis())
235236
FUNCTION_ANALYSIS("domfrontier", DominanceFrontierAnalysis())
236237
FUNCTION_ANALYSIS("domtree", DominatorTreeAnalysis())
237238
FUNCTION_ANALYSIS("func-properties", FunctionPropertiesAnalysis())
239+
FUNCTION_ANALYSIS("gc-function", GCFunctionAnalysis())
238240
FUNCTION_ANALYSIS("inliner-size-estimator", InlineSizeEstimatorAnalysis())
239241
FUNCTION_ANALYSIS("lazy-value-info", LazyValueAnalysis())
240242
FUNCTION_ANALYSIS("loops", LoopAnalysis())

0 commit comments

Comments
 (0)