<div dir="ltr"><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr"><strong>From: </strong>David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span><br><strong>Date: </strong>Mon, May 6, 2019 at 4:39 PM<br><strong>To: </strong>Richard Trieu<br><strong>Cc: </strong>cfe-commits<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, May 6, 2019 at 4:24 PM Richard Trieu <<a href="mailto:rtrieu@google.com" target="_blank">rtrieu@google.com</a>> wrote:<br>
><br>
> There was no cycle for this crash.<br>
<br>
Oh, yeah, didn't mean to imply there were - but that a system designed<br>
to prevent cycles might also be used/help prevent redundant work like<br>
this.<br>
<br>
> What happened is that an exponential runtime is reduced to a linear runtime.  Without this revision, ODR hashing would have worked if the machine had enough memory and the user waited long enough.<br>
><br>
> void foo(int a, int b) {}<br>
> When computing the ODR hash for function foo, it will visit the type int twice, once per parameter.  In general, re-visiting types shouldn't be a problem, and in most cases, should be pretty fast.<br>
<br>
It does mean some potentially problematic worst-case situations where<br>
non-trivial types are mentioned more than once (eg: if, instead of<br>
int, it was a complex struct type - it wouldn't cycle, but it would do<br>
all that work twice (or many more times if it appears in more places<br>
in the entity being hashed)<br></blockquote><div><br></div><div>See below in the answer to DWARF.  ODRHash did have a system, it worked for a while until it didn't, and was since removed. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> class S {<br>
>   void bar(S* s);<br>
> };<br>
> There's actually two ways to visit the Decl behind S, ODR::AddCXXRecordDecl and ODR::AddDecl.  When computing the ODR hash of S, ODR::AddCXXRecordDecl is used for a deep dive into the AST of S.  When reaching S another way, (via FunctionDecl bar, parameter s, PointerType S*, RecordType S), then the CXXRecordDecl gets processed through ODR::AddDecl, which only processes enough information to identify S, but not any of its deeper details.  This allows self-reference without introducing cycles.<br>
<br>
Ah, OK - specifically to break the cycle.<br>
<br>
So the ODR hash of the function "void f(S*)" doesn't hash the<br>
implementation of S, (it uses AddDecl, not AddCXXRecordDecl)? But if<br>
it were "void f(S)" it would hash S? What about a member function that<br>
takes a parameter by value? ("struct S { void bar(S); }")<br></blockquote><div><br></div><div>The three functions AddCXXRecordDecl, AddFunctionDecl, and AddEnumDecl are the entry points from outside to use the ODRHash and nothing inside ODRHash will call these functions.  That means hashing "class S {};"  AddCXXRecordDecl is called with S.  Every other example, "void f(S)", "void bar(S);", etc will be called into AddDecl.  The next question is probably, how do you know if two functions "void f(S)" in two files refer to same class S?  The answer is, ODRHash doesn't know and doesn't care.  But when Clang imports both "void f(S)" functions, it will also import both S classes.  Since Clang checks, ODRHash doesn't need to.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> I think it would be possible to add some checks in debug mode to catch cycles.  I'm not sure it can detect redundant work as the function foo example above shows that visiting the same types over multiple times is expected.<br>
<br>
Both for efficiency and to avoid these cycles, it might be worthwhile<br>
to consider a different way to resolve this issue<br>
<br>
The reason these ideas come to my mind is that DWARF has a type hash<br>
that works in a different way to avoid cycles and redundant work.<br>
<br>
<a href="http://dwarfstd.org/doc/DWARF5.pdf" rel="noreferrer" target="_blank">http://dwarfstd.org/doc/DWARF5.pdf</a> - 7.32, Type Signature Computation.<br>
It works by assigning every type a number when it's first encountered<br>
(even before its contents are hashed), and if it's ever encountered<br>
again, hash the number again rather than going back into hashing the<br>
implementation.<br>
<br></blockquote><div>Originally, ODR hashing did have a system similar to what DWARF had.  Relevant portions of 7.32 are 1, 4.a, and 4.b.  Basically, maintain a list of Type's, when first visiting a Type, add it to the list and process it, and if the Type is ever seen again, use the index number instead reprocessing.  Worked well, and then the AST had a small change in it where now we needed two different Type's to hash to the same thing.  <a href="https://reviews.llvm.org/rL335853" target="_blank">https://reviews.llvm.org/rL335853</a> ripped this out.  It's possible to replace it, but it needs to be better than what we currently have.</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">
This way no type is hashed more than once, avoiding cycles and redundant work.<br>
<br>
><br>
><br>
><br>
> From: David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>><br>
> Date: Sat, May 4, 2019 at 9:06 AM<br>
> To: Richard Trieu<br>
> Cc: cfe-commits<br>
><br>
>> Does the ODR hashing have some sort of cycle breaking infrastructure -<br>
>> so that if the same type is seen more than once (eg: classes have<br>
>> members that have pointers back to the outer class type, etc) they<br>
>> don't cause indefinite cycles? Should that infrastructure have caught<br>
>> these cases & avoided the redundant work?<br>
>><br>
>> I'm curious to understand better how these things work/overlap/or don't.<br>
>><br>
>> On Fri, May 3, 2019 at 9:20 PM Richard Trieu via cfe-commits<br>
>> <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>> wrote:<br>
>> ><br>
>> > Author: rtrieu<br>
>> > Date: Fri May  3 21:22:33 2019<br>
>> > New Revision: 359960<br>
>> ><br>
>> > URL: <a href="http://llvm.org/viewvc/llvm-project?rev=359960&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=359960&view=rev</a><br>
>> > Log:<br>
>> > Reduce amount of work ODR hashing does.<br>
>> ><br>
>> > When a FunctionProtoType is in the original type in a DecayedType, the decayed<br>
>> > type is a PointerType which points back the original FunctionProtoType.  The<br>
>> > visitor for ODRHashing will attempt to process both Type's, doing double work.<br>
>> > By chaining together multiple DecayedType's and FunctionProtoType's, this would<br>
>> > result in 2^N Type's visited only N DecayedType's and N FunctionProtoType's<br>
>> > exsit.  Another bug where VisitDecayedType and VisitAdjustedType did<br>
>> > redundant work doubled the work at each level, giving 4^N Type's visited.  This<br>
>> > patch removed the double work and detects when a FunctionProtoType decays to<br>
>> > itself to only check the Type once.  This lowers the exponential runtime to<br>
>> > linear runtime.  Fixes <a href="https://bugs.llvm.org/show_bug.cgi?id=41625" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=41625</a><br>
>> ><br>
>> > Modified:<br>
>> >     cfe/trunk/lib/AST/ODRHash.cpp<br>
>> >     cfe/trunk/test/Modules/odr_hash.cpp<br>
>> ><br>
>> > Modified: cfe/trunk/lib/AST/ODRHash.cpp<br>
>> > URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ODRHash.cpp?rev=359960&r1=359959&r2=359960&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ODRHash.cpp?rev=359960&r1=359959&r2=359960&view=diff</a><br>
>> > ==============================================================================<br>
>> > --- cfe/trunk/lib/AST/ODRHash.cpp (original)<br>
>> > +++ cfe/trunk/lib/AST/ODRHash.cpp Fri May  3 21:22:33 2019<br>
>> > @@ -703,14 +703,36 @@ public:<br>
>> >    void VisitType(const Type *T) {}<br>
>> ><br>
>> >    void VisitAdjustedType(const AdjustedType *T) {<br>
>> > -    AddQualType(T->getOriginalType());<br>
>> > -    AddQualType(T->getAdjustedType());<br>
>> > +    QualType Original = T->getOriginalType();<br>
>> > +    QualType Adjusted = T->getAdjustedType();<br>
>> > +<br>
>> > +    // The original type and pointee type can be the same, as in the case of<br>
>> > +    // function pointers decaying to themselves.  Set a bool and only process<br>
>> > +    // the type once, to prevent doubling the work.<br>
>> > +    SplitQualType split = Adjusted.split();<br>
>> > +    if (auto Pointer = dyn_cast<PointerType>(split.Ty)) {<br>
>> > +      if (Pointer->getPointeeType() == Original) {<br>
>> > +        Hash.AddBoolean(true);<br>
>> > +        ID.AddInteger(split.Quals.getAsOpaqueValue());<br>
>> > +        AddQualType(Original);<br>
>> > +        VisitType(T);<br>
>> > +        return;<br>
>> > +      }<br>
>> > +    }<br>
>> > +<br>
>> > +    // The original type and pointee type are different, such as in the case<br>
>> > +    // of a array decaying to an element pointer.  Set a bool to false and<br>
>> > +    // process both types.<br>
>> > +    Hash.AddBoolean(false);<br>
>> > +    AddQualType(Original);<br>
>> > +    AddQualType(Adjusted);<br>
>> > +<br>
>> >      VisitType(T);<br>
>> >    }<br>
>> ><br>
>> >    void VisitDecayedType(const DecayedType *T) {<br>
>> > -    AddQualType(T->getDecayedType());<br>
>> > -    AddQualType(T->getPointeeType());<br>
>> > +    // getDecayedType and getPointeeType are derived from getAdjustedType<br>
>> > +    // and don't need to be separately processed.<br>
>> >      VisitAdjustedType(T);<br>
>> >    }<br>
>> ><br>
>> ><br>
>> > Modified: cfe/trunk/test/Modules/odr_hash.cpp<br>
>> > URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/odr_hash.cpp?rev=359960&r1=359959&r2=359960&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/odr_hash.cpp?rev=359960&r1=359959&r2=359960&view=diff</a><br>
>> > ==============================================================================<br>
>> > --- cfe/trunk/test/Modules/odr_hash.cpp (original)<br>
>> > +++ cfe/trunk/test/Modules/odr_hash.cpp Fri May  3 21:22:33 2019<br>
>> > @@ -4587,6 +4587,43 @@ int num = bar();<br>
>> >  #endif<br>
>> >  }<br>
>> ><br>
>> > +namespace FunctionProtoTypeDecay {<br>
>> > +#if defined(FIRST)<br>
>> > +struct S1 {<br>
>> > +  struct X {};<br>
>> > +  using Y = X(X());<br>
>> > +};<br>
>> > +#elif defined(SECOND)<br>
>> > +struct S1 {<br>
>> > +  struct X {};<br>
>> > +  using Y = X(X(X()));<br>
>> > +};<br>
>> > +#else<br>
>> > +S1 s1;<br>
>> > +// expected-error@first.h:* {{'FunctionProtoTypeDecay::S1::Y' from module 'FirstModule' is not present in definition of 'FunctionProtoTypeDecay::S1' in module 'SecondModule'}}<br>
>> > +// expected-note@second.h:* {{declaration of 'Y' does not match}}<br>
>> > +#endif<br>
>> > +<br>
>> > +#if defined(FIRST)<br>
>> > +struct S2 {<br>
>> > +  struct X {};<br>
>> > +  using Y =<br>
>> > +      X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(<br>
>> > +      X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(<br>
>> > +      X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(<br>
>> > +      X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(X(<br>
>> > +      ))))))))))))))))<br>
>> > +      ))))))))))))))))<br>
>> > +      ))))))))))))))))<br>
>> > +      ))))))))))))))));<br>
>> > +};<br>
>> > +#elif defined(SECOND)<br>
>> > +#else<br>
>> > +S2 s2;<br>
>> > +#endif<br>
>> > +<br>
>> > +}<br>
>> > +<br>
>> >  // Keep macros contained to one file.<br>
>> >  #ifdef FIRST<br>
>> >  #undef FIRST<br>
>> ><br>
>> ><br>
>> > _______________________________________________<br>
>> > cfe-commits mailing list<br>
>> > <a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
>> > <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div>