diff mbox series

clang: Fix crash with -fzero-call-used-regs on RISC-V large frames

Message ID 20261020033153.1124574-1-khem.raj@oss.qualcomm.com
State Under Review
Headers show
Series clang: Fix crash with -fzero-call-used-regs on RISC-V large frames | expand

Commit Message

Khem Raj Oct. 20, 2026, 3:31 a.m. UTC
Backport the upstream fix which runs insertZeroCallUsedRegs after the
frame virtual registers are scavenged. RISC-V uses a virtual register
to adjust the stack pointer when the frame is too large for addi
immediates (more than ~6KB), and clang 23.1.3 crashes in Prologue/
Epilogue Insertion when -fzero-call-used-regs=used-gpr is also used.

QEMU enables this flag by default, so building qemu for riscv64 with
clang fails on contrib/plugins/uftrace.c:

  Running pass 'Prologue/Epilogue Insertion & Frame Finalization' on
  function '@cpu_unwind_stack'
  clang: error: clang frontend command failed due to signal

Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
---
 ...eroCallUsedRegs-after-materializing-.patch | 72 +++++++++++++++++++
 meta/recipes-devtools/clang/common.inc        |  1 +
 2 files changed, 73 insertions(+)
 create mode 100644 meta/recipes-devtools/clang/clang/0035-PEI-Call-insertZeroCallUsedRegs-after-materializing-.patch

Comments

patchtest@automation.yoctoproject.org Oct. 20, 2026, 3:46 a.m. UTC | #1
Thank you for your submission. Patchtest identified one
or more issues with the patch. Please see the log below for
more information:

---
Testing patch /home/patchtest/share/mboxes/clang-Fix-crash-with--fzero-call-used-regs-on-RISC-V-large-frames.patch

FAIL: test commit message user tags: Mbox includes one or more GitHub-style username tags. Ensure that any "@" symbols are stripped out of usernames (test_mbox.TestMbox.test_commit_message_user_tags)

PASS: test CVE tag format (test_patch.TestPatch.test_cve_tag_format)
PASS: test Signed-off-by presence (test_mbox.TestMbox.test_signed_off_by_presence)
PASS: test Signed-off-by presence (test_patch.TestPatch.test_signed_off_by_presence)
PASS: test Upstream-Status presence (test_patch.TestPatch.test_upstream_status_presence_format)
PASS: test auh changelog truncation notice (test_mbox.TestMbox.test_auh_changelog_truncation_notice)
PASS: test author valid (test_mbox.TestMbox.test_author_valid)
PASS: test commit message presence (test_mbox.TestMbox.test_commit_message_presence)
PASS: test max line length (test_metadata.TestMetadata.test_max_line_length)
PASS: test mbox format (test_mbox.TestMbox.test_mbox_format)
PASS: test non-AUH upgrade (test_mbox.TestMbox.test_non_auh_upgrade)
PASS: test shortlog format (test_mbox.TestMbox.test_shortlog_format)
PASS: test shortlog length (test_mbox.TestMbox.test_shortlog_length)
PASS: test target mailing list (test_mbox.TestMbox.test_target_mailing_list)

SKIP: pretest pylint: No python related patches, skipping test (test_python_pylint.PyLint.pretest_pylint)
SKIP: pretest src uri left files: No modified recipes, skipping pretest (test_metadata.TestMetadata.pretest_src_uri_left_files)
SKIP: test CVE check ignore: No modified recipes or older target branch, skipping test (test_metadata.TestMetadata.test_cve_check_ignore)
SKIP: test bugzilla entry format: No bug ID found (test_mbox.TestMbox.test_bugzilla_entry_format)
SKIP: test lic files chksum modified not mentioned: No modified recipes, skipping test (test_metadata.TestMetadata.test_lic_files_chksum_modified_not_mentioned)
SKIP: test lic files chksum presence: No added recipes, skipping test (test_metadata.TestMetadata.test_lic_files_chksum_presence)
SKIP: test license presence: No added recipes, skipping test (test_metadata.TestMetadata.test_license_presence)
SKIP: test pylint: No python related patches, skipping test (test_python_pylint.PyLint.test_pylint)
SKIP: test series merge on head: Merge test is disabled for now (test_mbox.TestMbox.test_series_merge_on_head)
SKIP: test src uri left files: No modified recipes, skipping test (test_metadata.TestMetadata.test_src_uri_left_files)
SKIP: test summary presence: No added recipes, skipping test (test_metadata.TestMetadata.test_summary_presence)

---

Please address the issues identified and
submit a new revision of the patch, or alternatively, reply to this
email with an explanation of why the patch should be accepted. If you
believe these results are due to an error in patchtest, please submit a
bug at https://bugzilla.yoctoproject.org/ (use the 'Patchtest' category
under 'Yocto Project Subprojects'). For more information on specific
failures, see: https://wiki.yoctoproject.org/wiki/Patchtest. Thank
you!
diff mbox series

Patch

