<div dir="ltr"><div>Really glad to see this work, virtualizing module cache is something we've wanted to experiment with for tooling, but never got to. I want to get into the patches in more detail, but some high-level thoughts...</div><div><br></div><div dir="ltr">On Wed, Jan 27, 2021 at 6:23 AM Duncan P. N. Exon Smith via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><b>TL;DR</b>: Let's virtualize compiler outputs in Clang. These patches would get us started:</div><div>- <a href="https://reviews.llvm.org/D95501" target="_blank">https://reviews.llvm.org/D95501</a> Add llvm::vfs::OutputManager</div><div>- <a href="https://reviews.llvm.org/D95502" target="_blank">https://reviews.llvm.org/D95502</a> Initial adoption of llvm::vfs::OutputManager in Clang.</div><div><br></div><div><b>Questions for the reader</b></div><div>- Should we virtualize compiler outputs in Clang? (Hint: <i>yes</i>.)</div></div></blockquote><div>Definitely agree.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><span style="white-space:pre-wrap"> </span>- Does this support belong in LLVM? (I think it does, so that non-Clang tools can easily reuse it.)</div></div></blockquote><div>Ideally, the core abstraction (path -> pwrite_stream) certainly belongs in LLVM, as well as the most common implementations of it.</div><div>Based on experience with VirtualFileSystem, I'd like this interface to be as narrow as possible to make it feasible to implement/wrap correctly, and to reason about how the wider system interacts with it.</div><div><br></div><div>This roughly corresponds to OutputBackend + OutputDestination in the patch, except:</div><div> - the OutputConfig seems like it belongs to particular backends, not the overall backend abstraction</div><div> - OutputDestination has a lot of stuff in it, I'll need to dig further into the patch to try to understand why</div><div><br></div><div>As for OutputManager itself, I think this belongs in clang, if it's needed. Its main job seems to be to store a set of default options and manage the lifecycle of backends, and it's not obvious that those sorts of concerns will generalize across tools or that there's much to be gained from sharing code here.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><span style="white-space:pre-wrap"> </span>- Is `llvm::vfs::` a reasonable namespace? (If not, suggestions? I think `llvm::` itself is too broad.)</div></div></blockquote><div>llvm::vfs:: is definitely the right namespace for the core writing stuff IMO.</div><div>If more ancillary bits (parts some but not all tools might use) need to go in llvm, llvm:: seems to be the best namespace we have (like e.g. SourceMgr) but maybe we should add a new one. But as mentioned, I'd prefer those to live in clang:: at least for now.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div>- Do you have a use case that this won't address well?</div><div><span style="white-space:pre-wrap"> </span>- Should that be fixed in the initial patch, or could this be evolved in-tree to address that?</div><div>- Any other major concerns / suggestions?</div></div></blockquote><div>Thread-safety of the core plug-in interface is something that would be nice to explicitly address, as this has been a pain-point with vfs::FileSystem.</div><div>It's tempting to say "not threadsafe, you should lock", but this results in throwing an unnecessary global lock around all FS accesses in multithreaded programs, in the common case that the real FS is being used.</div><div><br></div><div>Relatedly, working-directory/relative-path handling should be considered.</div><div><br></div><div>(And a question/concern about the relationship between input and output virtualization, elaborated at the bottom)</div><div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div>- If you think the above patches should be split up for initial review / commit, how?</div></div></blockquote><div>Obviously my favorite would be to see a minimal core writable VFS interface extracted and land that first. What's built on top of it is less critical, and I'm not concerned about it landing in larger chunks.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><br></div><div>(Other feedback welcome too!)</div><div><br></div><div><b>Longer version</b></div><div>There are a number of use cases for capturing compiler outputs, which I'm hoping this proposal is a step toward addressing.</div><div><br></div><div>- Tooling wants to capture outputs directly, without going through the filesystem.</div><div><span style="white-space:pre-wrap"> </span>- Sometimes, tertiary outputs can be ignored, or still need to be written to disk.</div><div>- clang-scan-deps is using a form of stripped down "implicit" modules to determine which modules need to be built explicitly. It doesn't really want to be using the on-disk module cache—in-memory would be better.</div><div>- clang's ModuleManager manually maintains an in-memory modules cache for implicit modules. This involves copying the PCM outputs into memory. It'd be better for these modules to be file-backed, instead of copies of the stream.</div><div><br></div><div>The patch has a bunch of details written / tested (<a href="https://reviews.llvm.org/D95501" target="_blank">https://reviews.llvm.org/D95501</a>). Here are the high-level structures in the design:</div><div><br></div><div>- OutputManager—a shared manager for creating outputs without knowing about the storage.</div><div>- OutputConfig—configuration set on the OutputManager that can be (partially) overridden for specific outputs.</div><div>- Output—opaque object with a raw_pwrite_stream, output path, and `erase`/`close` functionality. Internally, it has a linked list of output destinations.</div><div>- OutputBackend—abstract interface installed in an OutputManager to create the "guts" of an output. While an OutputManager only has one installed, these can be layered / forked / mirrored.</div><div>- OutputDestination—abstract interface paired with an OutputBackend, whose lifetime is managed by an Output.</div><div>- ContentBuffer—actual content to allow efficient use of data by multiple backends. For example, the installed backend is a mirror between an on-disk and in-memory backend, the in-memory backend will either get the content moved directly into an llvm::MemoryBuffer, or a just-written mmap'ed file.</div><div><br></div><div>The patch includes a few backends:</div><div><br></div>- NullOutputBackend, for ignoring all backends.<br>- OnDiskOutputBackend, for writing to disk (the default), initially based on the logic in `clang::CompilerInstance`.<br>- InMemoryOutputBackend, for writing to an `InMemoryFileSystem`.<br>- MirroringOutputBackend, for writing to multiple backends. OutputDestination's API is designed around supporting this.<br>- FilteringOutputBackend, for filtering which outputs get written to the underlying backend.<br><br><b>Why doesn't this inherit from llvm::vfs::FileSystem?</b><br>Separating the input and output abstractions seems a bit cleaner. It's easy enough to join them, when useful: e.g., write to an `InMemoryFileSystem` (via an `InMemoryOutputBackend`) and install this same FS in the `FileManager` (maybe indirectly via an `OverlayFileSystem`).</div></blockquote><div>I agree with separating the interfaces. In hosted environments your input VFS is often read-only and outputs go somewhere else. </div><div><br></div><div>But I wonder, is there an implicit assumption that data written to OutputManager is readable via the (purportedly independent) vfs::FileSystem? This seems like a helpful assumption for module caching, but is extremely constraining and eliminates many of the simplest and most interesting possibilities.</div><div><br></div><div>If you're going to require the FileSystem and OutputBackend to be linked, then I think they *should* be the same object.</div><div>But if it's mostly module caching that requires that, then it seems simpler and less invasive to virtualize module caching directly (put(module, key) + get(key)) rather than file access.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><b>Other work in the area</b></div><div>See also: <a href="https://reviews.llvm.org/D78058" target="_blank">https://reviews.llvm.org/D78058</a> (thanks to Marc Rasi for posting that patch, and to Sam McCall for some feedback on an earlier version of this proposal).<br><br>Thanks for reading!<div>Duncan</div></div></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>