[llvm] [SampleFDO][TypeProf]Support vtable type profiling for ext-binary and text format (PR #148002)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 26 11:41:32 PDT 2025


================
@@ -1000,6 +1028,46 @@ class FunctionSamples {
     return CallsiteSamples;
   }
 
+  /// Returns vtable access samples for the C++ types collected in this
+  /// function.
+  const CallsiteTypeMap &getCallsiteTypeCounts() const {
+    return VirtualCallsiteTypeCounts;
+  }
+
+  /// Returns the vtable access samples for the C++ types for \p Loc.
+  /// Under the hood, the caller-specified \p Loc will be un-drifted before the
+  /// type sample lookup if possible.
+  TypeCountMap &getTypeSamplesAt(const LineLocation &Loc) {
+    return VirtualCallsiteTypeCounts[mapIRLocToProfileLoc(Loc)];
+  }
+
+  /// Scale \p Other sample counts by \p Weight and add the scaled result to the
+  /// type samples for \p Loc. Under the hoold, the caller-provided \p Loc will
+  /// be un-drifted before the type sample lookup if possible.
+  /// typename T is either a std::map or a DenseMap.
+  template <typename T>
+  sampleprof_error addCallsiteVTableTypeProfAt(const LineLocation &Loc,
+                                               const T &Other,
+                                               uint64_t Weight = 1) {
+    static_assert((std::is_same_v<typename T::key_type, StringRef> ||
+                   std::is_same_v<typename T::key_type, FunctionId>) &&
+                      std::is_same_v<typename T::mapped_type, uint64_t>,
+                  "T must be a map with StringRef or FunctionId as key and "
+                  "uint64_t as value");
+    TypeCountMap &TypeCounts = getTypeSamplesAt(Loc);
+    bool Overflowed = false;
+
+    for (const auto [Type, Count] : Other) {
----------------
mingmingl-llvm wrote:

If I understand the question, it's about `(const auto [Type, Count]: Other)` (current) vs `(const auto& [Type, Count]: Other)`.

https://github.com/llvm/llvm-project/blob/f767f231e8ba0cb53d9dbadba3e9c75138f03f09/llvm/include/llvm/ProfileData/SampleProf.h#L1064-L1071 shows both key and value types in `Other` are lightweight types (StringRef, [FunctionId](https://github.com/llvm/llvm-project/blob/f767f231e8ba0cb53d9dbadba3e9c75138f03f09/llvm/include/llvm/ProfileData/FunctionId.h#L36-L42), uint64_t) and I think it's preferred to use value (not reference).

What do you think?



https://github.com/llvm/llvm-project/pull/148002


More information about the llvm-commits mailing list