@@ -29,6 +29,15 @@ cl::opt<std::string>
2929 UseCtxProfile (" use-ctx-profile" , cl::init(" " ), cl::Hidden,
3030 cl::desc(" Use the specified contextual profile file" ));
3131
32+ static cl::opt<CtxProfAnalysisPrinterPass::PrintMode> PrintLevel (
33+ " ctx-profile-printer-level" ,
34+ cl::init (CtxProfAnalysisPrinterPass::PrintMode::JSON), cl::Hidden,
35+ cl::values(clEnumValN(CtxProfAnalysisPrinterPass::PrintMode::Everything,
36+ " everything" , " print everything - most verbose" ),
37+ clEnumValN(CtxProfAnalysisPrinterPass::PrintMode::JSON, " json" ,
38+ " just the json representation of the profile" )),
39+ cl::desc(" Verbosity level of the contextual profile printer pass." ));
40+
3241namespace llvm {
3342namespace json {
3443Value toJSON (const PGOCtxProfContext &P) {
@@ -96,12 +105,20 @@ GlobalValue::GUID AssignGUIDPass::getGUID(const Function &F) {
96105}
97106AnalysisKey CtxProfAnalysis::Key;
98107
99- CtxProfAnalysis::CtxProfAnalysis (StringRef Profile)
100- : Profile(Profile.empty() ? UseCtxProfile : Profile) {}
108+ CtxProfAnalysis::CtxProfAnalysis (std::optional<StringRef> Profile)
109+ : Profile([&]() -> std::optional<StringRef> {
110+ if (Profile)
111+ return *Profile;
112+ if (UseCtxProfile.getNumOccurrences ())
113+ return UseCtxProfile;
114+ return std::nullopt ;
115+ }()) {}
101116
102117PGOContextualProfile CtxProfAnalysis::run (Module &M,
103118 ModuleAnalysisManager &MAM) {
104- ErrorOr<std::unique_ptr<MemoryBuffer>> MB = MemoryBuffer::getFile (Profile);
119+ if (!Profile)
120+ return {};
121+ ErrorOr<std::unique_ptr<MemoryBuffer>> MB = MemoryBuffer::getFile (*Profile);
105122 if (auto EC = MB.getError ()) {
106123 M.getContext ().emitError (" could not open contextual profile file: " +
107124 EC.message ());
@@ -150,7 +167,6 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M,
150167 // If we made it this far, the Result is valid - which we mark by setting
151168 // .Profiles.
152169 // Trim first the roots that aren't in this module.
153- DenseSet<GlobalValue::GUID> ProfiledGUIDs;
154170 for (auto &[RootGuid, _] : llvm::make_early_inc_range (*MaybeCtx))
155171 if (!Result.FuncInfo .contains (RootGuid))
156172 MaybeCtx->erase (RootGuid);
@@ -165,11 +181,14 @@ PGOContextualProfile::getDefinedFunctionGUID(const Function &F) const {
165181 return 0 ;
166182}
167183
184+ CtxProfAnalysisPrinterPass::CtxProfAnalysisPrinterPass (raw_ostream &OS)
185+ : OS(OS), Mode(PrintLevel) {}
186+
168187PreservedAnalyses CtxProfAnalysisPrinterPass::run (Module &M,
169188 ModuleAnalysisManager &MAM) {
170189 CtxProfAnalysis::Result &C = MAM.getResult <CtxProfAnalysis>(M);
171190 if (!C) {
172- M. getContext (). emitError ( " Invalid CtxProfAnalysis " ) ;
191+ OS << " No contextual profile was provided. \n " ;
173192 return PreservedAnalyses::all ();
174193 }
175194
0 commit comments