# Weird dyn\_cast code gen / performance problem

**URL:** <https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343>\
**Category:** Compiler\
**Created:** [January 1, 2018, 4:30pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343 "2018-01-01T16:30:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Zarzycki](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_Zarzycki](https://forums.swift.org/u/David_Zarzycki)\
**Post date:** [January 1, 2018, 4:30pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/1 "2018-01-01T16:30:31Z")

</div>

Hello,

I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.

When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:

\<+35\>: movb 0x8(%rbx), %cl ; getKind()  
\<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
\<+41\>: je 0x1377df6 ; \<+54\>  
\<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
\<+46\>: jne 0x1377df6 ; \<+54\>  
\<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
\<+52\>: jmp 0x1377e06 ; \<+70\>  
\<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
\<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
\<+59\>: je 0x1377e09 ; \<+73\>  
\<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
\<+64\>: jne 0x1377e09 ; \<+73\>  
\<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
\<+70\>: movq (%rbx), %rax ; load the decl pointer  
\<+73\>: popq %rbx  
\<+74\>: retq

I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.

FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).

Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?

Dave

---

<div class="post-metadata">

**Author:** ![Michael\_Gottesman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_gottesman/32/280_2.png) [@Michael\_Gottesman](https://forums.swift.org/u/Michael_Gottesman)\
**Post date:** [January 1, 2018, 5:53pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/2 "2018-01-01T17:53:36Z")

</div>

Do you have the llvm-ir handy?

> **···**
>
> > On Jan 1, 2018, at 11:30 AM, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> > 
> > Hello,
> > 
> > I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.
> > 
> > When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:
> > 
> > \<+35\>: movb 0x8(%rbx), %cl ; getKind()  
> > \<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
> > \<+41\>: je 0x1377df6 ; \<+54\>  
> > \<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
> > \<+46\>: jne 0x1377df6 ; \<+54\>  
> > \<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
> > \<+52\>: jmp 0x1377e06 ; \<+70\>  
> > \<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
> > \<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
> > \<+59\>: je 0x1377e09 ; \<+73\>  
> > \<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
> > \<+64\>: jne 0x1377e09 ; \<+73\>  
> > \<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
> > \<+70\>: movq (%rbx), %rax ; load the decl pointer  
> > \<+73\>: popq %rbx  
> > \<+74\>: retq
> > 
> > I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.
> > 
> > FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).
> > 
> > Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?
> > 
> > Dave  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-dev mailing list  
> > swift-dev@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)

---

<div class="post-metadata">

**Author:** ![David\_Zarzycki](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_Zarzycki](https://forums.swift.org/u/David_Zarzycki)\
**Post date:** [January 1, 2018, 6:10pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/3 "2018-01-01T18:10:00Z")

</div>

I don’t have the IR handy. You can easily generate it for yourself though. Just drop the following into any file (I use swift/lib/AST/Type.cpp) and recompile swift.

Decl \*my\_test\_function(Type t) {  
&nbsp;&nbsp;return t-\>getClassOrBoundGenericClass();  
}

> **···**
>
> > On Jan 1, 2018, at 12:53, Michael Gottesman \<mgottesman@apple.com\> wrote:
> > 
> > Do you have the llvm-ir handy?
> > 
> > > On Jan 1, 2018, at 11:30 AM, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> > > 
> > > Hello,
> > > 
> > > I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.
> > > 
> > > When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:
> > > 
> > > \<+35\>: movb 0x8(%rbx), %cl ; getKind()  
> > > \<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
> > > \<+41\>: je 0x1377df6 ; \<+54\>  
> > > \<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
> > > \<+46\>: jne 0x1377df6 ; \<+54\>  
> > > \<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
> > > \<+52\>: jmp 0x1377e06 ; \<+70\>  
> > > \<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
> > > \<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
> > > \<+59\>: je 0x1377e09 ; \<+73\>  
> > > \<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
> > > \<+64\>: jne 0x1377e09 ; \<+73\>  
> > > \<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
> > > \<+70\>: movq (%rbx), %rax ; load the decl pointer  
> > > \<+73\>: popq %rbx  
> > > \<+74\>: retq
> > > 
> > > I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.
> > > 
> > > FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).
> > > 
> > > Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?
> > > 
> > > Dave  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-dev mailing list  
> > > swift-dev@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)

