Issue 242730
Summary [LoongArch] Debug uses block offset folding in LoongArchMergeBaseOffset
Labels debuginfo, new issue, backend:loongarch
Assignees
Reporter yqtian-se
    With `-g`, `LoongArchMergeBaseOffset` does not fold a constant offset into the
`pcalau12i`/`addi.d` pair, so the code differs from `-g0`.

Reproducer (`input.c`):

```c
__attribute__((visibility("hidden"))) extern char a[100];
char *f(void){ char *p = a; return p+5; }
```

```sh
clang --target=loongarch64 -O2 -g  -c input.c -o g.o
clang --target=loongarch64 -O2 -g0 -c input.c -o g0.o
llvm-objdump -d --no-show-raw-insn -j .text g.o > g.txt
llvm-objdump -d --no-show-raw-insn -j .text g0.o > g0.txt
diff g0.txt g.txt
```

Actual (`-g0` vs `-g`, LLVM main `9ce8918945cf`):

```diff
-	pcalau12i	$a0, %pc_hi20(a+5)
-	addi.d	$a0, $a0, %pc_lo12(a+5)
+	pcalau12i	$a0, %pc_hi20(a)
+	addi.d	$a0, $a0, %pc_lo12(a)
+	addi.d	$a0, $a0, 5
```

Expected: the same code with and without `-g`.

**Possible cause.** `detectAndFoldOffset`
(https://github.com/llvm/llvm-project/blob/9ce8918945cf/llvm/lib/Target/LoongArch/LoongArchMergeBaseOffset.cpp#L453) folds the
following `ADDI` only if the address register has one use, checked with
`hasOneUse()`. The `DBG_VALUE` of `p` counts as a second use.
Note that counting only non-debug uses (`hasOneNonDBGUse()` and
`use_instr_nodbg_begin()`) removes the difference, but it is not enough on its
own: after the fold the register holds `a+5`, so the `DBG_VALUE` would show `p`
as `a+5`. `RISCVMergeBaseOffset::foldOffset` already handles this by appending
the negated offset to the debug users' expressions. Perhaps LoongArch could
use a similar fix.

Related: #221117, #230290 and #231718 fixed other merge-base-offset paths;
open PRs #242356 and #242357 change `foldLargeOffset`, not this check.

This issue is assisted by Claude and reviewed by yqtian-se.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to