<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 24 February 2017 at 14:25, 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">OK, sounds fair - so GetExternalDeclStmt needs at least a Deserializing object to defer deserialization further. Does it also need a SavedStreamPosition, do you think? I can't immediately think of how/why it might, but they seemed to be paired in other places.</div></blockquote><div><br></div><div>I think this is an accurate summary:</div><div> * You need a Deserializing object if you're going to deserialize (or more accurately, temporarily violate invariants, use global cursors, ...) and your caller might not be deserializing</div><div> * You need a SavedStreamPosition object if you're going to use a global cursor and your caller might be deserializing</div><div>... so you'd see a pair of them whenever someone is using a global cursor and doesn't know whether their caller is deserializing.</div><div><br></div><div>I think GetExternalDeclStmt doesn't need a SavedStreamPosition, but should assert that NumCurrentElementsDeserializing == 0 before it creates its Deserializing object.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><div class="gmail_quote"><div dir="ltr">On Fri, Feb 24, 2017 at 2:22 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="m_6945530598186804072gmail_msg"><div class="gmail_extra m_6945530598186804072gmail_msg"><div class="gmail_quote m_6945530598186804072gmail_msg">On 24 February 2017 at 09:41, David Blaikie <span dir="ltr" class="m_6945530598186804072gmail_msg"><<a href="mailto:dblaikie@gmail.com" class="m_6945530598186804072gmail_msg" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br class="m_6945530598186804072gmail_msg"><blockquote class="gmail_quote m_6945530598186804072gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="m_6945530598186804072gmail_msg"><div dir="ltr" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><div dir="ltr" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">I've spent a few days debugging/trying to understand the following, so I'm writing it all down in the hopes of getting it straight/clear and potentially getting some external perspective on what's going on, whether I've understood it correctly, and what might be a good way to solve it.<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">Starting with this test case:<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">foo.h:<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"></font><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  struct foo {};</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  inline void e() { foo(); }<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">bar.h:<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"></font><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  #include "foo.h"</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  template <typename T></font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  foo bar(foo &f) {</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">    return f;</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  }</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  void z() { (void)&bar<int>; }<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"></font><br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">Build each into a separate module, run modular codegen on bar.pcm:<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><div style="font-family:monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  clang-tot -cc1 -fmodules-codegen -xc++ -emit-module -fmodules \<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">    -fmodule-name=foo foo.cppmap -o foo.pcm</div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  clang-tot -cc1 -fmodules-codegen -xc++ -emit-module -fmodules \<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">    -fmodule-name=bar bar.cppmap -o bar.pcm -fmodule-file=foo.pcm</div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  clang-tot -c bar.pcm -o bar.o<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"></div></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">(I haven't fully understood why the use of 'foo' in foo.h is necessary, nor why 'bar' needs to be a template - those might provide some further insight about how this should/could work)</div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">This patch makes the failure a bit more visible/immediate:<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  diff --git lib/Serialization/<wbr>ASTReaderStmt.cpp lib/Serialization/<wbr>ASTReaderStmt.cpp</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  index b4718367d4..1fb48e9560 100644</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  @@ -3903,7 +3904,9 @@ Stmt *ASTReader::<wbr>ReadStmtFromStream(ModuleFile &F) {</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">       ++NumStatementsRead;</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"> </font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">       if (S && !IsStmtReference) {</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  +      auto X = Cursor.GetCurrentBitNo();</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">         Reader.Visit(S);</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">  +      assert(X == Cursor.GetCurrentBitNo() && "Narf");</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">         StmtEntries[Cursor.<wbr>GetCurrentBitNo()] = S;</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><font face="monospace" class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">       }</font></div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"> </div><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">This is layered on top of the modular codegen rewrite/refactor (to use a bit on function definitions, instead of to imply modular codegen from the Module object) and some other fixes. I'll include the full patch I'm working with.</div><br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">The sequence of relevant steps (as best as I can figure)<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><ol class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">bar() body deserialization begins</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">bar module's DeclCursor is used, jumping to the start of the bar()'s definition</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">clang::ASTReader::<wbr>ReadStmtFromStream iterates through Stmts in bar()<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">no SavedStreamPosition nor Deserializing object here</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">deserializing the EXPR_CXX_CONSTRUCT gets interesting:</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">eventually involves deserializing foo(const foo&)</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">foo module's DeclCursor is used</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">foo module's DeclCursor is saved/preserved with a SavedStreamPosition</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">a ExternalASTSource::<wbr>Deserializing context is started</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">the definition of foo(const foo&) (in the bar module) is deserialized</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">this definition is 'interesting' and added to the ASTReader's InterestingDecls</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">The end of the Deserializing context (8) is reached</li><ol class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">In the non-modular-codegen case, DeclMustBeEmitted is not true for foo(const foo&) and it is shelved for lazy emission, end of story</li></ol><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">In the modular-codegen case, foo(const foo&) in the bar module must be emitted (all inline functions (implicitly or explicitly) defined in a module are emitted weak, etc)<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"></li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">So foo(const foo&) is non-lazily deserialized and emitted</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">The bar module's DeclCursor is used for this, unprotected</li><li class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">execution eventually gets back to the deserialization of bar() - and the DeclCursor it's using is out of position -> badness.</li></ol><div class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">Maybe there's a better way to provide this timeline, I'm not sure - hopefully it makes sense.<br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg"><br class="m_6945530598186804072m_4127207265168833327m_833859486294641431gmail_msg m_6945530598186804072gmail_msg">Essentially the way ASTReader::ReadDeclRecord (started in (5) that contains the SavedStreamPosition and Deserializing context) seems to assume that the only things that will be non-lazily deserialized will come from the same module. Modular codegen breaks that invariant at the moment (well, with the patch provided).<br class="m_6945530598186804072gmail_msg"><br class="m_6945530598186804072gmail_msg">I did try SavedStreamPosition-protecting the call from 3->4 (specifically the "Visit(Expr)" call in ReadStmtFromStream) though still hit some crashes. Maybe that's the right path to go down still, but need to do more?<br class="m_6945530598186804072gmail_msg"><br class="m_6945530598186804072gmail_msg">When I tried to copy the DeclCursor in ReadStmtFromStream that actually broke things pretty significantly (compile errors on valid code even without modular codegen enabled). But I don't know much about the cursors - evidently more than only an efficiency concern, I guess the non-offset state in the copy of the cursor changes and so the original DeclCursor isn't updated, etc. (I wonder about splitting these cursors into a shared state + offset, share the state with shared_ptr and make it cheap to copy the actual offset state around so there's less reason to risk these sharing situations & all the SavedStreamPosition protection that requires).<br class="m_6945530598186804072gmail_msg"><br class="m_6945530598186804072gmail_msg">Long & short of it: What should I do here? What're the likely goals I should be trying to move the code towards, if any?</div></div></div></div></div></div></div></blockquote><div class="m_6945530598186804072gmail_msg"><br class="m_6945530598186804072gmail_msg"></div></div></div></div><div dir="ltr" class="m_6945530598186804072gmail_msg"><div class="gmail_extra m_6945530598186804072gmail_msg"><div class="gmail_quote m_6945530598186804072gmail_msg"><div class="m_6945530598186804072gmail_msg">We should have at least one instance of Deserializing extant during all deserialization. It looks like one is missing from GetExternalDeclStmt.</div><div class="m_6945530598186804072gmail_msg"><br class="m_6945530598186804072gmail_msg"></div><div class="m_6945530598186804072gmail_msg">I think it's correct that ReadStmtFromStream does not try to maintain the stream position: it's intending to be called with the DeclsCursor pointing at the Stmt to read, and intends to leave the stream pointing to the record after that point. The external caller (GetExternalDeclStmt) jumps to the correct bit location (as do all other users of the DeclsCursor) before calling it, and does not expect to ever be called reentrantly. But a reentrant call to GetExternalDeclStmt is exactly what's happening in your case, because it also doesn't /defend/ against reentrancy from end-of-deserialization actions through a Deserializing object.</div></div></div></div>
</blockquote></div>
</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>