gcc的asm是否相当于递归的gfortran默认设置?

rom*_*ric 8 c++ optimization assembly gcc fortran

我只是在使用递归函数C++,Fortran并且我意识到一个简单的递归函数Fortran几乎是它的等效C++函数的两倍.现在,在进入这个之前,我知道这里有类似的问题,具体来说:

  1. 为什么添加汇编注释导致生成的代码发生如此根本的变化?
  2. 工作的asm volatile("" ::: "memory")
  3. 相当于gfortran中的asm volatile

不过,我有点更加具体和困惑的Fortran编译器似乎是在做什么,你可以实现asm volatilegcc.为了给你一些上下文,让我们考虑以下递归Fibonacci number实现:

Fortran代码:

module test
implicit none
private
public fib

contains

! Fibonacci function
integer recursive function fib(n) result(r)
    integer, intent(in) :: n
    if (n < 2) then
        r = n
    else
        r = fib(n-1) + fib(n-2)
    end if
end function  ! end of Fibonacci function
end module

program fibonacci
use test, only: fib
implicit none
integer :: r,i 
integer :: n = 1e09
real(8) :: start, finish, cum_time

cum_time=0
do i= 1,n 
    call cpu_time(start)
    r = fib(20)
    call cpu_time(finish) 
    cum_time = cum_time + (finish - start)
    if (cum_time >0.5) exit
enddo  

print*,i,'runs, average elapsed time is', cum_time/i/1e-06, 'us' 
end program
Run Code Online (Sandbox Code Playgroud)

编译:

gfortran -O3 -march=native
Run Code Online (Sandbox Code Playgroud)

C++代码:

#include <iostream>
#include <chrono>
using namespace std;

// Fib function
int fib(const int n)
{
    int r;
    if (n < 2)
        r = n;
    else
        r = fib(n-1) + fib(n-2);
    return r;
} // end of fib

template<typename T, typename ... Args>
double timeit(T (*func)(Args...), Args...args)
{
    double counter = 1.0;
    double mean_time = 0.0;
    for (auto iter=0; iter<1e09; ++iter){
        std::chrono::time_point<std::chrono::system_clock> start, end;
        start = std::chrono::system_clock::now();

        func(args...);

        end = std::chrono::system_clock::now();
        std::chrono::duration<double> elapsed_seconds = end-start;

        mean_time += elapsed_seconds.count();
        counter++;

        if (mean_time > 0.5){
            mean_time /= counter;
            std::cout << static_cast<long int>(counter)
            << " runs, average elapsed time is "
            << mean_time/1.0e-06 << " \xC2\xB5s" << std::endl; 
            break;
        }
    }
    return mean_time;
}

