[AArch64] DBG_VALUE at successor entry prevents post-index store formation
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.
@llvm/issue-subscribers-backend-aarch64
Author: Yongqiang Tian (yqtian-se)
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->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.
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:
- 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.
- 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.
- 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.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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)
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->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.
Can i try this
i have reproduced the bug and started to work on this.
the fix worked, adding regression tests now, will be opening pr soon.
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?
Unassigning @anandkvinu since it’s been a month without a PR, to make this good first issue open for others.
So can I try this?I think it's funny.
Yeah,I will reproduce it and resolve it as soon as possible.