diff options
| author | Teresa Johnson <tejohnson@google.com> | 2023-12-06 08:41:44 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-06 08:41:44 -0800 |
| commit | 88fbc4d3df7c24105eb36232ff6250da95c8a202 (patch) | |
| tree | 4bc633dafcb36d16958bf88510d83b3d3ce7e913 /llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | |
| parent | 8186e1500b1d9709f10199b7c599274f8a6f8e3a (diff) | |
| download | llvm-88fbc4d3df7c24105eb36232ff6250da95c8a202.zip llvm-88fbc4d3df7c24105eb36232ff6250da95c8a202.tar.gz llvm-88fbc4d3df7c24105eb36232ff6250da95c8a202.tar.bz2 | |
[ThinLTO] Add tail call flag to call edges in summary (#74043)
This adds support for a HasTailCall flag on function call edges in the
ThinLTO summary. It is intended for use in aiding discovery of missing
frames from tail calls in profiled call stacks for MemProf of profiled
binaries that did not disable tail call elimination. A follow on change
will add the use of this new flag during MemProf context disambiguation.
The new flag is encoded in the bitcode along with either the hotness
flag from the profile, or the relative block frequency under the
-write-relbf-to-summary flag when there is no profile data.
Because we now will always have some additional call edge information, I
have removed the non-profile function summary record format, and we
simply encode the tail call flag along with a hotness type of none when
there is no profile information or relative block frequency. The change
of record format and name caused most of the test case changes.
I have added explicit testing of generation of the new tail call flag
into the bitcode and IR assembly format as part of the changes to
llvm/test/Bitcode/thinlto-function-summary-refgraph.ll. I have also
added round trip testing through assembly and bitcode to
llvm/test/Assembler/thinlto-summary.ll.
Diffstat (limited to 'llvm/lib/Bitcode/Writer/BitcodeWriter.cpp')
| -rw-r--r-- | llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 101 |
1 files changed, 44 insertions, 57 deletions
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 4969809..4e0379f 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -1157,6 +1157,24 @@ static uint64_t getEncodedGVarFlags(GlobalVarSummary::GVarFlags Flags) { return RawFlags; } +static uint64_t getEncodedHotnessCallEdgeInfo(const CalleeInfo &CI) { + uint64_t RawFlags = 0; + + RawFlags |= CI.Hotness; // 3 bits + RawFlags |= (CI.HasTailCall << 3); // 1 bit + + return RawFlags; +} + +static uint64_t getEncodedRelBFCallEdgeInfo(const CalleeInfo &CI) { + uint64_t RawFlags = 0; + + RawFlags |= CI.RelBlockFreq; // CalleeInfo::RelBlockFreqBits bits + RawFlags |= (CI.HasTailCall << CalleeInfo::RelBlockFreqBits); // 1 bit + + return RawFlags; +} + static unsigned getEncodedVisibility(const GlobalValue &GV) { switch (GV.getVisibility()) { case GlobalValue::DefaultVisibility: return 0; @@ -4009,8 +4027,9 @@ static void writeFunctionHeapProfileRecords( // Helper to emit a single function summary record. void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord( SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary, - unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev, - unsigned CallsiteAbbrev, unsigned AllocAbbrev, const Function &F) { + unsigned ValueID, unsigned FSCallsRelBFAbbrev, + unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev, + unsigned AllocAbbrev, const Function &F) { NameVals.push_back(ValueID); FunctionSummary *FS = cast<FunctionSummary>(Summary); @@ -4037,21 +4056,21 @@ void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord( for (auto &RI : FS->refs()) NameVals.push_back(VE.getValueID(RI.getValue())); - bool HasProfileData = - F.hasProfileData() || ForceSummaryEdgesCold != FunctionSummary::FSHT_None; + const bool UseRelBFRecord = + WriteRelBFToSummary && !F.hasProfileData() && + ForceSummaryEdgesCold == FunctionSummary::FSHT_None; for (auto &ECI : FS->calls()) { NameVals.push_back(getValueId(ECI.first)); - if (HasProfileData) - NameVals.push_back(static_cast<uint8_t>(ECI.second.Hotness)); - else if (WriteRelBFToSummary) - NameVals.push_back(ECI.second.RelBlockFreq); + if (UseRelBFRecord) + NameVals.push_back(getEncodedRelBFCallEdgeInfo(ECI.second)); + else + NameVals.push_back(getEncodedHotnessCallEdgeInfo(ECI.second)); } - unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev); + unsigned FSAbbrev = + (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev); unsigned Code = - (HasProfileData ? bitc::FS_PERMODULE_PROFILE - : (WriteRelBFToSummary ? bitc::FS_PERMODULE_RELBF - : bitc::FS_PERMODULE)); + (UseRelBFRecord ? bitc::FS_PERMODULE_RELBF : bitc::FS_PERMODULE_PROFILE); // Emit the finished record. Stream.EmitRecord(Code, NameVals, FSAbbrev); @@ -4160,17 +4179,14 @@ void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() { Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt - // numrefs x valueid, n x (valueid, hotness) + // numrefs x valueid, n x (valueid, hotness+tailcall flags) Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv)); - // Abbrev for FS_PERMODULE or FS_PERMODULE_RELBF. + // Abbrev for FS_PERMODULE_RELBF. Abbv = std::make_shared<BitCodeAbbrev>(); - if (WriteRelBFToSummary) - Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_RELBF)); - else - Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE)); + Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_RELBF)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount @@ -4178,10 +4194,10 @@ void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() { Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt - // numrefs x valueid, n x (valueid [, rel_block_freq]) + // numrefs x valueid, n x (valueid, rel_block_freq+tailcall]) Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); - unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv)); + unsigned FSCallsRelBFAbbrev = Stream.EmitAbbrev(std::move(Abbv)); // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS. Abbv = std::make_shared<BitCodeAbbrev>(); @@ -4253,9 +4269,9 @@ void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() { continue; } auto *Summary = VI.getSummaryList()[0].get(); - writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F), - FSCallsAbbrev, FSCallsProfileAbbrev, - CallsiteAbbrev, AllocAbbrev, F); + writePerModuleFunctionSummaryRecord( + NameVals, Summary, VE.getValueID(&F), FSCallsRelBFAbbrev, + FSCallsProfileAbbrev, CallsiteAbbrev, AllocAbbrev, F); } // Capture references from GlobalVariable initializers, which are outside @@ -4326,25 +4342,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { Stream.EmitRecord(bitc::FS_STACK_IDS, StackIds, StackIdAbbvId); } - // Abbrev for FS_COMBINED. - auto Abbv = std::make_shared<BitCodeAbbrev>(); - Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED)); - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt - // numrefs x valueid, n x (valueid) - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); - unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv)); - // Abbrev for FS_COMBINED_PROFILE. - Abbv = std::make_shared<BitCodeAbbrev>(); + auto Abbv = std::make_shared<BitCodeAbbrev>(); Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid @@ -4355,7 +4354,7 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt - // numrefs x valueid, n x (valueid, hotness) + // numrefs x valueid, n x (valueid, hotness+tailcall flags) Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv)); @@ -4535,14 +4534,6 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { NameVals[7] = RORefCnt; NameVals[8] = WORefCnt; - bool HasProfileData = false; - for (auto &EI : FS->calls()) { - HasProfileData |= - EI.second.getHotness() != CalleeInfo::HotnessType::Unknown; - if (HasProfileData) - break; - } - for (auto &EI : FS->calls()) { // If this GUID doesn't have a value id, it doesn't have a function // summary and we don't need to record any calls to it. @@ -4550,16 +4541,12 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { if (!CallValueId) continue; NameVals.push_back(*CallValueId); - if (HasProfileData) - NameVals.push_back(static_cast<uint8_t>(EI.second.Hotness)); + NameVals.push_back(getEncodedHotnessCallEdgeInfo(EI.second)); } - unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev); - unsigned Code = - (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED); - // Emit the finished record. - Stream.EmitRecord(Code, NameVals, FSAbbrev); + Stream.EmitRecord(bitc::FS_COMBINED_PROFILE, NameVals, + FSCallsProfileAbbrev); NameVals.clear(); MaybeEmitOriginalName(*S); }); |
