@@ -553,6 +553,11 @@ SmallString<128> getFilePath(DataObject *Object, StringRef Dir) {
553553 return Path;
554554}
555555
556+ // TODO: Move inputFromFile and outputToFile within AMDGPUCompiler
557+ //
558+ // Currently, we only invoke these two methods in the context of AMDGPUCompiler.
559+ // Moreover, member functions that deal with file I/O should not worry whether
560+ // the underlying filesystem being used is virtual or real.
556561amd_comgr_status_t inputFromFile (DataObject *Object, StringRef Path) {
557562 ProfilePoint Point (" FileIO" );
558563 auto BufOrError = MemoryBuffer::getFile (Path);
@@ -646,7 +651,7 @@ void logArgv(raw_ostream &OS, StringRef ProgramName,
646651
647652amd_comgr_status_t executeCommand (const Command &Job, raw_ostream &LogS,
648653 DiagnosticOptions &DiagOpts,
649- llvm::vfs::FileSystem &VFS ) {
654+ llvm::vfs::FileSystem &FS ) {
650655 TextDiagnosticPrinter DiagClient (LogS, &DiagOpts);
651656 IntrusiveRefCntPtr<DiagnosticIDs> DiagID (new DiagnosticIDs);
652657 DiagnosticsEngine Diags (DiagID, &DiagOpts, &DiagClient, false );
@@ -672,17 +677,19 @@ amd_comgr_status_t executeCommand(const Command &Job, raw_ostream &LogS,
672677
673678 std::unique_ptr<CompilerInstance> Clang (new CompilerInstance ());
674679 Clang->setVerboseOutputStream (LogS);
680+ Clang->setFileManager (new FileManager (Clang->getFileSystemOpts (), &FS));
675681 if (!Argv.back ()) {
676682 Argv.pop_back ();
677683 }
684+
678685 if (!CompilerInvocation::CreateFromArgs (Clang->getInvocation (), Argv,
679686 Diags)) {
680687 return AMD_COMGR_STATUS_ERROR;
681688 }
682689 // Internally this call refers to the invocation created above, so at
683690 // this point the DiagnosticsEngine should accurately reflect all user
684691 // requested configuration from Argv.
685- Clang->createDiagnostics (VFS , &DiagClient, /* ShouldOwnClient */ false );
692+ Clang->createDiagnostics (FS , &DiagClient, /* ShouldOwnClient */ false );
686693 if (!Clang->hasDiagnostics ()) {
687694 return AMD_COMGR_STATUS_ERROR;
688695 }
@@ -755,12 +762,11 @@ AMDGPUCompiler::executeInProcessDriver(ArrayRef<const char *> Args) {
755762 IntrusiveRefCntPtr<DiagnosticIDs> DiagID (new DiagnosticIDs);
756763 DiagnosticsEngine Diags (DiagID, &*DiagOpts, DiagClient);
757764
758- auto VFS = llvm::vfs::getRealFileSystem ();
759- ProcessWarningOptions (Diags, *DiagOpts, *VFS, /* ReportDiags=*/ false );
765+ ProcessWarningOptions (Diags, *DiagOpts, *OverlayFS, /* ReportDiags=*/ false );
760766
761767 Driver TheDriver ((Twine (env::getLLVMPath ()) + " /bin/clang" ).str (),
762768 llvm::sys::getDefaultTargetTriple (), Diags,
763- " AMDGPU Code Object Manager" , VFS );
769+ " AMDGPU Code Object Manager" , OverlayFS );
764770 TheDriver.setCheckInputsExist (false );
765771
766772 // Log arguments used to build compilation
@@ -782,7 +788,7 @@ AMDGPUCompiler::executeInProcessDriver(ArrayRef<const char *> Args) {
782788
783789 auto Cache = CommandCache::get (LogS);
784790 for (auto &Job : C->getJobs ()) {
785- ClangCommand C (Job, *DiagOpts, *VFS , executeCommand);
791+ ClangCommand C (Job, *DiagOpts, *OverlayFS , executeCommand);
786792 if (Cache) {
787793 if (auto Status = Cache->execute (C, LogS)) {
788794 return Status;
@@ -1089,8 +1095,18 @@ amd_comgr_status_t AMDGPUCompiler::addDeviceLibraries() {
10891095 for (auto DeviceLib : getDeviceLibraries ()) {
10901096 llvm::SmallString<128 > DeviceLibPath = DeviceLibsDir;
10911097 path::append (DeviceLibPath, std::get<0 >(DeviceLib));
1092- if (auto Status = outputToFile (std::get<1 >(DeviceLib), DeviceLibPath)) {
1093- return Status;
1098+ // TODO: We should abstract the logic of deciding whether to use the VFS
1099+ // or the real file system within inputFromFile and outputToFile.
1100+ if (UseVFS) {
1101+ if (!InMemoryFS->addFile (
1102+ DeviceLibPath, /* ModificationTime */ 0 ,
1103+ llvm::MemoryBuffer::getMemBuffer (std::get<1 >(DeviceLib)))) {
1104+ return AMD_COMGR_STATUS_ERROR;
1105+ }
1106+ } else {
1107+ if (auto Status = outputToFile (std::get<1 >(DeviceLib), DeviceLibPath)) {
1108+ return Status;
1109+ }
10941110 }
10951111 }
10961112 }
@@ -1944,6 +1960,25 @@ AMDGPUCompiler::AMDGPUCompiler(DataAction *ActionInfo, DataSet *InSet,
19441960 : ActionInfo(ActionInfo), InSet(InSet), OutSetT(DataSet::convert(OutSet)),
19451961 LogS (LogS) {
19461962 initializeCommandLineArgs (Args);
1963+
1964+ // Initialize OverlayFS with the real file system which helps redirect
1965+ // non-VFS reads and writes.
1966+ OverlayFS = new vfs::OverlayFileSystem (vfs::getRealFileSystem ());
1967+
1968+ std::optional<bool > VFSStatus = env::shouldUseVFS ();
1969+ if ((VFSStatus.has_value () && *VFSStatus) ||
1970+ (!VFSStatus.has_value () && ActionInfo->ShouldUseVFS )) {
1971+ if (env::shouldEmitVerboseLogs ()) {
1972+ LogS << " File System: VFS\n " ;
1973+ }
1974+ UseVFS = true ;
1975+ InMemoryFS = new vfs::InMemoryFileSystem;
1976+ OverlayFS->pushOverlay (InMemoryFS);
1977+ } else {
1978+ if (env::shouldEmitVerboseLogs ()) {
1979+ LogS << " File System: Real\n " ;
1980+ }
1981+ }
19471982}
19481983
19491984AMDGPUCompiler::~AMDGPUCompiler () {
0 commit comments