Skip to content

Commit 540321e

Browse files
committed
[llvm][clang] Trace VFS calls
1 parent e72c949 commit 540321e

File tree

18 files changed

+241
-13
lines changed

18 files changed

+241
-13
lines changed

clang/include/clang/Driver/Compilation.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class Compilation {
115115
/// -ftime-trace result files.
116116
ArgStringMap TimeTraceFiles;
117117

118+
/// -fvfs-trace result files.
119+
ArgStringMap VFSTraceFiles;
120+
118121
/// Optional redirection for stdin, stdout, stderr.
119122
std::vector<std::optional<StringRef>> Redirects;
120123

@@ -280,6 +283,14 @@ class Compilation {
280283
TimeTraceFiles[JA] = Name;
281284
}
282285

286+
const char *getVFSTraceFile(const JobAction *JA) const {
287+
return VFSTraceFiles.lookup(JA);
288+
}
289+
void addVFSTraceFile(const char *Name, const JobAction *JA) {
290+
assert(!VFSTraceFiles.contains(JA));
291+
VFSTraceFiles[JA] = Name;
292+
}
293+
283294
/// CleanupFile - Delete a given file.
284295
///
285296
/// \param IssueErrors - Report failures as errors.

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,6 +3904,13 @@ def ftime_trace_EQ : Joined<["-"], "ftime-trace=">, Group<f_Group>,
39043904
HelpText<"Similar to -ftime-trace. Specify the JSON file or a directory which will contain the JSON file">,
39053905
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>,
39063906
MarshallingInfoString<FrontendOpts<"TimeTracePath">>;
3907+
def fvfs_trace : Flag<["-"], "fvfs-trace">, Group<f_Group>,
3908+
HelpText<"Turn of virtual file system profiler. Generates text file based on output filename.">,
3909+
Visibility<[ClangOption, CLOption, DXCOption]>;
3910+
def fvfs_trace_EQ : Joined<["-"], "fvfs-trace=">, Group<f_Group>,
3911+
HelpText<"Similar to -fvfs-trace. Specify the text file or a directory that will contain the text file">,
3912+
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>,
3913+
MarshallingInfoString<FrontendOpts<"VFSTracePath">>;
39073914
def fproc_stat_report : Joined<["-"], "fproc-stat-report">, Group<f_Group>,
39083915
HelpText<"Print subprocess statistics">;
39093916
def fproc_stat_report_EQ : Joined<["-"], "fproc-stat-report=">, Group<f_Group>,

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ namespace llvm {
3535
class raw_fd_ostream;
3636
class Timer;
3737
class TimerGroup;
38+
namespace vfs {
39+
struct InstrumentingFileSystem;
40+
}
3841
}
3942

4043
namespace clang {
@@ -89,6 +92,11 @@ class CompilerInstance : public ModuleLoader {
8992
/// Auxiliary Target info.
9093
IntrusiveRefCntPtr<TargetInfo> AuxTarget;
9194

95+
public:
96+
/// The instrumenting file system.
97+
IntrusiveRefCntPtr<llvm::vfs::InstrumentingFileSystem> IVFS;
98+
private:
99+
92100
/// The file manager.
93101
IntrusiveRefCntPtr<FileManager> FileMgr;
94102

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ArgList;
3939
namespace vfs {
4040

4141
class FileSystem;
42+
struct InstrumentingFileSystem;
4243

4344
} // namespace vfs
4445

@@ -390,13 +391,14 @@ class CowCompilerInvocation : public CompilerInvocationBase {
390391
/// @}
391392
};
392393

393-
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
394-
createVFSFromCompilerInvocation(const CompilerInvocation &CI,
395-
DiagnosticsEngine &Diags);
394+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
395+
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
396+
IntrusiveRefCntPtr<llvm::vfs::InstrumentingFileSystem> *TracingFS = {});
396397

397398
IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
398399
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
399-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
400+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
401+
IntrusiveRefCntPtr<llvm::vfs::InstrumentingFileSystem> *TracingFS = {});
400402

401403
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
402404
createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ class FrontendOptions {
568568
/// Path which stores the output files for -ftime-trace
569569
std::string TimeTracePath;
570570

571+
/// Path which stores the output files for -fvfs-trace
572+
std::string VFSTracePath;
573+
571574
public:
572575
FrontendOptions()
573576
: DisableFree(false), RelocatablePCH(false), ShowHelp(false),

clang/lib/Driver/Driver.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5429,6 +5429,36 @@ static void handleTimeTrace(Compilation &C, const ArgList &Args,
54295429
C.addResultFile(ResultFile, JA);
54305430
}
54315431

5432+
static void handleVFSTrace(Compilation &C, const ArgList &Args,
5433+
const JobAction *JA, const char *BaseInput,
5434+
const InputInfo &Result) {
5435+
Arg *A = Args.getLastArg(options::OPT_fvfs_trace, options::OPT_fvfs_trace_EQ);
5436+
if (!A)
5437+
return;
5438+
SmallString<128> Path;
5439+
if (A->getOption().matches(options::OPT_fvfs_trace_EQ)) {
5440+
Path = A->getValue();
5441+
if (llvm::sys::fs::is_directory(Path)) {
5442+
SmallString<128> Tmp(Result.getFilename());
5443+
llvm::sys::path::replace_extension(Tmp, "vfs.txt");
5444+
llvm::sys::path::append(Path, llvm::sys::path::filename(Tmp));
5445+
}
5446+
} else {
5447+
if (Arg *DumpDir = Args.getLastArgNoClaim(options::OPT_dumpdir)) {
5448+
// The trace file is ${dumpdir}${basename}.vfs.txt. Note that dumpdir may
5449+
// not end with a path separator.
5450+
Path = DumpDir->getValue();
5451+
Path += llvm::sys::path::filename(BaseInput);
5452+
} else {
5453+
Path = Result.getFilename();
5454+
}
5455+
llvm::sys::path::replace_extension(Path, "vfs.txt");
5456+
}
5457+
const char *ResultFile = C.getArgs().MakeArgString(Path);
5458+
C.addVFSTraceFile(ResultFile, JA);
5459+
C.addResultFile(ResultFile, JA);
5460+
}
5461+
54325462
InputInfoList Driver::BuildJobsForActionNoCache(
54335463
Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
54345464
bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
@@ -5678,8 +5708,10 @@ InputInfoList Driver::BuildJobsForActionNoCache(
56785708
AtTopLevel, MultipleArchs,
56795709
OffloadingPrefix),
56805710
BaseInput);
5681-
if (T->canEmitIR() && OffloadingPrefix.empty())
5711+
if (T->canEmitIR() && OffloadingPrefix.empty()) {
56825712
handleTimeTrace(C, Args, JA, BaseInput, Result);
5713+
handleVFSTrace(C, Args, JA, BaseInput, Result);
5714+
}
56835715
}
56845716

56855717
if (CCCPrintBindings && !CCGenDiagnostics) {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6759,6 +6759,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
67596759
Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ);
67606760
}
67616761

