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

[AArch64] DBG_VALUE at successor entry prevents post-index store formation

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

A DBG_VALUE at the beginning of a successor block can prevent the AArch64 load/store optimizer from forming a post-index addressing mode. As a result, compiling the same source with -g emits an additional add instruction.

This reproduces with LLVM 24.0.0git at revision 2fd31faf6ec53ad7fc0c92355856367b9c5f3ad8 and remains present on main at a32be176a59c306f98677cf6e44a7289b5ae7508.

Reproducer

https://godbolt.org/z/6zvqW51aK https://godbolt.org/z/KdaevnPE3

void h(int *);

static inline void put_next(int *q) {
  h(q + 1);
}

void f(int *p, int v, int c) {
  *p = v;
  if (c)
    put_next(p);
}

Compile with and without debug information:

clang --target=aarch64-linux-gnu -O2 -S input.c -o no-debug.s
clang --target=aarch64-linux-gnu -O2 -g -S input.c -o debug.s

The difference is also present at -O1, -O3, and -Os.

Actual behavior

Without -g, the store and pointer update are combined:

f:
        str     w1, [x0], #4
        cbz     w2, .LBB0_2
        b       h
.LBB0_2:
        ret

With -g, the pointer update remains separate:

f:
        str     w1, [x0]
        cbz     w2, .LBB0_2
        add     x0, x0, #4
        b       h
.LBB0_2:
        ret

Immediately before aarch64-ldst-opt, the ordinary Machine IR is equivalent. The debug build additionally has a DBG_VALUE $x0 at the beginning of the successor block.

This is not wrong code, but it makes debug information affect code size and performance. The difference can occur inside loops.

Cause

AArch64LoadStoreOpt::findMatchingUpdateInsnForward() skips debug instructions while scanning within a basic block. When it continues the search into a single-predecessor successor, however, it restarts at the raw beginning of that block:

CurMBB = SuccToVisit;
MBBI = CurMBB->begin();

If the first instruction is a DBG_VALUE or DBG_PHI referring to the base register, LiveRegUnits::accumulateUsedDefed() records the debug operand as a register use. The subsequent availability check then stops the update search.

Starting the successor scan with skipDebugInstructionsForward(CurMBB->begin(), CurMBB->end()) makes the debug and non-debug variants generate the same code and is consistent with the existing in-block scan.

The cross-block successor search was introduced by PR #145583. Existing issue #36619 concerns load pairing within one block and appears to be a different site and mechanism.

Expected behavior

Debug-only instructions should not prevent formation of the indexed memory operation. The -g and non--g variants should both form the post-index store.

repro.sh

min.mir.txt

yqtian-se avatar Oct 06 '26 04:10 yqtian-se

@llvm/issue-subscribers-backend-aarch64

Author: Yongqiang Tian (yqtian-se)

A `DBG_VALUE` at the beginning of a successor block can prevent the AArch64 load/store optimizer from forming a post-index addressing mode. As a result, compiling the same source with `-g` emits an additional `add` instruction.

This reproduces with LLVM 24.0.0git at revision 2fd31faf6ec53ad7fc0c92355856367b9c5f3ad8 and remains present on main at a32be176a59c306f98677cf6e44a7289b5ae7508.

Reproducer

void h(int *);

static inline void put_next(int *q) {
  h(q + 1);
}

void f(int *p, int v, int c) {
  *p = v;
  if (c)
    put_next(p);
}

Compile with and without debug information:

clang --target=aarch64-linux-gnu -O2 -S input.c -o no-debug.s
clang --target=aarch64-linux-gnu -O2 -g -S input.c -o debug.s

The difference is also present at -O1, -O3, and -Os.

Actual behavior

Without -g, the store and pointer update are combined:

f:
        str     w1, [x0], #<!-- -->4
        cbz     w2, .LBB0_2
        b       h
.LBB0_2:
        ret

With -g, the pointer update remains separate:

f:
        str     w1, [x0]
        cbz     w2, .LBB0_2
        add     x0, x0, #<!-- -->4
        b       h
.LBB0_2:
        ret

Immediately before aarch64-ldst-opt, the ordinary Machine IR is equivalent. The debug build additionally has a DBG_VALUE $x0 at the beginning of the successor block.

This is not wrong code, but it makes debug information affect code size and performance. The difference can occur inside loops.

Cause

