Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MCP] Skip invalidating def constant regs during forward propagation #111129

Merged
merged 3 commits into from
Oct 10, 2024

Conversation

vladimirradosavljevic
Copy link
Contributor

@vladimirradosavljevic vladimirradosavljevic commented Oct 4, 2024

Before this patch, redundant COPY couldn't be removed for the following case:

  %reg1 = COPY %const-reg
  ... // There is a def of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:

  ... // There is a def of %const-reg
  %reg2 = COPY %const-reg

This patch allows for such optimization by not invalidating defined constant registers. This is safe, as architectures like AArch64 and RISCV replace a dead definition of a GPR with a zero constant register for certain instructions.

Copy link

github-actions bot commented Oct 4, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Oct 4, 2024

@llvm/pr-subscribers-backend-aarch64

Author: Vladimir Radosavljevic (vladimirradosavljevic)

Changes

Before this patch, redundant COPY couldn't be removed for the following case:

  %reg1 = COPY %const-reg
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:

  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY %const-reg

This patch enables this by skipping invalidating constant regs, which should be safe even for defs since architectures like AArch64 and RISCV for some instructions are replacing a dead definition of a GPR with zero constant register.


Full diff: https://github.com/llvm/llvm-project/pull/111129.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineCopyPropagation.cpp (+4)
  • (added) llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir (+19)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 8bcc437cbfb865..d1f8b0b9729ce7 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -885,6 +885,10 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
       assert(!Reg.isVirtual() &&
              "MachineCopyPropagation should be run after register allocation!");
 
