<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.apple-style-span
        {mso-style-name:apple-style-span;}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Hi,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Yes.   It could be done fairly easily with memory mapped files, which would probably be the most efficient for this type of buffered access, using MapViewOfFile and FlushViewOfFile.   I’d have to do some speed tests to be sure though.  I’ll begin playing around with it soon (probably this weekend, work and all that) and try to determine whether a single streamed write on flush or a memory map ends up being faster.  <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Cheers,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Gordon<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Nick Kledzik [mailto:kledzik@apple.com] <br><b>Sent:</b> Tuesday, May 08, 2012 6:41 PM<br><b>To:</b> Gordon Keiser<br><b>Cc:</b> LLVM Developers Mailing List<br><b>Subject:</b> Re: [LLVMdev] [RFC] llvm/include/Support/OutputBuffer.h<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On May 8, 2012, at 3:52 AM, Gordon Keiser wrote:<o:p></o:p></p></div><p class=MsoNormal><br><br><o:p></o:p></p><div><div><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>FWIW, I'd be interested in working on the Windows implementation.   I've been knee-deep in *nixes lately and wouldn't mind the refresher.  <span class=apple-converted-space> </span></span><span style='font-size:11.0pt;font-family:Wingdings;color:#1F497D'>J</span><o:p></o:p></p></div></div><div><p class=MsoNormal>Cool!   <o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Does my proposed interface make sense to implement on top of Windows APIs?  <o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>-Nick<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><p class=MsoNormal><a name="_MailEndCompose"><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'> </span></a><o:p></o:p></p></div><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt;border-width:initial;border-color:initial'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in;border-width:initial;border-color:initial'><div><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span class=apple-converted-space><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> </span></span><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'><a href="mailto:llvmdev-bounces@cs.uiuc.edu">llvmdev-bounces@cs.uiuc.edu</a><span class=apple-converted-space> </span><a href="mailto:[mailto:llvmdev-bounces@cs.uiuc.edu]">[mailto:llvmdev-bounces@cs.uiuc.edu]</a><span class=apple-converted-space> </span><b>On Behalf Of<span class=apple-converted-space> </span></b>Nick Kledzik<br><b>Sent:</b><span class=apple-converted-space> </span>Monday, May 07, 2012 3:57 PM<br><b>To:</b><span class=apple-converted-space> </span>LLVM Developers Mailing List<br><b>Subject:</b><span class=apple-converted-space> </span>[LLVMdev] [RFC] llvm/include/Support/OutputBuffer.h</span><o:p></o:p></p></div></div></div><div><p class=MsoNormal> <o:p></o:p></p></div><div><div><p class=MsoNormal>For the reasons listed in my 03-May-2012 email, I am proposing a new llvm/Support class for using in writing binary files:<o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><p class=MsoNormal><span style='font-size:7.5pt'>/// OutputBuffer - This interface provides simple way to create an in-memory<br>/// buffer which when done will be written to a file. During the lifetime of <br>/// </span><span class=apple-style-span><span style='font-size:10.0pt'>these</span></span>  <span style='font-size:7.5pt'>objects, the content or existence of the specified file is undefined. <br>/// That is, creating an OutputBuffer for a file may immediately remove the <br>/// file.<br>/// If the OutputBuffer is committed, the target file's content will become <br>/// the buffer content at the time of the commit.  If the OutputBuffer is not <br>/// committed, the file will be deleted in the OutputBuffer buffer destructor.<br>class OutputBuffer {<br>public:<br>  enum Flags {<br>    F_executable = 1, /// set the 'x' bit on the resulting file<br>  }; <br><br>  /// Factory method to create an OutputBuffer object which manages a read/write<br>  /// buffer of the specified size. When committed, the buffer will be written<br>  /// to the file at the specified path.  <br>  static error_code createFile(StringRef filePath, Flags flags, size_t size, <br>                               OwningPtr<OutputBuffer> &result);<br>  <br><br>  /// Returns a pointer to the start of the buffer.<br>  uint8_t *bufferStart();<br>  <br>  /// Returns a pointer to the end of the buffer.<br>  uint8_t *bufferEnd();<br>  <br>  /// Returns size of the buffer.<br>  size_t size();<br>    <br>  /// Flushes the content of the buffer to its file and deallocates the <br>  /// buffer.  If commit() is not called before this object's destructor<br>  /// is called, the file is deleted in the destructor. The optional parameter<br>  /// is used if it turns out you want the file size to be smaller than<br>  /// initially requested.<br>  void commit(int64_t newSmallerSize = -1);<br>};</span><o:p></o:p></p></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal>The Flags will probable need to be extended over time to handle other clients needs.<o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal>For Unix/Darwin, my plan is to implement this by:<o:p></o:p></p></div></div><div><div><p class=MsoNormal>1) delete the file<o:p></o:p></p></div></div><div><div><p class=MsoNormal>2) create a new file with a random name in same directory<o:p></o:p></p></div></div><div><div><p class=MsoNormal>3) truncate the file to the new size<o:p></o:p></p></div></div><div><div><p class=MsoNormal>4) mmap() in the file r/w<o:p></o:p></p></div></div><div><div><p class=MsoNormal>5) On commit, unmap the file, rename() to final name<o:p></o:p></p></div></div><div><div><p class=MsoNormal>6) In destructor, if not committed, unmap, delete the randomly named file<o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal>I'll leave the windows implementation empty and let someone with windows experience do the implementation.<o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal>Comments? Suggestions?<o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal>-Nick<o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div></div><div><div><p class=MsoNormal> <o:p></o:p></p></div><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><p class=MsoNormal>On May 3, 2012, at 6:10 PM, Nick Kledzik wrote:<o:p></o:p></p></div></div></blockquote></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><p class=MsoNormal>Existing llvm code tends to use raw_ostream for writing files.  But raw_ostream is not a good match for a linker for a couple of reasons:<br><br>1) When the linker creates an executable, the file needs the 'x' bit set.  Currently raw_fd_ostream has no way to set that.<br><br>2) The Unix conformance suite actually has some test cases where the linker is run and the output file does exists but is not writable, or is not writable but is in a writable directory, or with funky umask values.   raw_fd_ostream interface has no way to match those semantics.<br><br>3) On darwin we have found the linker performs better if it opens the output file, truncates it to the output size, then mmaps in the file, then writes directly into that memory buffer.  This avoids the memory copy from the private buffer to the OS file system buffer in the write() syscall.<br><br>4) In the model we are using for lld, a streaming output interface is not optimal.   Currently, lld copies chunks of code from the (read-only) input files, to a temporary buffer, then applies any fixups (relocations), then streams out that temporary buffer.  If instead we had a big output buffer, the linker could copy the code chunks directly to the output buffer and apply the fixups there, avoiding an extra copy. <o:p></o:p></p></div></div><div><p class=MsoNormal> <o:p></o:p></p></div></blockquote></div></div></div></blockquote></div><p class=MsoNormal><o:p> </o:p></p></div></div></body></html>