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

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

Open yqtian-se opened this issue 2 months ago • 6 comments

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:

$ 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 issue-reproducer.sh

yqtian-se avatar Sep 18 '26 14:09 yqtian-se

@llvm/issue-subscribers-debuginfo

Author: Yongqiang Tian (yqtian-se)

## 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:

$ 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 issue-reproducer.sh

@llvm/issue-subscribers-backend-aarch64

Author: Yongqiang Tian (yqtian-se)

## 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:

$ 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 issue-reproducer.sh

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Yongqiang Tian (yqtian-se)

## 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:

$ 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 issue-reproducer.sh

Hi, I would like to work it. Can you please assign it to me. I had started looking into similar issue which looks like is solved now.

priyatam19 avatar Oct 01 '26 16:10 priyatam19

Hello, I would like to work on this issue. Its not assigned to anyone, so I'll start looking into it and prepare a PR.

adiroy28852 avatar Oct 05 '26 02:10 adiroy28852