Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is clang over-complicating my simple factorial function?

Consider a simple factorial function:

static int factorial(int n) {
    if (n <= 0) return 1;
    return n * factorial(n - 1);
}

int main(int argc, char** argv) {
    return factorial(argc);
}

Compiling with -O2 yields a very interesting difference:

  • g++ 7.3: I get virtually the same loop structure converted into assembly with 10-ish instructions.
  • clang++ 5.0.0: I get a huge mess of 220+ instructions and I have no idea what is going on.

See the comparison here (Compiler explorer)

Building locally and comparing runtimes, the simple g++ binary definitely runs faster for all values within reason (i.e. that don't cause overflow) on Ubuntu 17.10.

Can anyone tell me why clang is going to all this trouble, and what it's trying to do (and failing in both size and speed)?

like image 445
naslundx Avatar asked Mar 09 '18 13:03

naslundx


1 Answers

Can anyone tell me why clang is going to all this trouble, and what it's trying to do (and failing in both size and speed)?

It's trying to minimise the number of test-and-branch operations by vectorising the code.

It's certainly failing on size. As for whether it's failing on speed, have you bench-marked it?

gcc will do the same if you add the command line option -ftree-vectorize.

like image 142
Richard Hodges Avatar answered Sep 28 '22 15:09

Richard Hodges