Skip to content

[MemProf] Match function's summary and definition strictly #83665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ static cl::opt<bool> UseLoopVersioningLICM(
cl::desc("Enable the experimental Loop Versioning LICM pass"));

namespace llvm {
cl::opt<bool> EnableMemProfContextDisambiguation(
"enable-memprof-context-disambiguation", cl::init(false), cl::Hidden,
cl::ZeroOrMore, cl::desc("Enable MemProf context disambiguation"));
extern cl::opt<bool> EnableMemProfContextDisambiguation;

extern cl::opt<bool> EnableInferAlignmentPass;
} // namespace llvm
Expand Down
10 changes: 8 additions & 2 deletions llvm/lib/Transforms/IPO/FunctionImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ static cl::opt<std::string> WorkloadDefinitions(
"}"),
cl::Hidden);

namespace llvm {
extern cl::opt<bool> EnableMemProfContextDisambiguation;
}

// Load lazily a module from \p FileName in \p Context.
static std::unique_ptr<Module> loadFile(const std::string &FileName,
LLVMContext &Context) {
Expand Down Expand Up @@ -1643,7 +1647,9 @@ Expected<bool> FunctionImporter::importFunctions(
if (Import) {
if (Error Err = F.materialize())
return std::move(Err);
if (EnableImportMetadata) {
// MemProf should match function's definition and summary,
// 'thinlto_src_module' is needed.
if (EnableImportMetadata || EnableMemProfContextDisambiguation) {
// Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
// statistics and debugging.
F.setMetadata(
Expand Down Expand Up @@ -1693,7 +1699,7 @@ Expected<bool> FunctionImporter::importFunctions(
LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID() << " "
<< GO->getName() << " from "
<< SrcModule->getSourceFileName() << "\n");
if (EnableImportMetadata) {
if (EnableImportMetadata || EnableMemProfContextDisambiguation) {
// Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
// statistics and debugging.
Fn->setMetadata(
Expand Down
24 changes: 20 additions & 4 deletions llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ static cl::opt<unsigned>
"frames through tail calls."));

namespace llvm {
cl::opt<bool> EnableMemProfContextDisambiguation(
"enable-memprof-context-disambiguation", cl::init(false), cl::Hidden,
cl::ZeroOrMore, cl::desc("Enable MemProf context disambiguation"));

// Indicate we are linking with an allocator that supports hot/cold operator
// new interfaces.
cl::opt<bool> SupportsHotColdNew(
Expand Down Expand Up @@ -3375,10 +3379,22 @@ bool MemProfContextDisambiguation::applyImport(Module &M) {

auto *GVSummary =
ImportSummary->findSummaryInModule(TheFnVI, M.getModuleIdentifier());
if (!GVSummary)
// Must have been imported, use the first summary (might be multiple if
// this was a linkonce_odr).
GVSummary = TheFnVI.getSummaryList().front().get();
if (!GVSummary) {
// Must have been imported, use the summary which matches the definition。
// (might be multiple if this was a linkonce_odr).
auto SrcModuleMD = F.getMetadata("thinlto_src_module");
assert(SrcModuleMD &&
"enable-import-metadata is needed to emit thinlto_src_module");
StringRef SrcModule =
dyn_cast<MDString>(SrcModuleMD->getOperand(0))->getString();
for (auto &GVS : TheFnVI.getSummaryList()) {
if (GVS->modulePath() == SrcModule) {
GVSummary = GVS.get();
break;
}
}
assert(GVSummary && GVSummary->modulePath() == SrcModule);
}

// If this was an imported alias skip it as we won't have the function
// summary, and it should be cloned in the original module.
Expand Down
Loading