AArch64LoadStoreOpt::findMatchingUpdateInsnForward() skips debug instructions while scanning within a basic block. When it continues the search into a single-predecessor successor, however, it restarts at the raw beginning of that block:

CurMBB = SuccToVisit;
MBBI = CurMBB-&gt;begin();

If the first instruction is a DBG_VALUE or DBG_PHI referring to the base register, LiveRegUnits::accumulateUsedDefed() records the debug operand as a register use. The subsequent availability check then stops the update search.

Starting the successor scan with skipDebugInstructionsForward(CurMBB-&gt;begin(), CurMBB-&gt;end()) makes the debug and non-debug variants generate the same code and is consistent with the existing in-block scan.

The cross-block successor search was introduced by PR #145583. Existing issue #36619 concerns load pairing within one block and appears to be a different site and mechanism.

Expected behavior

Debug-only instructions should not prevent formation of the indexed memory operation. The -g and non--g variants should both form the post-index store.

repro.sh

min.mir.txt

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)

A `DBG_VALUE` at the beginning of a successor block can prevent the AArch64 load/store optimizer from forming a post-index addressing mode. As a result, compiling the same source with `-g` emits an additional `add` instruction.

This reproduces with LLVM 24.0.0git at revision 2fd31faf6ec53ad7fc0c92355856367b9c5f3ad8 and remains present on main at a32be176a59c306f98677cf6e44a7289b5ae7508.

Reproducer

void h(int *);

static inline void put_next(int *q) {
  h(q + 1);
}

void f(int *p, int v, int c) {
  *p = v;
  if (c)
    put_next(p);
}

Compile with and without debug information:

clang --target=aarch64-linux-gnu -O2 -S input.c -o no-debug.s
clang --target=aarch64-linux-gnu -O2 -g -S input.c -o debug.s

The difference is also present at -O1, -O3, and -Os.

Actual behavior

Without -g, the store and pointer update are combined:

f:
        str     w1, [x0], #<!-- -->4
        cbz     w2, .LBB0_2
        b       h
.LBB0_2:
        ret

With -g, the pointer update remains separate:

f:
        str     w1, [x0]
        cbz     w2, .LBB0_2
        add     x0, x0, #<!-- -->4
        b       h
.LBB0_2:
        ret

Immediately before aarch64-ldst-opt, the ordinary Machine IR is equivalent. The debug build additionally has a DBG_VALUE $x0 at the beginning of the successor block.

This is not wrong code, but it makes debug information affect code size and performance. The difference can occur inside loops.

Cause

AArch64LoadStoreOpt::findMatchingUpdateInsnForward() skips debug instructions while scanning within a basic block. When it continues the search into a single-predecessor successor, however, it restarts at the raw beginning of that block:

CurMBB = SuccToVisit;
MBBI = CurMBB-&gt;begin();

If the first instruction is a DBG_VALUE or DBG_PHI referring to the base register, LiveRegUnits::accumulateUsedDefed() records the debug operand as a register use. The subsequent availability check then stops the update search.

Starting the successor scan with skipDebugInstructionsForward(CurMBB-&gt;begin(), CurMBB-&gt;end()) makes the debug and non-debug variants generate the same code and is consistent with the existing in-block scan.

The cross-block successor search was introduced by PR #145583. Existing issue #36619 concerns load pairing within one block and appears to be a different site and mechanism.

Expected behavior

Debug-only instructions should not prevent formation of the indexed memory operation. The -g and non--g variants should both form the post-index store.

repro.sh

min.mir.txt

Can i try this

anandkvinu avatar Oct 06 '26 07:10 anandkvinu

i have reproduced the bug and started to work on this.

anandkvinu avatar Oct 06 '26 10:10 anandkvinu

the fix worked, adding regression tests now, will be opening pr soon.

anandkvinu avatar Oct 10 '26 16:10 anandkvinu

10. the fix worked, adding regression tests now, will be opening pr soon.

I found the same issue. do you have a fix for it?

orionsst avatar Oct 22 '26 10:10 orionsst

Unassigning @anandkvinu since it’s been a month without a PR, to make this good first issue open for others.

IvyXu420 avatar Nov 06 '26 06:11 IvyXu420

So can I try this?I think it's funny.

dianyuzi avatar Nov 06 '26 06:11 dianyuzi

Yeah,I will reproduce it and resolve it as soon as possible.

dianyuzi avatar Nov 07 '26 01:11 dianyuzi