Skip to content

Commit b35f406

Browse files
[MemProf] Change the STACK_ID record to fixed width values (llvm#116448)
The stack ids are hashes that are close to 64 bits in size, so emitting as a pair of 32-bit fixed-width values is more efficient than a VBR. This reduced the summary bitcode size for a large target by about 1%. Bump the index version and ensure we can read the old format.
1 parent 94d100f commit b35f406

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ class ModuleSummaryIndex {
14631463
// in the way some record are interpreted, like flags for instance.
14641464
// Note that incrementing this may require changes in both BitcodeReader.cpp
14651465
// and BitcodeWriter.cpp.
1466-
static constexpr uint64_t BitcodeSummaryVersion = 11;
1466+
static constexpr uint64_t BitcodeSummaryVersion = 12;
14671467

14681468
// Regular LTO module name for ASM writer
14691469
static constexpr const char *getRegularLTOModuleName() {

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7997,7 +7997,16 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
79977997
case bitc::FS_STACK_IDS: { // [n x stackid]
79987998
// Save stack ids in the reader to consult when adding stack ids from the
79997999
// lists in the stack node and alloc node entries.
8000-
StackIds = ArrayRef<uint64_t>(Record);
8000+
if (Version <= 11) {
8001+
StackIds = ArrayRef<uint64_t>(Record);
8002+
break;
8003+
}
8004+
// This is an array of 32-bit fixed-width values, holding each 64-bit
8005+
// context id as a pair of adjacent (most significant first) 32-bit words.
8006+
assert(Record.size() % 2 == 0);
8007+
StackIds.reserve(Record.size() / 2);
8008+
for (auto R = Record.begin(); R != Record.end(); R += 2)
8009+
StackIds.push_back(*R << 32 | *(R + 1));
80018010
break;
80028011
}
80038012

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,12 +4429,17 @@ void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
44294429
StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
44304430
// numids x stackid
44314431
StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4432-
// FIXME: The stack ids are hashes that are close to 64 bits in size, so
4433-
// emitting as a pair of 32-bit fixed-width values, as we do for context
4434-
// ids, would be more efficient.
4435-
StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4432+
// The stack ids are hashes that are close to 64 bits in size, so emitting
4433+
// as a pair of 32-bit fixed-width values is more efficient than a VBR.
4434+
StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
44364435
unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4437-
Stream.EmitRecord(bitc::FS_STACK_IDS, Index->stackIds(), StackIdAbbvId);
4436+
SmallVector<uint32_t> Vals;
4437+
Vals.reserve(Index->stackIds().size() * 2);
4438+
for (auto Id : Index->stackIds()) {
4439+
Vals.push_back(static_cast<uint32_t>(Id >> 32));
4440+
Vals.push_back(static_cast<uint32_t>(Id));
4441+
}
4442+
Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId);
44384443
}
44394444

44404445
// n x context id
@@ -4624,9 +4629,17 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
46244629
StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
46254630
// numids x stackid
46264631
StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4627-
StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4632+
// The stack ids are hashes that are close to 64 bits in size, so emitting
4633+
// as a pair of 32-bit fixed-width values is more efficient than a VBR.
4634+
StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
46284635
unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4629-
Stream.EmitRecord(bitc::FS_STACK_IDS, StackIds, StackIdAbbvId);
4636+
SmallVector<uint32_t> Vals;
4637+
Vals.reserve(StackIds.size() * 2);
4638+
for (auto Id : StackIds) {
4639+
Vals.push_back(static_cast<uint32_t>(Id >> 32));
4640+
Vals.push_back(static_cast<uint32_t>(Id));
4641+
}
4642+
Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId);
46304643
}
46314644

46324645
// Abbrev for FS_COMBINED_PROFILE.

llvm/test/Bitcode/summary_version.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: opt -module-summary %s -o - | llvm-bcanalyzer -dump | FileCheck %s
33

44
; CHECK: <GLOBALVAL_SUMMARY_BLOCK
5-
; CHECK: <VERSION op0=11/>
5+
; CHECK: <VERSION op0=12/>
66

77

88

Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
;; Check that we can read the old STACK_ID summary format that encoded the id as
2+
;; a VBR8 instead of as a pair of 32-bit fixed-width values.
3+
;;
4+
;; The old bitcode was generated by the older compiler from `opt -thinlto-bc`
5+
;; on the following LLVM assembly:
6+
;;
7+
;; target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
8+
;; target triple = "x86_64-unknown-linux-gnu"
9+
;;
10+
;; define void @bar() {
11+
;; call void @foo(), !callsite !0
12+
;; ret void
13+
;; }
14+
;;
15+
;; declare void @foo()
16+
;;
17+
;; !0 = !{i64 9086428284934609951}
18+
19+
; RUN: llvm-dis %S/Inputs/memprof-old-stackid-summary.bc -o - | FileCheck %s
20+
; CHECK: stackIds: (9086428284934609951)

0 commit comments

Comments
 (0)