【问题标题】:Can LLC -O=3 not omit frame pointers from unoptimized llvm bytecode produced by clang -O0?LLC -O=3 可以不从 clang -O0 生成的未优化 llvm 字节码中省略帧指针吗?
【发布时间】:2020-12-23 03:57:23
【问题描述】:

我有一个简单的 c 代码:

// main.c
#include <stdio.h>
void foo()
{ 
}

int main()
{
    return 0;
}

以下命令的输出

clang -O3 -emit-llvm -c main.c -o main.bc; llc main.bc -o main.S; cat main.S;

我明白了:

...
foo:                                    # @foo
        .cfi_startproc
# %bb.0:
        retq
...

这是预期的。 foo() 函数已转换为 retq 指令。

但是如果我运行以下命令:

clang -emit-llvm -c main.c -o main.bc; llc -O=3 main.bc -o main.S; cat main.S;

我明白了:

...
foo:                                    # @foo
        .cfi_startproc
# %bb.0:
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq
...

功能上这没问题,但foo() 在这种情况下没有必要为空函数处理帧指针。第一种情况和第二种情况的区别在于,在第一种情况下,我们使用 -O3 表示 clang,而在第二种情况下,我们使用 -O3 表示 llc。我认为这两者是等价的。

我还尝试了以下方法:

clang -emit-llvm -fomit-frame-pointer -c main.c -o main.bc ; llc -O=3 main.bc -o main.S; cat main.S;

我得到:

...
foo:                                    # @foo
        .cfi_startproc
# %bb.0:
        retq
...

那么,这是否意味着如果clang 决定将帧指针发送到 LLVM 字节码中,llc 不够聪明,无法删除它们? (llc 似乎没有 -fomit-frame-pointer 选项,虽然它确实有 -disable-fp-elim 选项 Disable frame pointer elimination optimization 所以我认为 llc 能够消除帧指针)

【问题讨论】:

  • 那么,您的问题是什么?看来你已经自己想通了。
  • 我的问题是,这不是我所期望的行为。为什么 llc 说它有优化删除它时不删除帧指针代码?还有哪些 llc 不能做但 clang 可以做的优化?我对编译器优化不是很熟悉。
  • @SakshamJain LLVM 字节码可能在某处有一个标志,表明如果可能的话,是否可以消除 FP。

标签: assembly clang llvm compiler-optimization llc


【解决方案1】:

感谢@fuz

似乎 clang 在位码中添加了禁用/启用帧指针消除的属性

使用 -O0 编译时,这些是位码中的属性:

attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

使用 -O3 编译时,这些是属性:

attributes #0 = { norecurse nounwind readnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

看到no-frame-pointer-elim 的值在两者中不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2012-04-07
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多