From 1b0c0a478e1f5443100325cf2e2b5f2cc40649b6 Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Tue, 1 Apr 2025 14:28:20 -0700 Subject: [PATCH] [ctxprof] Option to move a whole tree to its own module --- llvm/lib/Transforms/IPO/FunctionImport.cpp | 32 +++++++++-- .../ThinLTO/X86/ctxprof-separate-module.ll | 56 +++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 llvm/test/ThinLTO/X86/ctxprof-separate-module.ll diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp index f0daf1a558316..cfefc0e8c43a8 100644 --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -38,6 +38,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/JSON.h" +#include "llvm/Support/Path.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/IPO/Internalize.h" @@ -175,6 +176,12 @@ static cl::opt WorkloadDefinitions( extern cl::opt UseCtxProfile; +static cl::opt CtxprofMoveRootsToOwnModule( + "thinlto-move-ctxprof-trees", + cl::desc("Move contextual profiling roots and the graphs under them in " + "their own module."), + cl::Hidden, cl::init(false)); + namespace llvm { extern cl::opt EnableMemProfContextDisambiguation; } @@ -535,7 +542,14 @@ class WorkloadImportsManager : public ModuleImportsManager { computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName, FunctionImporter::ImportMapTy &ImportList) override { - auto SetIter = Workloads.find(ModName); + StringRef Filename = ModName; + if (CtxprofMoveRootsToOwnModule) { + Filename = sys::path::filename(ModName); + // Drop the file extension. + Filename = Filename.substr(0, Filename.find_last_of('.')); + } + auto SetIter = Workloads.find(Filename); + if (SetIter == Workloads.end()) { LLVM_DEBUG(dbgs() << "[Workload] " << ModName << " does not contain the root of any context.\n"); @@ -748,10 +762,18 @@ class WorkloadImportsManager : public ModuleImportsManager { << RootVI.getSummaryList().size() << ". Skipping.\n"); continue; } - StringRef RootDefiningModule = - RootVI.getSummaryList().front()->modulePath(); - LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid - << " is : " << RootDefiningModule << "\n"); + std::string RootDefiningModule = + RootVI.getSummaryList().front()->modulePath().str(); + if (CtxprofMoveRootsToOwnModule) { + RootDefiningModule = std::to_string(RootGuid); + LLVM_DEBUG( + dbgs() << "[Workload] Moving " << RootGuid + << " to a module with the filename without extension : " + << RootDefiningModule << "\n"); + } else { + LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid + << " is : " << RootDefiningModule << "\n"); + } auto &Set = Workloads[RootDefiningModule]; Root.getContainedGuids(ContainedGUIDs); for (auto Guid : ContainedGUIDs) diff --git a/llvm/test/ThinLTO/X86/ctxprof-separate-module.ll b/llvm/test/ThinLTO/X86/ctxprof-separate-module.ll new file mode 100644 index 0000000000000..c7891d336cc89 --- /dev/null +++ b/llvm/test/ThinLTO/X86/ctxprof-separate-module.ll @@ -0,0 +1,56 @@ +; Test workload based importing via -thinlto-pgo-ctx-prof with moving the whole +; graph to a new module. +; Use external linkage symbols so we don't depend on module paths which are +; used when computing the GUIDs of internal linkage symbols. +; +; Set up +; RUN: rm -rf %t +; RUN: mkdir -p %t +; RUN: split-file %s %t +; +; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m1.ll -o %t/m1.bc +; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m2.ll -o %t/m2.bc +; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/6019442868614718803.ll -o %t/6019442868614718803.bc + +; RUN: llvm-ctxprof-util fromYAML --input %t/ctxprof.yaml --output %t/ctxprof.bitstream +; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc %t/6019442868614718803.bc -thinlto-move-ctxprof-trees \ +; RUN: -o %t/result.o -save-temps \ +; RUN: -use-ctx-profile=%t/ctxprof.bitstream \ +; RUN: -r %t/m1.bc,m1_f1,plx \ +; RUN: -r %t/m2.bc,m2_f1,plx +; RUN: llvm-dis %t/result.o.3.3.import.bc -o - | FileCheck %s +; +; +; CHECK: m1_f1() +; CHECK: m2_f1() +; +;--- ctxprof.yaml +Contexts: + - + Guid: 6019442868614718803 + TotalRootEntryCount: 5 + Counters: [1] + Callsites: + - - + Guid: 15593096274670919754 + Counters: [1] + +;--- m1.ll +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +define dso_local void @m1_f1() { + ret void +} + +;--- m2.ll +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +define dso_local void @m2_f1() { + ret void +} + +;--- 6019442868614718803.ll +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu"