<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 29 November 2017 at 14:34, David Blaikie via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><div class="gmail-h5"><div dir="ltr">On Tue, Nov 21, 2017 at 5:06 PM Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> wrote:<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 dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 20 November 2017 at 15:04, David Blaikie via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Richard,<br><br>(Lang, you're here because I mentioned stumbling across this on Friday in ORC - this is the reduced test case (where 't' is the NameMutex member and 'nt' is the Name member))<br><br>Working on getting all LLVM binaries linking successfully under modular codegen, I've hit something that seems it'll need a bit more feature work (which I'm happy/planning to do myself - though always happy for help/advice/etc)... <br><br>The test case I have boils down to the following modular header:<br><font face="monospace"><br></font><div><font face="monospace">  struct trivial {};</font></div><div><font face="monospace">  struct nontrivial { nontrivial(); };</font></div><div><font face="monospace">  // namespace foo {</font></div><div><font face="monospace">  void sink(void *);</font></div><div><font face="monospace">  template <typename T> struct bar {</font></div><div><font face="monospace">    static void baz() {</font></div><div><font face="monospace">      sink(&t);</font></div><div><font face="monospace">      sink(&nt);</font></div><div><font face="monospace">    }</font></div><div><font face="monospace">    static trivial t;<br></font><div><font face="monospace">    static nontrivial nt;</font></div><div><font face="monospace">  };</font></div><div><font face="monospace">  template <typename T> trivial bar<T>::t;</font></div><div><font face="monospace">  template <typename T> nontrivial bar<T>::nt;</font></div><div><font face="monospace">  //} // namespace foo</font></div><div><font face="monospace">  template struct bar<int>;</font></div><div><font face="monospace">  // inline void use() { (void)bar<int>::baz(); }</font><br><br>To build with modular codegen:<br><br><font face="monospace">  $ echo 'module foo { header "foo.h" }' > foo.cppmap<br>  $ clang++ -cc1 -xc++ -emit-module -fmodules -w -fmodule-name=foo foo.cppmap -o foo.pcm -fmodules-codegen <br></font><br>So here are some interesting facts I know, some of which may be relevant, some of which may not:<br><ol><li> Code as written ends up with <font face="monospace" color="#660000">linkonce_odr</font> definitions for <font face="monospace" color="#660000">t</font> and <font face="monospace" color="#660000">nt</font><br></li><li>Use <font face="monospace" color="#660000">use</font> instead of the explicit instantiation and are both <font face="monospace" color="#660000">t</font> and <font face="monospace" color="#660000">nt</font> are only declarations</li><li>Add the outer namespace <font face="monospace">foo</font> and then <font face="monospace" color="#660000">t</font> is emitted as a <font face="monospace" color="#660000">linkonce_odr</font> definition and <font face="monospace" color="#660000">nt</font> is emitted as a declaration</li></ol>That last one (which was the first result I got) really confuses me - any ideas why a namespace would change the behavior here?<br></div></div></div></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>My first guess would be that something has registered a ASTConsumer::<wbr>HandleTopLevelDecl callback or similar, and they're assuming that it gets called for every namespace-scope declaration.</div></div></div></div></blockquote></div></div><div><br>Do you reckon that's worth looking into that? Or just some unimportant oddity?<br></div></div></div></blockquote><div><br></div><div>It would be useful to understand what's going on here, as I'd guess it has impact in other cases too. But it's not something I'd prioritize.</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 dir="ltr"><div class="gmail_quote"><div></div><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><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 dir="ltr"><div><div>In any case, all those mysteries/differences in behavior might be aside to actually fixing the behavior here, which is what this email is really about.<br><br>This is basically the same problem as inline variables, and maybe even would allow some support for static variables in headers too (not sure, will see).<br><br>Any ideas what the behavior should be here? Since there's a desire not to run all global initializers if their specific submodule header isn't included in the program (for iostream's sake), how would this be done correctly under modular codegen?<br>My initial thought is potentially to defer the global initializers to the includers (that seems necessary to get the lazy/only-those-included behavior, right?) But that may not account for indirect inclusion? I guess that's already handled somehow for the iostreams non-modular case, so maybe it works.<br></div></div></div></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>The explicit instantiation definition case is not especially interesting, because by [temp.spec]/5.1, such things should never appear in modular headers (because the header could only ever be used in one translation unit).<br></div></div></div></div></blockquote></span><div><br>Ah, good point.<br> </div><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div></div><div>So let's focus in on the "inline void use()" case. We need some kind of mental model for what modular codegen means in order to figure out what should happen. The way I'm thinking about modular codegen is roughly:<br></div><div><br></div><div>For each header for which we perform modular codegen, we act as if</div><div> * that header is a separate translation unit in the program (in *addition* to being included into other places), and</div><div> * for that translation unit, we happen to emit definitions of inline functions and class metadata, even if they're not otherwise used, and</div><div> * in other translation units, we don't need to emit those symbols as a consequence.</div></div></div></div></blockquote></span><div><br>One minor difference here is that the whole module is a translation unit, when it comes to the build details (which are somewhat relevant) - or at least a single object file. Which means used/unused behavior of linkers work at the module granularity, not  the header. (eg: if you use a function from one header module object, you'll pull in all the functions from the module, not just the one header (and all its initializers... ))<br> </div><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Under that model, emitting the definition of use() should cause us to emit linkonce_odr definitions of both t and nt into the modular codegen translation unit. But it should not suppress the emission of linkonce_odr definitions of t and nt in other translation units too.<br></div><div><br></div><div>However, we also want to *not* run initializers for modules that are not actually used (eg, we don't want linking against the standard library to run the iostreams initializer -- and thus link in the iostreams library -- if it's not used, such as for a freestanding / embedded compilation). For modular codegen, this presumably also needs function sections, and section GC enabled in the linker.</div></div></div></div></blockquote><div><br></div></span><div>Hmm, not sure I followed this last bit. Which part /needs/ functions sections & GC sections?</div></div></div></blockquote><div><br></div><div>I... am not sure exactly what case I had in mind there. But I don't think this impacts the direction we ended up with in our lunchtime discussion.</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 dir="ltr"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><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 dir="ltr"><div><div>& then the modular object file would perhaps have the weak_odr definition of the global variable itself, but no global initializer - depending on any live codepaths that reference the global necessarily requiring the using TU to have caused the initializer to run? That seems vaguely concerning... <br></div></div></div></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>It does. Mostly I think it works out: if another TU is relying on an inline function definition to be provided by a modular codegen object, they must have run the notional initializer for that module, which in turn would have initialized those globals.</div><div><br></div><div>There's one case I'm concerned by: suppose the module is never actually imported, and all the TUs actually include the header textually. Now, suppose the inline function and global variables from the modular codegen TU are selected at link time, and the other copies are all discarded, and we cleverly put the per-variable global initializers in a COMDAT with the variables, so they get discarded too. Now we're left with a reference to an uninitialized global.</div><div><br></div><div>Perhaps we need to make a distinction between internal linkage globals with dynamic initialization in headers (eg the iostreams initializer), which we run in every user of the modular codegen header, and external linkage globals with dynamic initialization in headers (eg, inline variables, static data members of class templates, ...) which we run as part of initializing the modular codegen translation unit itself. If we do make that distinction, I worry that we'll lose some of the initialization order guarantees, though.</div></div></div></div></blockquote></span><div><br>Right, so we (Richard & I) talked about this over lunch & summarizing our (mostly Richard's) thoughts on this to the mailing list for posterity:<br><br>The initialization order guarantees are that, if I recall correctly - a "happens after" thing. If there's inline variable A and B, B happens after A at least. Now some other inline variable X might happen before B but after A (if A and X appear in some other translation unit) but importantly B can't happen before A.<br></div></div></div></blockquote><div><br></div><div>I think the argument was this: Every global variable has either unordered initialization (in which case we don't need to do anything special), or ordered initialization (in which case we are required to initialize all such variables in declaration order), or partially-ordered initialization (whose order relative to other ordered or partially ordered initialization must be at least somewhat preserved: specifically, if an inline variable A is initialized before another variable B in every TU where both appear, then A's initializer happens before B's: <a href="http://eel.is/c++draft/basic.start.dynamic#2.2">http://eel.is/c++draft/basic.start.dynamic#2.2</a>). In a modular header, there can be no variables with ordered initialization and external linkage, because that would be an ODR violation, as variables with ordered initialization have strong linkage. So the only remaining cases are partially-ordered initialization (inline variables) and internal linkage globals.</div><div><br></div><div>For a modular header that appears in at least two distinct translation units (which happens for any modular header that is imported), the relevant ordering guarantees then reduce to: (1) internal linkage globals must be initialized in declaration order, (2) inline variables must be initialized in declaration order, and (3) the initialization of any inline variable must happen before the initialization of any later-declared internal linkage global. In particular, this permits us to either run the initializers in declaration order, or to run all of the inline variable initializers first followed by all of the internal-linkage variable initializers.</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 dir="ltr"><div class="gmail_quote"><div>General goal/premise/proposed solution:<br>Treat modular codegen objects the same as a translation unit that includes the headers, /except/ for the internal linkage globals (eg: iostreams init).<br><br>This preserves ordering - it's just as correct (except for the internal linkage globals) as if you had a separate source file that included all the modular headers & weren't using modules to compile anything.<br><br>The only place it breaks down is the case where your modular codegen non-internal globals (class template static members, variable templates, inline variables, etc) use or depend on the internal linkage global initializer (eg: an inline variable with an initializer that prints to a stream, etc). We're just going to accept this as broken, I think... <br><br>Is that right, Richard? I feel like I left this too long and didn't remember/include quite all the nuance about ordering, etc. Feel free to add things.<br></div></div></div></blockquote><div><br></div><div>I think it's actually not even broken. :) That is, given two translation units both containing this:</div><div><br></div><div>#include <iostream> // internal linkage variable initializes iostreams<br></div><div>inline auto &foo = std::cout << "hello world";</div><div><br></div><div>... there is no guarantee the iostream initializer runs before the 'foo' variable's initializer. (The partial ordering rule doesn't apply.)</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 dir="ltr"><div class="gmail_quote"><div>Practically speaking, currently all the globals aren't quite handled in modular codegen because a lot of this work is triggered specifically by the presence/handling of a module import. So I'll need to refactor and reuse that code, that should make all the globals be handled - then specifically opt-out/skip over the internal linkage variables to get back to the right behavior there.<br><br>- Dave<br> </div><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><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"></blockquote></div></div></div><div dir="ltr"><div class="gmail_extra"><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 dir="ltr"><div><div>Is this making sense? Any good ideas? Pointers to where to start, etc?<br><br>Thanks,<br>- Dave<br><br></div></div></div>
<br></blockquote></div></div></div><div dir="ltr"><div class="gmail_extra"><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">______________________________<wbr>_________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>
</blockquote></span></div></div>
<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>