Issue 141638
Summary Code with goto is 3x faster than code with continue
Labels new issue
Assignees
Reporter gbaraldi
    While debugging https://github.com/JuliaLang/julia/issues/47542 I managed to reproduce the same slowdown with C code, https://godbolt.org/z/789GdG3bE.

The code is 
```c
__attribute__((noinline)) size_t foo(int64_t *arr, size_t len) {
 size_t i = 0;
    foo:
    while (i < len) {
        if (arr[i] == 0) {
 i += 1;
            goto foo;
        }
        i += 2;
    }
 return i;
}

__attribute__((noinline)) size_t bar(int64_t *arr, size_t len) {
    size_t i = 0;
    while (i < len) {
        if (arr[i] == 0) {
            i += 1;
            continue;
        }
        i += 2;
 }
    return i;
}
```

The difference is that code with continue gets compiled to a select where the goto keeps the branch. I imagine this is a choice done in SimplifyCFG but I imagine that branch prediction/speculation on this is good enough that the conditional increment done on the continue case is much worse. Goto was faster on everything I tested (2 aarch64 cpus and 2 x86 cpus)

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to