<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On May 24, 2019, at 17:58, Duncan Exon Smith via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 2019 May 24, at 16:50, Richard Smith <<a href="mailto:richard@metafoo.co.uk" class="">richard@metafoo.co.uk</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><div dir="ltr" class="">On Tue, 26 Mar 2019 at 17:14, Duncan Exon Smith via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">I have the beginnings of a patch for a CompilerInstance builder. I'd like some early feedback before going further.<br class=""><br class="">## Questions ##<br class=""><br class="">- Is a builder API a good idea?<br class="">- Should it look something like this? If not, what?<br class="">- What should be in it?<br class="">- How should it be staged?<br class=""><br class="">## Motivation ##<br class=""><br class="">My motivation for looking at this is a little tangential: while working on the InMemoryModuleCache recently (e.g., r355778) I realized that this cache should be leveraging the FileManager to dedup paths by inode. However when I tried a naïve fix I was getting crashes, and I had trouble understanding FileManager/VFS lifetime and ownership. Ultimately, I'm hoping to clarify what the invariants are around an instance of CompilerInstance and its FileManager.<br class=""><br class="">## Is a builder API a good idea? ##<br class=""><br class="">A builder API seems a little cleaner than how we currently manage the boilerplate in constructing a CompilerInstance.<br class=""><br class="">The status quo is that each client default-constructs and then calls various mutators like `setInstance` to set it up. However, this API allows clients to swap in different data structures after construction. If clients don't need or use that flexibility, the code for maintaining that flexibility will be brittle, some combination of broken and confusing.<br class=""><br class="">For example, I cleaned up some confusing logic in r357037 in CompilerInstance::setFileManager and CompilerInstance::createFileManager that were trying to adapt to a CompilerInstance's VFS and FileManager changing at arbitrary times and keep them in sync (in that case, it was easy: just use FileManager's VFS directly).<br class=""><br class="">With a builder API, we can lock down CompilerInstance mutators so that it's clear what is actually allowed to change mid-flight and what isn't. It's probably also easier to handle "obvious defaults" in a sane way for clients that only want to specify one or two things (e.g., unit tests).<br class=""><br class="">## Should it look something like this? If not, what? ##<br class=""><br class="">Here's the WIP patch I have:<br class=""><a href="https://reviews.llvm.org/differential/diff/192381/" rel="noreferrer" target="_blank" class="">https://reviews.llvm.org/differential/diff/192381/</a><br class="">(also attached below, but without the -U9999999 context)<br class=""><br class="">The core idea is to remove `CompilerInstance::set*` and `CompilerInstance::create*` in cases where a CompilerInstance never needs more than one version of a data structure over its lifetime. So far the patch removes CompilerInstance::setInvocation, CompilerInstance::setDiagnostics, and CompilerInstance::createDiagnostics.<br class=""><br class="">The meat is in two files:<br class="">- CompilerInstanceBuilder.h (<a href="https://reviews.llvm.org/differential/diff/192381/#change-AjNDpfG5S3AZ" rel="noreferrer" target="_blank" class="">https://reviews.llvm.org/differential/diff/192381/#change-AjNDpfG5S3AZ</a>): a move-only data structure for collecting the parameters for building a CompilerInstance.<br class="">- CompilerInstance.cpp (<a href="https://reviews.llvm.org/differential/diff/192381/#change-wS2d87BZOVCm" rel="noreferrer" target="_blank" class="">https://reviews.llvm.org/differential/diff/192381/#change-wS2d87BZOVCm</a>): logic to process those parameters at CompilerInstance construction time.<br class=""><br class="">Note on one semantic change I made: CompilerInstance *always* has a DiagnosticsEngine now. I did that because there were tons of sites calling CompilerInstance::createDiagnostics with no arguments. Originally I had a zero-parameter version of CompilerInstanceBuilder::diags that controlled whether to create one, but just doing it all the time seemed to improve the code. My suspicion is that the DiagnosticsEngine is not intended to be optional, it's just hard to sort out how to construct one (without a builder API).<br class=""><br class="">The rest of it all looks like removal of boilerplate to me.<br class=""><br class="">Thoughts on this direction?<br class=""></blockquote><div class=""><br class=""></div><div class="">This seems like a nice idea to me. Building and configuring a CompilerInstance is certainly a mess right now, and this seems to make it cleaner and harder to get wrong.</div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">## What should be in it? ##<br class=""><br class="">My personal goal here is to pull in the prerequisites for removing CompilerInstance::setFileManager. It's possible this won't be possible, but if not I'll figure out why not in the process.<br class=""><br class="">What else should be in there? Here are some options:<br class=""><br class="">- FileManager (I hope so!)<br class="">- SourceManager (I think so!)<br class="">- Preprocessor?<br class="">- TargetInfo?<br class="">- Sema (probably not, it seems truly optional)<br class="">- ModuleManager (probably not?)<br class=""></blockquote><div class=""><br class=""></div><div class="">I think it'd make sense to see how far you can get with the builder pattern. Some of the above need to be created by the FrontendAction, possibly once for each input, and might not fit well into a builder-style approach -- but maybe we're missing a layer there, and there should be some separate object representing the specific action invocation (whose lifetime is the duration of an iteration of the BeginSourceFile/Execute/EndSourceFile loop in CompilerInstance::ExecuteAction) rather than lumping per-action state into the CompilerInvocation and resetting it between each action invocation?</div></div></div></div></blockquote></div></div></div></blockquote><div><br class=""></div><div>Another layer could help. I'll think about this a bit.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><div class="gmail_quote"><div class=""> At least right now, the file manager and source manager are torn down and recreated for each action execution.</div></div></div></div></blockquote></div></div></div></blockquote><div><br class=""></div><div>The FileManager is also reused between module generation actions during implicit module builds.</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><div class="gmail_quote"><div class="">I think we'll need to figure out:</div><div class=""> 1) Do we allow multiple actions to be run on a single compiler instance, and if so, what does that mean? (Are they run in the same context or different contexts?)</div><div class=""> 2) Do we allow multiple inputs to be passed to a single frontend action, and if so, what does that mean? (Are they run in the same context or different contexts?)</div><div class=""><br class=""></div><div class="">Right now, the answer to both 1 and 2 is yes, and they're run in different contexts (we tear down and recreate most of the compiler instance each time). I think that's probably a mistake, particularly for (2): the one frontend action that has a reason to support multiple input files is GenerateHeaderModuleAction, and it has to work around the default behavior of the CompilerInstance so that it can consume all the inputs in the same action invocation. I'd also like to eventually add a "final build" mode to clang, to parse multiple source files into the same AST and generate a single IR module from them (but with them properly isolated from each other by module visibility, to avoid the usual problems with that mode), and again that suggests that multiple input files to an action should be processed as part of the same AST / Sema / CodeGen context.</div><div class=""><br class=""></div><div class="">Maybe the answer to 1 should be no. In which case we might find that we end up wanting just</div><div class=""><br class=""></div><div class=""> runAction(CompilerInstanceBuilder, FrontendAction)</div><div class=""><br class=""></div><div class="">and no exposed CompilerInstance object at all.</div></div></div></div></blockquote></div></div></blockquote><div><br class=""></div><div>Interesting, that would be pretty clean.</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">## How should it be staged? ##<br class=""><br class="">Assuming this directly looks good, I'm not sure how to stage this. Should I create a monolithic patch and get it reviewed as a whole, or post a patch for a skeletal builder and then patches for adding arguments to it incrementally?<br class=""><br class="">The latter is the usual approach, and is what I've been following locally. I have two patches:<br class=""><br class="">- Add a builder that handles the current constructor arguments: PCHContainerOps and InMemoryModuleCache.<br class="">- Add a CompilerInvocation and DiagnosticsEngine to the builder and remove createDiagnostics, setDiagnostics, and setInvocation. The construction of these two is quite intertwined so there didn't seem to be a clean way to do this separately.<br class=""><br class="">I'm hesitating slightly since I'll be touching roughly the same lines of code in every incremental patch, but I'm still inclined to go that way so that incremental progress doesn't bitrot.<br class=""></blockquote><div class=""><br class=""></div><div class="">The incremental path sounds preferable to me. I think you're far enough along that we can imagine what a further-progressed result would look like without having a complete patch series. Touching the same lines repeatedly seems fine :) </div></div></div></div></blockquote></div><br class=""><div class="">Thanks for the feedback! Things have probably bitrotted since March, but I'll try to get back to this soon, rebase, and start sending patches.</div></div></blockquote><br class=""></div><div>It didn't bitrot too much and I found some time this afternoon to rebase. Sent:</div><div><a href="https://reviews.llvm.org/D62457" class="">https://reviews.llvm.org/D62457</a></div><div><a href="https://reviews.llvm.org/D62458" class="">https://reviews.llvm.org/D62458</a></div></body></html>