---

<div class="post-metadata">

**Author:** ![David\_Zarzycki](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_Zarzycki](https://forums.swift.org/u/David_Zarzycki)\
**Post date:** [January 1, 2018, 10:50pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/4 "2018-01-01T22:50:56Z")

</div>

Hi Michael,

I reduced it down to a simple test case. I was wrong about this requiring two or more dyn\_casts. This actually affects any C++ code that uses the “if (auto x = y(z))” convention. What follows is the reduction (compiled with “clang++ -O3 -c” if it matters):

// Uncomment the next line to see the expected code gen (albeit not inlined)  
//\_\_attribute\_\_((used,noinline))  
int \*x(void \*arg) {  
&nbsp;&nbsp;return ((long long)arg & 1) ? (int \*)arg : nullptr;  
}

int test(void \*arg) {  
&nbsp;&nbsp;if (auto y = x(arg))  
&nbsp;&nbsp;&nbsp;&nbsp;return \*y;  
&nbsp;&nbsp;return 42;  
}

It seems like inlining ‘x’ causes the compiler to effectively generate the following pseudo-code:

int test(void \*arg) {  
&nbsp;&nbsp;if (arg != nullptr)  
&nbsp;&nbsp;&nbsp;&nbsp;if (arg & 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \*arg;  
&nbsp;&nbsp;return 42;  
}

Which is surprising in multiple ways and (as far as I can tell) difficult to workaround without lots of source churn.

Where should I file a bug?

Dave

> **···**
>
> > On Jan 1, 2018, at 13:10, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> > 
> > I don’t have the IR handy. You can easily generate it for yourself though. Just drop the following into any file (I use swift/lib/AST/Type.cpp) and recompile swift.
> > 
> > Decl \*my\_test\_function(Type t) {  
> > &nbsp;&nbsp;return t-\>getClassOrBoundGenericClass();  
> > }
> > 
> > > On Jan 1, 2018, at 12:53, Michael Gottesman \<mgottesman@apple.com \<mailto:mgottesman@apple.com\>\> wrote:
> > > 
> > > Do you have the llvm-ir handy?
> > > 
> > > > On Jan 1, 2018, at 11:30 AM, David Zarzycki via swift-dev \<swift-dev@swift.org \<mailto:swift-dev@swift.org\>\> wrote:
> > > > 
> > > > Hello,
> > > > 
> > > > I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.
> > > > 
> > > > When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:
> > > > 
> > > > \<+35\>: movb 0x8(%rbx), %cl ; getKind()  
> > > > \<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
> > > > \<+41\>: je 0x1377df6 ; \<+54\>  
> > > > \<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
> > > > \<+46\>: jne 0x1377df6 ; \<+54\>  
> > > > \<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
> > > > \<+52\>: jmp 0x1377e06 ; \<+70\>  
> > > > \<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
> > > > \<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
> > > > \<+59\>: je 0x1377e09 ; \<+73\>  
> > > > \<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
> > > > \<+64\>: jne 0x1377e09 ; \<+73\>  
> > > > \<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
> > > > \<+70\>: movq (%rbx), %rax ; load the decl pointer  
> > > > \<+73\>: popq %rbx  
> > > > \<+74\>: retq
> > > > 
> > > > I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.
> > > > 
> > > > FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).
> > > > 
> > > > Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?
> > > > 
> > > > Dave  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-dev mailing list  
> > > > swift-dev@swift.org \<mailto:swift-dev@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-dev mailing list  
> > swift-dev@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [January 1, 2018, 11:20pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/5 "2018-01-01T23:20:45Z")

</div>

> Hi Michael,
> 
> I reduced it down to a simple test case. I was wrong about this requiring two or more dyn\_casts. This actually affects any C++ code that uses the “if (auto x = y(z))” convention. What follows is the reduction (compiled with “clang++ -O3 -c” if it matters):
> 
> // Uncomment the next line to see the expected code gen (albeit not inlined)  
> //\_\_attribute\_\_((used,noinline))  
> int \*x(void \*arg) {  
> &nbsp;&nbsp;return ((long long)arg & 1) ? (int \*)arg : nullptr;  
> }
> 
> int test(void \*arg) {  
> &nbsp;&nbsp;if (auto y = x(arg))  
> &nbsp;&nbsp;&nbsp;&nbsp;return \*y;  
> &nbsp;&nbsp;return 42;  
> }
> 
> It seems like inlining ‘x’ causes the compiler to effectively generate the following pseudo-code:
> 
> int test(void \*arg) {  
> &nbsp;&nbsp;if (arg != nullptr)  
> &nbsp;&nbsp;&nbsp;&nbsp;if (arg & 1)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \*arg;  
> &nbsp;&nbsp;return 42;  
> }
> 
> Which is surprising in multiple ways and (as far as I can tell) difficult to workaround without lots of source churn.
> 
> Where should I file a bug?

bugs.llvm.org \<[http://bugs.llvm.org/&gt](http://bugs.llvm.org/&gt); would be best. Including both your reduced test case and the fact that it was reduced from dyn\_cast patterns should make them sit up and take notice.

John.

> **···**
>
> > On Jan 1, 2018, at 5:51 PM, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> 
> > Dave
> > 
> > > On Jan 1, 2018, at 13:10, David Zarzycki via swift-dev \<swift-dev@swift.org \<mailto:swift-dev@swift.org\>\> wrote:
> > > 
> > > I don’t have the IR handy. You can easily generate it for yourself though. Just drop the following into any file (I use swift/lib/AST/Type.cpp) and recompile swift.
> > > 
> > > Decl \*my\_test\_function(Type t) {  
> > > &nbsp;&nbsp;return t-\>getClassOrBoundGenericClass();  
> > > }
> > > 
> > > > On Jan 1, 2018, at 12:53, Michael Gottesman \<mgottesman@apple.com \<mailto:mgottesman@apple.com\>\> wrote:
> > > > 
> > > > Do you have the llvm-ir handy?
> > > > 
> > > > > On Jan 1, 2018, at 11:30 AM, David Zarzycki via swift-dev \<swift-dev@swift.org \<mailto:swift-dev@swift.org\>\> wrote:
> > > > > 
> > > > > Hello,
> > > > > 
> > > > > I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.
> > > > > 
> > > > > When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:
> > > > > 
> > > > > \<+35\>: movb 0x8(%rbx), %cl ; getKind()  
> > > > > \<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
> > > > > \<+41\>: je 0x1377df6 ; \<+54\>  
> > > > > \<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
> > > > > \<+46\>: jne 0x1377df6 ; \<+54\>  
> > > > > \<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
> > > > > \<+52\>: jmp 0x1377e06 ; \<+70\>  
> > > > > \<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
> > > > > \<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
> > > > > \<+59\>: je 0x1377e09 ; \<+73\>  
> > > > > \<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
> > > > > \<+64\>: jne 0x1377e09 ; \<+73\>  
> > > > > \<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
> > > > > \<+70\>: movq (%rbx), %rax ; load the decl pointer  
> > > > > \<+73\>: popq %rbx  
> > > > > \<+74\>: retq
> > > > > 
> > > > > I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.
> > > > > 
> > > > > FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).
> > > > > 
> > > > > Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?
> > > > > 
> > > > > Dave  
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-dev mailing list  
> > > > > swift-dev@swift.org \<mailto:swift-dev@swift.org\>  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-dev mailing list  
> > > swift-dev@swift.org \<mailto:swift-dev@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-dev mailing list  
> > swift-dev@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)

