Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion llvm/include/llvm/CodeGen/GCMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,46 @@ class GCFunctionInfo {
size_t live_size(const iterator &p) const { return roots_size(); }
};

struct GCStrategyMap {
class GCStrategyMap {
StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
SmallVector<GCStrategy *, 1> StrategyList; // For bidirectional iterator.
Comment on lines 155 to +156
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be a set vector?

Not really sure why these need indexing by name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need a flat map here:

GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
assert(MI && "AsmPrinter didn't require GCModuleInfo?");
for (const auto &I : *MI)
if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
MP->beginAssembly(M, *MI, *this);

for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
if (GCMetadataPrinter *MP = getOrCreateGCPrinter(**--I))
MP->finishAssembly(M, *MI, *this);

Indexed by name here because GC printers need it (see OCamlGCPrinter.cpp).

Copy link
Contributor

Choose a reason for hiding this comment

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

How many entries will this have? 1? 2? Can it just get rid of the map?

using StrategyListT = SmallVector<GCStrategy *, 1>;

FunctionAnalysisManager *FAM = nullptr;

public:
GCStrategyMap() = default;
GCStrategyMap(GCStrategyMap &&) = default;
GCStrategyMap(FunctionAnalysisManager &FAM) : FAM(&FAM) {}

/// Handle invalidation explicitly.
bool invalidate(Module &M, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &Inv);

GCFunctionInfo &getFunctionInfo(Function &F);

bool empty() const { return StrategyMap.empty(); }

bool contains(StringRef Name) const { return StrategyMap.contains(Name); }

/// Insert a new strategy if it is not existed, otherwise do nothing.
void insert(StringRef Name, std::unique_ptr<GCStrategy> Strategy) {
auto &S = StrategyMap[Name];
Copy link
Contributor

Choose a reason for hiding this comment

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

Use insert and check if insert succeeded?

if (!S) {
S = std::move(Strategy);
StrategyList.push_back(S.get());
}
}

GCStrategy &at(StringRef Name) { return *StrategyMap.at(Name); }

// This class must support bidirectional iterator which is used by AsmPrinter.
using iterator = StrategyListT::iterator;
iterator begin() { return StrategyList.begin(); }
iterator end() { return StrategyList.end(); }
using reverse_iterator = StrategyListT::reverse_iterator;
reverse_iterator rbegin() { return StrategyList.rbegin(); }
reverse_iterator rend() { return StrategyList.rend(); }
};

/// An analysis pass which caches information about the entire Module.
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/GCMetadataPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class AsmPrinter;
class GCMetadataPrinter;
class GCModuleInfo;
class GCStrategy;
class GCStrategyMap;
class Module;
class StackMaps;
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
using ModuleAnalysisManager = AnalysisManager<Module>;

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

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

/// Called when the stack maps are generated. Return true if
/// stack maps with a custom format are generated. Otherwise
Expand Down
22 changes: 15 additions & 7 deletions llvm/lib/CodeGen/GCMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ using namespace llvm;

bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &) {
auto PAC = PA.getChecker<CollectorMetadataAnalysis>();
if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>())
return false;

for (const auto &F : M) {
if (F.isDeclaration() || !F.hasGC())
continue;
Expand All @@ -40,17 +44,22 @@ AnalysisKey CollectorMetadataAnalysis::Key;

CollectorMetadataAnalysis::Result
CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
Result R;
auto &Map = R.StrategyMap;
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
Result R(FAM);
for (auto &F : M) {
if (F.isDeclaration() || !F.hasGC())
continue;
if (auto GCName = F.getGC(); !Map.contains(GCName))
Map[GCName] = getGCStrategy(GCName);
auto GCName = F.getGC();
R.insert(GCName, getGCStrategy(GCName));
}
return R;
}

GCFunctionInfo &llvm::GCStrategyMap::getFunctionInfo(Function &F) {
assert(FAM && "Need initialize!");
return FAM->getResult<GCFunctionAnalysis>(F);
}

AnalysisKey GCFunctionAnalysis::Key;

GCFunctionAnalysis::Result
Expand All @@ -63,9 +72,8 @@ 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;
GCFunctionInfo Info(F, *Map[F.getGC()]);
*MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent());
GCFunctionInfo Info(F, Map.at(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 @@ -110,7 +110,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