<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Dead code partially eliminated"
   href="https://bugs.llvm.org/show_bug.cgi?id=37231">37231</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Dead code partially eliminated
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lukasz.kuszner@intel.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20224" name="attach_20224" title="bad and good dump as described">attachment 20224</a> <a href="attachment.cgi?id=20224&action=edit" title="bad and good dump as described">[details]</a></span>
bad and good dump as described

Given two files:

foo.c
---
void foo(double *x, double *y, double *result, int arr_sizes, int iter) {
  while (iter-->0) {
    double sum=0;
    for (int j=0; j<arr_sizes; j++) {
      for (int i=0; i<arr_sizes; i++)
        sum += x[i]*y[i];
      result[j] = sum;
    }
  }
}
---

reproducer.c
---
#include<stdlib.h>
#include<stdio.h>

void foo(double *x, double *y, double *result, int arr_sizes, int iter);

int main() {
  int N = 1024;
  double *Y = (double*) malloc(sizeof(double)*N);
  double *X = (double*) malloc(sizeof(double)*N);
  double *R = (double*) malloc(sizeof(double)*N);

  int collatz=11;

  int cycles=1;
  while(collatz>1) {
    if (collatz%2==0) {
      collatz/=2;
    } else {
        collatz*=3;
        collatz++;
    }
    foo(X,Y,R,N,cycles);
    cycles *= 2;
  }
  printf("%d\n", cycles);
  free(X);
  free(Y);
  free(R);
  return 0;
}
---

Compiled with clang (tested with trunk build from 2018 March 16):
clang -O3  -fno-unroll-loops -g -flto reproduce.c foo.c


Results in the assembly with the function foo partially eliminated from the
code (bad case). That is the 
---
sum += x[i]*y[i];
---
instruction is replaced by dummy increment.

The -fno-unroll-loops is not necessary but makes the assembly analysis easier.

Merging two files into one file makes the thing easier (good case) for the
compiler and the whole foo is eliminated as expected.

Attached dump files: bad_dump.txt for bad case and good_dump.txt for good case
were produced by:

---
objdump -S -d -M intel <executable>  >  <dump_file.txt> 
---</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>