6762+
if (const char *Name = C.getVFSTraceFile(&JA))
6763+
CmdArgs.push_back(Args.MakeArgString("-fvfs-trace=" + Twine(Name)));
6764+
67626765
if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
67636766
CmdArgs.push_back("-ftrapv-handler");
67646767
CmdArgs.push_back(A->getValue());

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ FileManager *CompilerInstance::createFileManager(
379379
if (!VFS)
380380
VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
381381
: createVFSFromCompilerInvocation(getInvocation(),
382-
getDiagnostics());
382+
getDiagnostics(), &IVFS);
383383
assert(VFS && "FileManager has no VFS?");
384384
FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));
385385
return FileMgr.get();

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,16 +4935,25 @@ void CompilerInvocation::clearImplicitModuleBuildOptions() {
49354935
}
49364936

49374937
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
4938-
clang::createVFSFromCompilerInvocation(const CompilerInvocation &CI,
4939-
DiagnosticsEngine &Diags) {
4940-
return createVFSFromCompilerInvocation(CI, Diags,
4941-
llvm::vfs::getRealFileSystem());
4938+
clang::createVFSFromCompilerInvocation(
4939+
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
4940+
IntrusiveRefCntPtr<llvm::vfs::InstrumentingFileSystem> *TracingFS) {
4941+
return createVFSFromCompilerInvocation(
4942+
CI, Diags, llvm::vfs::getRealFileSystem(), TracingFS);
49424943
}
49434944

49444945
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
49454946
clang::createVFSFromCompilerInvocation(
49464947
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
4947-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
4948+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
4949+
IntrusiveRefCntPtr<llvm::vfs::InstrumentingFileSystem> *TracingFS) {
4950+
if (!CI.getFrontendOpts().VFSTracePath.empty()) {
4951+
auto TFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InstrumentingFileSystem>(
4952+
std::move(BaseFS));
4953+
if (TracingFS)
4954+
*TracingFS = TFS;
4955+
BaseFS = std::move(TFS);
4956+
}
49484957
return createVFSFromOverlayFiles(CI.getHeaderSearchOpts().VFSOverlayFiles,
49494958
Diags, std::move(BaseFS));
49504959
}

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ class DependencyScanningAction : public tooling::ToolAction {
338338
ScanInstance.getFrontendOpts().GenerateGlobalModuleIndex = false;
339339
ScanInstance.getFrontendOpts().UseGlobalModuleIndex = false;
340340
ScanInstance.getFrontendOpts().ModulesShareFileManager = false;
341+
ScanInstance.getFrontendOpts().VFSTracePath.clear();
341342
ScanInstance.getHeaderSearchOpts().ModuleFormat = "raw";
342343
ScanInstance.getHeaderSearchOpts().ModulesIncludeVFSUsage =
343344
any(OptimizeArgs & ScanningOptimizations::VFS);

0 commit comments

Comments
 (0)