llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DebugInfo][SimplifyCFG] Dropping a block also drops a #dbg_value update, causing LLDB to show a stale value

Open MrEven132 opened this issue 4 months ago • 2 comments

Description

simplifycfg deletes a basic block that contains a #dbg_value update for a source variable. After that transformation, the optimized binary still has valid program behavior, but the debug info for variable l becomes stale.

In the source program, l is initialized to -1 and later assigned 0 in if ((l = 0)). At the final return 0;, LLDB should see l == 0. Before simplifycfg, LLDB reports 0; after simplifycfg, LLDB reports -1.

This also reproduces through function-simplification<O1/O2/O3>, since that pipeline reuses simplifycfg.

Reproducer

case.c:

signed char func_1()
{
    short l = -1;

    for (int i = 0; i < 1; i++)
        ;

    if ((l = 0))
    {
        return 1;
    }
    else
    {
        for (int i = 0; i < 1; i++)
            ;
    }

    return 0;
}

int main()
{
    func_1();
    return 0;
}

Build pipeline:

clang -g -O0 -Xclang -disable-O0-optnone -emit-llvm -S case.c -o case.ll
opt -passes="mem2reg" -S case.ll -o unopt.ll
opt -passes=simplifycfg -S unopt.ll -o opt.ll

clang unopt.ll -o src.out
clang opt.ll -o tgt.out

I put links to the full unopt.ll and opt.ll here: Full IR Files.

Debugging Script

#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

for exe in src.out tgt.out; do
  echo "== $exe =="
  lldb --batch \
    -o 'breakpoint set --file case.c --line 18' \
    -o run \
    -o 'frame variable l' \
    -- "./$exe"
  echo
done

Actual Behavior

Before simplifycfg, LLDB sees the expected value:

== src.out ==
Breakpoint 1: where = src.out`func_1 + 83 at case.c:18:5

frame #0: ... src.out`func_1 at case.c:18:5
   18      return 0;

(lldb) frame variable l
(short) l = 0

After simplifycfg, LLDB sees the stale initial value:

== tgt.out ==
Breakpoint 1: where = tgt.out`func_1 + 60 at case.c:19:1

frame #0: ... tgt.out`func_1 at case.c:19:1
   18      return 0;
-> 19  }

(lldb) frame variable l
(short) l = -1

Expected Behavior

At the final return 0;, LLDB should report l = 0, matching the source-level assignment if ((l = 0)).

If the assignment value cannot be preserved through the transformation, the variable should be reported as unavailable/optimized out for the affected range. It should not keep showing the stale initializer value -1.

Relevant Debug Info Clue

Before simplifycfg, unopt.ll contains a debug value update for l = 0:

#dbg_value(i16 0, !14, !DIExpression(), !16)

After simplifycfg, that update is gone and only the initial debug value for l = -1 remains:

#dbg_value(i16 -1, !14, !DIExpression(), !16)

This suggests that when simplifycfg removes the block containing the l = 0 debug update, it fails to preserve/salvage the source variable value for the remaining reachable code.

Environment

clang version 23.0.0git
llvm-project revision: 58203500b5bd1628401c1a103eb264b7d78ea3bb

lldb version 23.0.0git

Notes

I noticed a previous issue #55115 also reported this bug of simplifycfg dropping a debug value update. In that case, the debug value dropping causes the variable unavailable, but in this case, the variable is still available but shows a stale value.

By the way, simplification<O1/O2/O3> reuses simplifycfg and this bug can also be reproduced through those pipelines with my case.c.

MrEven132 avatar Jul 20 '26 08:07 MrEven132

@llvm/issue-subscribers-debuginfo

Author: MrEven132

## Description

simplifycfg deletes a basic block that contains a #dbg_value update for a source variable. After that transformation, the optimized binary still has valid program behavior, but the debug info for variable l becomes stale.

In the source program, l is initialized to -1 and later assigned 0 in if ((l = 0)). At the final return 0;, LLDB should see l == 0. Before simplifycfg, LLDB reports 0; after simplifycfg, LLDB reports -1.

