[llvm] b5149f4 - [LTO] Fix assertion failed when flushing bitcode incrementally for LTO output.
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 4 21:40:29 PST 2022
Author: Xu Mingjie
Date: 2022-01-04T21:40:23-08:00
New Revision: b5149f4e66a49a98b67e8e2de4e24a4af8e2781b
URL: https://github.com/llvm/llvm-project/commit/b5149f4e66a49a98b67e8e2de4e24a4af8e2781b
DIFF: https://github.com/llvm/llvm-project/commit/b5149f4e66a49a98b67e8e2de4e24a4af8e2781b.diff
LOG: [LTO] Fix assertion failed when flushing bitcode incrementally for LTO output.
In https://reviews.llvm.org/D86905, we introduce an optimization, when lld emits LLVM bitcode,
we allow bitcode writer flush data to disk early when buffered data size is above some threshold.
But when `--plugin-opt=emit-llvm` and `-o /dev/null` are used,
lld will trigger assertion `BytesRead >= 0 && static_cast<size_t>(BytesRead) == BytesFromDisk`.
When we write output to /dev/null, BytesRead is zero, but at this program point BytesFromDisk is always non-zero.
Reviewed By: stephan.yichao.zhao, MaskRay
Differential Revision: https://reviews.llvm.org/D112297
Added:
Modified:
lld/test/ELF/lto/emit-llvm.ll
llvm/include/llvm/Support/raw_ostream.h
llvm/lib/Support/raw_ostream.cpp
Removed:
################################################################################
diff --git a/lld/test/ELF/lto/emit-llvm.ll b/lld/test/ELF/lto/emit-llvm.ll
index a2020b46069c0..e80ef570b4e81 100644
--- a/lld/test/ELF/lto/emit-llvm.ll
+++ b/lld/test/ELF/lto/emit-llvm.ll
@@ -4,6 +4,10 @@
; RUN: ld.lld --plugin-opt=emit-llvm -o %t.out.o %t.o
; RUN: llvm-dis < %t.out.o -o - | FileCheck %s
+;; Regression test for D112297: bitcode writer used to crash when
+;; --plugin-opt=emit-llvmis enabled and the output is /dev/null.
+; RUN: ld.lld --plugin-opt=emit-llvm -mllvm -bitcode-flush-threshold=0 -o /dev/null %t.o
+
; CHECK: define internal void @main()
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 98c26ef0b1e52..995b660695d28 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -444,6 +444,7 @@ class raw_fd_ostream : public raw_pwrite_stream {
int FD;
bool ShouldClose;
bool SupportsSeeking = false;
+ bool IsRegularFile = false;
mutable Optional<bool> HasColors;
#ifdef _WIN32
@@ -514,6 +515,8 @@ class raw_fd_ostream : public raw_pwrite_stream {
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);
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index 4590a3d19b0dd..6881c7be039cd 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -643,13 +643,14 @@ raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered,
// 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 @@ raw_fd_stream::raw_fd_stream(StringRef Filename, std::error_code &EC)
if (EC)
return;
- // Do not support non-seekable files.
- if (!supportsSeeking())
+ if (!isRegularFile())
EC = std::make_error_code(std::errc::invalid_argument);
}
More information about the llvm-commits
mailing list