[clang] [Clang] Initialize bypassed variables w/ trivial-auto-var-init (PR #181937)
Oliver Hunt via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 12 01:48:20 PDT 2026
ojhunt wrote:
> The new implementation miscompiles the following:
>
> ```
> void f(int g(int*)) {
> while (true) {
> X:
> goto Y;
> int x;
> Y:
> if (g(&x))
> goto X;
> goto Y;
> }
> }
> ```
>
> It optimizes down to the following, which is wrong; `x` is not supposed to be zeroed if `g()` returns zero.
>
> ```
> define dso_local void @f(ptr noundef readonly captures(none) %g) local_unnamed_addr #0 {
> entry:
> %x = alloca i32, align 4
> br label %Y
>
> Y: ; preds = %Y, %entry
> store i32 0, ptr %x, align 4, !annotation !8
> %call = call i32 %g(ptr noundef nonnull %x) #1
> br label %Y
> }
> ```
>
> Whether a goto bypasses initialization depends on both the source and destination of the goto.
Doing this correctly really requires lifting the initialization to be ahead of any dominating labels (or even switching it - for large buffer - to a cheaply initialized flag and a post label check for initialization, which would work but be terrible)
https://github.com/llvm/llvm-project/pull/181937
More information about the cfe-commits
mailing list