Enum ctor fn + branch = missed optimization/inlining
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 49/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- rust
- 领域
- compilers, performance
调研方向
The report includes blockmaker and blockmaker2 examples and assembly output, but names no rustc source file or test. Start by reproducing the difference on the linked Godbolt setup with nightly and -C opt-level=3, then trace how enum variant constructors used as function values are optimized. Done when the constructor function branch produces equivalent optimized output to the direct conditional construction, with a regression test covering the case.
由索引模型根据 Issue 内容生成。
描述
I tried this code:
pub enum Block {
Outlined(i32),
Hollow(i32),
}
#[unsafe(no_mangle)]
pub fn blockmaker(condition: bool, mass: i32) -> Block {
(if condition { Block::Hollow } else { Block::Outlined }) (mass)
}
#[unsafe(no_mangle)]
pub fn blockmaker2(condition: bool, mass: i32) -> Block {
if condition { Block::Hollow(mass) } else { Block::Outlined(mass) }
}
I expected to see this happen: blockmaker should result in the same assembly as blockmaker2
Instead, this happened: They compile to different assembly:
example[9d75cd1f3a21f3ed]::Block::Hollow:
mov eax, 1
mov edx, edi
ret
example[9d75cd1f3a21f3ed]::Block::Outlined:
xor eax, eax
mov edx, edi
ret
blockmaker:
lea rax, [rip + example[9d75cd1f3a21f3ed]::Block::Hollow]
lea rcx, [rip + example[9d75cd1f3a21f3ed]::Block::Outlined]
test edi, edi
cmovne rcx, rax
mov edi, esi
jmp rcx
blockmaker2:
mov edx, esi
mov eax, edi
ret
blockmaker uses a weird way to do conditional jumps (instead of using the traditional jnz/jz + jmp). Meanwhile, blockmaker2 seems to be properly inlined and optimized.
This missed optimization happens on all three channels (stable 1.99.0/beta/nightly) on godbolt, for amd64, with -C opt-level=3
- 主要语言
- Rust
- 星标
- 122k
- 派生
- 16.7k
- 平均合并
- 2 天 16 小时
- 30 天内合并 PR
- 515
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
rust-lang/rust 的其他 Issue
-
Remove unnecessary parentheses when suggesting to remove '&' from a pattern可能已有人在做 @BartSimpson001 于 1 天前认领。 未关闭A-diagnostics D-invalid-suggestion T-compiler
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
Windows `File::set_permissions` silently does nothing when clearing `readonly` leaves `FileAttributes == 0`可能已有人在做 @ChrisDenton 于 11 天前认领。 未关闭A-filesystem C-bug O-windows T-libs
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
A-run-make relnotes relnotes-needs-review relnotes-tracking-issue T-lang T-libs
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 1 天内回复
-
A-LLVM O-Arm O-UEFI O-x86_64 relnotes relnotes-needs-review relnotes-tracking-issue T-compiler
难度 1/5 1 小时以内 新手友好度 72/100
rust-lang/rust#165504 · 1 条评论 ·
维护者通常 1 天内回复
-
relnotes relnotes-tracking-issue T-libs
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 84/100
antithesishq/bombadil#473 ·
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
libracoder/kiri#669 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
维护者通常 5 天内回复
-
autofixer: follow-up
难度 2/5 1-3 小时 新手友好度 75/100
tursodatabase/turso#12217 ·
维护者通常 1 天内回复
-
Declare the CMS schema library in the optional recipients extra可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭area:python in-progress
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复