<div dir="ltr">There was no cycle for this crash. 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.<div><br></div><div>void foo(int a, int b) {}</div><div>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.</div><div><br></div><div>class S {</div><div> void bar(S* s);</div><div>};</div><div>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.</div><div><br></div><div>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.</div></div><div dir="ltr"><div dir="ltr"><br></div><div dir="ltr"><div><br></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>Sat, May 4, 2019 at 9:06 AM<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">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>
</div>