---

<div class="post-metadata">

**Author:** ![David\_Zarzycki](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_Zarzycki](https://forums.swift.org/u/David_Zarzycki)\
**Post date:** [January 1, 2018, 11:37pm UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/6 "2018-01-01T23:37:26Z")

</div>

> > Hi Michael,
> > 
> > I reduced it down to a simple test case. I was wrong about this requiring two or more dyn\_casts. This actually affects any C++ code that uses the “if (auto x = y(z))” convention. What follows is the reduction (compiled with “clang++ -O3 -c” if it matters):
> > 
> > // Uncomment the next line to see the expected code gen (albeit not inlined)  
> > //\_\_attribute\_\_((used,noinline))  
> > int \*x(void \*arg) {  
> > &nbsp;&nbsp;return ((long long)arg & 1) ? (int \*)arg : nullptr;  
> > }
> > 
> > int test(void \*arg) {  
> > &nbsp;&nbsp;if (auto y = x(arg))  
> > &nbsp;&nbsp;&nbsp;&nbsp;return \*y;  
> > &nbsp;&nbsp;return 42;  
> > }
> > 
> > It seems like inlining ‘x’ causes the compiler to effectively generate the following pseudo-code:
> > 
> > int test(void \*arg) {  
> > &nbsp;&nbsp;if (arg != nullptr)  
> > &nbsp;&nbsp;&nbsp;&nbsp;if (arg & 1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \*arg;  
> > &nbsp;&nbsp;return 42;  
> > }
> > 
> > Which is surprising in multiple ways and (as far as I can tell) difficult to workaround without lots of source churn.
> > 
> > Where should I file a bug?
> 
> bugs.llvm.org \<[http://bugs.llvm.org/&gt](http://bugs.llvm.org/&gt); would be best. Including both your reduced test case and the fact that it was reduced from dyn\_cast patterns should make them sit up and take notice.

Thanks John,

In case anybody wants to cherry-pick the fix once it is found:  
&nbsp;&nbsp;[35790 – if (auto x = y(z)) generates bad/slow code (found via analysis of dyn\_cast)](https://bugs.llvm.org/show_bug.cgi?id=35790)

> **···**
>
> > On Jan 1, 2018, at 18:20, John McCall \<rjmccall@apple.com\> wrote:
> > 
> > > On Jan 1, 2018, at 5:51 PM, David Zarzycki via swift-dev \<swift-dev@swift.org \<mailto:swift-dev@swift.org\>\> wrote:
> 
> > John.
> > 
> > > Dave
> > > 
> > > > On Jan 1, 2018, at 13:10, David Zarzycki via swift-dev \<swift-dev@swift.org \<mailto:swift-dev@swift.org\>\> wrote:
> > > > 
> > > > I don’t have the IR handy. You can easily generate it for yourself though. Just drop the following into any file (I use swift/lib/AST/Type.cpp) and recompile swift.
> > > > 
> > > > Decl \*my\_test\_function(Type t) {  
> > > > &nbsp;&nbsp;return t-\>getClassOrBoundGenericClass();  
> > > > }
> > > > 
> > > > > On Jan 1, 2018, at 12:53, Michael Gottesman \<mgottesman@apple.com \<mailto:mgottesman@apple.com\>\> wrote:
> > > > > 
> > > > > Do you have the llvm-ir handy?
> > > > > 
> > > > > > On Jan 1, 2018, at 11:30 AM, David Zarzycki via swift-dev \<swift-dev@swift.org \<mailto:swift-dev@swift.org\>\> wrote:
> > > > > > 
> > > > > > Hello,
> > > > > > 
> > > > > > I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.
> > > > > > 
> > > > > > When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:
> > > > > > 
> > > > > > \<+35\>: movb 0x8(%rbx), %cl ; getKind()  
> > > > > > \<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
> > > > > > \<+41\>: je 0x1377df6 ; \<+54\>  
> > > > > > \<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
> > > > > > \<+46\>: jne 0x1377df6 ; \<+54\>  
> > > > > > \<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
> > > > > > \<+52\>: jmp 0x1377e06 ; \<+70\>  
> > > > > > \<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
> > > > > > \<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
> > > > > > \<+59\>: je 0x1377e09 ; \<+73\>  
> > > > > > \<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
> > > > > > \<+64\>: jne 0x1377e09 ; \<+73\>  
> > > > > > \<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
> > > > > > \<+70\>: movq (%rbx), %rax ; load the decl pointer  
> > > > > > \<+73\>: popq %rbx  
> > > > > > \<+74\>: retq
> > > > > > 
> > > > > > I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.
> > > > > > 
> > > > > > FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).
> > > > > > 
> > > > > > Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?
> > > > > > 
> > > > > > Dave  
> > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > swift-dev mailing list  
> > > > > > swift-dev@swift.org \<mailto:swift-dev@swift.org\>  
> > > > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-dev mailing list  
> > > > swift-dev@swift.org \<mailto:swift-dev@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-dev mailing list  
> > > swift-dev@swift.org \<mailto:swift-dev@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)

---

<div class="post-metadata">

**Author:** ![Michael\_Gottesman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_gottesman/32/280_2.png) [@Michael\_Gottesman](https://forums.swift.org/u/Michael_Gottesman)\
**Post date:** [January 2, 2018, 2:47am UTC](https://forums.swift.org/t/weird-dyn-cast-code-gen-performance-problem/7343/7 "2018-01-02T02:47:56Z")

</div>

I posted a quick comment there + the llvm ir. It seems like something that could be a simple SimplifyInstructions transformation.

Michael

> **···**
>
> > On Jan 1, 2018, at 6:37 PM, David Zarzycki \<dave@znu.io\> wrote:
> > 
> > > On Jan 1, 2018, at 18:20, John McCall \<rjmccall@apple.com\> wrote:
> > > 
> > > > On Jan 1, 2018, at 5:51 PM, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> > > > 
> > > > Hi Michael,
> > > > 
> > > > I reduced it down to a simple test case. I was wrong about this requiring two or more dyn\_casts. This actually affects any C++ code that uses the “if (auto x = y(z))” convention. What follows is the reduction (compiled with “clang++ -O3 -c” if it matters):
> > > > 
> > > > // Uncomment the next line to see the expected code gen (albeit not inlined)  
> > > > //\_\_attribute\_\_((used,noinline))  
> > > > int \*x(void \*arg) {  
> > > > &nbsp;&nbsp;return ((long long)arg & 1) ? (int \*)arg : nullptr;  
> > > > }
> > > > 
> > > > int test(void \*arg) {  
> > > > &nbsp;&nbsp;if (auto y = x(arg))  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;return \*y;  
> > > > &nbsp;&nbsp;return 42;  
> > > > }
> > > > 
> > > > It seems like inlining ‘x’ causes the compiler to effectively generate the following pseudo-code:
> > > > 
> > > > int test(void \*arg) {  
> > > > &nbsp;&nbsp;if (arg != nullptr)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;if (arg & 1)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \*arg;  
> > > > &nbsp;&nbsp;return 42;  
> > > > }
> > > > 
> > > > Which is surprising in multiple ways and (as far as I can tell) difficult to workaround without lots of source churn.
> > > > 
> > > > Where should I file a bug?
> > > 
> > > bugs.llvm.org would be best. Including both your reduced test case and the fact that it was reduced from dyn\_cast patterns should make them sit up and take notice.
> > 
> > Thanks John,
> > 
> > In case anybody wants to cherry-pick the fix once it is found:  
> > &nbsp;&nbsp;[35790 – if (auto x = y(z)) generates bad/slow code (found via analysis of dyn\_cast)](https://bugs.llvm.org/show_bug.cgi?id=35790)
> > 
> > > John.
> > > 
> > > > Dave
> > > > 
> > > > > On Jan 1, 2018, at 13:10, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> > > > > 
> > > > > I don’t have the IR handy. You can easily generate it for yourself though. Just drop the following into any file (I use swift/lib/AST/Type.cpp) and recompile swift.
> > > > > 
> > > > > Decl \*my\_test\_function(Type t) {  
> > > > > &nbsp;&nbsp;return t-\>getClassOrBoundGenericClass();  
> > > > > }
> > > > > 
> > > > > > On Jan 1, 2018, at 12:53, Michael Gottesman \<mgottesman@apple.com\> wrote:
> > > > > > 
> > > > > > Do you have the llvm-ir handy?
> > > > > > 
> > > > > > > On Jan 1, 2018, at 11:30 AM, David Zarzycki via swift-dev \<swift-dev@swift.org\> wrote:
> > > > > > > 
> > > > > > > Hello,
> > > > > > > 
> > > > > > > I noticed recently that the code gen of CanType::getClassOrBoundGenericClass() could be better and along the way I found a clang/LLVM bug. Where exactly, I do not know, although my bet is the LLVM optimizer.
> > > > > > > 
> > > > > > > When more than one dyn\_cast() happens in a row, LLVM/clang emits redundant and pointless nullptr checks. Both Apple clang-900.0.39.2 and clang/llvm top-of-tree generate essentially the same code:
> > > > > > > 
> > > > > > > \<+35\>: movb 0x8(%rbx), %cl ; getKind()  
> > > > > > > \<+38\>: testq %rbx, %rbx ; XXX - nullptr check after deref is pointless  
> > > > > > > \<+41\>: je 0x1377df6 ; \<+54\>  
> > > > > > > \<+43\>: cmpb $0x12, %cl ; isa\<ClassType\>()  
> > > > > > > \<+46\>: jne 0x1377df6 ; \<+54\>  
> > > > > > > \<+48\>: addq $0x10, %rbx ; (void\*)this + offsetof(ClassType, TheDecl)  
> > > > > > > \<+52\>: jmp 0x1377e06 ; \<+70\>  
> > > > > > > \<+54\>: xorl %eax, %eax ; the default return value (nullptr)  
> > > > > > > \<+56\>: testq %rbx, %rbx ; XXX - another pointless nullptr check?  
> > > > > > > \<+59\>: je 0x1377e09 ; \<+73\>  
> > > > > > > \<+61\>: cmpb $0x29, %cl ; isa\<BoundGenericClassType\>()  
> > > > > > > \<+64\>: jne 0x1377e09 ; \<+73\>  
> > > > > > > \<+66\>: addq $0x18, %rbx ; (void\*)this + offsetof(BoundGenericClassType, TheDecl)  
> > > > > > > \<+70\>: movq (%rbx), %rax ; load the decl pointer  
> > > > > > > \<+73\>: popq %rbx  
> > > > > > > \<+74\>: retq
> > > > > > > 
> > > > > > > I’ve tried adding different “nonnull” spellings in various parts of both Swift and LLVM’s casting machinery, but with no luck. The only thing that seems to work is to create a free function that takes a non-null “const TypeBase \*” parameter and then have CanType::getClassOrBoundGenericClass() call that.
> > > > > > > 
> > > > > > > FWIW – I \*suspect\* this is because LLVM’s casting machinery internally converts traditional pointers into C++ references before ultimately calling classof(&Val).
> > > > > > > 
> > > > > > > Before I file a bug against clang/llvm, might I be missing something? Can anybody think of a good workaround?
> > > > > > > 
> > > > > > > Dave  
> > > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > > swift-dev mailing list  
> > > > > > > swift-dev@swift.org  
> > > > > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-dev mailing list  
> > > > > swift-dev@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-dev mailing list  
> > > > swift-dev@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
