Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

unreachable reduces performance #25951

Open Quuxplusone opened 8 years ago

Quuxplusone commented 8 years ago
Bugzilla Link PR25952
Status NEW
Importance P normal
Reported by Gunnar Bergmann (g.bergmann.official@gmail.com)
Reported on 2015-12-27 17:39:51 -0800
Last modified on 2015-12-27 17:39:51 -0800
Version 3.7
Hardware PC Linux
CC llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments unreachable-example.tar.gz (20480 bytes, application/x-gzip)
Blocks
Blocked by
See also
Created attachment 15526
brainfuck interpreter and example data

Delaring cases in a switch statement like below as unreachable may reduce
performance:
switch x {
    case 0: // ...
        break;
    case 1: // ...
        break;
 // ....
    case 7: // ...
        break;

    default:
        __builtin_unreachable();
}

Removing the __builtin_unreachable() improves the performance. On my PC the
attached example needs about 25 with it and 17 without.
The attached code is a simple brainfuck interpreter that can be tested by
calling "brainfuck-optimized mandel.brainfuck".
It was test with options -O3 and -O2 as 64 bit build on a haswell processor.
The problem is reproducible in Rust.

Profiling shows a suspiciously high number of branch mispredictions. From what
I have seen of the assembler code, I think that nested conditional branches
like a binary search are faster than a branch table by causing fewer
mispredictions.
Quuxplusone commented 8 years ago

Attached unreachable-example.tar.gz (20480 bytes, application/x-gzip): brainfuck interpreter and example data