Skip to content

[InitUndef] handleSubReg should skip artificial subregs. #116248

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

Merged
merged 2 commits into from
Nov 14, 2024

Conversation

sdesmalen-arm
Copy link
Collaborator

When enabling subreg liveness tracking for AArch64, this pass fails because it tries to get the register class for the artificial subreg sub_32_hi of a 64-bit GPR. It tries to create an INIT_UNDEF instruction for the top 32-bits of the 64-bit GPR, which are not directly addressable, so getSubRegisterClass() returns a nullptr, crashing this pass.

It should instead just avoid trying to create the INIT_UNDEF instruction.

When enabling subreg liveness tracking for AArch64, this pass fails
because it tries to get the register class for the artificial subreg
`sub_32_hi` of a 64-bit GPR. It tries to create an INIT_UNDEF instruction
for the top 32-bits of the 64-bit GPR, which are not directly addressable,
so getSubRegisterClass() returns a nullptr, crashing this pass.

It should instead just avoid trying to create the INIT_UNDEF instruction.
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2024

@llvm/pr-subscribers-backend-aarch64

Author: Sander de Smalen (sdesmalen-arm)

Changes

When enabling subreg liveness tracking for AArch64, this pass fails because it tries to get the register class for the artificial subreg sub_32_hi of a 64-bit GPR. It tries to create an INIT_UNDEF instruction for the top 32-bits of the 64-bit GPR, which are not directly addressable, so getSubRegisterClass() returns a nullptr, crashing this pass.

It should instead just avoid trying to create the INIT_UNDEF instruction.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/InitUndef.cpp (+9)
  • (modified) llvm/test/CodeGen/AArch64/init-undef.mir (+2-1)
diff --git a/llvm/lib/CodeGen/InitUndef.cpp b/llvm/lib/CodeGen/InitUndef.cpp
index d4ac131a32a959..7b00611b63b7bf 100644
--- a/llvm/lib/CodeGen/InitUndef.cpp
+++ b/llvm/lib/CodeGen/InitUndef.cpp
@@ -164,6 +164,15 @@ bool InitUndef::handleSubReg(MachineFunction &MF, MachineInstr &MI,
     TRI->getCoveringSubRegIndexes(*MRI, TargetRegClass, NeedDef,
                                   SubRegIndexNeedInsert);
 
+    // It's not possible to create the INIT_UNDEF when there is no register
+    // class associated for the subreg. This may happen for artificial subregs
+    // that are not directly addressable.
+    if (any_of(SubRegIndexNeedInsert,
+               [&TRI = TRI, &TargetRegClass](unsigned ind) -> bool {
+                 return !TRI->getSubRegisterClass(TargetRegClass, ind);
+               }))
+      continue;
+
     Register LatestReg = Reg;
     for (auto ind : SubRegIndexNeedInsert) {
       Changed = true;
diff --git a/llvm/test/CodeGen/AArch64/init-undef.mir b/llvm/test/CodeGen/AArch64/init-undef.mir
index 7935c09d7df5ec..c9d23006d35234 100644
--- a/llvm/test/CodeGen/AArch64/init-undef.mir
+++ b/llvm/test/CodeGen/AArch64/init-undef.mir
@@ -1,5 +1,6 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
-# RUN: llc -mtriple=aarch64-- -run-pass=init-undef -o - %s | FileCheck %s
+# RUN: llc -mtriple=aarch64-- -aarch64-enable-subreg-liveness-tracking=false -run-pass=init-undef -o - %s | FileCheck %s
+# RUN: llc -mtriple=aarch64-- -aarch64-enable-subreg-liveness-tracking=true -run-pass=init-undef -o - %s | FileCheck %s
 
 ---
 name:            test_stxp_undef

Copy link
Contributor

@Stylie777 Stylie777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sdesmalen-arm
Copy link
Collaborator Author

Thank you for the prompt reviews!

Copy link

github-actions bot commented Nov 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@sdesmalen-arm sdesmalen-arm merged commit be15fd5 into llvm:main Nov 14, 2024
5 of 7 checks passed
vitalybuka added a commit that referenced this pull request Nov 22, 2024
…6248)" (#117365)

Maybe not needed but to avoid conflicts with #117307
Without revert of this one, but reverting #117307, the
regenerated init-undef.mir became empty.

This reverts commit be15fd5.
sdesmalen-arm added a commit that referenced this pull request Nov 28, 2024
…6248)"

This patch can now reland after 318c69d relanded #114827.

This reverts commit 1683f84.
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.

4 participants