Skip to content

Commit 2c32821

Browse files
committed
Add supporting for remaining UseIRProfile cases
1 parent 7a01d0c commit 2c32821

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

include/swift/AST/DiagnosticsIRGen.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ ERROR(too_few_output_filenames,none,
4141
ERROR(no_input_files_for_mt,none,
4242
"no swift input files for multi-threaded compilation", ())
4343

44+
ERROR(ir_profile_read_failed, none,
45+
"error reading profile '%0': %1", (StringRef, StringRef))
46+
ERROR(ir_profile_invalid, none,
47+
"invalid ir profile '%0'", (StringRef))
48+
4449
ERROR(alignment_dynamic_type_layout_unsupported,none,
4550
"'@_alignment' is not supported on types with dynamic layout", ())
4651
ERROR(alignment_less_than_natural,none,

lib/IRGen/IRGen.cpp

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "llvm/Passes/PassBuilder.h"
7373
#include "llvm/Passes/PassPlugin.h"
7474
#include "llvm/Passes/StandardInstrumentations.h"
75+
#include "llvm/ProfileData/InstrProfReader.h"
7576
#include "llvm/Remarks/Remark.h"
7677
#include "llvm/Remarks/RemarkStreamer.h"
7778
#include "llvm/Support/CommandLine.h"
@@ -214,8 +215,23 @@ static void align(llvm::Module *Module) {
214215
}
215216
}
216217

218+
static std::unique_ptr<llvm::IndexedInstrProfReader>
219+
getProfileReader(const Twine &ProfileName, llvm::vfs::FileSystem &FS,
220+
DiagnosticEngine &Diags) {
221+
auto ReaderOrErr = llvm::IndexedInstrProfReader::create(ProfileName, FS);
222+
if (auto E = ReaderOrErr.takeError()) {
223+
llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
224+
Diags.diagnose(SourceLoc(), diag::ir_profile_read_failed,
225+
ProfileName.str(), EI.message());
226+
});
227+
return nullptr;
228+
}
229+
return std::move(*ReaderOrErr);
230+
}
231+
217232
static void populatePGOOptions(std::optional<PGOOptions> &Out,
218-
const IRGenOptions &Opts) {
233+
const IRGenOptions &Opts,
234+
DiagnosticEngine &Diags) {
219235
if (!Opts.UseSampleProfile.empty()) {
220236
Out = PGOOptions(
221237
/*ProfileFile=*/ Opts.UseSampleProfile,
@@ -234,31 +250,60 @@ static void populatePGOOptions(std::optional<PGOOptions> &Out,
234250
if (Opts.EnableCSIRProfileGen) {
235251
const bool hasUse = !Opts.UseIRProfile.empty();
236252
Out = PGOOptions(
237-
/*ProfileFile=*/ Opts.UseIRProfile,
238-
/*CSProfileGenFile=*/ Opts.InstrProfileOutput,
239-
/*ProfileRemappingFile=*/ "",
240-
/*MemoryProfile=*/ "",
241-
/*FS=*/ llvm::vfs::getRealFileSystem(),
242-
/*Action=*/ hasUse ? PGOOptions::IRUse : PGOOptions::NoAction,
243-
/*CSPGOAction=*/ PGOOptions::CSIRInstr,
244-
/*ColdType=*/ PGOOptions::ColdFuncOpt::Default,
245-
/*DebugInfoForProfiling=*/ Opts.DebugInfoForProfiling
246-
);
253+
/*ProfileFile=*/Opts.UseIRProfile,
254+
/*CSProfileGenFile=*/Opts.InstrProfileOutput,
255+
/*ProfileRemappingFile=*/"",
256+
/*MemoryProfile=*/"",
257+
/*FS=*/llvm::vfs::getRealFileSystem(),
258+
/*Action=*/hasUse ? PGOOptions::IRUse : PGOOptions::NoAction,
259+
/*CSPGOAction=*/PGOOptions::CSIRInstr,
260+
/*ColdType=*/PGOOptions::ColdFuncOpt::Default,
261+
/*DebugInfoForProfiling=*/Opts.DebugInfoForProfiling);
247262
return;
248263
}
249264

250265
if (Opts.EnableIRProfileGen) {
251266
Out = PGOOptions(
252-
/*ProfileFile=*/ Opts.InstrProfileOutput,
253-
/*CSProfileGenFile=*/ "",
254-
/*ProfileRemappingFile=*/ "",
255-
/*MemoryProfile=*/ "",
256-
/*FS=*/ llvm::vfs::getRealFileSystem(),
257-
/*Action=*/ PGOOptions::IRInstr,
258-
/*CSPGOAction=*/ PGOOptions::NoCSAction,
259-
/*ColdType=*/ PGOOptions::ColdFuncOpt::Default,
260-
/*DebugInfoForProfiling=*/ Opts.DebugInfoForProfiling
261-
);
267+
/*ProfileFile=*/Opts.InstrProfileOutput,
268+
/*CSProfileGenFile=*/"",
269+
/*ProfileRemappingFile=*/"",
270+
/*MemoryProfile=*/"",
271+
/*FS=*/llvm::vfs::getRealFileSystem(),
272+
/*Action=*/PGOOptions::IRInstr,
273+
/*CSPGOAction=*/PGOOptions::NoCSAction,
274+
/*ColdType=*/PGOOptions::ColdFuncOpt::Default,
275+
/*DebugInfoForProfiling=*/Opts.DebugInfoForProfiling);
276+
return;
277+
}
278+
279+
if (!Opts.UseIRProfile.empty()) {
280+
auto FS = llvm::vfs::getRealFileSystem();
281+
std::unique_ptr<llvm::IndexedInstrProfReader> Reader =
282+
getProfileReader(Opts.UseIRProfile.c_str(), *FS, Diags);
283+
if (!Reader)
284+
return;
285+
286+
// Currently memprof profiles are only added at the IR level. Mark the
287+
// profile type as IR in that case as well and the subsequent matching
288+
// needs to detect which is available (might be one or both).
289+
const bool IsIR = Reader->isIRLevelProfile() || Reader->hasMemoryProfile();
290+
const bool IsCS = Reader->hasCSIRLevelProfile();
291+
if (!IsIR) {
292+
Diags.diagnose(SourceLoc(), diag::ir_profile_invalid,
293+
Opts.UseIRProfile.c_str());
294+
return;
295+
}
296+
297+
Out = PGOOptions(
298+
/*ProfileFile=*/Opts.UseIRProfile,
299+
/*CSProfileGenFile=*/"",
300+
/*ProfileRemappingFile=*/"",
301+
/*MemoryProfile=*/"",
302+
/*FS=*/FS,
303+
/*Action=*/PGOOptions::IRUse,
304+
/*CSPGOAction=*/IsCS ? PGOOptions::CSIRUse : PGOOptions::NoCSAction,
305+
/*ColdType=*/PGOOptions::ColdFuncOpt::Default,
306+
/*DebugInfoForProfiling=*/Opts.DebugInfoForProfiling);
262307
return;
263308
}
264309

@@ -297,7 +342,7 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
297342
llvm::TargetMachine *TargetMachine,
298343
llvm::raw_pwrite_stream *out) {
299344
std::optional<PGOOptions> PGOOpt;
300-
populatePGOOptions(PGOOpt, Opts);
345+
populatePGOOptions(PGOOpt, Opts, Diags);
301346

302347
PipelineTuningOptions PTO;
303348

0 commit comments

Comments
 (0)