+      // Skip invalidating constant registers.
+      if (MRI->isReserved(Reg) && MRI->isConstantPhysReg(Reg))
+        continue;
+
       if (MO.isDef() && !MO.isEarlyClobber()) {
         Defs.push_back(Reg.asMCReg());
         continue;
diff --git a/llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir b/llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir
new file mode 100644
index 00000000000000..da4309a3d4772a
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir
@@ -0,0 +1,19 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass machine-cp -verify-machineinstrs -o - %s | FileCheck %s
+
+---
+name: test
+body: |
+  bb.0:
+    liveins: $w2
+    ; CHECK-LABEL: name: test
+    ; CHECK: liveins: $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: dead $wzr = SUBSWri killed renamable $w2, 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = COPY $wzr
+    ; CHECK-NEXT: RET_ReallyLR implicit killed $w0
+    renamable $w1 = COPY $wzr
+    dead $wzr = SUBSWri killed renamable $w2, 0, 0, implicit-def $nzcv
+    renamable $w0 = COPY killed renamable $w1
+    RET_ReallyLR implicit killed $w0
+...

@llvmbot
Copy link
Member

llvmbot commented Oct 4, 2024

@llvm/pr-subscribers-llvm-regalloc

Author: Vladimir Radosavljevic (vladimirradosavljevic)

Changes

Before this patch, redundant COPY couldn't be removed for the following case:

  %reg1 = COPY %const-reg
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:

  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY %const-reg

This patch enables this by skipping invalidating constant regs, which should be safe even for defs since architectures like AArch64 and RISCV for some instructions are replacing a dead definition of a GPR with zero constant register.


Full diff: https://github.com/llvm/llvm-project/pull/111129.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineCopyPropagation.cpp (+4)
  • (added) llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir (+19)
diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
index 8bcc437cbfb865..d1f8b0b9729ce7 100644
--- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp
@@ -885,6 +885,10 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
       assert(!Reg.isVirtual() &&
              "MachineCopyPropagation should be run after register allocation!");
 
+      // Skip invalidating constant registers.
+      if (MRI->isReserved(Reg) && MRI->isConstantPhysReg(Reg))
+        continue;
+
       if (MO.isDef() && !MO.isEarlyClobber()) {
         Defs.push_back(Reg.asMCReg());
         continue;
diff --git a/llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir b/llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir
new file mode 100644
index 00000000000000..da4309a3d4772a
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/machine-cp-constant-reg.mir
@@ -0,0 +1,19 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass machine-cp -verify-machineinstrs -o - %s | FileCheck %s
+
+---
+name: test
+body: |
+  bb.0:
+    liveins: $w2
+    ; CHECK-LABEL: name: test
+    ; CHECK: liveins: $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: dead $wzr = SUBSWri killed renamable $w2, 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = COPY $wzr
+    ; CHECK-NEXT: RET_ReallyLR implicit killed $w0
+    renamable $w1 = COPY $wzr
+    dead $wzr = SUBSWri killed renamable $w2, 0, 0, implicit-def $nzcv
+    renamable $w0 = COPY killed renamable $w1
+    RET_ReallyLR implicit killed $w0
+...

@vladimirradosavljevic
Copy link
Contributor Author

@qcolombet @arsenm @topperc PTAL.

vladimirradosavljevic added a commit to matter-labs/era-compiler-llvm that referenced this pull request Oct 9, 2024
Before this patch, redundant COPY couldn't be removed
for the following case:
  %reg1 = COPY %const-reg
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY %const-reg

This patch allows for such optimization by not
invalidating constant registers. This is safe
even where constant registers are defined, as
architectures like AArch64 and RISCV replace a
dead definition of a GPR with a zero constant
register for certain instructions.

Upstream PR:
llvm/llvm-project#111129

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
vladimirradosavljevic added a commit to matter-labs/era-compiler-llvm that referenced this pull request Oct 9, 2024
Before this patch, redundant COPY couldn't be removed
for the following case:
  %reg1 = COPY %const-reg
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY %const-reg

This patch allows for such optimization by not
invalidating constant registers. This is safe
even where constant registers are defined, as
architectures like AArch64 and RISCV replace a
dead definition of a GPR with a zero constant
register for certain instructions.

Upstream PR:
llvm/llvm-project#111129

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
akiramenai pushed a commit to matter-labs/era-compiler-llvm that referenced this pull request Oct 10, 2024
Before this patch, redundant COPY couldn't be removed
for the following case:
  %reg1 = COPY %const-reg
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:
  ... // No use of %reg1 but there is a def/use of %const-reg
  %reg2 = COPY %const-reg

This patch allows for such optimization by not
invalidating constant registers. This is safe
even where constant registers are defined, as
architectures like AArch64 and RISCV replace a
dead definition of a GPR with a zero constant
register for certain instructions.

Upstream PR:
llvm/llvm-project#111129

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Before this patch, redundant COPY couldn't be removed
for the following case:
  %reg1 = COPY %const-reg
  ... // There is a def of %const-reg
  %reg2 = COPY killed %reg1

where this can be optimized to:
  ... // There is a def of %const-reg
  %reg2 = COPY %const-reg

This patch allows for such optimization by not
invalidating defined constant registers. This is
safe, as architectures like AArch64 and RISCV
replace a dead definition of a GPR with a zero
constant register for certain instructions.
@vladimirradosavljevic vladimirradosavljevic changed the title [MCP] Skip invalidating constant regs during forward propagation [MCP] Skip invalidating def constant regs during forward propagation Oct 10, 2024
@vladimirradosavljevic
Copy link
Contributor Author

@arsenm Thanks for the review! Could you please merge this PR on my behalf?

@arsenm arsenm merged commit dabb0dd into llvm:main Oct 10, 2024
8 checks passed
Copy link

@vladimirradosavljevic Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
…lvm#111129)

Before this patch, redundant COPY couldn't be removed for the following
case:
```
  %reg1 = COPY %const-reg
  ... // There is a def of %const-reg
  %reg2 = COPY killed %reg1
```
where this can be optimized to:
```
  ... // There is a def of %const-reg
  %reg2 = COPY %const-reg
```

This patch allows for such optimization by not invalidating defined
constant registers. This is safe, as architectures like AArch64 and
RISCV replace a dead definition of a GPR with a zero constant register
for certain instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants