Author: Jan Svoboda Date: 2025-09-11T09:08:47-07:00 New Revision: d1c0b1b6203d4005ad9a1baefe843ab45b3693a8
URL: https://github.com/llvm/llvm-project/commit/d1c0b1b6203d4005ad9a1baefe843ab45b3693a8 DIFF: https://github.com/llvm/llvm-project/commit/d1c0b1b6203d4005ad9a1baefe843ab45b3693a8.diff LOG: [clang] Use VFS for `-fopenmp-host-ir-file-path` (#156727) This is a follow-up to #150124. This PR makes it so that the `-fopenmp-host-ir-file-path` respects VFS overlays, like any other input file. Added: clang/test/OpenMP/host-ir-file-vfs.c Modified: clang/lib/CodeGen/CGOpenMPRuntime.cpp clang/lib/CodeGen/CodeGenModule.cpp llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp index 8d67fe21367ac..e80aa1592f252 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -1038,7 +1038,8 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM) /*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false, hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false); OMPBuilder.initialize(); - OMPBuilder.loadOffloadInfoMetadata(CGM.getLangOpts().OpenMPIsTargetDevice + OMPBuilder.loadOffloadInfoMetadata(*CGM.getFileSystem(), + CGM.getLangOpts().OpenMPIsTargetDevice ? CGM.getLangOpts().OMPHostIRFile : StringRef{}); OMPBuilder.setConfig(Config); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 5e96f5bd6e5f8..a16dfb52f4d90 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -582,8 +582,7 @@ void CodeGenModule::createOpenCLRuntime() { } void CodeGenModule::createOpenMPRuntime() { - if (!LangOpts.OMPHostIRFile.empty() && - !llvm::sys::fs::exists(LangOpts.OMPHostIRFile)) + if (!LangOpts.OMPHostIRFile.empty() && !FS->exists(LangOpts.OMPHostIRFile)) Diags.Report(diag::err_omp_host_ir_file_not_found) << LangOpts.OMPHostIRFile; diff --git a/clang/test/OpenMP/host-ir-file-vfs.c b/clang/test/OpenMP/host-ir-file-vfs.c new file mode 100644 index 0000000000000..394d8fbcc94ac --- /dev/null +++ b/clang/test/OpenMP/host-ir-file-vfs.c @@ -0,0 +1,33 @@ +// This test checks that the OpenMP host IR file goes through VFS overlays. + +// RUN: rm -rf %t +// RUN: split-file %s %t + +// RUN: sed -e "s|DIR|%/t|g" %t/vfs.json.in > %t/vfs.json +// RUN: %clang_cc1 -fopenmp-simd -triple i386-unknown-unknown -fopenmp-targets=i386-pc-linux-gnu -emit-llvm-bc %t/host.c -o %t/host.bc + +// RUN: %clang_cc1 -fopenmp-simd -triple i386-unknown-unknown -fopenmp-targets=i386-pc-linux-gnu -emit-llvm %t/device.c -o - \ +// RUN: -fopenmp-is-target-device -fopenmp-host-ir-file-path %t/virtual/host.bc -ivfsoverlay %t/vfs.json -verify + +//--- vfs.json.in +{ + 'version': 0, + 'use-external-names': true, + 'roots': [ + { + 'name': 'DIR/virtual', + 'type': 'directory', + 'contents': [ + { + 'name': 'host.bc', + 'type': 'file', + 'external-contents': 'DIR/host.bc' + } + ] + } + ] +} + +//--- host.c +//--- device.c +// expected-no-diagnostics diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h index 1050e3d8b08dd..cc1177ba3d11c 100644 --- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -39,6 +39,10 @@ class Loop; class LoopAnalysis; class LoopInfo; +namespace vfs { +class FileSystem; +} // namespace vfs + /// Move the instruction after an InsertPoint to the beginning of another /// BasicBlock. /// @@ -3629,7 +3633,8 @@ class OpenMPIRBuilder { /// \param HostFilePath The path to the host IR file, /// used to load in offload metadata for the device, allowing host and device /// to maintain the same metadata mapping. - LLVM_ABI void loadOffloadInfoMetadata(StringRef HostFilePath); + LLVM_ABI void loadOffloadInfoMetadata(vfs::FileSystem &VFS, + StringRef HostFilePath); /// Gets (if variable with the given name already exist) or creates /// internal global variable with the specified Name. The created variable has diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index 3d5e487c8990f..c955ecd403633 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -50,6 +50,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" @@ -10531,11 +10532,12 @@ void OpenMPIRBuilder::loadOffloadInfoMetadata(Module &M) { } } -void OpenMPIRBuilder::loadOffloadInfoMetadata(StringRef HostFilePath) { +void OpenMPIRBuilder::loadOffloadInfoMetadata(vfs::FileSystem &VFS, + StringRef HostFilePath) { if (HostFilePath.empty()) return; - auto Buf = MemoryBuffer::getFile(HostFilePath); + auto Buf = VFS.getBufferForFile(HostFilePath); if (std::error_code Err = Buf.getError()) { report_fatal_error(("error opening host file from host file path inside of " "OpenMPIRBuilder: " + diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp index 2ab6bb0a73200..5e194dc715d59 100644 --- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -33,6 +33,7 @@ #include "llvm/IR/MDBuilder.h" #include "llvm/IR/ReplaceConstant.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/TargetParser/Triple.h" #include "llvm/Transforms/Utils/ModuleUtils.h" @@ -6334,7 +6335,9 @@ LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation( if (auto filepathAttr = dyn_cast<StringAttr>(attr)) { llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); - ompBuilder->loadOffloadInfoMetadata(filepathAttr.getValue()); + auto VFS = llvm::vfs::getRealFileSystem(); + ompBuilder->loadOffloadInfoMetadata(*VFS, + filepathAttr.getValue()); return success(); } return failure(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits