gpt4 book ai didi

optimization - LLVM opt mem2reg无效

转载 作者:行者123 更新时间:2023-12-03 15:55:46 27 4
gpt4 key购买 nike

我目前正在使用LLVM,并尝试编写一些优化器以熟悉opt和clang。我写了一个test.c文件,如下所示:

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

我编译了源代码并生成了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

这两个.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)"}

因此,看来我的mem2reg通行证无效!有什么问题吗?

最佳答案

最近,当用-O0编译时,clang开始向每个函数添加optnone属性,这阻止了之后进一步的优化,包括mem2reg pass。为了防止这种情况,请将-Xclang -disable-O0-optnone添加到clang中。

关于optimization - LLVM opt mem2reg无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com