Skip to content

Commit c0042c0

Browse files
committed
[SDAG] Ensure load is included in output chain of sincos expansion
The load not being included in the chain meant that it could materialize after a `@llvm.lifetime.end` annotation on the pointer. This could result in miscompiles if the stack slot is reused for another value. Fixes #140491
1 parent aa943b1 commit c0042c0

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,16 +2625,19 @@ bool SelectionDAG::expandMultipleResultFPLibCall(
26252625
continue;
26262626
}
26272627
MachinePointerInfo PtrInfo;
2628+
SDValue LoadResult =
2629+
getLoad(Node->getValueType(ResNo), DL, CallChain, ResultPtr, PtrInfo);
2630+
SDValue OutChain = LoadResult.getValue(1);
2631+
26282632
if (StoreSDNode *ST = ResultStores[ResNo]) {
26292633
// Replace store with the library call.
2630-
ReplaceAllUsesOfValueWith(SDValue(ST, 0), CallChain);
2634+
ReplaceAllUsesOfValueWith(SDValue(ST, 0), OutChain);
26312635
PtrInfo = ST->getPointerInfo();
26322636
} else {
26332637
PtrInfo = MachinePointerInfo::getFixedStack(
26342638
getMachineFunction(), cast<FrameIndexSDNode>(ResultPtr)->getIndex());
26352639
}
2636-
SDValue LoadResult =
2637-
getLoad(Node->getValueType(ResNo), DL, CallChain, ResultPtr, PtrInfo);
2640+
26382641
Results.push_back(LoadResult);
26392642
}
26402643

llvm/test/CodeGen/X86/sincos-lifetimes-issue-140491.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,29 @@ define hidden noundef i32 @sincos_stack_slot_with_lifetime(float %in) local_unna
1010
; CHECK: # %bb.0: # %entry
1111
; CHECK-NEXT: pushq %rbx
1212
; CHECK-NEXT: .cfi_def_cfa_offset 16
13-
; CHECK-NEXT: subq $16, %rsp
14-
; CHECK-NEXT: .cfi_def_cfa_offset 32
13+
; CHECK-NEXT: subq $32, %rsp
14+
; CHECK-NEXT: .cfi_def_cfa_offset 48
1515
; CHECK-NEXT: .cfi_offset %rbx, -16
1616
; CHECK-NEXT: leaq 12(%rsp), %rdi
1717
; CHECK-NEXT: leaq 8(%rsp), %rbx
1818
; CHECK-NEXT: movq %rbx, %rsi
1919
; CHECK-NEXT: callq sincosf@PLT
20+
; CHECK-NEXT: movss 8(%rsp), %xmm0 # xmm0 = mem[0],zero,zero,zero
21+
; CHECK-NEXT: movaps %xmm0, 16(%rsp) # 16-byte Spill
2022
; CHECK-NEXT: movq %rbx, %rdi
2123
; CHECK-NEXT: callq use_ptr
2224
; CHECK-NEXT: movss 12(%rsp), %xmm0 # xmm0 = mem[0],zero,zero,zero
2325
; CHECK-NEXT: xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
2426
; CHECK-NEXT: movss %xmm0, 8(%rsp)
2527
; CHECK-NEXT: leaq 8(%rsp), %rdi
2628
; CHECK-NEXT: callq use_ptr
27-
; CHECK-NEXT: movss 8(%rsp), %xmm0 # xmm0 = mem[0],zero,zero,zero
29+
; CHECK-NEXT: movaps 16(%rsp), %xmm0 # 16-byte Reload
2830
; CHECK-NEXT: xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
2931
; CHECK-NEXT: movss %xmm0, 8(%rsp)
3032
; CHECK-NEXT: leaq 8(%rsp), %rdi
3133
; CHECK-NEXT: callq use_ptr
3234
; CHECK-NEXT: xorl %eax, %eax
33-
; CHECK-NEXT: addq $16, %rsp
35+
; CHECK-NEXT: addq $32, %rsp
3436
; CHECK-NEXT: .cfi_def_cfa_offset 16
3537
; CHECK-NEXT: popq %rbx
3638
; CHECK-NEXT: .cfi_def_cfa_offset 8

0 commit comments

Comments
 (0)