简体   繁体   English

LLVM opt mem2reg无效

[英]LLVM opt mem2reg has no effect

I am currently playing around with LLVM and am trying to write a few optimizers to familiarize myself with opt and clang. 我目前正在玩LLVM,我正在尝试编写一些优化器来熟悉opt和clang。 I wrote a test.c file that is as follow: 我写了一个test.c文件,如下所示:

int foo(int aa, int bb, int cc){
    int sum = aa + bb;
    return sum/cc;
}

I compiled the source code and generated 2 .ll files, one unoptimized and one with mem2reg optimizer pass: 我编译了源代码并生成了2个.ll文件,一个未经优化,一个用mem2reg优化器传递:

clang -emit-llvm -O0 -c test.c -o test.bc
llvm-dis test.bc
opt -mem2reg -S test.ll -o test-mem2reg.ll

Both .ll files gave me the following output: 两个.ll文件都给了我以下输出:

ModuleID = 'test.bc'
source_filename = "test.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define i32 @foo(i32 %aa, i32 %bb, i32 %cc) #0 {
entry:
  %aa.addr = alloca i32, align 4
  %bb.addr = alloca i32, align 4
  %cc.addr = alloca i32, align 4
  %sum = alloca i32, align 4
  store i32 %aa, i32* %aa.addr, align 4
  store i32 %bb, i32* %bb.addr, align 4
  store i32 %cc, i32* %cc.addr, align 4
  %0 = load i32, i32* %aa.addr, align 4
  %1 = load i32, i32* %bb.addr, align 4
  %add = add nsw i32 %0, %1
  store i32 %add, i32* %sum, align 4
  %2 = load i32, i32* %sum, align 4
  %3 = load i32, i32* %cc.addr, align 4
  %div = sdiv i32 %2, %3
  ret i32 %div
}

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" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 6.0.0 (trunk 314616)"}

So it seems that my mem2reg pass didn't work! 所以看来我的mem2reg传递不起作用! What would be the problem? 会出现什么问题?

Recently, when compiled with -O0, clang started to add optnone attribute to each function, which prevents further optimizations afterwards including mem2reg pass. 最近,当使用-O0编译时,clang开始为每个函数添加optnone属性,这阻止了包括mem2reg传递之后的进一步优化。 To prevent that, add -Xclang -disable-O0-optnone to clang. 为防止这种情况,请将-Xclang -disable-O0-optnone到clang。

Another answer already points out that with -O0 (or without -O option), your functions are annotated with the optnone attribute. 另一个答案已经指出,使用-O0 (或不使用-O选项),您的函数将使用optnone属性进行注释。 Another effect of lowering the optimization level is that no TBAA metadata seems to be generated, which also affects later optimizations. 降低优化级别的另一个影响是似乎没有生成TBAA元数据,这也会影响以后的优化。

So to prepare a file for opt , I found that it is better to keep your optimization level, and pass the option -Xclang -disable-llvm-passes (the help text for this option reads "Use together with -emit-llvm to get pristine LLVM IR from the frontend by not running any LLVM passes at all"). 因此,要为opt准备文件,我发现保持优化级别更好,并传递选项-Xclang -disable-llvm-passes (此选项的帮助文本为“与-emit-llvm一起使用”以获取来自前端的原始LLVM IR完全没有运行任何LLVM传递“)。

The complete invocation becomes: 完整的调用变为:

clang -S -emit-llvm -O -Xclang -disable-llvm-passes source.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM