[llvm] [StaticDataLayout][PGO] Add profile format for static data layout, and the classes to operate on the profiles. (PR #138170)
Mingming Liu via llvm-commits
llvm-commits at lists.llvm.org
Tue May 6 16:36:15 PDT 2025
================
@@ -0,0 +1,257 @@
+#include "llvm/ProfileData/DataAccessProf.h"
+#include "llvm/ADT/DenseMapInfoVariant.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ProfileData/InstrProf.h"
+#include "llvm/Support/Compression.h"
+#include "llvm/Support/Endian.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/StringSaver.h"
+#include "llvm/Support/raw_ostream.h"
+#include <sys/types.h>
+
+namespace llvm {
+namespace data_access_prof {
+
+// If `Map` has an entry keyed by `Str`, returns the entry iterator. Otherwise,
+// creates an owned copy of `Str`, adds a map entry for it and returns the
+// iterator.
+static MapVector<StringRef, uint64_t>::iterator
+saveStringToMap(MapVector<StringRef, uint64_t> &Map,
+ llvm::UniqueStringSaver &Saver, StringRef Str) {
+ auto [Iter, Inserted] = Map.try_emplace(Saver.save(Str), Map.size());
+ return Iter;
+}
+
+// Returns the canonical name or error.
+static Expected<StringRef> getCanonicalName(StringRef Name) {
+ if (Name.empty())
+ return make_error<StringError>("Empty symbol name",
+ llvm::errc::invalid_argument);
+ return InstrProfSymtab::getCanonicalName(Name);
+}
+
+const DataAccessProfRecord *
+DataAccessProfData::getProfileRecord(const SymbolHandle SymbolID) const {
+ auto Key = SymbolID;
+ if (std::holds_alternative<StringRef>(SymbolID)) {
+ StringRef Name = std::get<StringRef>(SymbolID);
+ assert(!Name.empty() && "Empty symbol name");
+ Key = InstrProfSymtab::getCanonicalName(Name);
----------------
mingmingl-llvm wrote:
Done.
This `getProfileRecord` suppresses name canonicalization error rather than further propagating it, mainly to make the return value type simple (i.e., no need to handle `Expected<Ptr>` type) for the common no-error path.
Added assertion to make the code intention clearer.
https://github.com/llvm/llvm-project/pull/138170
More information about the llvm-commits
mailing list