llvm / llvm/llvm-project

Missed constant propagation of unchanged struct field across call

Open
#221,792 1 comment 0 reactions 0 assignees View on GitHub
llvm:transforms missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

LLVM fails to infer that a struct field remains unchanged across a call when the callee only modifies a different field of the same struct.

Godbolt: https://godbolt.org/z/xaaWK47Kj

In the example, `v.size` is initialized to 0 and `makeSize()` only writes to `v.capacity`. After the call, `v.size` is still known to be 0, so:

return (v.size == 0) ? 1 : 2;

could be folded to:

return 1;

GCC performs this optimization, while Clang reloads `v.size` from the stack and emits a runtime comparison.

Contributor guide

Open the contributing guide

Research direction

Reproduce the supplied Godbolt example first and compare Clang’s output with GCC’s. The issue names no local files or tests, so locate the LLVM optimization pass responsible for constant propagation across calls and its regression tests. Done means the unchanged v.size comparison is folded to return 1 without a runtime reload.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.