diff --git a/meta/recipes-devtools/clang/clang/0035-PEI-Call-insertZeroCallUsedRegs-after-materializing-.patch b/meta/recipes-devtools/clang/clang/0035-PEI-Call-insertZeroCallUsedRegs-after-materializing-.patch
new file mode 100644
index 0000000000..d2b27deb5b
--- /dev/null
+++ b/meta/recipes-devtools/clang/clang/0035-PEI-Call-insertZeroCallUsedRegs-after-materializing-.patch
@@ -0,0 +1,72 @@ 
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Lucas Chollet <lucas.chollet@free.fr>
+Date: Thu, 3 Sep 2026 21:31:08 +0200
+Subject: [PATCH] [PEI] Call insertZeroCallUsedRegs after materializing registers
+ (#214147)
+
+As insertZeroCallUsedRegs only operates on physical registers, we need
+to call after all registers are materialized, so after
+scavengeFrameVirtualRegs.
+
+This fixes a crash that occurs when virtual registers reach
+insertZeroCallUsedRegs. Virtual registers can be introduced during the
+pass by emit{Prologue,Epilogue}. For example, RISC-V uses a virtual
+register to hold the size of a large alloca.
+
+Upstream-Status: Backport [https://github.com/llvm/llvm-project/commit/7a27b9779e73d3bcb0cf2deb1f74f68ec725595b]
+Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
+---
+ llvm/lib/CodeGen/PrologEpilogInserter.cpp      |  5 ++---
+ llvm/test/CodeGen/RISCV/zero-call-used-regs.ll | 17 +++++++++++++++++
+ 2 files changed, 19 insertions(+), 3 deletions(-)
+
+diff --git a/llvm/lib/CodeGen/PrologEpilogInserter.cpp b/llvm/lib/CodeGen/PrologEpilogInserter.cpp
+index 43c7e89..d3ad107 100644
+--- a/llvm/lib/CodeGen/PrologEpilogInserter.cpp
++++ b/llvm/lib/CodeGen/PrologEpilogInserter.cpp
+@@ -285,6 +285,8 @@ bool PEIImpl::run(MachineFunction &MF) {
+   if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
+     scavengeFrameVirtualRegs(MF, *RS);
+ 
++  insertZeroCallUsedRegs(MF);
++
+   // Warn on stack size when we exceeds the given limit.
+   MachineFrameInfo &MFI = MF.getFrameInfo();
+   uint64_t StackSize = MFI.getStackSize();
+@@ -1179,9 +1181,6 @@ void PEIImpl::insertPrologEpilogCode(MachineFunction &MF) {
+   for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
+     TFI.emitEpilogue(MF, *RestoreBlock);
+ 
+-  // Zero call used registers before restoring callee-saved registers.
+-  insertZeroCallUsedRegs(MF);
+-
+   for (MachineBasicBlock *SaveBlock : SaveBlocks)
+     TFI.inlineStackProbe(MF, *SaveBlock);
+ 
+diff --git a/llvm/test/CodeGen/RISCV/zero-call-used-regs.ll b/llvm/test/CodeGen/RISCV/zero-call-used-regs.ll
+index 46b0cba..78198b4 100644
+--- a/llvm/test/CodeGen/RISCV/zero-call-used-regs.ll
++++ b/llvm/test/CodeGen/RISCV/zero-call-used-regs.ll
+@@ -335,5 +335,22 @@ entry:
+   ret double %mul2
+ }
+ 
++define void @huge_stack() #0 "zero-call-used-regs"="used-gpr" {
++; CHECK-LABEL: huge_stack:
++; CHECK:       # %bb.0:
++; CHECK-NEXT:    lui a0, 10
++; CHECK-NEXT:    addi a0, a0, -944
++; CHECK-NEXT:    sub sp, sp, a0
++; CHECK-NEXT:    .cfi_def_cfa_offset 40016
++; CHECK-NEXT:    lui a0, 10
++; CHECK-NEXT:    addi a0, a0, -944
++; CHECK-NEXT:    add sp, sp, a0
++; CHECK-NEXT:    .cfi_def_cfa_offset 0
++; CHECK-NEXT:    li a0, 0
++; CHECK-NEXT:    ret
++  %1 = alloca [10000 x i32], align 4
++  ret void
++}
++
+ attributes #0 = { "target-cpu"="generic" "target-features"="+m,+f,+d" }
+ attributes #1 = { "target-cpu"="generic" "target-features"="+m,+zdinx" }
diff --git a/meta/recipes-devtools/clang/common.inc b/meta/recipes-devtools/clang/common.inc
index 6465fe3399..2662371746 100644
--- a/meta/recipes-devtools/clang/common.inc
+++ b/meta/recipes-devtools/clang/common.inc
@@ -54,6 +54,7 @@  SRC_URI = "\
     file://0032-llvm-libgcc-Fix-symlink-path-for-libcc-when-LLVM_ENA.patch \
     file://0033-Consolidate-and-fix-sysroot-based-compiler-rt-search.patch \
     file://0034-clang-tools-extra-Add-separate-CLANG_TOOLS_EXTRA_INC.patch \
+    file://0035-PEI-Call-insertZeroCallUsedRegs-after-materializing-.patch \
 "
 # Fallback to no-PIE if not set
 GCCPIE ??= ""