# NVCC Loop Invariant Optimization

**URL:** <https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870>\
**Category:** CUDA Programming and Performance\
**Created:** [August 13, 2019, 6:30pm UTC](https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870 "2019-08-13T18:30:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wayao](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wayao](https://forums.developer.nvidia.com/u/wayao)\
**Post date:** [August 13, 2019, 6:30pm UTC](https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870/1 "2019-08-13T18:30:58Z")

</div>

Hi,

I have a question regarding whether nvcc can do loop invariant optimization.

I have such a code snippet in a larger computation:

```auto
...
    #pragma unroll
    for (int kw_inner = 0; kw_inner < 7; ++kw_inner) {
      #pragma unroll
      for (int occ_c = 0; occ_c < 2; ++occ_c) {
        #pragma unroll
        for (int oc_block_c = 0; oc_block_c < 4; ++oc_block_c) {
          #pragma unroll
          for (int ic_block = 0; ic_block < 3; ++ic_block) {
            if (((((int)blockIdx.z) * 2) + (((int)threadIdx.z) / 8)) < n) {
                ...

```

The if condition doesn’t involve for loop variables, and should be able to move up to top level:

```auto
...
    if (((((int)blockIdx.z) * 2) + (((int)threadIdx.z) / 8)) < n) {
      #pragma unroll
      for (int kw_inner = 0; kw_inner < 7; ++kw_inner) {
        #pragma unroll
        for (int occ_c = 0; occ_c < 2; ++occ_c) {
          #pragma unroll
          for (int oc_block_c = 0; oc_block_c < 4; ++oc_block_c) {
            #pragma unroll
            for (int ic_block = 0; ic_block < 3; ++ic_block) {
                ...

```

The second kernel can achieve 2x performance comparing to the first one. Doesn nvcc do such kind of optimization?

My nvcc info:  
nvcc: NVIDIA (R) Cuda compiler driver  
Copyright (c) 2005-2017 NVIDIA Corporation  
Built on Fri\_Sep\_\_1\_21:08:03\_CDT\_2017  
Cuda compilation tools, release 9.0, V9.0.176

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 13, 2019, 6:45pm UTC](https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870/2 "2019-08-13T18:45:20Z")

</div>

It doesn’t do it in every imaginable case.

Here is another example:

[url][[solved] Array not fully transfered to device memory - CUDA Programming and Performance - NVIDIA Developer Forums](https://devtalk.nvidia.com/default/topic/1057205/cuda-programming-and-performance/-solved-array-not-fully-transfered-to-device-memory/post/5360735/#5360735%5B/url%5D)

If you’d like to see a change in nvcc behavior, you may wish to file a bug. The instructions are linked in a sticky post at the top of this sub-forum.

I would also encourage you to test on the most recently released compiler. Items get addressed all the time. I’m not saying I know for sure anything is fixed in CUDA 10.1, but I would test against CUDA 10.1 (or the latest CUDA) before filing a bug.

---

<div class="post-metadata">

**Author:** ![wayao](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wayao](https://forums.developer.nvidia.com/u/wayao)\
**Post date:** [August 13, 2019, 7:22pm UTC](https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870/3 "2019-08-13T19:22:30Z")

</div>

Thank you for your help! I’ll try 10.1.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [August 13, 2019, 8:24pm UTC](https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870/4 "2019-08-13T20:24:43Z")

</div>

An alternative hypothesis would be that the if-statement within the inner-most loop interferes with the desired complete unrolling of all loops. This would explain the suprisingly large performance difference observed. Did you analyze the generated machine code to determine what exactly is responsible for the performance difference?

---

<div class="post-metadata">

**Author:** ![wayao](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wayao](https://forums.developer.nvidia.com/u/wayao)\
**Post date:** [August 14, 2019, 12:32am UTC](https://forums.developer.nvidia.com/t/nvcc-loop-invariant-optimization/79870/5 "2019-08-14T00:32:44Z")

</div>

> [@](#):
>
> An alternative hypothesis would be that the if-statement within the inner-most loop interferes with the desired complete unrolling of all loops. This would explain the suprisingly large performance difference observed. Did you analyze the generated machine code to determine what exactly is responsible for the performance difference?

I haven’t check the assembly code yet. For quick check, I removed unroll pragma but it didn’t help.
