From e70e3346641f4c018197e748733be90d3320c35c Mon Sep 17 00:00:00 2001 From: Sander de Smalen Date: Wed, 30 Oct 2024 15:41:48 +0000 Subject: [PATCH 1/2] [InitUndef] handleSubReg should skip artificial subregs. 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. --- llvm/lib/CodeGen/InitUndef.cpp | 9 +++++++++ llvm/test/CodeGen/AArch64/init-undef.mir | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/llvm/lib/CodeGen/InitUndef.cpp b/llvm/lib/CodeGen/InitUndef.cpp index d4ac131a32a95..7b00611b63b7b 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 7935c09d7df5e..c9d23006d3523 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 From f1357d52a5f37e89207ff44275518789a67997b0 Mon Sep 17 00:00:00 2001 From: Sander de Smalen Date: Thu, 14 Nov 2024 16:00:44 +0000 Subject: [PATCH 2/2] Update llvm/lib/CodeGen/InitUndef.cpp Co-authored-by: Nikita Popov --- llvm/lib/CodeGen/InitUndef.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/llvm/lib/CodeGen/InitUndef.cpp b/llvm/lib/CodeGen/InitUndef.cpp index 7b00611b63b7b..d8b3190f31003 100644 --- a/llvm/lib/CodeGen/InitUndef.cpp +++ b/llvm/lib/CodeGen/InitUndef.cpp @@ -167,10 +167,9 @@ bool InitUndef::handleSubReg(MachineFunction &MF, MachineInstr &MI, // 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); - })) + if (any_of(SubRegIndexNeedInsert, [&](unsigned Ind) -> bool { + return !TRI->getSubRegisterClass(TargetRegClass, Ind); + })) continue; Register LatestReg = Reg;