| Issue |
240617
|
| Summary |
[CodeGen] DBG_VALUE inside a bundle prevents BranchFolding tail merging
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
yqtian-se
|
A debug instruction nested inside one `BUNDLE` makes `MachineInstr::isIdenticalTo` consider it different from an otherwise identical bundle. BranchFolding then finds a shorter common tail, so enabling `-g` keeps extra ordinary instructions.
This reproduces with LLVM checkout `0c2745ad307a`; the relevant generic CodeGen and ARM files matched `main` at the time of testing.
### Reproducer
<details>
<summary>Reduced C source (88 lines)</summary>
```c
struct rb_node {
unsigned long __rb_parent_color;
struct rb_node *rb_right;
struct rb_node *rb_left;
};
struct rb_root {
struct rb_node *rb_node;
};
static inline struct rb_node *rb_parent(struct rb_node *rb) {
return (struct rb_node *)(rb->__rb_parent_color & ~3UL);
}
static inline int rb_color(struct rb_node *rb) {
return rb->__rb_parent_color & 1;
}
static inline int rb_is_red(struct rb_node *rb) { return !rb_color(rb); }
static inline int rb_is_black(struct rb_node *rb) { return rb_color(rb); }
static inline void rb_set_parent_color(struct rb_node *rb, struct rb_node *p,
int color) {
rb->__rb_parent_color = (unsigned long)p + color;
}
static inline void rb_set_black(struct rb_node *rb) {
rb->__rb_parent_color += 1;
}
static inline void __rb_change_child(struct rb_node *old, struct rb_node *new,
struct rb_node *parent,
struct rb_root *root) {
if (parent && parent->rb_left == old)
root->rb_node = new;
}
static inline void __rb_rotate_set_parents(struct rb_node *old,
struct rb_node *new,
struct rb_root *root, int color) {
struct rb_node *parent = rb_parent(old);
rb_set_parent_color(old, new, color);
__rb_change_child(old, new, parent, root);
}
void erase_color(struct rb_node *parent, struct rb_root *root,
void (*augment_rotate)(struct rb_node *, struct rb_node *)) {
struct rb_node *node = 0, *sibling, *tmp1, *tmp2;
while (1) {
sibling = parent->rb_right;
if (node != sibling) {
tmp1 = sibling->rb_right;
if (!tmp1 || rb_is_black(tmp1)) {
tmp2 = sibling->rb_left;
if (!tmp2 || rb_is_black(tmp2)) {
if (rb_is_red(parent))
rb_set_black(parent);
}
augment_rotate(sibling, tmp2);
sibling = tmp2;
}
tmp2 = sibling->rb_left;
rb_set_parent_color(tmp1, sibling, 1);
{
struct rb_node *saved_parent = parent;
if (tmp2) {
unsigned long parent_color = rb_color(tmp2) + (unsigned long)parent;
parent = 0;
tmp2->__rb_parent_color = parent_color;
}
parent = saved_parent;
}
__rb_rotate_set_parents(parent, sibling, root, 1);
augment_rotate(parent, sibling);
break;
} else {
sibling = parent->rb_left;
tmp1 = sibling->rb_left;
if (!tmp1 || rb_is_black(tmp1)) {
tmp2 = sibling->rb_right;
if (!tmp2 || rb_is_black(tmp2)) {
if (rb_is_red(parent))
rb_set_black(parent);
}
augment_rotate(sibling, tmp2);
}
tmp2 = sibling->rb_right;
rb_set_parent_color(tmp1, sibling, 1);
if (tmp2) {
tmp2->__rb_parent_color = rb_color(tmp2) + (unsigned long)parent;
}
__rb_rotate_set_parents(parent, sibling, root, 1);
augment_rotate(parent, sibling);
break;
}
}
}
```
</details>
Save the source as `natural-repro.c`, then run:
```console
clang --target=arm-linux-gnueabi -mthumb -march=armv7-a -O2 -g \
-S -emit-llvm natural-repro.c -o debug.ll
clang --target=arm-linux-gnueabi -mthumb -march=armv7-a -O2 -g0 \
-S -emit-llvm natural-repro.c -o nodebug.ll
llc -O2 -relocation-model=static -filetype=obj debug.ll -o debug.o
llc -O2 -relocation-model=static -filetype=obj nodebug.ll -o nodebug.o
llvm-objdump -d --no-show-raw-insn debug.o
llvm-objdump -d --no-show-raw-insn nodebug.o
```
The `-g` object contains 82 ordinary instructions; the `-g0` object contains
76. If both `llc` commands are rerun with `-enable-tail-merge=false`, both
objects contain 90 ordinary instructions and their ordinary disassembly is
identical. This isolates the difference to tail merging rather than an earlier
code-generation difference.
The expected result is that the nested debug record does not prevent two
otherwise identical executable tails from being merged.
### Cause and fix scope
After Thumb2 if-conversion, the relevant instructions are members of an IT
bundle. BranchFolding skips standalone debug instructions, but
`MachineInstr::isIdenticalTo` compares the members of two bundles in lockstep,
including `DBG_VALUE`. The extra record in one bundle is therefore compared
with the next executable instruction in the other bundle, and the common-tail
search stops early.
Simply making `isIdenticalTo` ignore every nested debug instruction does fix
this reproducer, but it is not sufficient as a global fix:
- MachineOutliner can then outline the bundle while copying its nested
`DBG_VALUE` into the outlined function, contrary to its existing policy of
dropping debug information from outlined instructions.
- BranchFolding can select the bundle containing a path-specific debug record
as the shared tail, which would make that location unconditional.
I think a more complete fix would be to add an opt-in comparison mode that ignores nested debug instructions, while requiring each caller to handle the unmatched debug records appropriately. However, I am not sure whether adding this policy to `isIdenticalTo` is the right abstraction, or whether it could affect other callers in unexpected ways. I would appreciate your thoughts on the preferred direction.
This issue was found by a non-AI automated testing tool. AI assisted with the
analysis and reproducer reduction.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs