Skip to content

Commit ebc9886

Browse files
authored
[clang-offload-bundler][NFC] Unify FileHandler interfaces with the upstream LLVM (#2823)
Signed-off-by: Sergey Dmitriev <serguei.n.dmitriev@intel.com>
1 parent 20cb0da commit ebc9886

File tree

1 file changed

+42
-93
lines changed

1 file changed

+42
-93
lines changed

clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp

Lines changed: 42 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,6 @@ class FileHandler {
163163
/// Read the current bundle and write the result into the stream \a OS.
164164
virtual Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) = 0;
165165

166-
/// Read the current bundle and write the result into the file \a FileName.
167-
/// The meaning of \a FileName depends on unbundling type - in some
168-
/// cases (type="oo") it will contain a list of actual outputs.
169-
virtual Error ReadBundle(StringRef FileName, MemoryBuffer &Input) {
170-
std::error_code EC;
171-
raw_fd_ostream OS(FileName, EC);
172-
173-
if (EC)
174-
return createFileError(FileName, EC);
175-
return ReadBundle(OS, Input);
176-
}
177-
178166
/// Write the header of the bundled file to \a OS based on the information
179167
/// gathered from \a Inputs.
180168
virtual Error WriteHeader(raw_fd_ostream &OS,
@@ -354,8 +342,6 @@ class BinaryFileHandler final : public FileHandler {
354342
return Error::success();
355343
}
356344

357-
using FileHandler::ReadBundle; // to avoid hiding via the overload below
358-
359345
Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) final {
360346
assert(CurBundleInfo != BundlesInfo.end() && "Invalid reader info!");
361347
StringRef FC = Input.getBuffer();
@@ -638,23 +624,14 @@ class ObjectFileHandler final : public FileHandler {
638624

639625
Error ReadBundleEnd(MemoryBuffer &Input) final { return Error::success(); }
640626

641-
Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) {
642-
llvm_unreachable("must not be called for the ObjectFileHandler");
643-
}
644-
645-
Error ReadBundle(StringRef OutName, MemoryBuffer &Input) final {
627+
Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) final {
646628
assert(CurBundle != TripleToBundleInfo.end() &&
647629
"all bundles have been read already");
648630

649631
// TODO: temporary workaround to copy fat object to the host output until
650632
// driver is fixed to correctly handle list file for the host bundle in
651633
// 'oo' mode.
652634
if (FilesType == "oo" && hasHostKind(CurBundle->getKey())) {
653-
std::error_code EC;
654-
raw_fd_ostream OS(OutName, EC);
655-
656-
if (EC)
657-
return createFileError(OutName, EC);
658635
OS.write(Input.getBufferStart(), Input.getBufferSize());
659636
return Error::success();
660637
}
@@ -676,35 +653,13 @@ class ObjectFileHandler final : public FileHandler {
676653
errc::invalid_argument,
677654
"'o' file type is requested, but the fat object contains multiple "
678655
"device objects; use 'oo' instead");
679-
std::string FileList;
680656

681657
// Iterate through individual objects and extract them
682658
for (size_t I = 0; I < NumObjects; ++I) {
683659
uint64_t ObjSize = SizeVec[I];
684-
// Flag for the special case used to "unbundle" host target object.
685-
bool HostTriple = ObjSize == 1;
686-
687-
StringRef ObjFileName = OutName;
688-
SmallString<128> Path;
689-
690-
// If not in file list mode there is no need in a temporary file - output
691-
// goes directly to what was specified in -outputs. The same is true for
692-
// the host triple.
693-
if (FileListMode && !HostTriple) {
694-
std::error_code EC =
695-
sys::fs::createTemporaryFile(TempFileNameBase, "devo", Path);
696-
ObjFileName = Path.data();
697-
698-
if (EC)
699-
return createFileError(ObjFileName, EC);
700-
}
701-
std::error_code EC;
702-
raw_fd_ostream OS(ObjFileName, EC);
703-
704-
if (EC)
705-
return createFileError(ObjFileName, EC);
706660

707-
if (HostTriple) {
661+
// Flag for the special case used to "unbundle" host target object.
662+
if (ObjSize == 1) {
708663
// Copy fat object contents to the output when extracting host bundle.
709664

710665
// In the partially linked fat object multiple dummy host bundles were
@@ -716,26 +671,31 @@ class ObjectFileHandler final : public FileHandler {
716671
OS.write(Input.getBufferStart(), Input.getBufferSize());
717672
break;
718673
}
719-
OS.write(ObjData, ObjSize);
720674

721675
if (FileListMode) {
676+
SmallString<128> ObjFileName;
677+
std::error_code EC =
678+
sys::fs::createTemporaryFile(TempFileNameBase, "devo", ObjFileName);
679+
if (EC)
680+
return createFileError(ObjFileName, EC);
681+
682+
raw_fd_ostream ObjOS(ObjFileName, EC);
683+
if (EC)
684+
return createFileError(ObjFileName, EC);
685+
686+
ObjOS.write(ObjData, ObjSize);
687+
if (ObjOS.has_error())
688+
return createFileError(ObjFileName, ObjOS.error());
689+
722690
// add the written file name to the output list of files
723-
FileList = (Twine(FileList) + Twine(ObjFileName) + Twine("\n")).str();
724-
}
691+
OS << ObjFileName << "\n";
692+
} else
693+
OS.write(ObjData, ObjSize);
694+
725695
// Move "object data" pointer to the next object within the concatenated
726696
// bundle.
727697
ObjData += ObjSize;
728698
}
729-
if (FileListMode) {
730-
// dump the list of files into the file list specified in -outputs for the
731-
// current target
732-
std::error_code EC;
733-
raw_fd_ostream OS1(OutName, EC);
734-
735-
if (EC)
736-
return createFileError(OutName, EC);
737-
OS1.write(FileList.data(), FileList.size());
738-
}
739699
return Error::success();
740700
}
741701

@@ -939,8 +899,6 @@ class TextFileHandler final : public FileHandler {
939899
return Error::success();
940900
}
941901

942-
using FileHandler::ReadBundle; // to avoid hiding via the overload below
943-
944902
Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) final {
945903
StringRef FC = Input.getBuffer();
946904
size_t BundleStart = ReadChars;
@@ -1056,7 +1014,7 @@ class ArchiveFileHandler final : public FileHandler {
10561014

10571015
Error ReadBundleEnd(MemoryBuffer &Input) override { return Error::success(); }
10581016

1059-
Error ReadBundle(StringRef OutName, MemoryBuffer &Input) override {
1017+
Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) override {
10601018
assert(CurrBundle->second && "attempt to extract nonexistent bundle");
10611019

10621020
bool FileListMode = FilesType == "aoo";
@@ -1068,11 +1026,6 @@ class ArchiveFileHandler final : public FileHandler {
10681026
"'ao' file type is requested, but the archive contains multiple "
10691027
"device objects; use 'aoo' instead");
10701028

1071-
// In file-list mode archive unbundling produces multiple files, so output
1072-
// file is a file list where we write the unbundled object names.
1073-
SmallVector<char, 0u> FileListBuf;
1074-
raw_svector_ostream FileList{FileListBuf};
1075-
10761029
// Read all children.
10771030
Error Err = Error::success();
10781031
for (auto &C : Ar->children(Err)) {
@@ -1102,25 +1055,30 @@ class ArchiveFileHandler final : public FileHandler {
11021055
// This is the bundle we are looking for. Create temporary file where
11031056
// the device part will be extracted if we are in the file-list mode,
11041057
// or write directly to the output file otherwise.
1105-
SmallString<128u> ChildFileName;
11061058
if (FileListMode) {
1059+
SmallString<128u> ChildFileName;
11071060
auto EC = sys::fs::createTemporaryFile(TempFileNameBase, "o",
11081061
ChildFileName);
11091062
if (EC)
11101063
return createFileError(ChildFileName, EC);
1111-
} else
1112-
ChildFileName = OutName;
11131064

1114-
// And extract the bundle.
1115-
if (Error Err = OFH.ReadBundle(ChildFileName, *Buf))
1116-
return Err;
1117-
if (Error Err = OFH.ReadBundleEnd(*Buf))
1118-
return Err;
1065+
raw_fd_ostream ChildOS(ChildFileName, EC);
1066+
if (EC)
1067+
return createFileError(ChildFileName, EC);
1068+
1069+
if (Error Err = OFH.ReadBundle(ChildOS, *Buf))
1070+
return Err;
1071+
1072+
if (ChildOS.has_error())
1073+
return createFileError(ChildFileName, ChildOS.error());
11191074

1120-
if (FileListMode)
11211075
// Add temporary file name with the device part to the output file
11221076
// list.
1123-
FileList << ChildFileName << "\n";
1077+
OS << ChildFileName << "\n";
1078+
} else if (Error Err = OFH.ReadBundle(OS, *Buf))
1079+
return Err;
1080+
if (Error Err = OFH.ReadBundleEnd(*Buf))
1081+
return Err;
11241082
}
11251083
NameOrErr = OFH.ReadBundleStart(*Buf);
11261084
if (!NameOrErr)
@@ -1129,22 +1087,9 @@ class ArchiveFileHandler final : public FileHandler {
11291087
}
11301088
if (Err)
11311089
return Err;
1132-
1133-
if (FileListMode) {
1134-
// Dump file list to the output file.
1135-
std::error_code EC;
1136-
raw_fd_ostream OS(OutName, EC);
1137-
if (EC)
1138-
return createFileError(OutName, EC);
1139-
OS << FileList.str();
1140-
}
11411090
return Error::success();
11421091
}
11431092

1144-
Error ReadBundle(raw_fd_ostream &OS, MemoryBuffer &Input) override {
1145-
llvm_unreachable("must not be called for the ArchiveFileHandler");
1146-
}
1147-
11481093
Error WriteHeader(raw_fd_ostream &OS,
11491094
ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs) override {
11501095
llvm_unreachable("unsupported for the ArchiveFileHandler");
@@ -1323,7 +1268,11 @@ static Error UnbundleFiles() {
13231268
continue;
13241269

13251270
// Check if the output file can be opened and copy the bundle to it.
1326-
if (Error Err = FH->ReadBundle(Output->second, Input))
1271+
std::error_code EC;
1272+
raw_fd_ostream OutputFile(Output->second, EC, sys::fs::OF_None);
1273+
if (EC)
1274+
return createFileError(Output->second, EC);
1275+
if (Error Err = FH->ReadBundle(OutputFile, Input))
13271276
return Err;
13281277
if (Error Err = FH->ReadBundleEnd(Input))
13291278
return Err;

0 commit comments

Comments
 (0)