Skip to content

Commit 683f2df

Browse files
authored
[SampleProfile] Fix bug where remapper returns empty string and crashing Sample Profile loader (#71479)
Normally SampleContext does not allow using an empty StirngRef to construct an object, this is to prevent bugs reading the profile. However empty names may be emitted by a function which its name is intentionally set to empty, or a bug in the remapper that returns an empty string. Regardless, converting it to FunctionId first will prevent the assert, and that assert check is unnecessary, which will be addressed in another patch
1 parent 199fc97 commit 683f2df

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

llvm/lib/ProfileData/SampleProfReader.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,8 +1788,11 @@ void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
17881788

17891789
std::optional<StringRef>
17901790
SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) {
1791-
if (auto Key = Remappings->lookup(Fname))
1792-
return NameMap.lookup(Key);
1791+
if (auto Key = Remappings->lookup(Fname)) {
1792+
StringRef Result = NameMap.lookup(Key);
1793+
if (!Result.empty())
1794+
return Result;
1795+
}
17931796
return std::nullopt;
17941797
}
17951798

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ void SampleProfileLoader::promoteMergeNotInlinedContextSamples(
16301630
// separate map so that it does not rehash the original profile.
16311631
if (!OutlineFS)
16321632
OutlineFS = &OutlineFunctionSamples[
1633-
FunctionSamples::getCanonicalFnName(Callee->getName())];
1633+
FunctionId(FunctionSamples::getCanonicalFnName(Callee->getName()))];
16341634
OutlineFS->merge(*FS, 1);
16351635
// Set outlined profile to be synthetic to not bias the inliner.
16361636
OutlineFS->SetContextSynthetic();
@@ -2675,12 +2675,12 @@ bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM)
26752675
// into base.
26762676
if (!Samples) {
26772677
StringRef CanonName = FunctionSamples::getCanonicalFnName(F);
2678-
auto It = OutlineFunctionSamples.find(CanonName);
2678+
auto It = OutlineFunctionSamples.find(FunctionId(CanonName));
26792679
if (It != OutlineFunctionSamples.end()) {
26802680
Samples = &It->second;
26812681
} else if (auto Remapper = Reader->getRemapper()) {
26822682
if (auto RemppedName = Remapper->lookUpNameInProfile(CanonName)) {
2683-
It = OutlineFunctionSamples.find(*RemppedName);
2683+
It = OutlineFunctionSamples.find(FunctionId(*RemppedName));
26842684
if (It != OutlineFunctionSamples.end())
26852685
Samples = &It->second;
26862686
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; This test should not crash.
2+
; RUN: opt %s -passes=sample-profile -sample-profile-file=%S/Inputs/remap.prof -sample-profile-remapping-file=%S/Inputs/remap.map
3+
4+
define void @foo() #0 {
5+
ret void
6+
}
7+
8+
attributes #0 = { "use-sample-profile" }

0 commit comments

Comments
 (0)