gpt4 book ai didi

llvm - llvm-IR 中的 phi 指令语义

转载 作者:行者123 更新时间:2023-12-05 04:01:43 29 4
gpt4 key购买 nike

尝试理解 llvm-IR 中的 phi 指令语义

( https://llvm.org/docs/LangRef.html#phi-instruction )

让我们考虑以下示例:

 ; Function Attrs: norecurse nounwind
define i32 @main( i32 %argc, i8** %argv) {
entry:
switch i32 %argc, label %L1 [ i32 0, label %L0
i32 1, label %L1 ]
L0:
%x = add i32 %argc, 1
br label %L1
L1:
%y = phi i32 [ %argc, %entry ], [ %x, %L0 ]
%z = sub i32 %y, 1
%w = udiv i32 100, %z
ret i32 %w
}

用clang-7.0.1编译

 $ clang-7.0.1 -O0 test.ll -o a.out
PHINode should have one entry for each predecessor of its parent basic
block!
%y = phi i32 [ %argc, %entry ], [ %x, %L0 ]
fatal error: error in backend: Broken function found, compilation

中止!

当我用“%y = add i32 2, 1”替换“%y = phi ...”时,测试编译成功。

这里的问题是关于错误信息:为什么不是所有的前辈都在测试中列出phi?从 LangRef.html#phi-instruction 中 phi 指令的描述我不能明白了。

最佳答案

当从 block A 到 block B 有多个边时,则 B 中的 PHI 节点必须列出 A 的次数与边的数量一样多,每次都具有相同的值。在您的情况下,从 entryL1 有两条边(一条用于开关的默认情况,一条用于 1 情况),所以 entry 需要在 PHI 节点中列出两次。

但在这种情况下,也许更简洁的解决方案是从您的交换机中删除 [i32 1, label %L1] 案例,因为无论如何这是多余的。那么只有一个边缘,您只需要一个条目即可 entry

关于llvm - llvm-IR 中的 phi 指令语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55144976/

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