Skip to content

Commit d1c0b1b

Browse files
authored
[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.
1 parent 05bbb94 commit d1c0b1b

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,8 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
10381038
/*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false,
10391039
hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false);
10401040
OMPBuilder.initialize();
1041-
OMPBuilder.loadOffloadInfoMetadata(CGM.getLangOpts().OpenMPIsTargetDevice
1041+
OMPBuilder.loadOffloadInfoMetadata(*CGM.getFileSystem(),
1042+
CGM.getLangOpts().OpenMPIsTargetDevice
10421043
? CGM.getLangOpts().OMPHostIRFile
10431044
: StringRef{});
10441045
OMPBuilder.setConfig(Config);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,7 @@ void CodeGenModule::createOpenCLRuntime() {
582582
}
583583

584584
void CodeGenModule::createOpenMPRuntime() {
585-
if (!LangOpts.OMPHostIRFile.empty() &&
586-
!llvm::sys::fs::exists(LangOpts.OMPHostIRFile))
585+
if (!LangOpts.OMPHostIRFile.empty() && !FS->exists(LangOpts.OMPHostIRFile))
587586
Diags.Report(diag::err_omp_host_ir_file_not_found)
588587
<< LangOpts.OMPHostIRFile;
589588

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This test checks that the OpenMP host IR file goes through VFS overlays.
2+
3+
// RUN: rm -rf %t
4+
// RUN: split-file %s %t
5+
6+
// RUN: sed -e "s|DIR|%/t|g" %t/vfs.json.in > %t/vfs.json
7+
// 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
8+
9+
// RUN: %clang_cc1 -fopenmp-simd -triple i386-unknown-unknown -fopenmp-targets=i386-pc-linux-gnu -emit-llvm %t/device.c -o - \
10+
// RUN: -fopenmp-is-target-device -fopenmp-host-ir-file-path %t/virtual/host.bc -ivfsoverlay %t/vfs.json -verify
11+
12+
//--- vfs.json.in
13+
{
14+
'version': 0,
15+
'use-external-names': true,
16+
'roots': [
17+
{
18+
'name': 'DIR/virtual',
19+
'type': 'directory',
20+
'contents': [
21+
{
22+
'name': 'host.bc',
23+
'type': 'file',
24+
'external-contents': 'DIR/host.bc'
25+
}
26+
]
27+
}
28+
]
29+
}
30+
31+
//--- host.c
32+
//--- device.c
33+
// expected-no-diagnostics

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class Loop;
3939
class LoopAnalysis;
4040
class LoopInfo;
4141

42+
namespace vfs {
43+
class FileSystem;
44+
} // namespace vfs
45+
4246
/// Move the instruction after an InsertPoint to the beginning of another
4347
/// BasicBlock.
4448
///
@@ -3629,7 +3633,8 @@ class OpenMPIRBuilder {
36293633
/// \param HostFilePath The path to the host IR file,
36303634
/// used to load in offload metadata for the device, allowing host and device
36313635
/// to maintain the same metadata mapping.
3632-
LLVM_ABI void loadOffloadInfoMetadata(StringRef HostFilePath);
3636+
LLVM_ABI void loadOffloadInfoMetadata(vfs::FileSystem &VFS,
3637+
StringRef HostFilePath);
36333638

36343639
/// Gets (if variable with the given name already exist) or creates
36353640
/// internal global variable with the specified Name. The created variable has

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/Support/CommandLine.h"
5151
#include "llvm/Support/ErrorHandling.h"
5252
#include "llvm/Support/FileSystem.h"
53+
#include "llvm/Support/VirtualFileSystem.h"
5354
#include "llvm/Target/TargetMachine.h"
5455
#include "llvm/Target/TargetOptions.h"
5556
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -10531,11 +10532,12 @@ void OpenMPIRBuilder::loadOffloadInfoMetadata(Module &M) {
1053110532
}
1053210533
}
1053310534

10534-
void OpenMPIRBuilder::loadOffloadInfoMetadata(StringRef HostFilePath) {
10535+
void OpenMPIRBuilder::loadOffloadInfoMetadata(vfs::FileSystem &VFS,
10536+
StringRef HostFilePath) {
1053510537
if (HostFilePath.empty())
1053610538
return;
1053710539

10538-
auto Buf = MemoryBuffer::getFile(HostFilePath);
10540+
auto Buf = VFS.getBufferForFile(HostFilePath);
1053910541
if (std::error_code Err = Buf.getError()) {
1054010542
report_fatal_error(("error opening host file from host file path inside of "
1054110543
"OpenMPIRBuilder: " +

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/IR/MDBuilder.h"
3434
#include "llvm/IR/ReplaceConstant.h"
3535
#include "llvm/Support/FileSystem.h"
36+
#include "llvm/Support/VirtualFileSystem.h"
3637
#include "llvm/TargetParser/Triple.h"
3738
#include "llvm/Transforms/Utils/ModuleUtils.h"
3839

@@ -6334,7 +6335,9 @@ LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
63346335
if (auto filepathAttr = dyn_cast<StringAttr>(attr)) {
63356336
llvm::OpenMPIRBuilder *ompBuilder =
63366337
moduleTranslation.getOpenMPBuilder();
6337-
ompBuilder->loadOffloadInfoMetadata(filepathAttr.getValue());
6338+
auto VFS = llvm::vfs::getRealFileSystem();
6339+
ompBuilder->loadOffloadInfoMetadata(*VFS,
6340+
filepathAttr.getValue());
63386341
return success();
63396342
}
63406343
return failure();

0 commit comments

Comments
 (0)