This also reproduces through function-simplification&lt;O1/O2/O3&gt;, since that pipeline reuses simplifycfg.

Reproducer

case.c:

signed char func_1()
{
    short l = -1;

    for (int i = 0; i &lt; 1; i++)
        ;

    if ((l = 0))
    {
        return 1;
    }
    else
    {
        for (int i = 0; i &lt; 1; i++)
            ;
    }

    return 0;
}

int main()
{
    func_1();
    return 0;
}

Build pipeline:

clang -g -O0 -Xclang -disable-O0-optnone -emit-llvm -S case.c -o case.ll
opt -passes="mem2reg" -S case.ll -o unopt.ll
opt -passes=simplifycfg -S unopt.ll -o opt.ll

clang unopt.ll -o src.out
clang opt.ll -o tgt.out

I put links to the full unopt.ll and opt.ll here: Full IR Files.

Debugging Script

#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

for exe in src.out tgt.out; do
  echo "== $exe =="
  lldb --batch \
    -o 'breakpoint set --file case.c --line 18' \
    -o run \
    -o 'frame variable l' \
    -- "./$exe"
  echo
done

Actual Behavior

Before simplifycfg, LLDB sees the expected value:

== src.out ==
Breakpoint 1: where = src.out`func_1 + 83 at case.c:18:5

frame #<!-- -->0: ... src.out`func_1 at case.c:18:5
   18      return 0;

(lldb) frame variable l
(short) l = 0

After simplifycfg, LLDB sees the stale initial value:

== tgt.out ==
Breakpoint 1: where = tgt.out`func_1 + 60 at case.c:19:1

frame #<!-- -->0: ... tgt.out`func_1 at case.c:19:1
   18      return 0;
-&gt; 19  }

(lldb) frame variable l
(short) l = -1

Expected Behavior

At the final return 0;, LLDB should report l = 0, matching the source-level assignment if ((l = 0)).

If the assignment value cannot be preserved through the transformation, the variable should be reported as unavailable/optimized out for the affected range. It should not keep showing the stale initializer value -1.

Relevant Debug Info Clue

Before simplifycfg, unopt.ll contains a debug value update for l = 0:

#dbg_value(i16 0, !14, !DIExpression(), !16)

After simplifycfg, that update is gone and only the initial debug value for l = -1 remains:

#dbg_value(i16 -1, !14, !DIExpression(), !16)

This suggests that when simplifycfg removes the block containing the l = 0 debug update, it fails to preserve/salvage the source variable value for the remaining reachable code.

Environment

clang version 23.0.0git
llvm-project revision: 58203500b5bd1628401c1a103eb264b7d78ea3bb
Target: x86_64-unknown-linux-gnu

lldb version 23.0.0git
Linux x86_64

Notes

I noticed a previous issue #55115 also reported this bug of simplifycfg dropping a debug value update. In that case, the debug value dropping causes the variable unavailable, but in this case, the variable is still available but shows a stale value.

By the way, simplification&lt;O1/O2/O3&gt; reuses simplifycfg and this bug can also be reproduced through those pipelines with my case.c.

Thanks for the comprehensive bug report! I think you're correct and this is another situation where the problem in #55115 appears: in your IR at the godbolt link, blocks 6 and 8 will be folded together, but the resulting block will then be deleted to form a critical edge between block 1 and block 9. There's no way to represent that in LLVMs debug-info today, alas. The difference in symptoms is a small variation: one variable-assignment record goes missing, but in your reproducer there are other records and the variable stays alive (with the wrong value). Wheras in #55115 all the variable assignment records are gone.

Thinking long term, over in https://discourse.llvm.org/t/rfc-refactoring-dilocation-to-use-compact-function-local-storage/85618 @SLTozer is proposing the creation of function-local metadata, and I think somewhere else there's speculation about block-local metadata. If that existed, we'd then be able to model this situation better in LLVMs debug-info. (This probably a long time away though).

jmorse avatar Jul 21 '26 14:07 jmorse