Author: Feng Zou Date: 2026-11-15T21:04:04+01:00 New Revision: 6a1cd3390561366f6c3dd741bc1e9f4dd1afccce
URL: https://github.com/llvm/llvm-project/commit/6a1cd3390561366f6c3dd741bc1e9f4dd1afccce DIFF: https://github.com/llvm/llvm-project/commit/6a1cd3390561366f6c3dd741bc1e9f4dd1afccce.diff LOG: [X86][APX] Save R16-R31 in interrupt and no_caller_saved_registers functions (#237109) x86_intrcc, "no_caller_saved_registers" and anyregcc functions must preserve every register they modify. With EGPR their save lists (CSR_64_AllRegs*) did not include R16-R31, so a handler that uses, or calls something that clobbers, an extended GPR silently corrupts the interrupted context. For example, a handler reading its interrupt frame loads it into r16/r17 and returns with iretq without restoring them. Add CSR_64_APX_AllRegs{,_NoSSE,_AVX,_AVX512}, the AllRegs lists plus R16-R31, and use them as the save lists when the subtarget has EGPR. The call-preserved masks are unchanged, so callers keep treating R16-R31 as clobbered. This stays correct for callees built without EGPR or by other or older compilers. Assisted-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit 6a30204d78bd45b56611bbd09cf8c53b78b7435d) Added: llvm/test/CodeGen/X86/apx/intr-egpr-csr-call.ll llvm/test/CodeGen/X86/apx/intr-egpr-csr.ll Modified: llvm/lib/Target/X86/X86CallingConv.td llvm/lib/Target/X86/X86RegisterInfo.cpp Removed: ################################################################################ diff --git a/llvm/lib/Target/X86/X86CallingConv.td b/llvm/lib/Target/X86/X86CallingConv.td index bde5f81eaa7b84..801b00f6d44531 100644 --- a/llvm/lib/Target/X86/X86CallingConv.td +++ b/llvm/lib/Target/X86/X86CallingConv.td @@ -1198,6 +1198,20 @@ def CSR_64_AllRegs_AVX512 : CalleeSavedRegs<(sub (add CSR_64_MostRegs, RAX, (sequence "ZMM%u", 0, 31), (sequence "K%u", 0, 7)), (sequence "XMM%u", 0, 15))>; + +// CSR_64_AllRegs* plus R16-R31: the save lists of x86_intrcc, +// "no_caller_saved_registers" and anyregcc functions when EGPR is available. +// Call sites keep the CSR_64_AllRegs* masks and treat R16-R31 as clobbered, +// since callees built without EGPR or by older compilers may not save them. +def CSR_64_APX_AllRegs : CalleeSavedRegs<(add CSR_64_AllRegs, + (sequence "R%u", 16, 31))>; +def CSR_64_APX_AllRegs_NoSSE : CalleeSavedRegs<(add CSR_64_AllRegs_NoSSE, + (sequence "R%u", 16, 31))>; +def CSR_64_APX_AllRegs_AVX : CalleeSavedRegs<(add CSR_64_AllRegs_AVX, + (sequence "R%u", 16, 31))>; +def CSR_64_APX_AllRegs_AVX512 : CalleeSavedRegs<(add CSR_64_AllRegs_AVX512, + (sequence "R%u", 16, 31))>; + def CSR_64_NoneRegs : CalleeSavedRegs<(add RBP)>; // Standard C + YMM6-15 diff --git a/llvm/lib/Target/X86/X86RegisterInfo.cpp b/llvm/lib/Target/X86/X86RegisterInfo.cpp index ba137c7e7350dc..641fb990bd6dab 100644 --- a/llvm/lib/Target/X86/X86RegisterInfo.cpp +++ b/llvm/lib/Target/X86/X86RegisterInfo.cpp @@ -270,8 +270,9 @@ X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { return CSR_NoRegs_SaveList; case CallingConv::AnyReg: if (HasAVX) - return CSR_64_AllRegs_AVX_SaveList; - return CSR_64_AllRegs_SaveList; + return HasEGPR ? CSR_64_APX_AllRegs_AVX_SaveList + : CSR_64_AllRegs_AVX_SaveList; + return HasEGPR ? CSR_64_APX_AllRegs_SaveList : CSR_64_AllRegs_SaveList; case CallingConv::PreserveMost: if (IsWin64) return HasEGPR ? CSR_Win64_APX_RT_MostRegs_SaveList @@ -341,12 +342,15 @@ X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { case CallingConv::X86_INTR: if (Is64Bit) { if (HasAVX512) - return CSR_64_AllRegs_AVX512_SaveList; + return HasEGPR ? CSR_64_APX_AllRegs_AVX512_SaveList + : CSR_64_AllRegs_AVX512_SaveList; if (HasAVX) - return CSR_64_AllRegs_AVX_SaveList; + return HasEGPR ? CSR_64_APX_AllRegs_AVX_SaveList + : CSR_64_AllRegs_AVX_SaveList; if (HasSSE) - return CSR_64_AllRegs_SaveList; - return CSR_64_AllRegs_NoSSE_SaveList; + return HasEGPR ? CSR_64_APX_AllRegs_SaveList : CSR_64_AllRegs_SaveList; + return HasEGPR ? CSR_64_APX_AllRegs_NoSSE_SaveList + : CSR_64_AllRegs_NoSSE_SaveList; } if (HasAVX512) return CSR_32_AllRegs_AVX512_SaveList; @@ -410,6 +414,8 @@ X86RegisterInfo::getCallPreservedMask(const MachineFunction &MF, case CallingConv::HiPE: return CSR_NoRegs_RegMask; case CallingConv::AnyReg: + // Callers keep R16-R31 clobbered: callees built without EGPR or by older + // compilers may not save them (see CSR_64_APX_AllRegs). if (HasAVX) return CSR_64_AllRegs_AVX_RegMask; return CSR_64_AllRegs_RegMask; @@ -480,6 +486,8 @@ X86RegisterInfo::getCallPreservedMask(const MachineFunction &MF, case CallingConv::X86_64_SysV: return CSR_64_RegMask; case CallingConv::X86_INTR: + // Callers keep R16-R31 clobbered: callees built without EGPR or by older + // compilers may not save them (see CSR_64_APX_AllRegs). if (Is64Bit) { if (HasAVX512) return CSR_64_AllRegs_AVX512_RegMask; diff --git a/llvm/test/CodeGen/X86/apx/intr-egpr-csr-call.ll b/llvm/test/CodeGen/X86/apx/intr-egpr-csr-call.ll new file mode 100644 index 00000000000000..f253764a2bcb85 --- /dev/null +++ b/llvm/test/CodeGen/X86/apx/intr-egpr-csr-call.ll @@ -0,0 +1,127 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+egpr,-sse | FileCheck %s --check-prefix=EGPR +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+egpr,+push2pop2,+ppx,-sse | FileCheck %s --check-prefix=PPX +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=-sse | FileCheck %s --check-prefix=NOEGPR + +; An x86_intrcc handler that makes a call must save every register the callee +; may clobber, R16-R31 included. -sse selects the NoSSE save list. + +%struct.frame = type { i64, i64, i64, i64, i64 } + +declare void @ext() + +define x86_intrcc void @intr_call(ptr byval(%struct.frame) %f) nounwind { +; EGPR-LABEL: intr_call: +; EGPR: # %bb.0: +; EGPR-NEXT: pushq %r31 +; EGPR-NEXT: pushq %r30 +; EGPR-NEXT: pushq %r29 +; EGPR-NEXT: pushq %r28 +; EGPR-NEXT: pushq %r27 +; EGPR-NEXT: pushq %r26 +; EGPR-NEXT: pushq %r25 +; EGPR-NEXT: pushq %r24 +; EGPR-NEXT: pushq %r23 +; EGPR-NEXT: pushq %r22 +; EGPR-NEXT: pushq %r21 +; EGPR-NEXT: pushq %r20 +; EGPR-NEXT: pushq %r19 +; EGPR-NEXT: pushq %r18 +; EGPR-NEXT: pushq %r17 +; EGPR-NEXT: pushq %r16 +; EGPR-NEXT: pushq %r11 +; EGPR-NEXT: pushq %r10 +; EGPR-NEXT: pushq %r9 +; EGPR-NEXT: pushq %r8 +; EGPR-NEXT: pushq %rdi +; EGPR-NEXT: pushq %rsi +; EGPR-NEXT: pushq %rdx +; EGPR-NEXT: pushq %rcx +; EGPR-NEXT: pushq %rax +; EGPR-NEXT: cld +; EGPR-NEXT: callq ext@PLT +; EGPR-NEXT: popq %rax +; EGPR-NEXT: popq %rcx +; EGPR-NEXT: popq %rdx +; EGPR-NEXT: popq %rsi +; EGPR-NEXT: popq %rdi +; EGPR-NEXT: popq %r8 +; EGPR-NEXT: popq %r9 +; EGPR-NEXT: popq %r10 +; EGPR-NEXT: popq %r11 +; EGPR-NEXT: popq %r16 +; EGPR-NEXT: popq %r17 +; EGPR-NEXT: popq %r18 +; EGPR-NEXT: popq %r19 +; EGPR-NEXT: popq %r20 +; EGPR-NEXT: popq %r21 +; EGPR-NEXT: popq %r22 +; EGPR-NEXT: popq %r23 +; EGPR-NEXT: popq %r24 +; EGPR-NEXT: popq %r25 +; EGPR-NEXT: popq %r26 +; EGPR-NEXT: popq %r27 +; EGPR-NEXT: popq %r28 +; EGPR-NEXT: popq %r29 +; EGPR-NEXT: popq %r30 +; EGPR-NEXT: popq %r31 +; EGPR-NEXT: iretq +; +; PPX-LABEL: intr_call: +; PPX: # %bb.0: +; PPX-NEXT: pushp %r31 +; PPX-NEXT: push2p %r29, %r30 +; PPX-NEXT: push2p %r27, %r28 +; PPX-NEXT: push2p %r25, %r26 +; PPX-NEXT: push2p %r23, %r24 +; PPX-NEXT: push2p %r21, %r22 +; PPX-NEXT: push2p %r19, %r20 +; PPX-NEXT: push2p %r17, %r18 +; PPX-NEXT: push2p %r11, %r16 +; PPX-NEXT: push2p %r9, %r10 +; PPX-NEXT: push2p %rdi, %r8 +; PPX-NEXT: push2p %rdx, %rsi +; PPX-NEXT: push2p %rax, %rcx +; PPX-NEXT: cld +; PPX-NEXT: callq ext@PLT +; PPX-NEXT: pop2p %rcx, %rax +; PPX-NEXT: pop2p %rsi, %rdx +; PPX-NEXT: pop2p %r8, %rdi +; PPX-NEXT: pop2p %r10, %r9 +; PPX-NEXT: pop2p %r16, %r11 +; PPX-NEXT: pop2p %r18, %r17 +; PPX-NEXT: pop2p %r20, %r19 +; PPX-NEXT: pop2p %r22, %r21 +; PPX-NEXT: pop2p %r24, %r23 +; PPX-NEXT: pop2p %r26, %r25 +; PPX-NEXT: pop2p %r28, %r27 +; PPX-NEXT: pop2p %r30, %r29 +; PPX-NEXT: popp %r31 +; PPX-NEXT: iretq +; +; NOEGPR-LABEL: intr_call: +; NOEGPR: # %bb.0: +; NOEGPR-NEXT: pushq %r11 +; NOEGPR-NEXT: pushq %r10 +; NOEGPR-NEXT: pushq %r9 +; NOEGPR-NEXT: pushq %r8 +; NOEGPR-NEXT: pushq %rdi +; NOEGPR-NEXT: pushq %rsi +; NOEGPR-NEXT: pushq %rdx +; NOEGPR-NEXT: pushq %rcx +; NOEGPR-NEXT: pushq %rax +; NOEGPR-NEXT: cld +; NOEGPR-NEXT: callq ext@PLT +; NOEGPR-NEXT: popq %rax +; NOEGPR-NEXT: popq %rcx +; NOEGPR-NEXT: popq %rdx +; NOEGPR-NEXT: popq %rsi +; NOEGPR-NEXT: popq %rdi +; NOEGPR-NEXT: popq %r8 +; NOEGPR-NEXT: popq %r9 +; NOEGPR-NEXT: popq %r10 +; NOEGPR-NEXT: popq %r11 +; NOEGPR-NEXT: iretq + call void @ext() + ret void +} diff --git a/llvm/test/CodeGen/X86/apx/intr-egpr-csr.ll b/llvm/test/CodeGen/X86/apx/intr-egpr-csr.ll new file mode 100644 index 00000000000000..8ba25d1d5e58cf --- /dev/null +++ b/llvm/test/CodeGen/X86/apx/intr-egpr-csr.ll @@ -0,0 +1,227 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+egpr | FileCheck %s --check-prefixes=EGPR,SSE +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+egpr,+avx | FileCheck %s --check-prefixes=EGPR,AVX +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+egpr,+avx512f | FileCheck %s --check-prefixes=EGPR,AVX512 +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+egpr,+push2pop2,+ppx | FileCheck %s --check-prefix=PPX +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s --check-prefix=NOEGPR + +; x86_intrcc, "no_caller_saved_registers" and anyregcc functions must preserve +; every register they modify, including R16-R31 when EGPR is available. + +%struct.frame = type { i64, i64, i64, i64, i64 } + +define x86_intrcc void @intr_clobber(ptr byval(%struct.frame) %f) nounwind { +; EGPR-LABEL: intr_clobber: +; EGPR: # %bb.0: +; EGPR-NEXT: pushq %r31 +; EGPR-NEXT: pushq %r16 +; EGPR-NEXT: pushq %rax +; EGPR-NEXT: cld +; EGPR-NEXT: #APP +; EGPR-NEXT: #NO_APP +; EGPR-NEXT: popq %rax +; EGPR-NEXT: popq %r16 +; EGPR-NEXT: popq %r31 +; EGPR-NEXT: iretq +; +; PPX-LABEL: intr_clobber: +; PPX: # %bb.0: +; PPX-NEXT: pushp %r31 +; PPX-NEXT: push2p %rax, %r16 +; PPX-NEXT: cld +; PPX-NEXT: #APP +; PPX-NEXT: #NO_APP +; PPX-NEXT: pop2p %r16, %rax +; PPX-NEXT: popp %r31 +; PPX-NEXT: iretq +; +; NOEGPR-LABEL: intr_clobber: +; NOEGPR: # %bb.0: +; NOEGPR-NEXT: pushq %rax +; NOEGPR-NEXT: cld +; NOEGPR-NEXT: #APP +; NOEGPR-NEXT: #NO_APP +; NOEGPR-NEXT: popq %rax +; NOEGPR-NEXT: iretq + call void asm sideeffect "", "~{r16},~{r31},~{rax}"() + ret void +} + +; The XMM clobber tells the SSE, AVX and AVX512 save lists apart. +define x86_intrcc void @intr_clobber_vec(ptr byval(%struct.frame) %f) nounwind { +; SSE-LABEL: intr_clobber_vec: +; SSE: # %bb.0: +; SSE-NEXT: pushq %r31 +; SSE-NEXT: pushq %r16 +; SSE-NEXT: pushq %rax +; SSE-NEXT: movaps %xmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill +; SSE-NEXT: cld +; SSE-NEXT: #APP +; SSE-NEXT: #NO_APP +; SSE-NEXT: movaps {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload +; SSE-NEXT: popq %rax +; SSE-NEXT: popq %r16 +; SSE-NEXT: popq %r31 +; SSE-NEXT: iretq +; +; AVX-LABEL: intr_clobber_vec: +; AVX: # %bb.0: +; AVX-NEXT: pushq %r31 +; AVX-NEXT: pushq %r16 +; AVX-NEXT: pushq %rax +; AVX-NEXT: vmovups %ymm1, {{[-0-9]+}}(%r{{[sb]}}p) # 32-byte Spill +; AVX-NEXT: cld +; AVX-NEXT: #APP +; AVX-NEXT: #NO_APP +; AVX-NEXT: vmovups {{[-0-9]+}}(%r{{[sb]}}p), %ymm1 # 32-byte Reload +; AVX-NEXT: popq %rax +; AVX-NEXT: popq %r16 +; AVX-NEXT: popq %r31 +; AVX-NEXT: iretq +; +; AVX512-LABEL: intr_clobber_vec: +; AVX512: # %bb.0: +; AVX512-NEXT: pushq %r31 +; AVX512-NEXT: pushq %r16 +; AVX512-NEXT: pushq %rax +; AVX512-NEXT: vmovups %zmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 64-byte Spill +; AVX512-NEXT: cld +; AVX512-NEXT: #APP +; AVX512-NEXT: #NO_APP +; AVX512-NEXT: vmovups {{[-0-9]+}}(%r{{[sb]}}p), %zmm1 # 64-byte Reload +; AVX512-NEXT: popq %rax +; AVX512-NEXT: popq %r16 +; AVX512-NEXT: popq %r31 +; AVX512-NEXT: iretq +; +; PPX-LABEL: intr_clobber_vec: +; PPX: # %bb.0: +; PPX-NEXT: pushp %r31 +; PPX-NEXT: push2p %rax, %r16 +; PPX-NEXT: movaps %xmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill +; PPX-NEXT: cld +; PPX-NEXT: #APP +; PPX-NEXT: #NO_APP +; PPX-NEXT: movaps {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload +; PPX-NEXT: pop2p %r16, %rax +; PPX-NEXT: popp %r31 +; PPX-NEXT: iretq +; +; NOEGPR-LABEL: intr_clobber_vec: +; NOEGPR: # %bb.0: +; NOEGPR-NEXT: pushq %rax +; NOEGPR-NEXT: movaps %xmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill +; NOEGPR-NEXT: cld +; NOEGPR-NEXT: #APP +; NOEGPR-NEXT: #NO_APP +; NOEGPR-NEXT: movaps {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload +; NOEGPR-NEXT: popq %rax +; NOEGPR-NEXT: iretq + call void asm sideeffect "", "~{r16},~{r31},~{rax},~{xmm1}"() + ret void +} + +define void @ncsr_clobber() nounwind "no_caller_saved_registers" { +; EGPR-LABEL: ncsr_clobber: +; EGPR: # %bb.0: +; EGPR-NEXT: pushq %r31 +; EGPR-NEXT: pushq %r16 +; EGPR-NEXT: pushq %rax +; EGPR-NEXT: #APP +; EGPR-NEXT: #NO_APP +; EGPR-NEXT: popq %rax +; EGPR-NEXT: popq %r16 +; EGPR-NEXT: popq %r31 +; EGPR-NEXT: retq +; +; PPX-LABEL: ncsr_clobber: +; PPX: # %bb.0: +; PPX-NEXT: pushp %r31 +; PPX-NEXT: push2p %rax, %r16 +; PPX-NEXT: #APP +; PPX-NEXT: #NO_APP +; PPX-NEXT: pop2p %r16, %rax +; PPX-NEXT: popp %r31 +; PPX-NEXT: retq +; +; NOEGPR-LABEL: ncsr_clobber: +; NOEGPR: # %bb.0: +; NOEGPR-NEXT: pushq %rax +; NOEGPR-NEXT: #APP +; NOEGPR-NEXT: #NO_APP +; NOEGPR-NEXT: popq %rax +; NOEGPR-NEXT: retq + call void asm sideeffect "", "~{r16},~{r31},~{rax}"() + ret void +} + +; anyregcc has no AVX512 save list, so the AVX512 run saves YMM, as AVX does. +define anyregcc void @anyreg_clobber() nounwind { +; SSE-LABEL: anyreg_clobber: +; SSE: # %bb.0: +; SSE-NEXT: pushq %r31 +; SSE-NEXT: pushq %r16 +; SSE-NEXT: pushq %rax +; SSE-NEXT: movaps %xmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill +; SSE-NEXT: #APP +; SSE-NEXT: #NO_APP +; SSE-NEXT: movaps {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload +; SSE-NEXT: popq %rax +; SSE-NEXT: popq %r16 +; SSE-NEXT: popq %r31 +; SSE-NEXT: retq +; +; AVX-LABEL: anyreg_clobber: +; AVX: # %bb.0: +; AVX-NEXT: pushq %r31 +; AVX-NEXT: pushq %r16 +; AVX-NEXT: pushq %rax +; AVX-NEXT: vmovups %ymm1, {{[-0-9]+}}(%r{{[sb]}}p) # 32-byte Spill +; AVX-NEXT: #APP +; AVX-NEXT: #NO_APP +; AVX-NEXT: vmovups {{[-0-9]+}}(%r{{[sb]}}p), %ymm1 # 32-byte Reload +; AVX-NEXT: popq %rax +; AVX-NEXT: popq %r16 +; AVX-NEXT: popq %r31 +; AVX-NEXT: vzeroupper +; AVX-NEXT: retq +; +; AVX512-LABEL: anyreg_clobber: +; AVX512: # %bb.0: +; AVX512-NEXT: pushq %r31 +; AVX512-NEXT: pushq %r16 +; AVX512-NEXT: pushq %rax +; AVX512-NEXT: vmovups %ymm1, {{[-0-9]+}}(%r{{[sb]}}p) # 32-byte Spill +; AVX512-NEXT: #APP +; AVX512-NEXT: #NO_APP +; AVX512-NEXT: vmovups {{[-0-9]+}}(%r{{[sb]}}p), %ymm1 # 32-byte Reload +; AVX512-NEXT: popq %rax +; AVX512-NEXT: popq %r16 +; AVX512-NEXT: popq %r31 +; AVX512-NEXT: vzeroupper +; AVX512-NEXT: retq +; +; PPX-LABEL: anyreg_clobber: +; PPX: # %bb.0: +; PPX-NEXT: pushp %r31 +; PPX-NEXT: push2p %rax, %r16 +; PPX-NEXT: movaps %xmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill +; PPX-NEXT: #APP +; PPX-NEXT: #NO_APP +; PPX-NEXT: movaps {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload +; PPX-NEXT: pop2p %r16, %rax +; PPX-NEXT: popp %r31 +; PPX-NEXT: retq +; +; NOEGPR-LABEL: anyreg_clobber: +; NOEGPR: # %bb.0: +; NOEGPR-NEXT: pushq %rax +; NOEGPR-NEXT: movaps %xmm1, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill +; NOEGPR-NEXT: #APP +; NOEGPR-NEXT: #NO_APP +; NOEGPR-NEXT: movaps {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload +; NOEGPR-NEXT: popq %rax +; NOEGPR-NEXT: retq + call void asm sideeffect "", "~{r16},~{r31},~{rax},~{xmm1}"() + ret void +} _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
