[PATCH] D112297: [LTO] Fix assertion failed when flushing bitcode incrementally for LTO output.

Xu Mingjie via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 7 06:15:05 PST 2021


Enna1 updated this revision to Diff 385337.
Enna1 added a comment.

Update patch.
Add a bool `IsRegularFile` field like bool `SupportsSeeking` in raw_fd_ostream.
Cache the result `Status.type() == sys::fs::file_type::regular_file` in the constructor for raw_fd_ostream,
and replace checking `SupportsSeeking` with checking `IsRegularFile` in raw_fd_stream constructor, if not a regular file, return an error code.

We can not add an additional check `Status.type() == sys::fs::file_type::regular_file` for `SupportsSeeking`, this will fail another two tests, @stephan.yichao.zhao wrote an explanation for this.

In D112297#3112195 <https://reviews.llvm.org/D112297#3112195>, @MaskRay wrote:

> You can upload a new diff.
>
> I support that `SupportsSeeking = ` additionally checks `Status.type() == sys::fs::file_type::regular_file`, so that Windows/non-Windows code paths are unified.

Here :)

In D112297#3093809 <https://reviews.llvm.org/D112297#3093809>, @stephan.yichao.zhao wrote:

> Thank you for testing this.
>
> The test cases fail because the code seeks <https://github.com/llvm/llvm-project/blob/main/llvm/lib/Support/raw_ostream.cpp#L820> on /dev/null.  Somehow /dev/null can be seeked to the current position, but not others, while BitcodeWriter fails because it seeks /dev/null to non-current position.
>
> BitcodeWriter relies on raw_fd_stream. If the creation of raw_fd_stream returns an error, BitcodeWriter will gets only raw_fd_ostream. raw_fd_stream uses supportsSeek <https://github.com/llvm/llvm-project/blob/main/llvm/lib/Support/raw_ostream.cpp#L918> to check if returning an error.
>
> raw_fd_stream was introduced <https://github.com/llvm/llvm-project/commit/0ece51c60c51f0d4c285dbda3b6cff794041bdd7> for BitcodeWriter to support that optimization.  So I am suggesting we put your original change here <https://github.com/llvm/llvm-project/blob/main/llvm/lib/Support/raw_ostream.cpp#L918> like this
>
>   sys::fs::file_status Status;
>   std::error_code EC = status(get_fd(), Status);
>   if (EC || Status.type() != sys::fs::file_type::regular_file)
>       EC = std::make_error_code(std::errc::invalid_argument);




CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112297/new/

https://reviews.llvm.org/D112297

Files:
  lld/test/ELF/lto/emit-llvm_assertion_failure.ll
  llvm/include/llvm/Support/raw_ostream.h
  llvm/lib/Support/raw_ostream.cpp


Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -643,13 +643,14 @@
 
   // Get the starting position.
   off_t loc = ::lseek(FD, 0, SEEK_CUR);
-#ifdef _WIN32
-  // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
   sys::fs::file_status Status;
   std::error_code EC = status(FD, Status);
-  SupportsSeeking = !EC && Status.type() == sys::fs::file_type::regular_file;
+  IsRegularFile = Status.type() == sys::fs::file_type::regular_file;
+#ifdef _WIN32
+  // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
+  SupportsSeeking = !EC && IsRegularFile;
 #else
-  SupportsSeeking = loc != (off_t)-1;
+  SupportsSeeking = !EC && loc != (off_t)-1;
 #endif
   if (!SupportsSeeking)
     pos = 0;
@@ -914,8 +915,7 @@
   if (EC)
     return;
 
-  // Do not support non-seekable files.
-  if (!supportsSeeking())
+  if (!isRegularFile())
     EC = std::make_error_code(std::errc::invalid_argument);
 }
 
Index: llvm/include/llvm/Support/raw_ostream.h
===================================================================
--- llvm/include/llvm/Support/raw_ostream.h
+++ llvm/include/llvm/Support/raw_ostream.h
@@ -444,6 +444,7 @@
   int FD;
   bool ShouldClose;
   bool SupportsSeeking = false;
+  bool IsRegularFile = false;
   mutable Optional<bool> HasColors;
 
 #ifdef _WIN32
@@ -514,6 +515,8 @@
 
   bool supportsSeeking() const { return SupportsSeeking; }
 
+  bool isRegularFile() const { return IsRegularFile; }
+
   /// Flushes the stream and repositions the underlying file descriptor position
   /// to the offset specified from the beginning of the file.
   uint64_t seek(uint64_t off);
Index: lld/test/ELF/lto/emit-llvm_assertion_failure.ll
===================================================================
--- /dev/null
+++ lld/test/ELF/lto/emit-llvm_assertion_failure.ll
@@ -0,0 +1,14 @@
+; Make sure that if --plugin-opt=emit-llvm is enabled and the output
+; is /dev/null, it should not cause a assertion failure in bitcode writer.
+
+; REQUIRES: x86
+
+; RUN: opt -module-hash -module-summary %s -o %t.o
+; RUN: ld.lld --plugin-opt=emit-llvm -mllvm -bitcode-flush-threshold=0 -o /dev/null %t.o
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @main() {
+  ret void
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112297.385337.patch
Type: text/x-patch
Size: 2431 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211107/77c6d227/attachment.bin>


More information about the llvm-commits mailing list