# Visual C++ 2015 Release build hangs forever, trying to compile itk::Math::Floor

**URL:** https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610
**Category:** Engineering
**Created:** [January 21, 2018, 9:54pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610 "2018-01-21T21:54:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 21, 2018, 9:54pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/1 "2018-01-21T21:54:24Z")

</div>

One of my colleagues encountered a Visual C++ 2015 compiler bug which causes the compiler to hang forever when it tries to instantiate BSplineInterpolationWeightFunction with TCoordRep = float and VSplineOrder = 1. Is it OK if I just add this template instantiation to [https://github.com/Kitware/ITK/blob/master/Modules/Core/Common/test/itkBSplineInterpolationWeightFunctionTest.cxx](https://github.com/Kitware/ITK/blob/master/Modules/Core/Common/test/itkBSplineInterpolationWeightFunctionTest.cxx) as a “WIP” commit? I would like to know if the issue can be reproduced by the Build Robot. As follows:

```
template class itk::BSplineInterpolationWeightFunction<float, 2U, 1U>;

```

It appears to me that the issue is triggered by a Math::Floor call at:  
[https://github.com/Kitware/ITK/blob/v4.13.0/Modules/Core/Common/include/itkBSplineInterpolationWeightFunction.hxx#L119](https://github.com/Kitware/ITK/blob/v4.13.0/Modules/Core/Common/include/itkBSplineInterpolationWeightFunction.hxx#L119)

Actually the issue can be reproduced without using any ITK, but doing something similar to itk::Math::Floor, as follows:

```
#include <emmintrin.h>

long long Floor_64(double x)
{
  return _mm_cvtsd_si64(_mm_set_sd(2 * x - 0.5)) >> 1;
}

float float32;
long long long64 = Floor_64(static_cast<double>(float32) - 0.0);

```

It appears that compiling this code takes forever, when using Visual C++ 2015 (Release, 64-bits). Is this a known issue?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [January 22, 2018, 1:49am UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/2 "2018-01-22T01:49:21Z")

</div>

The Visual Studio Build Robots are currently having health issues, so it would be better not to take one down at the moment. Once we move to GitHub, it would be tried.

It would be helpful to report the issue to the Visual Studio issue tracker.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 22, 2018, 10:43am UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/3 "2018-01-22T10:43:51Z")

</div>

Thanks for your reply, @matt.mccormick I hope the issue can be given a high priority.

FYI, I just posted at a Visual C++ forum: "VC++ 2015 x64 Release build (/O2) takes forever, trying to compile \_mm\_cvtsd\_si64(\_mm\_set\_sd(…)), a known compiler bug?"  
[https://social.msdn.microsoft.com/Forums/en-US/be92f3ae-8910-4709-9de5-4b8ba2b74968/vc-2015-x64-release-build-o2-takes-forever-trying-to-compile-mmcvtsdsi64mmsetsd?forum=vclanguage](https://social.msdn.microsoft.com/Forums/en-US/be92f3ae-8910-4709-9de5-4b8ba2b74968/vc-2015-x64-release-build-o2-takes-forever-trying-to-compile-mmcvtsdsi64mmsetsd?forum=vclanguage)

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 22, 2018, 5:11pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/4 "2018-01-22T17:11:46Z")

</div>

My proposed workaround for this compiler bug:  
"COMP: Worked around endless VS2015 Release compilation on Math::Floor"  
[http://review.source.kitware.com/#/c/23046/](http://review.source.kitware.com/#/c/23046/)

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 23, 2018, 3:11pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/5 "2018-01-23T15:11:05Z")

</div>

Thank you for approving at merging my proposed workaround to ITK: [https://itk.org/gitweb?p=ITK.git;a=commit;h=5788f5fe34f4825297880684eae12b6e65b70203](https://itk.org/gitweb?p=ITK.git;a=commit;h=5788f5fe34f4825297880684eae12b6e65b70203)

It was noted by Darran Rowe at [https://social.msdn.microsoft.com/Forums/en-US/be92f3ae-8910-4709-9de5-4b8ba2b74968/vc-2015-x64-release-build-o2-takes-forever-trying-to-compile-mmcvtsdsi64mmsetsd?forum=vclanguage](https://social.msdn.microsoft.com/Forums/en-US/be92f3ae-8910-4709-9de5-4b8ba2b74968/vc-2015-x64-release-build-o2-takes-forever-trying-to-compile-mmcvtsdsi64mmsetsd?forum=vclanguage) that the problem does _not_ occur when _Link Time Code Generation_ (LTCG) is enabled (compiler flag /GL). Is there any reason why ITK would not use LTCG? It appears to me that Whole Program Optimization is disabled for Visual C++ ITK projects, even for the Release configuration.

LTCG is enabled by default for Release configurations, when Visual C++ project created directly from Visual Studio (2013, 2015, 2017), through Visual Studio File menu, New, Project… It is recommended by Terry Mahaffey at [https://github.com/TriangleCppDevelopersGroup/TerryMahaffeyCppTalk/blob/master/compiler.pptm](https://github.com/TriangleCppDevelopersGroup/TerryMahaffeyCppTalk/blob/master/compiler.pptm)

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [January 23, 2018, 3:24pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/6 "2018-01-23T15:24:50Z")

</div>

> [@Niels\_Dekker](#):
>
> Thank you for approving at merging my proposed workaround to ITK: [https://itk.org/gitweb?p=ITK.git;a=commit;h=5788f5fe34f4825297880684eae12b6e65b70203](https://itk.org/gitweb?p=ITK.git;a=commit;h=5788f5fe34f4825297880684eae12b6e65b70203)

Thank you for contributing the fix, @Niels_Dekker! ☀

> [@Niels\_Dekker](#):
>
> It was noted by Darran Rowe at [https://social.msdn.microsoft.com/Forums/en-US/be92f3ae-8910-4709-9de5-4b8ba2b74968/vc-2015-x64-release-build-o2-takes-forever-trying-to-compile-mmcvtsdsi64mmsetsd?forum=vclanguage](https://social.msdn.microsoft.com/Forums/en-US/be92f3ae-8910-4709-9de5-4b8ba2b74968/vc-2015-x64-release-build-o2-takes-forever-trying-to-compile-mmcvtsdsi64mmsetsd?forum=vclanguage) that the problem does not occur when Link Time Code Generation (LTCG) is enabled (compiler flag /GL). Is there any reason why ITK would not use LTCG? It appears to me that Whole Program Optimization is disabled for Visual C++ ITK projects, even for the Release configuration.

That’s interesting.

I don’t think Whole Program Optimization is disabled – it is just not enabled 😛.

> [@Niels\_Dekker](#):
>
> LTCG is enabled by default for Release configurations, when Visual C++ project created directly from Visual Studio (2013, 2015, 2017), through Visual Studio File menu, New, Project… It is recommended by Terry Mahaffey at [TerryMahaffeyCppTalk/compiler.pptm at master · TriangleCppDevelopersGroup/TerryMahaffeyCppTalk · GitHub](https://github.com/TriangleCppDevelopersGroup/TerryMahaffeyCppTalk/blob/master/compiler.pptm)

The benefits of LTCG will come when the application that uses ITK as a library links its final binary. So, this is something that client applications want to use. We could try to propagate this flag when ITK is built in `Release` so it is automatically added with [`ITK_REQUIRED_LINK_FLAGS`](https://github.com/InsightSoftwareConsortium/ITK/blob/7f62477f8eb173f789979f46a6f75c20f7f874b2/CMake/ITKConfig.cmake.in#L19-L22).

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 23, 2018, 6:12pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/7 "2018-01-23T18:12:22Z")

</div>

Maybe this Link Time Code Generation issue should eventually be solved by a revision of CMake itself. When CMake generates a Visual C++ project which does not explicitly specify any compiler flags, I think it should add LTCG _by default_, for Release configurations. As it is also the default for Visual C++ projects that are created directly by Visual Studio.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 24, 2018, 10:07pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/8 "2018-01-24T22:07:22Z")

</div>

In the meantime I asked at the CMake mailing list whether CMake could add LTCG by default, for Release configurations:

[[CMake] Link Time Code Generation (LTGC) by default, for Release configurations of Visual Studio projects?](https://public.kitware.com/pipermail/cmake/2018-January/066970.html)

The reply by Cristian Adam sounds hopeful: [https://public.kitware.com/pipermail/cmake/2018-January/066971.html](https://public.kitware.com/pipermail/cmake/2018-January/066971.html)

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [February 6, 2018, 2:10pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/9 "2018-02-06T14:10:03Z")

</div>

FYI, My CMake feature request on this issue is here: _Link Time Code Generation (LTGC) by default, for Release configurations of Visual Studio projects_[https://gitlab.kitware.com/cmake/cmake/issues/17720](https://gitlab.kitware.com/cmake/cmake/issues/17720)

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [September 18, 2018, 4:54pm UTC](https://discourse.itk.org/t/visual-c-2015-release-build-hangs-forever-trying-to-compile-itk-math-floor/610/11 "2018-09-18T16:54:07Z")

</div>

> [@Niels\_Dekker](#):
>
> FYI, My CMake feature request on this issue is here: _Link Time Code Generation (LTCG) by default, for Release configurations of Visual Studio projects_ [https://gitlab.kitware.com/cmake/cmake/issues/17720](https://gitlab.kitware.com/cmake/cmake/issues/17720)

Update on this topic: With the help of @brad.king, I made a patch to support enabling Link Time Code Generation (Whole Program Optimization) for Visual Studio, by means of a CMake property, `INTERPROCEDURAL_OPTIMIZATION`. This patch has just been merged ([https://gitlab.kitware.com/cmake/cmake/issues/16748](https://gitlab.kitware.com/cmake/cmake/issues/16748)), and is scheduled to be included with CMake release 3.13.0

Note that the patch does not yet enable LTCG “by default”. You’d have to switch it on! The following code should enable LTCG for the Release configuration (which is what I would suggest people to do):

```
cmake_minimum_required( VERSION 3.13)
project( MyProject )
add_executable(MyProject MyMain.cpp MySource.cpp)
set_property(TARGET MyProject PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)

```

I think this could be an interesting option for both ITK users and ITK developers.
