llvm / llvm/llvm-project

[GlobalISel] Ignore debug instructions in the trunc-store search budget

Open
#224,660 1 comment 0 reactions 0 assignees View on GitHub
debuginfo llvm:globalisel
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

Compiling the same C source with `-g0` and `-g` can produce different AArch64
machine code under GlobalISel. Five source labels lower to five `DBG_LABEL`
instructions between two byte stores. They consume the fixed backward-search
budget in `LoadStoreOptImpl::mergeTruncStore`, preventing a store merge that
succeeds without debug information.

This reproduces with an assertions-enabled LLVM 24.0.0git build at revision
`54a71b0d38b63fc6df48fde0dc5ffd84b9f12a50` on x86-64 Linux. The backend is
run for AArch64 at `-O2` with GlobalISel, abort-on-fallback, and machine
verification enabled.

## Reproducer

Attach `source.c` and `issue-reproducer.sh`, then run:

```console
$ bash issue-reproducer.sh
g0: pre-DBG_LABEL=0 post-G_STORE=1 text-bytes=8
debug: pre-DBG_LABEL=5 post-G_STORE=2 text-bytes=16
```

## Actual behavior

Immediately before `load-store-opt`, the `-g0` and `-g` MachineFunctions have
the same ordinary instructions. The debug build has five additional
`DBG_LABEL` instructions. The `-g0` build merges the two byte stores into one
16-bit store and emits 8 bytes of `.text`; the debug build retains two byte
stores and emits 16 bytes.

A calibrated C-source ladder confirms the boundary: four labels still permit
the merge, while five do not. Placing the same five labels after both stores
also permits the merge.

## Cause

`LoadStoreOptImpl::mergeTruncStore` increments `NumInstsChecked` for every raw
`MachineInstr` encountered during a backward search with
`MaxInstsToCheck = 10`. Debug instructions therefore consume a budget intended
to bound the ordinary code searched.

I have a candidate patch that skips debug instructions before incrementing the
counter. It continues to count every non-debug instruction, including pseudo
probes.

## Expected behavior

Debug-only instructions should not affect the truncating-store search budget.
The `-g0` and `-g` variants should make the same merge decision and emit the
same ordinary machine code.

[source.c](https://github.com/user-attachments/files/32385843/source.c)
[issue-reproducer.sh](https://github.com/user-attachments/files/32385844/issue-reproducer.sh)

Contributor guide

Open the contributing guide

Research direction

Start at LoadStoreOptImpl::mergeTruncStore and inspect how the backward search updates NumInstsChecked for debug and non-debug MachineInstrs. Run issue-reproducer.sh with source.c to confirm the -g0 and -g difference. Done when debug-only instructions no longer consume the search budget and both variants make the same merge decision and emit the same ordinary machine code.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.