Skip to content

Commit

Permalink
[X86] Do not elect to tail call if caller must preserve all registers
Browse files Browse the repository at this point in the history
A miscompilation issue has been addressed with improved checking.

Fixes: #97758.
  • Loading branch information
antoniofrighetto committed Oct 12, 2024
1 parent c04361d commit ec6b311
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions llvm/lib/Target/X86/X86ISelLoweringCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2856,6 +2856,13 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
return false;
}

// The stack frame of the caller cannot be replaced by the tail-callee one's
// if the function is required to preserve all the registers. Conservatively
// prevent tail optimization even if hypothetically all the registers are used
// for passing formal parameters or returning values.
if (CLI.CB->getFunction()->hasFnAttribute("no_caller_saved_registers"))
return false;

unsigned StackArgsSize = CCInfo.getStackSize();

// If the callee takes no arguments then go on to check the results of the
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/X86/tailcall-caller-nocsr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ define void @caller(i32 %0, i32 %1) #0 {
; CHECK-NEXT: pushq %rdx
; CHECK-NEXT: pushq %rcx
; CHECK-NEXT: pushq %rax
; CHECK-NEXT: movl %esi, %edx
; CHECK-NEXT: movl %edi, %esi
; CHECK-NEXT: movl $.L.str, %edi
; CHECK-NEXT: callq printf@PLT
; CHECK-NEXT: popq %rax
; CHECK-NEXT: popq %rcx
; CHECK-NEXT: popq %rdx
; CHECK-NEXT: popq %r8
; CHECK-NEXT: popq %r9
; CHECK-NEXT: popq %r10
; CHECK-NEXT: popq %r11
; CHECK-NEXT: jmp printf@PLT # TAILCALL
; CHECK-NEXT: retq
%3 = tail call i32 @printf(ptr @.str, i32 %0, i32 %1)
ret void
}
Expand Down

0 comments on commit ec6b311

Please sign in to comment.