int main(){
    timeit(fib,20);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:

g++ -O3 -march=native
Run Code Online (Sandbox Code Playgroud)

定时:

Fortran: 24991 runs, average elapsed time is 20.087 us
C++    : 12355 runs, average elapsed time is 40.471 µs
Run Code Online (Sandbox Code Playgroud)

所以gfortran比那里快两倍gcc.看看汇编代码,我明白了

大会(Fortran):

.L28:
    cmpl    $1, %r13d
    jle .L29
    leal    -8(%rbx), %eax
    movl    %ecx, 12(%rsp)
    movl    %eax, 48(%rsp)
    leaq    48(%rsp), %rdi
    leal    -9(%rbx), %eax
    movl    %eax, 16(%rsp)
    call    __bench_MOD_fib
    leaq    16(%rsp), %rdi
    movl    %eax, %r13d
    call    __bench_MOD_fib
    movl    12(%rsp), %ecx
    addl    %eax, %r13d
Run Code Online (Sandbox Code Playgroud)

汇编(C++):

.L28:
    movl    72(%rsp), %edx
    cmpl    $1, %edx
    movl    %edx, %eax
    jle .L33
    subl    $3, %eax
    movl    $0, 52(%rsp)
    movl    %eax, %esi
    movl    %eax, 96(%rsp)
    movl    92(%rsp), %eax
    shrl    %eax
    movl    %eax, 128(%rsp)
    addl    %eax, %eax
    subl    %eax, %esi
    movl    %edx, %eax
    subl    $1, %eax
    movl    %esi, 124(%rsp)
    movl    %eax, 76(%rsp)
Run Code Online (Sandbox Code Playgroud)

两个汇编代码由几乎相似的块/标签组成,一遍又一遍地重复.正如您所看到的,Fortran程序集进行了两次fib函数调用,而在C++程序集中,gcc可能已经展开了所有递归调用,这可能需要更多的堆栈push/pop和尾部跳转.

现在,如果我只是在C++代码中添加一个内联汇编注释

修改后的C++代码:

// Fib function
int fib(const int n)
{
    int r;
    if (n < 2)
        r = n;
    else
        r = fib(n-1) + fib(n-2);
    asm("");
    return r;
} // end of fib
Run Code Online (Sandbox Code Playgroud)

生成的汇编代码,更改为

汇编(C++修改):

.L7:
    cmpl    $1, %edx
    jle .L17
    leal    -4(%rbx), %r13d
    leal    -5(%rbx), %edx
    cmpl    $1, %r13d
    jle .L19
    leal    -5(%rbx), %r14d
    cmpl    $1, %r14d
    jle .L55
    leal    -6(%rbx), %r13d
    movl    %r13d, %edi
    call    _Z3fibi
    leal    -7(%rbx), %edi
    movl    %eax, %r15d
    call    _Z3fibi
    movl    %r13d, %edi
    addl    %eax, %r15d
Run Code Online (Sandbox Code Playgroud)

您现在可以看到两个fib函数调用.定时他们给了我

定时:

Fortran: 24991 runs, average elapsed time is 20.087 us
C++    : 25757 runs, average elapsed time is 19.412 µs
Run Code Online (Sandbox Code Playgroud)

我知道asm没有输出的效果asm volatile是抑制积极的编译器优化,但在这种情况下,gcc认为它太聪明,但最终生成一个效率较低的代码.

所以问题是:

  • 为什么gcc看不到这种"优化"的时候gfortan显然可以?
  • 内联装配线必须在返回语句之前.把它放在别处,它将没有任何效果.为什么?
  • 这种行为编译器是否具体?例如,您可以使用clang/MSVC模仿相同的行为吗?
  • 是否有更安全的方法可以更快地进行递归CC++(不依赖于内联汇编或迭代式编码)?可能是变形模板?

更新:

  • 上面显示的结果都是gcc 4.8.4.我也试图与编译它gcc 4.9.2,并gcc 5.2和我得到相同的结果.
  • 问题也可以复制(修复?),如果不是将asm输入参数声明为volatile,(volatile int n)而不是(const int n),虽然这会导致我的机器运行时间稍微慢一些.
  • 正如Michael Karcher所提到的,我们可以通过-fno-optimize-sibling-calls标志来解决这个问题.由于此标志在-O2级别及更高级别被激活,因此即使编译也会-O1修复此问题.
  • 我用clang 3.5.1with 运行相同的例子,-O3 -march=native虽然情况不一样,但clang似乎生成更快的代码asm.

Clang Timing:

clang++ w/o asm    :  8846 runs, average elapsed time is 56.4555 µs
clang++ with asm   : 10427 runs, average elapsed time is 47.8991 µs 
Run Code Online (Sandbox Code Playgroud)

Mic*_*her 4

请参阅本答案末尾附近的粗体字,了解如何获得由 gcc 生成的快速程序。阅读答案以获取对四个问题的答复。

你的第一个问题假设gfortran能够看到gcc未能看到的优化可能性。事实上,情况正好相反。gcc发现了一些它认为是优化可能性的东西,但却gfortran错过了它。唉,gcc这是错误的,它应用的优化结果是你的系统速度损失了 100%(与我的系统相当)。

为了解决你的第二个问题:该asm语句阻止了内部转换,从而导致了gcc错误优化的可能性。如果没有该asm语句,您的代码将(有效)转换为:

int fib(const int n)
{
    if (n < 2)
        return n;
    else
        return fib(n-1) + fib(n-2);
}
Run Code Online (Sandbox Code Playgroud)

包含递归调用的 return 语句会触发“兄弟调用优化”,从而使您的代码变得悲观。包含 asm 语句可防止在其中移动返回指令。

目前,我手头只有 gcc,所以我无法尝试其他编译器的行为来通过证据回答你的第三个问题,但这似乎绝对依赖于编译器。您遇到了 gcc 的一个怪癖(或错误,无论您如何称呼它),它在尝试优化它时会生成错误的代码。不同编译器的优化器有很大不同,因此其他编译器很可能不会像那样错误地优化您的代码gcc。另一方面,用于优化的代码转换是一个经过深入研究的主题,并且大多数编译器都在实现类似的优化方法,因此另一个编译器可能会陷入与gcc.

解决最后一个问题:这不是 C/C++ 与 Fortan 的问题,而是gcc这个示例程序(以及可能类似的生产程序)混乱的问题。因此,没有办法使 中的递归速度更快C++,但是有一种方法可以通过禁用有问题的优化来加速中的gcc-fno-optimize-sibling-calls这个示例: ,这会导致(在我的系统上,在单个测试运行中)比仅仅插入更快的代码该asm声明。