This doesn’t seem necessary, as the problem in the referenced cl only affects memory mapped files.  Furthermore, this will probably adversely impact performance.<br><div class="gmail_quote"><div dir="ltr">On Tue, May 8, 2018 at 7:27 PM Bob Haarman via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">inglorion created this revision.<br>
inglorion added reviewers: rnk, zturner.<br>
Herald added subscribers: hiraditya, mehdi_amini.<br>
<br>
We have observed a problem where files we write on Windows are<br>
sometimes read back with bytes having value 0, when that was not the<br>
value that was written. <a href="https://reviews.llvm.org/D42925" rel="noreferrer" target="_blank">https://reviews.llvm.org/D42925</a> works around this problem for files we<br>
write to by memory-mapping them. This change applies a similar fix for<br>
files we write using raw_fd_ostream, as we do for archives and in the<br>
LTO cache.<br>
<br>
<br>
<a href="https://reviews.llvm.org/D46621" rel="noreferrer" target="_blank">https://reviews.llvm.org/D46621</a><br>
<br>
Files:<br>
  llvm/lib/Support/raw_ostream.cpp<br>
<br>
<br>
Index: llvm/lib/Support/raw_ostream.cpp<br>
===================================================================<br>
--- llvm/lib/Support/raw_ostream.cpp<br>
+++ llvm/lib/Support/raw_ostream.cpp<br>
@@ -551,6 +551,15 @@<br>
   if (FD >= 0) {<br>
     flush();<br>
     if (ShouldClose) {<br>
+#if defined(LLVM_ON_WIN32)<br>
+      // On Windows, we have observed that programs that read a file after it<br>
+      // was written will sometimes read bytes with value 0, when that isn't<br>
+      // actually the value that was written. Calling FlushFileBuffers before<br>
+      // closing the file seems to avoid the issue.<br>
+      HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));<br>
+      if (FileHandle != INVALID_HANDLE_VALUE)<br>
+        ::FlushFileBuffers(FileHandle);<br>
+#endif<br>
       if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))<br>
         error_detected(EC);<br>
     }<br>
<br>
<br>
</blockquote></div>