1

I'm trying to make some optimizations on a C program. I want GCC to autovectorize if possible. To check what's doing I generate the assembly file of the program with the "-S" option, but whenever the optimization level is greater than 0, GCC outputs an almost empty .s file. I'm using gcc-7, but I tried with older versions and it does the same thing.

C code:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define HEIGHT 2000
#define WIDTH 1000

static unsigned int idx(unsigned int x, unsigned int y, unsigned int stride){
  return y * stride + x;
}

static void integral_image(const unsigned char * restrict in, unsigned int * restrict out){
  unsigned int row_sum = 0;
  unsigned char *newin = __builtin_assume_aligned(in, 16);
  unsigned char *newout = __builtin_assume_aligned(out, 16);

  for(unsigned int x = 0; x < WIDTH; ++x){
    row_sum += newin[x];
    newout[x] = row_sum;
  }

  for(unsigned int y = 1; y < HEIGHT; ++y){
    row_sum = 0;

    for(unsigned w = 0; w < WIDTH; w += 4){
      const unsigned int i1 = idx(w, y, WIDTH);
      const unsigned int old1 = idx(w, y - 1, WIDTH);
      const unsigned int i2 = idx(w + 1, y, WIDTH);
      const unsigned int old2 = idx(w, y - 1, WIDTH);
      const unsigned int i3 = idx(w + 2, y, WIDTH);
      const unsigned int old3 = idx(w + 2, y - 1, WIDTH);
      const unsigned int i4 = idx(w + 3, y, WIDTH);
      const unsigned int old4 = idx(w + 3, y - 1, WIDTH);

      row_sum += newin[i1];
      newout[i1] = row_sum + newout[old1];

      row_sum += newin[i2];
      newout[i2] = row_sum + newout[old2];

      row_sum += newin[i3];
      newout[i3] = row_sum + newout[old3];

      row_sum += newin[i4];
      newout[i4] = row_sum + newout[old4];
    }
  }
}

Now the GCC generated .s file:

.file   "thrash.c"
.ident  "GCC: (Ubuntu 7.1.0-10ubuntu1~16.04.york0) 7.1.0"
.section    .note.GNU-stack,"",@progbits

Thank you!

1
  • 4
    Well.. it doesn't have main. Functions are static, so they cannot be linked to external main. So I am with the compiler. Commented May 9, 2018 at 15:58

1 Answer 1

6

Both your functions are declared static, which means that neither one is visible to other compilation units (i.e. other .o files) during linking.

When you enable optimization, GCC notices that, since the functions are not externally visible nor are they called from any externally visible function, their presence is meaningless and so it omits them entirely to save space.

To fix this, remove the static keyword from your function signatures.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very very much! We were giben some basic code so I didn't pay attention to that. Anyway, I also didn't know this gcc's behavior. Again, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.