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

[AArch64][GlobalISel] Debug information changes mul-by-constant code generation at -O1

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

On AArch64 at -O1, adding debug information changes how GlobalISel lowers a multiplication by six followed by an addition. The -g build selects two shifted adds, while the -g0 build selects an immediate move and madd. SelectionDAG produces identical code with and without debug information.

This reproduces with an assertions-enabled LLVM 24.0.0git build at revision aa797754ed4f on x86-64 Linux, targeting AArch64. All four compilations also pass -verify-machineinstrs.

Reproducer

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

issue-reproducer.sh reduced-source.c

$ bash issue-reproducer.sh
c03da3f7254f2a95d22bda37243d0a57a785ed2d7b8dbb551bb57e202e1a6197  .../gisel.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../gisel.g0.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g0.text
GlobalISel: -g and -g0 .text DIFFER
SelectionDAG: -g and -g0 .text equal

The source is:

long f(long x, long y) {
  long product = x * 6;
  return product + y;
}

Actual behavior

GlobalISel emits the following ordinary instructions with -g:

add x8, x0, x0, lsl #1
add x0, x1, x8, lsl #1
ret

With -g0, it emits:

mov  w8, #6
madd x0, x0, x8, x1
ret

The ordinary Machine IR is equal before aarch64-postlegalizer-combiner. In the debug build, a DBG_VALUE for the multiply result appears before its sole ordinary G_ADD user. The first ordinary difference appears in that combiner: the multiply is expanded into shift/add operations only in the debug build.

Cause analysis

matchAArch64MulConstCombine() in llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerCombiner.cpp first uses MRI.hasOneNonDBGUse(Dst) to establish that the multiply result has one ordinary use. It then retrieves that use with MRI.use_instr_begin(Dst), which does not skip debug instructions. When the first use is the DBG_VALUE, the following opcode check sees a debug opcode instead of the ordinary G_ADD, so the guard against expanding a multiply that may fold into madd is bypassed.

As a causal check, I changed only the lookup to use MRI.use_nodbg_instructions(Dst). With otherwise matched compiler builds, that change makes the -g output equal to -g0 for the source above and also restores equality for the reduced verifier-valid MIR trigger, its $noreg control, and its debug-free projection.

This is distinct from #227628. That issue originates in AArch64InstructionSelector::selectDebugInstr() at -O0, where a DBG_VALUE changes a virtual register's register class. This issue originates in the post-legalizer combiner at -O1, where a raw use iterator returns a debug use after a non-debug use-count query.

Expected behavior

The combine should inspect the sole non-debug use established by hasOneNonDBGUse(). A DBG_VALUE should not change whether the multiply is kept for folding into madd, and the -g and -g0 builds should produce the same ordinary machine code.

yqtian-se avatar Sep 30 '26 10:09 yqtian-se

@llvm/issue-subscribers-debuginfo

Author: Yongqiang Tian (yqtian-se)

On AArch64 at `-O1`, adding debug information changes how GlobalISel lowers a multiplication by six followed by an addition. The `-g` build selects two shifted adds, while the `-g0` build selects an immediate move and `madd`. SelectionDAG produces identical code with and without debug information.

This reproduces with an assertions-enabled LLVM 24.0.0git build at revision aa797754ed4f on x86-64 Linux, targeting AArch64. All four compilations also pass -verify-machineinstrs.

Reproducer

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

issue-reproducer.sh reduced-source.c

$ bash issue-reproducer.sh
c03da3f7254f2a95d22bda37243d0a57a785ed2d7b8dbb551bb57e202e1a6197  .../gisel.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../gisel.g0.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g0.text
GlobalISel: -g and -g0 .text DIFFER
SelectionDAG: -g and -g0 .text equal

The source is:

long f(long x, long y) {
  long product = x * 6;
  return product + y;
}

Actual behavior

GlobalISel emits the following ordinary instructions with -g:

add x8, x0, x0, lsl #<!-- -->1
add x0, x1, x8, lsl #<!-- -->1
ret

With -g0, it emits:

mov  w8, #<!-- -->6
madd x0, x0, x8, x1
ret

The ordinary Machine IR is equal before aarch64-postlegalizer-combiner. In the debug build, a DBG_VALUE for the multiply result appears before its sole ordinary G_ADD user. The first ordinary difference appears in that combiner: the multiply is expanded into shift/add operations only in the debug build.

Cause analysis

matchAArch64MulConstCombine() in llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerCombiner.cpp first uses MRI.hasOneNonDBGUse(Dst) to establish that the multiply result has one ordinary use. It then retrieves that use with MRI.use_instr_begin(Dst), which does not skip debug instructions. When the first use is the DBG_VALUE, the following opcode check sees a debug opcode instead of the ordinary G_ADD, so the guard against expanding a multiply that may fold into madd is bypassed.

As a causal check, I changed only the lookup to use MRI.use_nodbg_instructions(Dst). With otherwise matched compiler builds, that change makes the -g output equal to -g0 for the source above and also restores equality for the reduced verifier-valid MIR trigger, its $noreg control, and its debug-free projection.

This is distinct from #227628. That issue originates in AArch64InstructionSelector::selectDebugInstr() at -O0, where a DBG_VALUE changes a virtual register's register class. This issue originates in the post-legalizer combiner at -O1, where a raw use iterator returns a debug use after a non-debug use-count query.

Expected behavior

The combine should inspect the sole non-debug use established by hasOneNonDBGUse(). A DBG_VALUE should not change whether the multiply is kept for folding into madd, and the -g and -g0 builds should produce the same ordinary machine code.

@llvm/issue-subscribers-backend-aarch64

Author: Yongqiang Tian (yqtian-se)

On AArch64 at `-O1`, adding debug information changes how GlobalISel lowers a multiplication by six followed by an addition. The `-g` build selects two shifted adds, while the `-g0` build selects an immediate move and `madd`. SelectionDAG produces identical code with and without debug information.

This reproduces with an assertions-enabled LLVM 24.0.0git build at revision aa797754ed4f on x86-64 Linux, targeting AArch64. All four compilations also pass -verify-machineinstrs.

Reproducer

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

issue-reproducer.sh reduced-source.c

$ bash issue-reproducer.sh
c03da3f7254f2a95d22bda37243d0a57a785ed2d7b8dbb551bb57e202e1a6197  .../gisel.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../gisel.g0.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g0.text
GlobalISel: -g and -g0 .text DIFFER
SelectionDAG: -g and -g0 .text equal

The source is:

long f(long x, long y) {
  long product = x * 6;
  return product + y;
}

Actual behavior

GlobalISel emits the following ordinary instructions with -g:

add x8, x0, x0, lsl #<!-- -->1
add x0, x1, x8, lsl #<!-- -->1
ret

With -g0, it emits:

mov  w8, #<!-- -->6
madd x0, x0, x8, x1
ret

The ordinary Machine IR is equal before aarch64-postlegalizer-combiner. In the debug build, a DBG_VALUE for the multiply result appears before its sole ordinary G_ADD user. The first ordinary difference appears in that combiner: the multiply is expanded into shift/add operations only in the debug build.

Cause analysis

matchAArch64MulConstCombine() in llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerCombiner.cpp first uses MRI.hasOneNonDBGUse(Dst) to establish that the multiply result has one ordinary use. It then retrieves that use with MRI.use_instr_begin(Dst), which does not skip debug instructions. When the first use is the DBG_VALUE, the following opcode check sees a debug opcode instead of the ordinary G_ADD, so the guard against expanding a multiply that may fold into madd is bypassed.

As a causal check, I changed only the lookup to use MRI.use_nodbg_instructions(Dst). With otherwise matched compiler builds, that change makes the -g output equal to -g0 for the source above and also restores equality for the reduced verifier-valid MIR trigger, its $noreg control, and its debug-free projection.

This is distinct from #227628. That issue originates in AArch64InstructionSelector::selectDebugInstr() at -O0, where a DBG_VALUE changes a virtual register's register class. This issue originates in the post-legalizer combiner at -O1, where a raw use iterator returns a debug use after a non-debug use-count query.

Expected behavior

The combine should inspect the sole non-debug use established by hasOneNonDBGUse(). A DBG_VALUE should not change whether the multiply is kept for folding into madd, and the -g and -g0 builds should produce the same ordinary machine code.

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)

On AArch64 at `-O1`, adding debug information changes how GlobalISel lowers a multiplication by six followed by an addition. The `-g` build selects two shifted adds, while the `-g0` build selects an immediate move and `madd`. SelectionDAG produces identical code with and without debug information.

This reproduces with an assertions-enabled LLVM 24.0.0git build at revision aa797754ed4f on x86-64 Linux, targeting AArch64. All four compilations also pass -verify-machineinstrs.

Reproducer

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

issue-reproducer.sh reduced-source.c

$ bash issue-reproducer.sh
c03da3f7254f2a95d22bda37243d0a57a785ed2d7b8dbb551bb57e202e1a6197  .../gisel.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../gisel.g0.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g.text
46092834bc51797e841663ec2fe62860b5fcb9dc8fbe7fd66a30fe17548cdae7  .../sdag.g0.text
GlobalISel: -g and -g0 .text DIFFER
SelectionDAG: -g and -g0 .text equal

The source is:

long f(long x, long y) {
  long product = x * 6;
  return product + y;
}

Actual behavior

GlobalISel emits the following ordinary instructions with -g:

add x8, x0, x0, lsl #<!-- -->1
add x0, x1, x8, lsl #<!-- -->1
ret

With -g0, it emits:

mov  w8, #<!-- -->6
madd x0, x0, x8, x1
ret

The ordinary Machine IR is equal before aarch64-postlegalizer-combiner. In the debug build, a DBG_VALUE for the multiply result appears before its sole ordinary G_ADD user. The first ordinary difference appears in that combiner: the multiply is expanded into shift/add operations only in the debug build.

Cause analysis

matchAArch64MulConstCombine() in llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerCombiner.cpp first uses MRI.hasOneNonDBGUse(Dst) to establish that the multiply result has one ordinary use. It then retrieves that use with MRI.use_instr_begin(Dst), which does not skip debug instructions. When the first use is the DBG_VALUE, the following opcode check sees a debug opcode instead of the ordinary G_ADD, so the guard against expanding a multiply that may fold into madd is bypassed.

As a causal check, I changed only the lookup to use MRI.use_nodbg_instructions(Dst). With otherwise matched compiler builds, that change makes the -g output equal to -g0 for the source above and also restores equality for the reduced verifier-valid MIR trigger, its $noreg control, and its debug-free projection.

This is distinct from #227628. That issue originates in AArch64InstructionSelector::selectDebugInstr() at -O0, where a DBG_VALUE changes a virtual register's register class. This issue originates in the post-legalizer combiner at -O1, where a raw use iterator returns a debug use after a non-debug use-count query.

Expected behavior

The combine should inspect the sole non-debug use established by hasOneNonDBGUse(). A DBG_VALUE should not change whether the multiply is kept for folding into madd, and the -g and -g0 builds should produce the same ordinary machine code.

Hey, I'd like to work on this issue. I'll start looking into it and prepare a PR.

GopinathBalaji avatar Sep 30 '26 11:09 GopinathBalaji

Hi, I'd like to work on this issue. As it's a good first issue, I believe it could be a great learning opportunity for me. Can you please assign it to me.

priyatam19 avatar Sep 30 '26 15:09 priyatam19