Skip to content

Commit 46f5dae

Browse files
[memprof] Make HeapProfileRecords optional (#155671)
memprof::AllMemProfData has become home to multiple types of memprof data -- heap profile and static data profile. When we write test cases for static data profile in YAML, we do not want to include empty heap profile. That would just add visual clutter. This patch makes HeapProfileRecords optional.
1 parent e8c2241 commit 46f5dae

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

llvm/include/llvm/ProfileData/DataAccessProf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ class DataAccessProfData {
185185
ArrayRef<uint64_t> getKnownColdHashes() const {
186186
return KnownColdHashes.getArrayRef();
187187
}
188+
[[nodiscard]] bool empty() const {
189+
return Records.empty() && KnownColdSymbols.empty() &&
190+
KnownColdHashes.empty();
191+
}
188192

189193
private:
190194
/// Serialize the symbol strings into the output stream.

llvm/include/llvm/ProfileData/MemProfYAML.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ template <> struct MappingTraits<memprof::YamlDataAccessProfData> {
263263

264264
template <> struct MappingTraits<memprof::AllMemProfData> {
265265
static void mapping(IO &Io, memprof::AllMemProfData &Data) {
266-
Io.mapRequired("HeapProfileRecords", Data.HeapProfileRecords);
266+
if (!Io.outputting() || !Data.HeapProfileRecords.empty())
267+
Io.mapOptional("HeapProfileRecords", Data.HeapProfileRecords);
267268
// Map data access profiles if reading input, or if writing output &&
268269
// the struct is populated.
269270
if (!Io.outputting() || !Data.YamlifiedDataAccessProfiles.isEmpty())

llvm/test/tools/llvm-profdata/memprof-yaml.test

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
; RUN: llvm-profdata show --memory %t/memprof-out.indexed > %t/memprof-out-no-dap.yaml
1919
; RUN: diff -b %t/memprof-in-no-dap.yaml %t/memprof-out-no-dap.yaml
2020

21+
; memprof-in-no-heap.yaml has empty heap access profiles.
22+
; RUN: llvm-profdata merge --memprof-version=4 %t/memprof-in-no-heap.yaml -o %t/memprof-out-no-heap.indexed
23+
; RUN: llvm-profdata show --memory %t/memprof-out-no-heap.indexed > %t/memprof-out-no-heap.yaml
24+
; RUN: diff -b %t/memprof-in-no-heap.yaml %t/memprof-out-no-heap.yaml
25+
2126
;--- memprof-in.yaml
2227
---
2328
# MemProfSummary:
@@ -151,3 +156,32 @@ HeapProfileRecords:
151156
- { Function: 0x8888888888888888, LineOffset: 88, Column: 80, IsInlineFrame: false }
152157
CalleeGuids: [ 0x300 ]
153158
...
159+
;--- memprof-in-no-heap.yaml
160+
---
161+
# MemProfSummary:
162+
# Total contexts: 0
163+
# Total cold contexts: 0
164+
# Total hot contexts: 0
165+
# Maximum cold context total size: 0
166+
# Maximum warm context total size: 0
167+
# Maximum hot context total size: 0
168+
---
169+
DataAccessProfiles:
170+
SampledRecords:
171+
- Symbol: abcde
172+
AccessCount: 100
173+
Locations:
174+
- FileName: file2.h
175+
Line: 123
176+
- FileName: file3.cpp
177+
Line: 456
178+
- Hash: 101010
179+
AccessCount: 200
180+
Locations:
181+
- FileName: file.cpp
182+
Line: 233
183+
KnownColdSymbols:
184+
- foo
185+
- bar
186+
KnownColdStrHashes: [ 999, 1001 ]
187+
...

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
760760
auto DataAccessProfData = Reader->takeDataAccessProfData();
761761

762762
// Check for the empty input in case the YAML file is invalid.
763-
if (MemProfData.Records.empty()) {
763+
if (MemProfData.Records.empty() &&
764+
(!DataAccessProfData || DataAccessProfData->empty())) {
764765
WC->Errors.emplace_back(
765766
make_error<StringError>("The profile is empty.", std::error_code()),
766767
Filename);

0 commit comments

Comments
 (0)