Skip to content

[IR] Optimize CFI in writeCombinedGlobalValueSummary #130382

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
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 41 additions & 24 deletions llvm/include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -1291,52 +1291,69 @@ struct TypeIdSummary {
};

class CfiFunctionIndex {
std::set<std::string, std::less<>> Index;
DenseMap<GlobalValue::GUID, std::set<std::string, std::less<>>> Index;
using IndexIterator =
DenseMap<GlobalValue::GUID,
std::set<std::string, std::less<>>>::const_iterator;
using NestedIterator = std::set<std::string, std::less<>>::const_iterator;

public:
class GUIDIterator
: public iterator_adaptor_base<
GUIDIterator, std::set<std::string, std::less<>>::const_iterator,
std::forward_iterator_tag, GlobalValue::GUID> {
using base = iterator_adaptor_base<
GUIDIterator, std::set<std::string, std::less<>>::const_iterator,
std::forward_iterator_tag, GlobalValue::GUID>;
// Iterates keys of the DenseMap.
class GUIDIterator : public iterator_adaptor_base<GUIDIterator, IndexIterator,
std::forward_iterator_tag,
GlobalValue::GUID> {
using base = GUIDIterator::iterator_adaptor_base;

public:
GUIDIterator() = default;
explicit GUIDIterator(std::set<std::string, std::less<>>::const_iterator I)
: base(std::move(I)) {}
explicit GUIDIterator(IndexIterator I) : base(I) {}

GlobalValue::GUID operator*() const {
return GlobalValue::getGUID(
GlobalValue::dropLLVMManglingEscape(*this->wrapped()));
}
GlobalValue::GUID operator*() const { return this->wrapped()->first; }
};

CfiFunctionIndex() = default;
template <typename It> CfiFunctionIndex(It B, It E) : Index(B, E) {}

std::set<std::string, std::less<>>::const_iterator begin() const {
return Index.begin();
template <typename It> CfiFunctionIndex(It B, It E) {
for (; B != E; ++B)
emplace(*B);
}

std::set<std::string, std::less<>>::const_iterator end() const {
return Index.end();
std::vector<StringRef> symbols() const {
std::vector<StringRef> Symbols;
for (auto &[GUID, Syms] : Index)
Symbols.insert(Symbols.end(), Syms.begin(), Syms.end());
return Symbols;
}

std::vector<StringRef> symbols() const { return {begin(), end()}; }

GUIDIterator guid_begin() const { return GUIDIterator(Index.begin()); }
GUIDIterator guid_end() const { return GUIDIterator(Index.end()); }
iterator_range<GUIDIterator> guids() const {
return make_range(guid_begin(), guid_end());
}

iterator_range<NestedIterator> forGuid(GlobalValue::GUID GUID) const {
auto I = Index.find(GUID);
if (I == Index.end())
return make_range(NestedIterator{}, NestedIterator{});
return make_range(I->second.begin(), I->second.end());
}

template <typename... Args> void emplace(Args &&...A) {
Index.emplace(std::forward<Args>(A)...);
StringRef S(std::forward<Args>(A)...);
GlobalValue::GUID GUID =
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
Index[GUID].emplace(S);
}

size_t count(StringRef S) const {
GlobalValue::GUID GUID =
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
auto I = Index.find(GUID);
if (I == Index.end())
return 0;
return I->second.count(S);
}

size_t count(StringRef S) const { return Index.count(S); }
bool empty() const { return Index.empty(); }
};

/// 160 bits SHA1
Expand Down
24 changes: 15 additions & 9 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5064,19 +5064,25 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
getReferencedTypeIds(FS, ReferencedTypeIds);
}

SmallVector<StringRef, 4> Functions;
auto EmitCfiFunctions = [&](const CfiFunctionIndex &CfiIndex,
bitc::GlobalValueSummarySymtabCodes Code) {
for (auto &S : CfiIndex) {
if (DefOrUseGUIDs.contains(
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S)))) {
NameVals.push_back(StrtabBuilder.add(S));
NameVals.push_back(S.size());
}
if (CfiIndex.empty())
return;
for (GlobalValue::GUID GUID : DefOrUseGUIDs) {
auto Defs = CfiIndex.forGuid(GUID);
Functions.insert(Functions.end(), Defs.begin(), Defs.end());
}
if (!NameVals.empty()) {
Stream.EmitRecord(Code, NameVals);
NameVals.clear();
if (Functions.empty())
return;
llvm::sort(Functions);
for (const auto &S : Functions) {
NameVals.push_back(StrtabBuilder.add(S));
NameVals.push_back(S.size());
}
Stream.EmitRecord(Code, NameVals);
NameVals.clear();
Functions.clear();
};

EmitCfiFunctions(Index.cfiFunctionDefs(), bitc::FS_CFI_FUNCTION_DEFS);
Expand Down
Loading