clang - 看似不必要的 bitcast -> phi -> 正在生成 bitcast
问题描述
我遇到了一个问题,Clang 似乎生成了不必要的位广播(从 f32 -> i32 -> f32。以下是生成的 IR(去除了不相关的部分。我尝试突出显示相关行,但这在代码中不起作用)堵塞。
问题是定义 %5 的位转换,然后是重复的 phi 节点(%6 和 %7),以及最后一行的位转换存储。我看不出这个数据路径被比特广播到 i32 的原因,它没有在其他任何地方使用。
对应的 c 代码只包含浮点数据类型,一些以嵌套数组作为数据类型的结构,if elseif else 构造一些浮点常量(它是生成代码)。不知道为什么。
(代码:https ://pastebin.com/c0gYkwhF,llvm ir: https ://pastebin.com/NSyhSkUa )
tldr;
- 为什么生成到 i32 的位广播
- 我能以某种方式阻止这种情况吗(不想处理 i32 数据类型)
; Function Attrs: nofree norecurse nounwind uwtable
define dso_local void @CurrentControl_step() local_unnamed_addr #0 {
entry:
; (...)
%mul3 = fmul fast float %4, 0x3FEFFFEF80000000
%add4 = fadd fast float %3, %mul3
%cmp = fcmp fast ogt float %add4, 0x3FEF400000000000
br i1 %cmp, label %if.then, label %if.else
; (...)
if.else7: ; preds = %if.else
store float %add4, float* getelementptr inbounds (%struct.DW_CurrentControl_T, %struct.DW_CurrentControl_T* @CurrentControl_DW, i64 0, i32 1, i64 0), align 4, !tbaa !2
%5 = bitcast float %add4 to i32
br label %if.end8
if.end8: ; preds = %if.then6, %if.else7, %if.then
%6 = phi i32 [ -1082523648, %if.then6 ], [ %5, %if.else7 ], [ 1064960000, %if.then ]
%7 = phi float [ 0xBFEF400000000000, %if.then6 ], [ %add4, %if.else7 ], [ 0x3FEF400000000000, %if.then ]
; (...)
%9 = fsub fast float %7, %mul9
; (...)
store i32 %6, i32* bitcast (float* getelementptr inbounds (%struct.DW_CurrentControl_T, %struct.DW_CurrentControl_T* @CurrentControl_DW, i64 0, i32 2, i64 0) to i32*), align 4, !tbaa !2
; (...)
编辑: 我有一个新的最小示例生成看似不必要的比特广播:
typedef struct {
float nestedArray[2];
} Foo;
Foo s;
float testVar;
void test(void) {
testVar = s.nestedArray[0];
}
生成:
%struct.Foo = type { [2 x float] }
@s = dso_local local_unnamed_addr global %struct.Foo zeroinitializer, align 4
@var = dso_local local_unnamed_addr global float 0.000000e+00, align 4
; Function Attrs: nofree norecurse nounwind uwtable
define dso_local void @test() local_unnamed_addr #0 {
entry:
%0 = load i32, i32* bitcast (%struct.Foo* @s to i32*), align 4, !tbaa !2
store i32 %0, i32* bitcast (float* @var to i32*), align 4, !tbaa !2
ret void
}
解决方案
Bitcast 是由以下原因引起的:doc
// Try to canonicalize loads which are only ever stored to operate over
// integers instead of any other type. We only do this when the loaded type
// is sized and has a size exactly the same as its store size and the store
// size is a legal integer type.
// Do not perform canonicalization if minmax pattern is found (to avoid
// infinite loop).
推荐阅读
- php - 'where 子句中的未知列“有”
- java - JList中来自mysql数据库的数据
- javascript - VueJS如何在字符串中查找特定元素并替换它们
- android - 使用android studio创建.a文件而不是.so?
- ios - 在 TensorFlowLite 图像分类示例 iOS 应用中使用通过 Google ML Kit 生成的 TFLite 文件
- r - 无法将 tidyselect `everything()` 与 `group_by()` 和 `fill()` 结合使用
- node.js - 计算日期范围的重叠量
- python - 如何解决提供的绑定数量不正确。当前语句使用 1,Delete 和 Excutemany 提供了 2?
- java - 根据模式匹配从文件中提取一组行
- ms-access - 根据客户和购买日期添加编号