[WebAssembly] Nullify dangling register DBG_VALUEs
authorHeejin Ahn <aheejin@gmail.com>
Wed, 30 Nov 2022 23:37:29 +0000 (15:37 -0800)
committerHeejin Ahn <aheejin@gmail.com>
Fri, 9 Dec 2022 18:59:08 +0000 (10:59 -0800)
commit981a28f7ae2cb99f75ca234b05f2c75ad3a356d7
tree0b50ea782756bd97be8c2ea4d46b8428b9b54471
parent9c5877f33d9f7fd317c07920fd24a041ad69077a
[WebAssembly] Nullify dangling register DBG_VALUEs

Register-based `DBG_VALUE` should have been
converted into either local-based or stack-operand-based, so at this
point they don't contain any information.

This CL nullifies them, i.e., turning them info
`DBG_VALUE $noreg, $noreg, ...`, which makes the variable appear as
"optimized out". It is not safe to simply remove these instruction
because at this point, because the fact that there is a `DBG_VALUE` here
means the location this variable resides has changed to somewhere else;
we've lost that information where that was.

---

The below is not really about the CL itself, but a pre-existing bug in
`llvm-locstats` variable coverage we are going to be affected after this
CL. Feel free to skip if you don't have time.

This CL has an unexpected side effect of increasing variable coverage
reported by `llvm-locstats`, due to a bug mentioned in
https://lists.llvm.org/pipermail/llvm-dev/2021-January/148139.html.
Currently inlined functions' formal parameters that don't have any
coverage, including those who only have `DBG_VALUE $noreg, $noreg`s
associated with them, don't generate `DW_TAG_formal_parameter` DIEs, and
they don't get into consideration in `llvm-dwarf --statistics` and
`llvm-locstats`. This is a known bug mentioned in
https://lists.llvm.org/pipermail/llvm-dev/2021-January/148139.html. For
example, this is a snippet of `llvm-dwarfdump` output of `wc`, where
`isword` is inlined into another function. `isword` has a formal
parameter `c`, which doesn't have any coverage in this inlined area, so
its `DW_TAG_formal_parameter` was not generated:
```
0x0000018d:     DW_TAG_inlined_subroutine
                  DW_AT_abstract_origin  (0x0000012c "isword")
                  DW_AT_low_pc  (0x00000166)
                  DW_AT_high_pc  (0x0000016d)
                  DW_AT_call_file  ("/usr/local/google/home/aheejin/test/dwarf-verify/wc/wc.c")
                  DW_AT_call_line  (100)
                  DW_AT_call_column  (0x0a)
```

But our dangling-register-based formal parameters currently generate
`DW_TAG_formal_parameter` DIEs, even though they don't have any
meaningful coverage, and this happened to generate correct statistics,
because it correctly sees this `c` doesn't have any coverage in this
inlined area. So our current `llvm-dwarfdump` (before this CL) is like:
```
0x0000018d:     DW_TAG_inlined_subroutine
                  DW_AT_abstract_origin  (0x0000012c "isword")
                  DW_AT_low_pc  (0x00000166)
                  DW_AT_high_pc  (0x0000016d)
                  DW_AT_call_file  ("/usr/local/google/home/aheejin/test/dwarf-verify/wc/wc.c")
                  DW_AT_call_line  (100)
                  DW_AT_call_column  (0x0a)

0x0000019a:       DW_TAG_formal_parameter
                    DW_AT_abstract_origin  (0x00000134 "c")
```
Note that this `DW_TAG_formal_parameter` doesn't have any
`DW_AT_location` attribute under it, meaning it doesn't have any
coverage.

On the other hand, if the `DW_TAG_formal_parameter` is not generated,
`llvm-dwarfdump --statistics` wouldn't even know about the parameter's
existence,  and doesn't include it in the coverage computation, making
the overall coverage (incorrectly) go up.

`DBG_VALUE $noreg, $noreg` used to generate this empty
`DW_TAG_formal_parameter`, but it changed for consistency in D95617.

It looks this bug in `llvm-dwarf --statistics` (and `llvm-locstats`,
which uses `llvm-dwarf --statistics`) has not been fixed so far. So we
get the coverage that's little incorrectly higher than our actual
coverage. But this bug apparently affects every target in LLVM.

Reviewed By: dschuff

Differential Revision: https://reviews.llvm.org/D139675
llvm/lib/Target/WebAssembly/WebAssemblyDebugFixup.cpp
llvm/test/DebugInfo/WebAssembly/remove-reg-dbg-value.mir [new file with mode: 0644]